-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetthecode.py
More file actions
279 lines (212 loc) · 9.97 KB
/
getthecode.py
File metadata and controls
279 lines (212 loc) · 9.97 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""This plugin provides an enhanced ``literalinclude`` directive for Sphinx Documentation Generator.
* https://www.sphinx-doc.org/en/master/extdev/index.html
"""
####################################################################################################
from pathlib import Path
import codecs
import os
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from docutils.writers.html4css1 import HTMLTranslator as BaseTranslator
from sphinx.util.nodes import set_source_info
import sphinx.util
import jinja2
####################################################################################################
logger = sphinx.util.logging.getLogger('getthecode')
####################################################################################################
# class="reference download internal"
# '<button id="copy-button" data-clipboard-target="clipboard_pre">Copy to Clipboard</button>'
# '<pre id="clipboard_pre">' + node.rawsource + </pre>'
HEADER_TEMPLATE = '''
<div class="getthecode-header">
<ul>
<li class="getthecode-filename">{{ filename }}</li>
<li class="getthecode-filename-link">
<a href="{{ url }}" download={{ filename }} type="text/x-python" target="_blank" rel="noreferrer noopener"><span>
{{ filename }}
</span></a>
</li>
{% if notebook_url %}
<li class="getthecode-notebook-link">
<a href="{{ notebook_url }}" download={{ notebook_filename }} type="application/x-ipynb+json" target="_blank" rel="noreferrer noopener"><span>
{{ notebook_filename }}
</span></a>
</li>
{% endif %}
</ul>
</div>
'''
####################################################################################################
class GetTheCode(nodes.literal_block):
pass
####################################################################################################
class GetTheCodeDirective(Directive):
"""This code is a copy-paste from :file:`sphinx/directives/code.py` :class:`LiteralInclude`. See
also :file:`sphinx/roles.py` :class:`XRefRole`.
"""
##############################################
has_content = False
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
'encoding': directives.encoding,
'hidden': directives.flag,
'language': directives.unchanged_required,
'linenos': directives.flag,
'notebook': directives.flag,
}
##############################################
def run(self):
document = self.state.document
if not document.settings.file_insertion_enabled:
return [document.reporter.warning('File insertion disabled', line=self.lineno)]
env = document.settings.env
# arguments = [relative_source_path, ]
relative_source_path, source_path = env.relfn2path(self.arguments[0])
encoding = self.options.get('encoding', env.config.source_encoding)
codec_info = codecs.lookup(encoding)
try:
fh = codecs.StreamReaderWriter(open(source_path, 'rb'), codec_info[2], codec_info[3], 'strict')
text = fh.read()
fh.close()
except (IOError, OSError):
return [
document.reporter.warning(
'Include file {} not found or reading it failed'.format(source_path),
line=self.lineno,
)
]
except UnicodeError:
template = 'Encoding {} used for reading included file {} seems to be wrong, try giving an :encoding: option'
return [document.reporter.warning(template.format(encoding, source_path))]
env.note_dependency(relative_source_path)
node = GetTheCode(text, text, source=source_path, filename=None)
set_source_info(self, node)
if self.options.get('language', ''):
node['language'] = self.options['language']
if 'linenos' in self.options:
node['linenos'] = True
if 'hidden' in self.options:
node['hidden'] = True
if 'notebook' in self.options:
# node['notebook'] = True
source_path = Path(source_path)
notebook_name = source_path.stem + '.ipynb'
notebook_path = source_path.parent.joinpath(notebook_name)
node['notebook_path'] = notebook_path
return [node]
####################################################################################################
def process_getthedoc(app, doctree):
""" This function is a *doctree-read* callback. It copies the download-able files to the
directory :directory:`_downloads`.
This code is a copy-paste with few modifications of the
:meth:`BuildEnvironment.process_downloads` method.
"""
# Called before visit_GetTheCode_html
# print('process_getthedoc')
env = app.builder.env
document_name = env.docname # examples/document-generator/full-test .rst
for node in doctree.traverse(GetTheCode):
# targetname = node['reftarget']
# /home/.../doc/sphinx/source/examples/document-generator/full-test.py
source_path = Path(node['source'])
relative_source_path, source_path = env.relfn2path(source_path.name, document_name)
env.dependencies.setdefault(document_name, set()).add(relative_source_path)
if not os.access(source_path, os.R_OK):
logger.warning('download file not readable: {}'.format(source_path), node)
continue
# c3e20896d45729b3dd37b566def9e52a/full-test.py
unique_name = env.dlfiles.add_file(document_name, source_path)
node['filename'] = unique_name # Fixme: filename -> ... ?
notebook_path = node.get('notebook_path', None)
if notebook_path is not None:
if not os.access(notebook_path, os.R_OK):
logger.warning('download file not readable: {}'.format(notebook_path), node)
continue
unique_name = env.dlfiles.add_file(document_name, str(notebook_path))
node['notebook_download_path'] = unique_name # Fixme: -> ... ?
####################################################################################################
def visit_GetTheCode_html(self, node):
"""
This code is a copy-paste from :file:`sphinx/writers/html.py`.
"""
# print('visit_GetTheCode_html')
# {
# 'rawsource': u"...",
# 'parent': <section "introduction": <title...><paragraph...><paragraph...><literal_block...> ...>,
# 'source': '/home.../open-source-frontends.rst',
# 'tagname': 'GetTheCode',
# 'attributes': {'language': 'python', 'dupnames': [], 'xml:space': 'preserve', 'ids': [], 'backrefs': [],
# 'source': u'/home.../SimpleRectifierWithTransformer-jmodelica.py',
# 'classes': [], 'names': []},
# 'line': 42,
# 'document': <document: <comment...><comment...><section "open source frontends"...>>,
# 'children': [<#text: 'from pymodelica import compile_fmu\nfrom pyfmi import load_fmu\n\ni ...'>]
# }
# Open the top div
self.body.append(self.starttag(node, 'div', CLASS=('getthecode')))
# self.context.append('</div>\n')
# c3e20896d45729b3dd37b566def9e52a/full-test.py
relative_path = Path(node['filename'])
download_path = Path(self.builder.dlpath)
# ../../_downloads/c3e20896d45729b3dd37b566def9e52a/full-test.py
url = download_path.joinpath(relative_path)
filename = relative_path.name
notebook_path = node.get('notebook_download_path', None)
if notebook_path is not None:
notebook_path = Path(notebook_path)
notebook_filename = notebook_path.name
notebook_url = download_path.joinpath(notebook_path)
else:
notebook_filename = None
notebook_url = None
template_str = self.builder.config.getthecode_header_template
template = jinja2.Template(template_str)
self.body.append(template.render(
filename=filename, url=url,
notebook_filename=notebook_filename, notebook_url=notebook_url
))
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
return BaseTranslator.visit_literal_block(self, node)
lang = node.get('language', 'default')
linenos = node.get('linenos', False)
highlight_args = node.get('highlight_args', {})
highlight_args['force'] = node.get('force', False)
if lang is self.builder.config.highlight_language:
# only pass highlighter options for original language
opts = self.builder.config.highlight_options
else:
opts = {}
highlighted = self.highlighter.highlight_block(
node.rawsource, lang, opts=opts, linenos=linenos,
location=(self.builder.current_docname, node.line), **highlight_args
)
_class = 'highlight-{}'.format(lang)
if node.get('hidden', False):
_class += ' highlight-hidden'
starttag = self.starttag(node, 'div', suffix='', CLASS=_class)
self.body.append(starttag + highlighted + '</div>\n')
# Close the top div
self.body.append('</div>\n')
# don't call depart_GetTheCode_html else dump source code
raise nodes.SkipNode
####################################################################################################
def depart_GetTheCode_html(self, node):
# print 'depart_GetTheCode_html'
pass
# BaseTranslator.depart_literal_block(self, node)
# self.body.append('\n</pre>\n')
####################################################################################################
def setup(app):
# https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_js_file
app.add_js_file('getthecode.js') # , async='async'
app.add_config_value('getthecode_header_template', HEADER_TEMPLATE, False)
app.add_node(
GetTheCode,
html=(visit_GetTheCode_html, depart_GetTheCode_html),
# text=(visit_GetTheCode_text, depart_GetTheCode_text),
)
app.add_directive('getthecode', GetTheCodeDirective)
app.connect('doctree-read', process_getthedoc)