Debian Codegraph @

doc/tools/rocqrst/notations/html.py

raw symbols SCIP
1 ##########################################################################
2 ## # The Rocq Prover / The Rocq Development Team ##
3 ## v # Copyright INRIA, CNRS and contributors ##
4 ## <O___,, # (see version control and CREDITS file for authors & dates) ##
5 ## \VV/ ###############################################################
6 ## // # This file is distributed under the terms of the ##
7 ## # GNU Lesser General Public License Version 2.1 ##
8 ## # (see LICENSE file for the text of the license) ##
9 ##########################################################################
10 """A visitor for ANTLR notation ASTs, producing raw HTML.
11
12 Uses the dominate package.
13 """
14
15 from dominate import tags
16 from dominate.utils import text
17
18 from .parsing import parse
19 from .TacticNotationsParser import TacticNotationsParser
20 from .TacticNotationsVisitor import TacticNotationsVisitor
21
22 class TacticNotationsToHTMLVisitor(TacticNotationsVisitor):
23 def visitAlternative(self, ctx:TacticNotationsParser.AlternativeContext):
24 with tags.span(_class='alternative'):
25 self.visitChildren(ctx)
26
27 def visitAltblock(self, ctx:TacticNotationsParser.AltblockContext):
28 with tags.span(_class='alternative-block'):
29 self.visitChildren(ctx)
30
31 def visitAltsep(self, ctx:TacticNotationsParser.AltsepContext):
32 tags.span('\u200b', _class="alternative-separator")
33
34 def visitRepeat(self, ctx:TacticNotationsParser.RepeatContext):
35 with tags.span(_class="repeat-wrapper"):
36 with tags.span(_class="repeat"):
37 self.visitChildren(ctx)
38 repeat_marker = ctx.LGROUP().getText()[1]
39 separator = ctx.ATOM()
40 tags.sup(repeat_marker)
41 if separator:
42 tags.sub(separator.getText())
43
44 def visitCurlies(self, ctx:TacticNotationsParser.CurliesContext):
45 sp = tags.span(_class="curlies")
46 sp.add("{")
47 with sp:
48 self.visitChildren(ctx)
49 sp.add("}")
50
51 def visitAtomic(self, ctx:TacticNotationsParser.AtomicContext):
52 tags.span(ctx.ATOM().getText())
53
54 def visitPipe(self, ctx:TacticNotationsParser.PipeContext):
55 text("|")
56
57 def visitHole(self, ctx:TacticNotationsParser.HoleContext):
58 tags.span(ctx.ID().getText()[1:], _class="hole")
59 sub = ctx.SUB()
60 if sub:
61 tags.sub(sub.getText()[1:])
62
63 def visitEscaped(self, ctx:TacticNotationsParser.EscapedContext):
64 tags.span(ctx.ESCAPED().getText().replace("%", ""))
65
66 def visitWhitespace(self, ctx:TacticNotationsParser.WhitespaceContext):
67 text(" ")
68
69 def htmlize(notation):
70 """Translate notation to a dominate HTML tree"""
71 top = tags.span(_class='notation')
72 with top:
73 TacticNotationsToHTMLVisitor().visit(parse(notation))
74 return top
75
76 def htmlize_str(notation):
77 """Translate notation to a raw HTML document"""
78 # ‘pretty=True’ introduces spurious spaces
79 return htmlize(notation).render(pretty=False)
80
81 def htmlize_p(notation):
82 """Like `htmlize`, wrapped in a ‘p’.
83 Does not return: instead, must be run in a dominate context.
84 """
85 with tags.p():
86 htmlize(notation)