Browse Source

Merge pull request #233 from patacrep/cache_yacc

Cache the latex yacc instance
pull/235/head
oliverpool 8 years ago
committed by GitHub
parent
commit
51595895c3
  1. 1
      NEWS.md
  2. 20
      patacrep/latex/syntax.py

1
NEWS.md

@ -15,6 +15,7 @@
* The `meta` directive is now supported: `{meta: COMMANDNAME:arg}` [#220](https://github.com/patacrep/patacrep/pull/220) * The `meta` directive is now supported: `{meta: COMMANDNAME:arg}` [#220](https://github.com/patacrep/patacrep/pull/220)
* LaTeX songs * LaTeX songs
* The `meta` directive is now supported: `\metacrep{COMMANDNAME}{arg}` [#220](https://github.com/patacrep/patacrep/pull/220) * The `meta` directive is now supported: `\metacrep{COMMANDNAME}{arg}` [#220](https://github.com/patacrep/patacrep/pull/220)
* Faster index generation [#233](https://github.com/patacrep/patacrep/pull/233)
# patacrep 5.0.0 # patacrep 5.0.0

20
patacrep/latex/syntax.py

@ -1,6 +1,7 @@
"""Very simple LaTeX parser""" """Very simple LaTeX parser"""
import logging import logging
from functools import lru_cache
import ply.yacc as yacc import ply.yacc as yacc
from patacrep.latex import ast from patacrep.latex import ast
@ -211,15 +212,20 @@ 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(), string,
).parse( lexer=SimpleLexer().lexer,
string, )
lexer=SimpleLexer().lexer,
)
) )
def parse_song(content, filename=None): def parse_song(content, filename=None):
@ -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