Browse Source

Language errors are now added to song errors

pull/176/head
Louis 9 years ago
parent
commit
723cf9e805
  1. 13
      patacrep/latex/__init__.py
  2. 32
      patacrep/songs/chordpro/__init__.py
  3. 2
      patacrep/songs/errors.py
  4. 14
      patacrep/templates.py

13
patacrep/latex/__init__.py

@ -78,7 +78,7 @@ BABEL_LANGUAGES = OrderedDict((
# ('??_??', 'welsh'), # ('??_??', 'welsh'),
)) ))
class UnknownLanguage(Exception): class UnknownLanguage(errors.SharedError):
"""Error: Unknown language.""" """Error: Unknown language."""
def __init__(self, *, original, fallback, message): def __init__(self, *, original, fallback, message):
@ -87,6 +87,11 @@ class UnknownLanguage(Exception):
self.fallback = fallback self.fallback = fallback
self.message = message self.message = message
@property
def babel(self):
"""Return the fallback babel language."""
return BABEL_LANGUAGES[self.fallback]
def __str__(self): def __str__(self):
return self.message return self.message
@ -128,10 +133,12 @@ def checklanguage(lang):
) )
) )
def lang2babel(lang): def lang2babel(lang, *, raise_unknown=False):
"""Return the language used by babel, corresponding to the language code""" """Return the language used by babel, corresponding to the language code"""
try: try:
return BABEL_LANGUAGES[checklanguage(lang)] return BABEL_LANGUAGES[checklanguage(lang)]
except UnknownLanguage as error: except UnknownLanguage as error:
LOGGER.warning(str(error)) LOGGER.warning(str(error))
return BABEL_LANGUAGES[error.fallback] if raise_unknown:
raise
return error.babel

32
patacrep/songs/chordpro/__init__.py

@ -4,14 +4,15 @@ import logging
import operator import operator
import os import os
from jinja2 import Environment, FileSystemLoader, contextfunction, ChoiceLoader from jinja2 import Environment, FileSystemLoader, ChoiceLoader
from jinja2 import contextfunction
import jinja2 import jinja2
from patacrep import encoding, files, pkg_datapath from patacrep import encoding, files, pkg_datapath
from patacrep.songs import Song from patacrep.songs import Song
from patacrep.songs.chordpro.syntax import parse_song from patacrep.songs.chordpro.syntax import parse_song
from patacrep.templates import Renderer from patacrep.templates import Renderer
from patacrep.latex import lang2babel from patacrep.latex import lang2babel, UnknownLanguage
from patacrep.files import path2posix from patacrep.files import path2posix
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -39,6 +40,11 @@ class ChordproSong(Song):
'song': song, 'song': song,
} }
@staticmethod
def _jinja2_filters():
"""Return additional jinja2 filters."""
return {}
def render(self, template="song"): # pylint: disable=arguments-differ def render(self, template="song"): # pylint: disable=arguments-differ
context = { context = {
'lang': self.lang, 'lang': self.lang,
@ -54,9 +60,9 @@ class ChordproSong(Song):
)) ))
jinjaenv.filters['search_image'] = self.search_image jinjaenv.filters['search_image'] = self.search_image
jinjaenv.filters['search_partition'] = self.search_partition jinjaenv.filters['search_partition'] = self.search_partition
jinjaenv.filters['lang2babel'] = lang2babel
jinjaenv.filters['sortargs'] = sort_directive_argument jinjaenv.filters['sortargs'] = sort_directive_argument
jinjaenv.filters['path2posix'] = path2posix jinjaenv.filters['path2posix'] = path2posix
jinjaenv.filters.update(self._jinja2_filters())
try: try:
return Renderer( return Renderer(
@ -123,6 +129,26 @@ class Chordpro2LatexSong(ChordproSong):
) )
return None return None
def _jinja2_filters(self):
return {
'lang2babel': self.lang2babel,
}
def lang2babel(self, lang):
"""Return the LaTeX babel code corresponding to `lang`.
Add an error to the list of errors if argument is invalid.
"""
try:
return lang2babel(lang, raise_unknown=True)
except UnknownLanguage as error:
error.message = "Song {}: {}".format(
self.fullpath,
error.message,
)
self.errors.append(error)
return error.babel
class Chordpro2ChordproSong(ChordproSong): class Chordpro2ChordproSong(ChordproSong):
"""Render chordpro song to chordpro code""" """Render chordpro song to chordpro code"""

2
patacrep/songs/errors.py

@ -31,5 +31,3 @@ class SongSyntaxError(SongError):
# class FileError(SongError): # class FileError(SongError):
# type = "file" # type = "file"
#
# class LanguageError(SongError):

14
patacrep/templates.py

@ -84,11 +84,19 @@ class Renderer:
self.jinjaenv.filters['escape_tex'] = _escape_tex self.jinjaenv.filters['escape_tex'] = _escape_tex
self.jinjaenv.trim_blocks = True self.jinjaenv.trim_blocks = True
self.jinjaenv.lstrip_blocks = True self.jinjaenv.lstrip_blocks = True
self.jinjaenv.filters["path2posix"] = files.path2posix self._fill_filters()
self.jinjaenv.filters["iter_datadirs"] = files.iter_datadirs
self.jinjaenv.filters["lang2babel"] = lang2babel
self.template = self.jinjaenv.get_template(template) self.template = self.jinjaenv.get_template(template)
def _fill_filters(self):
"""Define some jinja2 filters, if not set yet."""
for key, value in [
("path2posix", files.path2posix),
("iter_datadirs", files.iter_datadirs),
("lang2babel", lang2babel),
]:
if key not in self.jinjaenv.filters:
self.jinjaenv.filters[key] = value
class TexBookRenderer(Renderer): class TexBookRenderer(Renderer):
"""Tex renderer for the whole songbook""" """Tex renderer for the whole songbook"""

Loading…
Cancel
Save