|
@ -14,7 +14,6 @@ from patacrep.songs.chordpro.syntax import parse_song |
|
|
from patacrep.songs.errors import FileNotFound, SongUnknownLanguage |
|
|
from patacrep.songs.errors import FileNotFound, SongUnknownLanguage |
|
|
from patacrep.templates import Renderer |
|
|
from patacrep.templates import Renderer |
|
|
from patacrep.latex import lang2babel, UnknownLanguage |
|
|
from patacrep.latex import lang2babel, UnknownLanguage |
|
|
from patacrep.files import path2posix |
|
|
|
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__) |
|
|
LOGGER = logging.getLogger(__name__) |
|
|
|
|
|
|
|
@ -22,6 +21,10 @@ def sort_directive_argument(directives): |
|
|
"""Sort directives by their argument.""" |
|
|
"""Sort directives by their argument.""" |
|
|
return sorted(directives, key=operator.attrgetter("argument")) |
|
|
return sorted(directives, key=operator.attrgetter("argument")) |
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_FILTERS = { |
|
|
|
|
|
'sortargs': sort_directive_argument, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
class ChordproSong(Song): |
|
|
class ChordproSong(Song): |
|
|
"""Chordpro song parser""" |
|
|
"""Chordpro song parser""" |
|
|
# pylint: disable=abstract-method |
|
|
# pylint: disable=abstract-method |
|
@ -41,10 +44,14 @@ class ChordproSong(Song): |
|
|
'song': song, |
|
|
'song': song, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@staticmethod |
|
|
def _filters(self): |
|
|
def _jinja2_filters(): |
|
|
|
|
|
"""Return additional jinja2 filters.""" |
|
|
"""Return additional jinja2 filters.""" |
|
|
return {} |
|
|
filters = DEFAULT_FILTERS.copy() |
|
|
|
|
|
filters.update({ |
|
|
|
|
|
'search_image': self.search_image, |
|
|
|
|
|
'search_partition': self.search_partition, |
|
|
|
|
|
}) |
|
|
|
|
|
return filters |
|
|
|
|
|
|
|
|
def render(self, template="song"): # pylint: disable=arguments-differ |
|
|
def render(self, template="song"): # pylint: disable=arguments-differ |
|
|
context = { |
|
|
context = { |
|
@ -59,11 +66,7 @@ class ChordproSong(Song): |
|
|
jinjaenv = Environment(loader=FileSystemLoader( |
|
|
jinjaenv = Environment(loader=FileSystemLoader( |
|
|
self.iter_datadirs("templates", "songs", "chordpro", self.output_language) |
|
|
self.iter_datadirs("templates", "songs", "chordpro", self.output_language) |
|
|
)) |
|
|
)) |
|
|
jinjaenv.filters['search_image'] = self.search_image |
|
|
jinjaenv.filters.update(self._filters()) |
|
|
jinjaenv.filters['search_partition'] = self.search_partition |
|
|
|
|
|
jinjaenv.filters['sortargs'] = sort_directive_argument |
|
|
|
|
|
jinjaenv.filters['path2posix'] = path2posix |
|
|
|
|
|
jinjaenv.filters.update(self._jinja2_filters()) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
return Renderer( |
|
|
return Renderer( |
|
@ -132,10 +135,12 @@ class Chordpro2LatexSong(ChordproSong): |
|
|
LOGGER.warning(message) |
|
|
LOGGER.warning(message) |
|
|
return None |
|
|
return None |
|
|
|
|
|
|
|
|
def _jinja2_filters(self): |
|
|
def _filters(self): |
|
|
return { |
|
|
parent = super()._filters() |
|
|
|
|
|
parent.update({ |
|
|
'lang2babel': self.lang2babel, |
|
|
'lang2babel': self.lang2babel, |
|
|
} |
|
|
}) |
|
|
|
|
|
return parent |
|
|
|
|
|
|
|
|
def lang2babel(self, lang): |
|
|
def lang2babel(self, lang): |
|
|
"""Return the LaTeX babel code corresponding to `lang`. |
|
|
"""Return the LaTeX babel code corresponding to `lang`. |
|
|