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 55 56 57
|
########################################################################################
# License: CC0 1.0 #
# #
# See full license text here: https://creativecommons.org/publicdomain/zero/1.0/ #
########################################################################################
# In the event that this code is useful to you, please feel free to use it without #
# attribution. The CC0 1.0 license is chosen in the event that you need a license. #
# #
# Note that this code is basically just a bad modification of the Sphinx tutorial: #
# https://www.sphinx-doc.org/en/latest/extdev/tutorial.html #
########################################################################################
import re
import textwrap
from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.statemachine import StringList
from sphinx.locale import _
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.absolute()))
from github_url import get_github_base_url
def visit_exhaleversion_node(self, node):
self.visit_admonition(node)
def depart_exhaleversion_node(self, node):
self.depart_admonition(node)
class exhaleversion(nodes.Admonition, nodes.Element):
pass
class ExhaleVersionDirective(Directive):
has_content = True
def run(self):
if len(self.content) != 0:
raise ValueError("`exhaleversion` directive does not allow any arguments.")
# Seriously there _HAS_ to be a better way to do this...
# Creating something of length 1 so that I can rage-replace it
self.content = StringList("?")
base = get_github_base_url()
commit = base.split("/")[-1]
self.content[0] = textwrap.dedent(f'''\
This documentation was generated using exhale version |version|
from `commit {commit} <{base}>`_.
''')
exhaleversion_node = exhaleversion("\n".join(self.content))
exhaleversion_node += nodes.title(_("Version"), _("Version"))
self.state.nested_parse(self.content, self.content_offset, exhaleversion_node)
return [exhaleversion_node]
|