| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
aces |
| 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) |