1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 from docutils
.nodes
import Text
, paragraph
, reference
8 from sphinx
.transforms
import SphinxTransform
11 class ConvertBugsToLinks(SphinxTransform
):
12 # Convert text entries in paragraphs that are in the style of "bug xxxxx"
13 # to a hyperlink that leads to the appropriate bugzilla bug link.
15 default_priority
= 400
16 bz_url
= "https://bugzilla.mozilla.org/show_bug.cgi?id={0}"
17 bz_reg
= r
"bug[' '][0-9]\d*"
20 def check_if_paragraph(o
):
21 return isinstance(o
, paragraph
)
25 not isinstance(o
.parent
, reference
)
26 and isinstance(o
, Text
)
27 and re
.search(self
.bz_reg
, o
, re
.IGNORECASE
)
32 changed
= self
.textToReferences(check_if_paragraph
, check_if_text
)
35 def textToReferences(self
, check_if_paragraph
, check_if_text
):
36 # Analyses the document and replaces from the paragraph nodes
37 # the Text element(s) that contain bz_reg matching strings.
38 # Whevever the matching strings are more than one and
39 # a correction is made, the function returns True.
41 for node
in self
.document
.traverse(check_if_paragraph
):
42 for text
in node
.traverse(check_if_text
):
43 bugs
= re
.findall(self
.bz_reg
, text
, re
.IGNORECASE
)
47 txtparts
= text
.split(bug
, 1)
51 refuri
=self
.bz_url
.format(bug
.split()[1]),
53 txt_0
= Text(txtparts
[0])
54 txt_1
= Text(txtparts
[1])
55 text
.parent
.replace(text
, [txt_0
, new_ref
, txt_1
])
62 app
.add_transform(ConvertBugsToLinks
)