File: docs-bundle-sphinxcontrib.jinja-extension.patch

package info (click to toggle)
buildbot 3.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,772 kB
  • sloc: python: 145,602; javascript: 11,856; sh: 1,020; makefile: 325
file content (116 lines) | stat: -rw-r--r-- 4,566 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
From: Robin Jarry <robin@jarry.cc>
Date: Wed, 16 Nov 2022 14:06:21 +0000
Subject: docs: bundle sphinxcontrib.jinja extension

This is required to generate the REST documentation but
sphinxcontrib.jinja is not available in debian.

Signed-off-by: Robin Jarry <robin@jarry.cc>
---
 master/docs/bbdocs/jinja.py | 83 +++++++++++++++++++++++++++++++++++++++++++++
 master/docs/conf.py         |  2 +-
 2 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100644 master/docs/bbdocs/jinja.py

diff --git a/master/docs/bbdocs/jinja.py b/master/docs/bbdocs/jinja.py
new file mode 100644
index 0000000..9c80012
--- /dev/null
+++ b/master/docs/bbdocs/jinja.py
@@ -0,0 +1,83 @@
+import codecs
+import os
+import sys
+import urllib
+
+from docutils import nodes
+from docutils.parsers.rst import Directive
+from docutils.parsers.rst import directives
+from docutils.statemachine import StringList
+from jinja2 import FileSystemLoader, Environment
+import sphinx.util
+
+
+class JinjaDirective(Directive):
+    has_content = True
+    optional_arguments = 1
+    option_spec = {
+        "file": directives.path,
+        "header_char": directives.unchanged,
+        "debug": directives.unchanged,
+    }
+    app = None
+
+    def run(self):
+        node = nodes.Element()
+        node.document = self.state.document
+        env = self.state.document.settings.env
+        docname = env.docname
+        template_filename = self.options.get("file")
+        debug_template = self.options.get("debug")
+        cxt = (self.app.config.jinja_contexts[self.arguments[0]].copy()
+               if self.arguments else {})
+        cxt["options"] = {
+            "header_char": self.options.get("header_char")
+        }
+        if template_filename:
+            if debug_template is not None:
+                print('')
+                print('********** Begin Jinja Debug Output: Template Before Processing **********')
+                print('********** From {} **********'.format(docname))
+                reference_uri = directives.uri(os.path.join('source', template_filename))
+                template_path = urllib.url2pathname(reference_uri)
+                encoded_path = template_path.encode(sys.getfilesystemencoding())
+                imagerealpath = os.path.abspath(encoded_path)
+                with codecs.open(imagerealpath, encoding='utf-8') as f:
+                    print(f.read())
+                print('********** End Jinja Debug Output: Template Before Processing **********')
+                print('')
+            tpl = Environment(
+                          loader=FileSystemLoader(
+                              self.app.config.jinja_base, followlinks=True)
+                      ).get_template(template_filename)
+        else:
+            if debug_template is not None:
+                print('')
+                print('********** Begin Jinja Debug Output: Template Before Processing **********')
+                print('********** From {} **********'.format(docname))
+                print('\n'.join(self.content))
+                print('********** End Jinja Debug Output: Template Before Processing **********')
+                print('')
+            tpl = Environment(
+                      loader=FileSystemLoader(
+                          self.app.config.jinja_base, followlinks=True)
+                  ).from_string('\n'.join(self.content))
+        new_content = tpl.render(**cxt)
+        if debug_template is not None:
+            print('')
+            print('********** Begin Jinja Debug Output: Template After Processing **********')
+            print(new_content)
+            print('********** End Jinja Debug Output: Template After Processing **********')
+            print('')
+        new_content = StringList(new_content.splitlines(), source='')
+        sphinx.util.nested_parse_with_titles(
+            self.state, new_content, node)
+        return node.children
+
+
+def setup(app):
+    JinjaDirective.app = app
+    app.add_directive('jinja', JinjaDirective)
+    app.add_config_value('jinja_contexts', {}, 'env')
+    app.add_config_value('jinja_base', os.path.abspath('.'), 'env')
+    return {'parallel_read_safe': True, 'parallel_write_safe': True}
diff --git a/master/docs/conf.py b/master/docs/conf.py
index 1973712..de7d07a 100755
--- a/master/docs/conf.py
+++ b/master/docs/conf.py
@@ -49,7 +49,7 @@ extensions = [
     'sphinx.ext.extlinks',
     'bbdocs.ext',
     'bbdocs.api_index',
-    'sphinx_jinja',
+    'bbdocs.jinja',
     'sphinx_rtd_theme',
 ]
 todo_include_todos = True