import string from funcparserlib lexer import make_tokenizer Token fro

 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
import string
from funcparserlib.lexer import make_tokenizer, Token
from funcparserlib.parser import some, a, skip, many
SPECS = [
('newline', (r'\n+', )),
('markup', (r'>>|<<|\(|\)', )),
('string', (r'[^\n|(|)|>|<]+', )),
]
tokenize = lambda x: list(make_tokenizer(SPECS)(x))
const = lambda s: lambda _: s
tokval = lambda tok: tok.value
join = string.join
markup = lambda s: skip(a(Token('markup', s)))
literal = some(lambda tok: tok.type == 'string') >> tokval
newline = some(lambda tok: tok.type == 'newline') >> const('<br>\n')
phrase = many(literal|newline) >> join
paren = (markup('(') + phrase + markup(')')
>> (lambda tok: '<span class="paren">(' + tok + ')</span>'))
chorus = (markup('>>') + many(paren|phrase) + markup('<<')
>> (lambda tok: '<div class="chorus">' + join(tok) + '</div>'))
top = many(chorus|paren|phrase) >> join