From 315915221ce241c4b7f6848fb360c48590c974e1 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 16 Jun 2014 07:34:55 +0200 Subject: [PATCH] Added \obeylines command to verse, verse* and chorus environments. --- songbook_core/data/latex/songs_minimal.sty | 8 ++++++ songbook_core/errors.py | 12 ++++++++ songbook_core/plastex.py | 32 +++++++++++++++++++--- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 songbook_core/data/latex/songs_minimal.sty diff --git a/songbook_core/data/latex/songs_minimal.sty b/songbook_core/data/latex/songs_minimal.sty new file mode 100644 index 00000000..7c3b8a51 --- /dev/null +++ b/songbook_core/data/latex/songs_minimal.sty @@ -0,0 +1,8 @@ +% Minimal songs.sty package, to be loaded by the songbook_core library, by the +% PlasTeX packages. Some songs.sty commands are implemented in Python, in the +% songbook_core/plastex*.py packages, and some commands are implemented in this +% file. + +\newenvironment{chorus}{\obeylines}{} +\newenvironment{verse}{\obeylines}{} +\newenvironment{verse*}{\obeylines}{} diff --git a/songbook_core/errors.py b/songbook_core/errors.py index b8e73466..27b15775 100644 --- a/songbook_core/errors.py +++ b/songbook_core/errors.py @@ -85,3 +85,15 @@ class UnknownStep(SongbookError): def __str__(self): return """Compilation step "{step}" unknown.""".format(step=self.step) + +class LatexImportError(SongbookError): + """Could not import package.""" + + def __init__(self, package): + super(LatexImportError, self).__init__() + self.package = package + + def __str__(self): + return """Error while importing LaTeX package "{}".""".format( + self.package + ) diff --git a/songbook_core/plastex.py b/songbook_core/plastex.py index b743bd00..0d4e14eb 100644 --- a/songbook_core/plastex.py +++ b/songbook_core/plastex.py @@ -11,6 +11,9 @@ import locale import os import sys +from songbook_core import __DATADIR__ +from songbook_core import errors + def process_unbr_spaces(node): r"""Replace '~' and '\ ' in node by nodes that @@ -49,16 +52,37 @@ class SongParser(object): """Analyseur syntaxique de fichiers .sg""" @staticmethod - def create_tex(): + def load_package(tex, package): + """Wrapper to 'tex.ownerDocument.context.loadPackage' catching errors. + """ + if not tex.ownerDocument.context.loadPackage(tex, package): + raise errors.LatexImportError(package) + + @classmethod + def create_tex(cls): """Create a TeX object, ready to parse a tex file.""" tex = TeX() tex.disableLogging() tex.ownerDocument.context.loadBaseMacros() + + # Setting paths sys.path.append(os.path.dirname(__file__)) - tex.ownerDocument.context.loadPackage(tex, "plastex_patchedbabel") - tex.ownerDocument.context.loadPackage(tex, "plastex_chord") - tex.ownerDocument.context.loadPackage(tex, "plastex_songs") + old_texinputs = os.environ.get("TEXINPUTS", '') + os.environ["TEXINPUTS"] = "{}:{}".format( + old_texinputs, + os.path.join(__DATADIR__, "latex"), + ) + + # Loading packages + cls.load_package(tex, "plastex_patchedbabel") + cls.load_package(tex, "plastex_chord") + cls.load_package(tex, "plastex_songs") + cls.load_package(tex, "songs_minimal.sty") + + # Unsetting paths sys.path.pop() + os.environ["TEXINPUTS"] = old_texinputs + return tex @classmethod