| 1 |
"""Display changelog with links to GitHub issues |
| 2 |
|
| 3 |
Usage |
| 4 |
----- |
| 5 |
The directive |
| 6 |
|
| 7 |
.. include_changelog:: ../CHANGELOG |
| 8 |
|
| 9 |
adds the content of the changelog file into the current document. |
| 10 |
References to GitHub issues are identified as "(#XY)" (with parentheses |
| 11 |
and hash) and a link is inserted |
| 12 |
|
| 13 |
https://github.com/RI-imaging/{PROJECT}/issues/{XY} |
| 14 |
|
| 15 |
where PROJECT ist the `project` variable defined in conf.py. |
| 16 |
""" |
| 17 |
import io |
| 18 |
import re |
| 19 |
|
| 20 |
from docutils.statemachine import ViewList |
| 21 |
from docutils.parsers.rst import Directive |
| 22 |
from sphinx.util.nodes import nested_parse_with_titles |
| 23 |
from docutils import nodes |
| 24 |
|
| 25 |
|
| 26 |
class IncludeDirective(Directive): |
| 27 |
required_arguments = 1 |
| 28 |
optional_arguments = 0 |
| 29 |
|
| 30 |
def run(self): |
| 31 |
full_path = self.arguments[0] |
| 32 |
project = self.state.document.settings.env.config.github_project |
| 33 |
|
| 34 |
def insert_github_link(reobj): |
| 35 |
line = reobj.string |
| 36 |
instr = line[reobj.start():reobj.end()] |
| 37 |
issue = instr.strip("#()") |
| 38 |
link = "https://github.com/{}/issues/".format(project) |
| 39 |
rstlink = "(`#{issue} <{link}{issue}>`_)".format(issue=issue, |
| 40 |
link=link) |
| 41 |
return rstlink |
| 42 |
|
| 43 |
with io.open(full_path, "r") as myfile: |
| 44 |
text = myfile.readlines() |
| 45 |
|
| 46 |
rst = [] |
| 47 |
for line in text: |
| 48 |
line = line.strip("\n") |
| 49 |
if line.startswith(" ") and line.strip().startswith("-"): |
| 50 |
|
| 51 |
rst.append("") |
| 52 |
if not line.startswith(" "): |
| 53 |
rst.append("") |
| 54 |
line = "version " + line |
| 55 |
rst.append(line) |
| 56 |
rst.append("-"*len(line)) |
| 57 |
elif not line.strip(): |
| 58 |
rst.append(line) |
| 59 |
else: |
| 60 |
line = re.sub(r"\(#[0-9]*\)", insert_github_link, line) |
| 61 |
rst.append(line) |
| 62 |
|
| 63 |
vl = ViewList(rst, "fakefile.rst") |
| 64 |
|
| 65 |
node = nodes.section() |
| 66 |
node.document = self.state.document |
| 67 |
|
| 68 |
nested_parse_with_titles(self.state, vl, node) |
| 69 |
return node.children |
| 70 |
|
| 71 |
|
| 72 |
def setup(app): |
| 73 |
app.add_config_value('github_project', "user/project", 'html') |
| 74 |
app.add_directive('include_changelog', IncludeDirective) |
| 75 |
return {'version': '0.1'} |