Browse Source

cache the latex yacc instance

pull/233/head
Oliverpool 9 years ago
parent
commit
e28eb51988
  1. 14
      patacrep/latex/syntax.py

14
patacrep/latex/syntax.py

@ -2,6 +2,7 @@
import logging import logging
import ply.yacc as yacc import ply.yacc as yacc
from functools import lru_cache
from patacrep.latex import ast from patacrep.latex import ast
from patacrep.latex.detex import detex from patacrep.latex.detex import detex
@ -211,12 +212,17 @@ def silent_yacc(*args, **kwargs):
**kwargs **kwargs
) )
@lru_cache()
def latex_yacc(filename=None):
"""Call the yacc with the LaTeX parser.
"""
parser = silent_yacc(module=LatexParser(filename))
return parser
def tex2plain(string): def tex2plain(string):
"""Parse string and return its plain text version.""" """Parse string and return its plain text version."""
return detex( return detex(
silent_yacc( latex_yacc().parse(
module=LatexParser(),
).parse(
string, string,
lexer=SimpleLexer().lexer, lexer=SimpleLexer().lexer,
) )
@ -231,7 +237,7 @@ def parse_song(content, filename=None):
display error messages. display error messages.
""" """
return detex( return detex(
silent_yacc(module=LatexParser(filename)).parse( latex_yacc(filename).parse(
content, content,
lexer=SongLexer().lexer, lexer=SongLexer().lexer,
).metadata ).metadata

Loading…
Cancel
Save