forked from cpitclaudel/alectryon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocutils.py
1546 lines (1284 loc) · 57.7 KB
/
docutils.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright © 2019 Clément Pit-Claudel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# pylint: disable=dangerous-default-value,unused-argument
"""reStructuredText support for Alectryon.
This file defines directives that format their contents using Alectryon.
These directives support various arguments to control the appearance of the
output; check out the README for details.
To use them, call ``docutils_support.register()`` before running your
reStructuredText to HTML converter. The generated code relies on CSS classes
defined in the ``assets/alectryon.css`` file.
A checkbox and an accompanying label (with classes ``alectryon-toggle`` and
``alectryon-toggle-label``) allowing users to reveal all goals and responses at
once is automatically added right before the document's first paragraph. You
can change its location by inserting an explicit ``.. alectryon-toggle::``
directive in your document, and you can ommit it entirely by setting
``AlectryonTransform.auto_toggle`` to ``False`` (to make styling easier, all
contents following the checkbox are wrapped in a container with class
``alectryon-container``).
To replace Pygments' default highlighters with Alectryon's
everywhere, call ``alectryon.pygments.replace_builtin_lexer()``.
If you write lots of inline code snippets, consider calling ``set_default_role``,
which will set the default role to ``:coq:``.
For convenience, ``alectryon.docutils.setup()`` can be used to perform all the
steps above at once.
A note on transforms: Sphinx has a nice API (``app.add_node``) for adding new
node types, so you can write visitors for each output format without creating
new translators. Docutils doesn't have such an API: it forces you to subclass
the default translator instead, which is a pain. The alternative is to use a
transform to replace custom nodes with "raw" nodes, but even this is non-trivial
because the transform doesn't know which output format the document uses.
To work around this issue we use a writer-dependent transform on the docutils
side, and a doctree-resolved event on the Sphinx side.
"""
from typing import Any, ClassVar, DefaultDict, Dict, Iterable, List, Tuple, Type
import re
import os.path
from copy import deepcopy
from collections import namedtuple, defaultdict
from importlib import import_module
import docutils
import docutils.frontend
import docutils.transforms
import docutils.utils
import docutils.writers
from docutils import nodes
from docutils.parsers.rst import directives, roles, states, Directive # type: ignore
from docutils.parsers.rst.directives.body import Topic # type: ignore
from docutils.parsers.rst.directives.misc import Role # type: ignore
from docutils.readers.standalone import Reader as StandaloneReader
from docutils.transforms import Transform
from docutils.writers import html4css1, html5_polyglot, latex2e, xetex
from . import core, transforms, html, latex, markers
from .core import Gensym, Position, PosStr
from .pygments import make_highlighter, added_tokens, validate_style, \
get_lexer, resolve_token, replace_builtin_lexers
# reST extensions
# ===============
def set_line(node, lineno, sm):
node.source, node.line = sm.get_source_and_line(lineno)
# Nodes
# -----
class alectryon_pending(nodes.pending):
pass
class alectryon_pending_toggle(nodes.pending):
pass
class alectryon_pending_mref(nodes.pending):
pass
class alectryon_pending_io(nodes.pending):
pass
class alectryon_pending_quote(nodes.pending):
pass
# Transforms
# ----------
TOGGLE_HTML = """
<input type="checkbox" class="alectryon-toggle" id="alectryon-toggle-{id}" />
<label for="alectryon-toggle-{id}" class="alectryon-toggle-label">
Display all goals and responses
</label>""".replace('\n', '')
LONG_LINE_THRESHOLD = 72
"""Threshold above which to warn about long lines."""
CACHE_DIRECTORY = None
"""Directory in which to store cached annotations."""
CACHE_COMPRESSION = None
"""Which compression to use for cache files.
See the documentation of --cache-compression."""
HTML_MINIFICATION = False
"""Whether to minify generated HTML files."""
def _node_error(document, node, msg):
err = document.reporter.error(msg, base_node=node, line=node.line)
errid = document.set_id(err)
pb = nodes.problematic(node.rawsource, node.rawsource, refid=errid)
pbid = document.set_id(pb)
err.add_backref(pbid)
node.replace_self(pb)
def _format_errors(src, *errs):
msg = "\n".join(map(str, errs))
msg = "\n" + core.indent(msg, " ") if len(errs) > 1 else " " + msg
if isinstance(src, nodes.Node):
src = getattr(src, "text", src.rawsource)
return "In {}:{}".format(src, msg)
def _try(document, fn, node, *args, **kwargs):
try:
return fn(node, *args, **kwargs)
except transforms.CollectedErrors as e:
errs = e.args
except ValueError as e:
errs = [e]
_node_error(document, node, _format_errors(node, *errs))
return None
# LATER: dataclass
class AlectryonState:
def __init__(self, document):
self.drivers_info: List[core.DriverInfo] = []
self.root_is_code: bool = False
self.transforms_executed = set()
self.embedded_assets = []
self.document = document
self._config = None
def populate_config(self):
# Lazy because `document` isn't initialized right away, but cached
# because constructing a ``Config`` mutates the document.
self._config = self._config or Config(self.document)
return self._config
@property
def config(self):
return self.populate_config()
def alectryon_state(document):
st = document.get("alectryon_state")
if st is None:
st = document["alectryon_state"] = AlectryonState(document)
return st
def _sphinx_attr(document, attr):
env = getattr(document.settings, "env", None)
return env and getattr(env, attr)
def _sphinx_app(document):
return _sphinx_attr(document, "app")
def _sphinx_config(document):
return _sphinx_attr(document, "config")
def _docutils_config(document, attr, default=None):
"""Look up `attr` in Sphinx config, falling back to docutils settings."""
value = getattr(document.settings, attr, default)
value = getattr(_sphinx_config(document), attr, value)
return value
def _note_pending(document, node: nodes.pending):
"""Register the transform associated to a pending node."""
app = _sphinx_app(document)
if app and node.transform.is_post_transform:
return # Post-transforms are handled in sphinx.py
document.note_pending(node)
def _gensym_stem(document, suffix=""):
source = document.get('source', "")
return nodes.make_id(os.path.basename(source)) + (source and suffix)
class Config:
@staticmethod
def _token_dict(): # Not a lambda because of pickling
return defaultdict(list)
def __init__(self, document):
self.tokens_by_lang = defaultdict(self._token_dict)
self.language_drivers = AlectryonTransform.LANGUAGE_DRIVERS.copy()
self.driver_args: DefaultDict[str, List[str]] = defaultdict(list)
for nm, args in AlectryonTransform.DRIVER_ARGS.items():
self.driver_args[nm] = list(args)
self.driver_args["sertop"].extend(AlectryonTransform.SERTOP_ARGS)
self.document = document
self.read_docinfo()
def read_docinfo(self):
# Sphinx doesn't translate ``field_list`` to ``docinfo``
selector = lambda n: isinstance(n, (nodes.field_list, nodes.docinfo))
for di in self.document.traverse(selector):
for field in di.traverse(nodes.field):
name, body = field.children
field.text = "`:{}:`".format(name.rawsource)
field.rawsource = ":{}: {}".format(name.rawsource, body.rawsource)
_try(self.document, self.parse_docinfo_field,
field, name.rawsource, body.rawsource)
for di in self.document.traverse(selector):
errors = []
for field in di.traverse(nodes.problematic):
errors.append(field)
field.parent.remove(field)
if errors:
di.parent.insert(di.parent.index(di) + 1, errors)
if not di.children:
di.parent.remove(di)
def parse_docinfo_field(self, node, name, body):
if name.startswith("alectryon/pygments/"):
name = name[len("alectryon/pygments/"):]
if "/" not in name:
name = "coq/" + name # legacy syntax doesn't have coq/
MSG = "Missing language name (did you mean `:alectryon/pygments/{}:`?)."
msg = _format_errors(node, MSG.format(name))
self.document.reporter.warning(msg, base_node=node, line=node.line)
lang, token = name.split("/", maxsplit=1)
resolve_token(token) # Check that this is a valid token
# LATER: It would be nice to support multi-words tokens. Using
# ``shlex.split(body)`` instead of ``body.split()`` would work find
# here, but the filter added by ``added_tokens`` processes words
# (“names”) one by one, so multi-word tokens would never match.
self.tokens_by_lang[lang][token].extend(body.split())
elif name == "alectryon/serapi/args":
import shlex
self.driver_args["sertop"].extend(self.parse_args(shlex.split(body)))
else:
return
node.parent.remove(node)
@staticmethod
def parse_args(args):
import argparse
p = argparse.ArgumentParser(prog=":alectryon/serapi/args:", add_help=False)
p.add_argument("-I", "--ml-include-path", dest="I", metavar="DIR",
nargs=1, action="append", default=[])
p.add_argument("-Q", "--load-path", dest="Q", metavar=("DIR", "COQDIR"),
nargs=2, action="append", default=[])
p.add_argument("-R", "--rec-load-path", dest="R", metavar=("DIR", "COQDIR"),
nargs=2, action="append", default=[])
for (arg, instances) in p.parse_args(args)._get_kwargs():
for vals in instances:
yield "-" + arg
yield ",".join(vals)
def get_driver_class_and_args(self, lang):
driver_name = self.language_drivers[lang]
driver_cls = core.resolve_driver(lang, driver_name)
driver_args = self.driver_args[driver_name]
assert driver_name == driver_cls.ID
return driver_cls, driver_args
def init_driver(self, lang):
cls, args = self.get_driver_class_and_args(lang)
return cls(args, fpath=self.document['source'])
class OneTimeTransform(Transform):
is_post_transform = False
def _apply(self):
raise NotImplementedError()
def _try(self, fn, node, *args, **kwargs):
return _try(self.document, fn, node, *args, **kwargs)
def apply(self, **_kwargs):
# Transforms added by pending() nodes are added multiple times: once per
# directive, and potentially once by add_transform in Sphinx, so we need
# to make sure that running them twice is safe (in particular, we must
# not overwrite the cache).
state = alectryon_state(self.document)
if type(self).__name__ not in state.transforms_executed:
state.transforms_executed.add(type(self).__name__)
self._apply()
class LoadConfigTransform(OneTimeTransform):
"""Process ``field_list`` and ``docinfo`` configuration.
This transform is not strictly necessary: a ``Config`` object will be
initialized anyway when later code calls ``AlectryonState.config``.
The point of this transform it to detect config issues at lint time.
"""
default_priority = 300
def _apply(self):
alectryon_state(self.document).populate_config()
class ActivateMathJaxTransform(OneTimeTransform):
"""Add the ``mathjax_process`` class on math nodes.
This is needed when another part of the pipeline adds mathjax_ignore on the
root of the document to turn off MathJax's math-detection heuristics.
"""
default_priority = 700
@staticmethod
def is_math(node):
return isinstance(node, (nodes.math, nodes.math_block))
def _apply(self, **kwargs):
for node in self.document.traverse(self.is_math):
node.attributes.setdefault("classes", []).append("mathjax_process")
class DocutilsObserver(core.Observer):
def __init__(self, document):
self.document = document
def _notify(self, n: core.Notification):
loc = n.location
beg = dict(line=loc.beg.line, column=loc.beg.col) if loc else {}
end = dict(end_line=loc.end.line, end_column=loc.end.col) if loc and loc.end else {}
self.document.reporter.system_message(n.level, n.message, **beg, **end)
def by_lang(pending_nodes):
partitioned = {}
for node in pending_nodes:
partitioned.setdefault(node.details["lang"], []).append(node)
return dict(sorted(partitioned.items()))
class AlectryonTransform(OneTimeTransform):
default_priority = 800
auto_toggle = True
SERTOP_ARGS = ()
"""DEPRECATED; use DRIVER_ARGS["sertop"] instead."""
LANGUAGE_DRIVERS: Dict[str, str] = core.DEFAULT_DRIVERS
DRIVER_ARGS: Dict[str, Iterable[str]] = {d: () for d in core.ALL_DRIVERS}
def check_for_long_lines(self, node, fragments):
if LONG_LINE_THRESHOLD is None:
return
for linum, s in transforms.find_long_lines(fragments, threshold=LONG_LINE_THRESHOLD):
msg = "Long line ({} characters)\n {}".format(len(s), s)
contents_line = getattr(node, "contents_line", None)
opts = dict(line=contents_line + linum) if contents_line else {}
w = self.document.reporter.warning(msg, base_node=node, **opts)
# We want a message on the command line but not in the document, so
# remove the node created by ``Reporter.system_message``:
self.document.transform_messages.remove(w)
def annotate(self, pending_nodes, lang, cache):
driver = alectryon_state(self.document).config.init_driver(lang)
driver.observer = DocutilsObserver(self.document)
chunks = [pending.details["contents"] for pending in pending_nodes]
annotated = cache.update(chunks, driver)
return cache.driver_info, annotated
def replace_node(self, pending, fragments, lang):
directive_annots = pending.details["directive_annots"]
fragments = transforms.inherit_io_annots(fragments, directive_annots)
fragments = transforms.default_transform(fragments, lang=lang, delay_errors=True)
self.check_for_long_lines(pending, fragments)
details = {**pending.details, "fragments": fragments}
io = alectryon_pending_io(AlectryonPostTransform, details)
_note_pending(self.document, io)
pending.replace_self(io)
def apply_drivers(self):
from .json import CacheSet
state = alectryon_state(self.document)
all_pending = self.document.traverse(alectryon_pending)
with CacheSet(CACHE_DIRECTORY, self.document['source'], CACHE_COMPRESSION) as caches:
for lang, pending_nodes in by_lang(all_pending).items():
driver_info, annotated = self.annotate(pending_nodes, lang, caches[lang])
state.drivers_info.append(driver_info)
for node, fragments in zip(pending_nodes, annotated):
self._try(self.replace_node, node, fragments, lang)
@staticmethod
def split_around(node):
parent = node.parent
idx = node.parent.index(node)
return parent.children[:idx], node, parent.children[idx + 1:]
@staticmethod
def insert_toggle_after(node, toggle, keep_node):
pre, node, post = AlectryonTransform.split_around(node)
if keep_node:
pre.append(node)
pre.append(toggle)
pre.append(nodes.container('', *post, classes=['alectryon-container']))
node.parent.children = pre
def apply_toggle(self):
toggle = lambda id: nodes.raw('', TOGGLE_HTML.format(id=id), format='html')
toggles = list(self.document.traverse(alectryon_pending_toggle))
for idx, node in enumerate(toggles):
self.insert_toggle_after(node, toggle(idx), False)
self.auto_toggle = False
if self.auto_toggle:
di = self.document.next_node(nodes.docinfo)
if di:
self.insert_toggle_after(di, toggle(0), True)
def _apply(self):
self.apply_drivers()
self.apply_toggle()
class CounterStyle(namedtuple("CounterStyle", "start digits")):
def fmt(self, num):
raise NotImplementedError
@staticmethod
def of_str(style):
digits = tuple(style.split())
if len(digits) < 2:
raise ValueError("Invalid counter style: {}".format(style))
if digits[0] == "_":
return Alphabetic(0, digits[1:])
return Numeric(1, digits)
class Alphabetic(CounterStyle):
def fmt(self, num):
s, num = "", num + 1 + self.start
while num:
num -= 1
num, rem = divmod(num, len(self.digits))
s = self.digits[rem] + s
return s
class Numeric(CounterStyle):
def fmt(self, num):
s, num = "", num + self.start
while num:
num, rem = divmod(num, len(self.digits))
s = self.digits[rem] + s
return s or self.digits[0]
class RefCounter:
def __init__(self):
self.counters = defaultdict(lambda: -1)
def next(self, style):
num = self.counters[style] = self.counters[style] + 1
return style.fmt(num)
class AlectryonMrefTransform(OneTimeTransform):
"""Convert Alectryon input/output pairs into HTML or LaTeX.
This transform is triggered by a ``pending`` node added by
the ``:mref:`` role.
"""
default_priority = 810
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.refcounter = RefCounter()
self.gensym = Gensym(_gensym_stem(self.document, "-"))
@classmethod
def _validate_target(cls, target):
if not target:
raise ValueError("Target is null")
@staticmethod
def _find_mref_io(path, ios, last_io):
io_name = path["io"]
io = ios.get(io_name) if io_name else last_io
if io is None:
if io_name:
raise ValueError("Reference to unknown Alectryon block.")
raise ValueError("Not sure which code block this refers to; "
"add ``.io#…`` to disambiguate.")
return io
@classmethod
def _find_mref_target(cls, path, io):
fragments = io.details["fragments"]
# LATER: Add a way to name sentences to make them easier to select
sentences = (fr for fr in fragments if isinstance(fr, core.RichSentence))
sentence = markers.find_one("sentence", markers.find_sentences, sentences, path["s"])
if "in" in path:
return sentence
if "msg" in path:
msgs = list(transforms.fragment_messages(sentence))
return markers.find_one("message", markers.find_contents, msgs, path["msg"])
if "g" in path:
goals = list(transforms.fragment_goals(sentence))
goal = markers.find_one("goal", markers.find_goals, goals, path["g"])
if "ccl" in path:
return goal.conclusion
if "h" in path:
hyps = goal.hypotheses
hyp = markers.find_one("hypothesis", markers.find_hyps, hyps, path["h"])
# Unfold to ensure visibility (but only if search succeeded)
if sentence.annots.unfold is None:
sentence.annots.unfold = True
goal.props.setdefault("unfold", True)
if "type" in path:
return hyp.type
if "body" in path:
return hyp.body
if "name" in path:
return hyp.names
return hyp
if "name" in path:
return goal.name
return goal
return sentence
def format_one_ref(self, target, node):
if not target.ids:
target_id = nodes.make_id(node.details["target"])
target.ids.append(self.gensym(target_id + "-")) # “-” avoids collisions
if not target.markers:
style = node.details["counter-style"]
target.markers.append(node.details["title"] or self.refcounter.next(style))
marker, refid = target.markers[-1], target.ids[-1]
return nodes.reference(node.rawsource, marker,
classes=["alectryon-mref"], refid=refid)
def format_one_quote(self, io, target, node):
if isinstance(target, core.RichSentence):
target = target.input
details = {**node.details, "lang": io.details["lang"], "target": target}
node = alectryon_pending_quote(AlectryonPostTransform, details, node.rawsource)
_note_pending(self.document, node)
return node
def replace_one_mref(self, node, ios, last_io):
kind, path = node.details["kind"], node.details["path"]
io = self._find_mref_io(path, ios, last_io)
target = self._find_mref_target(path, io)
self._validate_target(target)
if kind == "ref":
repl = self.format_one_ref(target, node)
elif kind == "quote":
repl = self.format_one_quote(io, target, node)
elif kind == "assert":
repl = None
else:
assert False
if repl:
node.replace_self(repl)
else:
node.parent.remove(node)
def _apply(self, **_kwargs):
ios = {id: node
for node in self.document.traverse(alectryon_pending_io)
for id in node.get("ids", ())}
last_io = None
io_or_mref = lambda n: isinstance(n, (alectryon_pending_io, alectryon_pending_mref))
for node in self.document.traverse(io_or_mref):
if isinstance(node, alectryon_pending_io):
last_io = node
elif isinstance(node, alectryon_pending_mref):
self._try(self.replace_one_mref, node, ios, last_io)
class AlectryonPostTransform(OneTimeTransform):
"""Convert Alectryon input/output pairs into HTML or LaTeX.
This transform is triggered by a ``pending`` node added by
``AlectryonTransform``. See ``docutils.components.transforms.Filter``.
"""
default_priority = 820
is_post_transform = True
def _formats(self):
app = _sphinx_app(self.document)
if app:
# https://github.com/sphinx-doc/sphinx/issues/9632: Sphinx sets
# ``document.transformer`` to ``None`` when reading from cache and
# ``transformer.components`` to ``[]`` when writing, so we can't use
# the writer's list of supported formats when compiling with Sphinx.
return app.tags
return self.document.transformer.components['writer'].supported
def init_generator(self):
formats = set(self._formats())
style = _docutils_config(self.document, "pygments_style")
if 'html' in formats:
highlighter = make_highlighter("html", None, style)
return "html", html.HtmlGenerator(
highlighter, _gensym_stem(self.document), HTML_MINIFICATION)
if {'latex', 'xelatex', 'lualatex'} & formats:
highlighter = make_highlighter("latex", None, style)
return "latex", latex.LatexGenerator(highlighter)
raise NotImplementedError("Unknown output format")
@staticmethod
def replace_one(node, fmt, rawtext, gen, *args, **kwargs):
ids = node.attributes.get("ids", ())
classes = node.attributes.pop("classes", ()) # visit_raw adds a <div> if it finds classes
dom = gen(*args, ids=ids, classes=classes, **kwargs)
node.replace_self(nodes.raw(rawtext, dom.render(pretty=False), format=fmt))
@classmethod
def replace_one_io(cls, node, fmt, generator):
fragments, contents = node.details["fragments"], node.details["contents"]
if transforms.all_hidden(fragments, node.details["directive_annots"]):
node.parent.remove(node) # Remove ``.. coq:: none`` blocks
else:
cls.replace_one(node, fmt, contents, generator.gen_fragments, fragments)
@classmethod
def replace_one_quote(cls, node, fmt, generator):
target = deepcopy(node.details["target"])
# LATER don't strip markers when block-quoting a full goal or hypothesis
target = transforms.strip_ids_and_props(target, {"enabled", "markers"})
with generator.highlighter.override(lang=node.details["language"]):
cls.replace_one(node, fmt, node.details["path"], generator.gen_part,
target, inline=node.details["inline"])
@classmethod
def replace_io_or_quote(cls, node, fmt, generator):
if isinstance(node, alectryon_pending_io):
cls.replace_one_io(node, fmt, generator)
elif isinstance(node, alectryon_pending_quote):
cls.replace_one_quote(node, fmt, generator)
else:
assert False
def _apply(self, **_kwargs):
io_or_quote = lambda n: isinstance(n, (alectryon_pending_io, alectryon_pending_quote))
all_pending = self.document.traverse(io_or_quote)
fmt, generator = self.init_generator() # Init once so gensym is shared
for lang, pending_nodes in by_lang(all_pending).items():
config = alectryon_state(self.document).config
with generator.highlighter.override(lang=lang):
with added_tokens(config.tokens_by_lang[lang], lang):
for node in pending_nodes:
self.replace_io_or_quote(node, fmt, generator)
# Directives
# ----------
INDENTATION_RE = re.compile(r"^ *(?=[^\s])")
def measure_indentation(line):
m = INDENTATION_RE.match(line)
return m.end() - m.start() if m else None
def measure_min_indentation(lines):
indents = (measure_indentation(l) for l in lines)
return min((i for i in indents if i is not None), default=0)
def recompute_contents(directive, real_indentation):
"""Compute the contents of `directive` relative to `real_indentation`.
This undoes the automatic gobbling performed by the reST parser, which is
useful when a Coq fragment is split across multiple code blocks; in these
cases reST's automatic gobbling would unindent all lines. Here is a
concrete example (reST renders it with all lines flushed left)::
.. code::
int main() {
.. code::
return 0;
.. code::
}
But beware: with alternative input languages like reCommonMark or MyST,
there's no guarantee that the contents are indented by at least three
spaces, so we must also measure the minimum indentation and respect that.
"""
if directive.content_offset <= directive.lineno: # MyST bug
return (0, "\n".join(directive.content))
block_lines = directive.block_text.splitlines()
block_header_len = directive.content_offset - directive.lineno + 1
header_indentation = measure_indentation(directive.block_text)
assert header_indentation is not None
body_lines = block_lines[block_header_len:]
min_indentation = measure_min_indentation(body_lines)
body_indentation = min(header_indentation + real_indentation, min_indentation)
contents = "\n".join(ln[body_indentation:] for ln in body_lines)
return body_indentation, contents
class AlectryonDirective(Directive): # pylint: disable=abstract-method
def _error(self, msg, line=None):
line = self.lineno if line is None else line
msg = 'Error in "{}" directive:\n{}'.format(self.name, msg)
literal = nodes.literal_block(self.block_text, self.block_text)
err = self.state_machine.reporter.error(msg, literal, line=self.lineno)
return [err]
def _try(self, fn, *args, default=None):
try:
return fn(*args), []
except ValueError as e:
return default, self._error(str(e))
class ProverDirective(AlectryonDirective):
"""Highlight and annotate a code snippet."""
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
option_spec = {'class': directives.class_option,
'name': directives.unchanged}
has_content = True
EXPECTED_INDENTATION = 3
@property
def header(self):
return "`{}`".format(self.block_text.partition('\n')[0])
def run(self):
self.assert_has_content()
document = self.state_machine.document
annotstr = " ".join(self.arguments)
annots, errors = self._try(transforms.read_all_io_flags,
annotstr, False, default=transforms.IOAnnots())
indent, contents = recompute_contents(self, ProverDirective.EXPECTED_INDENTATION)
source, contents_line = self.state_machine.get_source_and_line(self.content_offset + 1)
col_offset = indent
if document.get('source', "") == source \
and alectryon_state(document).root_is_code:
col_offset = 0
pos = Position(source, contents_line, col_offset)
contents = PosStr(contents, pos, indent)
roles.set_classes(self.options)
details = {"lang": self.name, "directive_annots": annots,
"contents": contents, "contents_line": contents_line}
pending = alectryon_pending(AlectryonTransform, details=details,
rawsource=self.header, **self.options)
set_line(pending, self.lineno, self.state_machine)
self.add_name(pending)
_note_pending(document, pending)
return [pending] + errors
def DriverDirective(lang: str):
return type("{}Directive".format(lang.capitalize()),
(ProverDirective,),
{"name": lang})
DRIVER_DIRECTIVES = [DriverDirective(lang) for lang in core.ALL_LANGUAGES]
class Lean3Directive(ProverDirective):
"""Highlight and annotate a Lean 3 snippet."""
name = "lean3"
class Lean4Directive(ProverDirective):
"""Highlight and annotate a Lean 4 snippet."""
name = "lean4"
class AlectryonToggleDirective(Directive):
"""Display a checkbox allowing readers to show all output at once."""
name = "alectryon-toggle"
required_arguments = 0
optional_arguments = 0
option_spec: Dict[str, Any] = {}
has_content = False
def run(self):
pending = alectryon_pending_toggle(AlectryonTransform)
set_line(pending, self.lineno, self.state_machine)
_note_pending(self.state_machine.document, pending)
return [pending]
# This is just a small example
class ExperimentalExerciseDirective(Topic, AlectryonDirective):
"""Introduce an exercise."""
name = "exercise"
required_arguments = 1
option_spec = {**Topic.option_spec,
"difficulty": directives.nonnegative_int,
"optional": directives.flag}
def run(self):
[node] = super().run()
node['difficulty'] = difficulty = self.options.get('difficulty')
node['optional'] = self.options.get('optional', False)
if not difficulty:
return self._error("Missing required option ':difficulty:'")
for title in node.traverse(nodes.title):
title.children.insert(0, nodes.Text("Exercise: "))
return [node]
def directive_without_arguments(directive):
"""Create a fake directive sharing `directive`'s options to """
return type("Converted", (directive,),
dict(has_content=False,
required_arguments=0,
optional_arguments=0))
# Derived from docutils.directives.misc.Role (public domain)
# LATER: Move to upstream
class DirectiveDirective(Directive): # pragma: no cover
"""Define an alias of a directive."""
name = "directive"
has_content = True
def run(self):
if self.content_offset > self.lineno or not self.content:
raise self.error('"%s" directive requires arguments on the first '
'line.' % self.name)
args = self.content[0]
match = Role.argument_pattern.match(args)
if not match or not match.group(3):
raise self.error('"%s" directive arguments not valid role names: '
'"%s".' % (self.name, args))
new_name, base_name = match.group(1), match.group(3)
messages = []
base, messages = directives.directive(
base_name, self.state_machine.language, self.state_machine.document)
if base is None:
error = self.state.reporter.error(
'Unknown directive "%s".' % base_name,
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return messages + [error]
try:
converted = directive_without_arguments(base)
(_arguments, options, _content, _content_offset) = (
self.state.parse_directive_block(
self.content[1:], self.content_offset, converted,
option_presets={}))
except states.MarkupError as detail:
error = self.state_machine.reporter.error(
'Error in "%s" directive:\n%s.' % (self.name, detail),
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return messages + [error]
if 'class' not in options:
try:
options['class'] = directives.class_option(new_name)
except ValueError as detail:
error = self.state_machine.reporter.error(
'Invalid argument for "%s" directive:\n%s.'
% (self.name, detail), nodes.literal_block(
self.block_text, self.block_text), line=self.lineno)
return messages + [error]
# FIXME convert `base` if it's a function instead of a class
class CustomDirective(base):
def run(self):
self.options = {**options, **self.options} # pylint: disable=attribute-defined-outside-init
return super().run()
# FIXME this leaks across documents
directives.register_directive(new_name, CustomDirective)
return messages
# Roles
# -----
def alectryon_bubble(role, rawtext, text, lineno, inliner,
options: Dict[str, Any]={}, content=[]):
node = nodes.inline(rawtext, classes=['alectryon-bubble'])
set_line(node, lineno, inliner.reporter)
return [node], []
alectryon_bubble.name = "alectryon-bubble" # type: ignore
def mk_code_role(lang):
def code_role(role, rawtext, text, lineno, inliner,
options: Dict[str, Any]={}, content=[]):
options = {**options, "language": lang}
roles.set_classes(options)
options.setdefault("classes", []).append("highlight")
return roles.code_role(role, rawtext, text, lineno, inliner, options, content)
code_role.name = lang
return code_role
CODE_ROLES = {lang: mk_code_role(lang) for lang in core.ALL_LANGUAGES}
COQ_ID_RE = re.compile(r"^(?P<title>.*?)(?:\s*<(?P<target>.*)>)?$")
COQ_IDENT_DB_URLS = [
("Coq", "https://coq.inria.fr/library/$modpath.html#$ident")
]
def _role_error(inliner, rawtext, msg, lineno):
msg = _format_errors(rawtext, msg)
err = inliner.reporter.error(msg, line=lineno)
return [inliner.problematic(rawtext, rawtext, err)], [err]
def _parse_ref(text):
mid = COQ_ID_RE.match(text)
title, target = mid.group("title"), mid.group("target")
return title, target
def coq_id_role(role, rawtext, text, lineno, inliner,
options: Dict[str, Any]={}, content=[]):
title, target = _parse_ref(text)
implicit = target is None
if implicit:
target = title
if "#" in target:
modpath, ident = target.rsplit("#", 1)
if implicit:
# Convert `A#b` to `b` and `A#` to `A`
title = ident if ident else modpath
elif "." in target:
modpath, ident = target.rsplit(".", 1)
else:
modpath, ident = "", target
# Options are set using the ‘.. role’ directive
url = options.get('url', None)
if url is None:
if not modpath:
msg = "{target!r} is not a fully-qualified name.".format(target=target)
return _role_error(inliner, rawtext, msg, lineno)
for prefix, url in COQ_IDENT_DB_URLS:
if prefix == modpath or modpath.startswith(prefix + "."):
break
else:
MSG = ("Not sure where to find documentation for {target}.\n"
"Make sure that ‘{target}’ is fully qualified"
" and that Alectryon knows where to find it.\n"
"Known prefixes: {prefixes}\n"
"(Add prefixes to alectryon.docutils.COQ_IDENT_DB_URLS or"
" derive a new role from ‘coqid’ with a custom :url:).")
prefixes = [prefix for (prefix, _) in COQ_IDENT_DB_URLS]
msg = MSG.format(target=target, prefixes=prefixes)
return _role_error(inliner, rawtext, msg, lineno)
from string import Template
uri = Template(url).safe_substitute(modpath=modpath, ident=ident)
roles.set_classes(options)
node = nodes.reference(rawtext, title, refuri=uri, **options)
set_line(node, lineno, inliner.reporter)
return [node], []
coq_id_role.name = "coqid" # type: ignore
coq_id_role.options = {'url': directives.unchanged} # type: ignore
COUNTER_STYLES = {
'decimal': '0 1 2 3 4 5 6 7 8 9',
'lower-alpha': '_ a b c d e f g h i j k l m n o p q r s t u v w x y z',
'upper-alpha': '_ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z',
'lower-greek': '_ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω',
'upper-greek': '_ Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω',
}
DEFAULT_COUNTER_STYLE = CounterStyle.of_str(COUNTER_STYLES['decimal'])
MREF_KINDS = ['ref', 'quote']
def _parse_mref_target(kind, target, prefix):
if target[0] in "#." or kind == "quote":