Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from base import BaseDirective
class AlgorithmDirective(BaseDirective):
"""
Adds a referenceable link for a given algorithm, a title,
and a screenshot of the algorithm to an rst file.
"""
required_arguments, optional_arguments = 1, 0
def run(self):
"""
Called by Sphinx when the ..algorithm:: directive is encountered
"""
algorithm_name = str(self.arguments[0])
# Seperate methods for each unique piece of functionality.
reference = self._make_reference_link(algorithm_name)
title = self._make_header(algorithm_name, True)
screenshot = self._get_algorithm_screenshot(algorithm_name)
return self._insert_rest(reference + title + screenshot)
def _make_reference_link(self, algorithm_name):
"""
Outputs a reference to the top of the algorithm's rst file.
Args:
algorithm_name (str): The name of the algorithm to reference.
Returns:
str: A ReST formatted reference.
"""
return ".. _" + algorithm_name.title() + ":" + "\n"
def _get_algorithm_screenshot(self, algorithm_name):
"""
Obtains the location of the screenshot for a given algorithm.
Args:
algorithm_name (str): The name of the algorithm.
Returns:
str: The location of the screenshot for the given algorithm.
"""
images_dir = self.state.document.settings.env.config["mantid_images"]
return ".. image:: " + images_dir + algorithm_name + ".png"
def setup(app):
app.add_config_value('mantid_images', 'mantid_images', 'env')
app.add_directive('algorithm', AlgorithmDirective)