Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

jinja.py 4.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. #
  2. # The sphinx-jinja extension is available from https://github.com/tardyp/sphinx-jinja,
  3. # licensed under the MIT License.
  4. #
  5. #
  6. # The MIT License
  7. #
  8. # Copyright (c) 2016-2019 Pierre Tardy
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining a copy
  11. # of this software and associated documentation files (the "Software"), to deal
  12. # in the Software without restriction, including without limitation the rights
  13. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. # copies of the Software, and to permit persons to whom the Software is
  15. # furnished to do so, subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be included in
  18. # all copies or substantial portions of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. # THE SOFTWARE.
  27. import codecs
  28. import os
  29. import sys
  30. import urllib
  31. from docutils import nodes
  32. from docutils.parsers.rst import Directive
  33. from docutils.parsers.rst import directives
  34. from docutils.statemachine import StringList
  35. from jinja2 import FileSystemLoader, Environment
  36. import sphinx.util
  37. class JinjaDirective(Directive):
  38. has_content = True
  39. optional_arguments = 1
  40. option_spec = {
  41. "file": directives.path,
  42. "header_char": directives.unchanged,
  43. "debug": directives.unchanged,
  44. }
  45. app = None
  46. def run(self):
  47. node = nodes.Element()
  48. node.document = self.state.document
  49. env = self.state.document.settings.env
  50. docname = env.docname
  51. template_filename = self.options.get("file")
  52. debug_template = self.options.get("debug")
  53. cxt = (self.app.config.jinja_contexts[self.arguments[0]].copy()
  54. if self.arguments else {})
  55. cxt["options"] = {
  56. "header_char": self.options.get("header_char")
  57. }
  58. if template_filename:
  59. if debug_template is not None:
  60. print('')
  61. print('********** Begin Jinja Debug Output: Template Before Processing **********')
  62. print('********** From {} **********'.format(docname))
  63. reference_uri = directives.uri(os.path.join('source', template_filename))
  64. template_path = urllib.url2pathname(reference_uri)
  65. encoded_path = template_path.encode(sys.getfilesystemencoding())
  66. imagerealpath = os.path.abspath(encoded_path)
  67. with codecs.open(imagerealpath, encoding='utf-8') as f:
  68. print(f.read())
  69. print('********** End Jinja Debug Output: Template Before Processing **********')
  70. print('')
  71. tpl = Environment(
  72. loader=FileSystemLoader(
  73. self.app.config.jinja_base, followlinks=True)
  74. ).get_template(template_filename)
  75. else:
  76. if debug_template is not None:
  77. print('')
  78. print('********** Begin Jinja Debug Output: Template Before Processing **********')
  79. print('********** From {} **********'.format(docname))
  80. print('\n'.join(self.content))
  81. print('********** End Jinja Debug Output: Template Before Processing **********')
  82. print('')
  83. tpl = Environment(
  84. loader=FileSystemLoader(
  85. self.app.config.jinja_base, followlinks=True)
  86. ).from_string('\n'.join(self.content))
  87. new_content = tpl.render(**cxt)
  88. if debug_template is not None:
  89. print('')
  90. print('********** Begin Jinja Debug Output: Template After Processing **********')
  91. print(new_content)
  92. print('********** End Jinja Debug Output: Template After Processing **********')
  93. print('')
  94. new_content = StringList(new_content.splitlines(), source='')
  95. sphinx.util.nested_parse_with_titles(
  96. self.state, new_content, node)
  97. return node.children
  98. def setup(app):
  99. JinjaDirective.app = app
  100. app.add_directive('jinja', JinjaDirective)
  101. app.add_config_value('jinja_contexts', {}, 'env')
  102. app.add_config_value('jinja_base', os.path.abspath('.'), 'env')
  103. return {'parallel_read_safe': True, 'parallel_write_safe': True}
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...