Skip to content

Commit

Permalink
Reorder things a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rblank committed Dec 15, 2024
1 parent 596c1f5 commit 9bbfd1e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
66 changes: 33 additions & 33 deletions tdoc/common/ext/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,20 @@


def setup(app):
app.add_node(exec, html=(visit_exec, depart_exec))
app.add_directive('exec', Exec)
app.add_node(exec, html=(visit_exec, depart_exec))
app.connect('builder-inited', ExecCollector.init)
app.add_env_collector(ExecCollector)
app.connect('doctree-resolved', check_references)
app.connect('html-page-context', add_js)
app.connect('write-started', write_static_files)
app.connect('html-page-context', add_js)
return {
'version': __version__,
'parallel_read_safe': True,
'parallel_write_safe': True,
}


class exec(nodes.literal_block): pass


def visit_exec(self, node):
try:
return self.visit_literal_block(node)
except nodes.SkipNode:
after = node.get('after')
then = node.get('then')
def subst(m): return f'{m.group(1)} {attrs}{m.group(2)}'
attrs = format_data_attrs(self,
after=' '.join(after) if after else None,
editor=node.get('editor'),
output_style=node.get('output-style'),
then=' '.join(then) if then else None,
when=node.get('when'))
if attrs:
self.body[-1] = div_attrs_re.sub(subst, self.body[-1], 1)
if attrs := format_attrs(self, style=node.get('style')):
self.body[-1] = pre_attrs_re.sub(subst, self.body[-1], 1)
raise


def depart_exec(self, node):
return self.depart_literal_block(node)


class Exec(code.CodeBlock):
languages = {'html', 'python', 'sql'}

Expand Down Expand Up @@ -115,6 +88,9 @@ def _update_node(self, node):
node['editor'] = ''


class exec(nodes.literal_block): pass


class ExecCollector(collectors.EnvironmentCollector):
@staticmethod
def init(app):
Expand Down Expand Up @@ -164,10 +140,6 @@ def check_refs(node, names, typ, doctree):
base_node=node)


div_attrs_re = re.compile(r'(?s)^(<div[^>]*)(>.*)$')
pre_attrs_re = re.compile(r'(?s)^(.*<pre[^>]*)(>.*)$')


def add_js(app, page, template, context, doctree):
if doctree:
for lang in sorted(Exec.find_nodes(doctree)):
Expand Down Expand Up @@ -196,3 +168,31 @@ def write_static_files(app, builder):
ct = zipfile.ZIP_DEFLATED if data else zipfile.ZIP_STORED
f.writestr(zipfile.ZipInfo(f'tdoc/{rel(path)}'),
data, compress_type=ct, compresslevel=9)


div_attrs_re = re.compile(r'(?s)^(<div[^>]*)(>.*)$')
pre_attrs_re = re.compile(r'(?s)^(.*<pre[^>]*)(>.*)$')


def visit_exec(self, node):
try:
return self.visit_literal_block(node)
except nodes.SkipNode:
after = node.get('after')
then = node.get('then')
def subst(m): return f'{m.group(1)} {attrs}{m.group(2)}'
attrs = format_data_attrs(self,
after=' '.join(after) if after else None,
editor=node.get('editor'),
output_style=node.get('output-style'),
then=' '.join(then) if then else None,
when=node.get('when'))
if attrs:
self.body[-1] = div_attrs_re.sub(subst, self.body[-1], 1)
if attrs := format_attrs(self, style=node.get('style')):
self.body[-1] = pre_attrs_re.sub(subst, self.body[-1], 1)
raise


def depart_exec(self, node):
return self.depart_literal_block(node)
10 changes: 5 additions & 5 deletions tdoc/common/ext/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setup(app):
app.add_enumerable_node(num, 'num', lambda n: '<num_title>',
html=(visit_num, depart_num))
app.connect('config-inited', update_numfig_format)
app.connect('builder-inited', init_env)
app.connect('builder-inited', NumCollector.init)
app.add_env_collector(NumCollector)
app.connect('env-get-updated', number_per_namespace, priority=999)
app.connect('doctree-resolved', update_num_nodes)
Expand Down Expand Up @@ -48,10 +48,6 @@ def update_numfig_format(app, config):
numfig_format[k] = NoNum()


def init_env(app):
app.env.tdoc_nums = {}


class Num(docutils.ReferenceRole):
def run(self):
node = num()
Expand All @@ -70,6 +66,10 @@ class num(nodes.Inline, nodes.TextElement): pass


class NumCollector(collectors.EnvironmentCollector):
@staticmethod
def init(app):
app.env.tdoc_nums = {}

def clear_doc(self, app, env, docname):
env.tdoc_nums.pop(docname, None)

Expand Down

0 comments on commit 9bbfd1e

Please sign in to comment.