Browse Source

Template generation is cleaner

pull/76/head
Louis 10 years ago
parent
commit
ff4d2ae06a
  1. 2
      patacrep/data/examples/songs/greensleeves.sgc
  2. 32
      patacrep/songs/chordpro/__init__.py
  3. 4
      patacrep/songs/chordpro/data/latex/chordpro.tex
  4. 2
      patacrep/songs/chordpro/data/latex/content_line.tex
  5. 5
      patacrep/songs/chordpro/data/latex/content_verse.tex

2
patacrep/data/examples/songs/greensleeves.sgc

@ -1,7 +1,7 @@
{language : english} {language : english}
{columns : 2} {columns : 2}
{ title : Greensleeves} { title : Greensleeves}
{ title : Un sous titre} {subtitle: Test of the chordpro format}
{artist: Traditionnel} {artist: Traditionnel}
{artist: Prénom Nom} {artist: Prénom Nom}
{cover : traditionnel } {cover : traditionnel }

32
patacrep/songs/chordpro/__init__.py

@ -1,6 +1,6 @@
"""Chordpro parser""" """Chordpro parser"""
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader, contextfunction
import pkg_resources import pkg_resources
import os import os
@ -12,8 +12,12 @@ from patacrep.templates import TexRenderer
class ChordproSong(Song): class ChordproSong(Song):
"""Chordpros song parser.""" """Chordpros song parser."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.texenv = None
def parse(self, config): def parse(self, config):
"""Parse content, and return the dictinory of song data.""" """Parse content, and return the dictionary of song data."""
with encoding.open_read(self.fullpath, encoding=self.encoding) as song: with encoding.open_read(self.fullpath, encoding=self.encoding) as song:
song = parse_song(song.read(), self.fullpath) song = parse_song(song.read(), self.fullpath)
self.authors = song.authors self.authors = song.authors
@ -33,15 +37,27 @@ class ChordproSong(Song):
"authors": ", ".join(["{} {}".format(name[1], name[0]) for name in self.authors]), "authors": ", ".join(["{} {}".format(name[1], name[0]) for name in self.authors]),
"metadata": self.data, "metadata": self.data,
"beginsong": self.cached['song'].meta_beginsong(), "beginsong": self.cached['song'].meta_beginsong(),
"content": self.cached['song'].content, "render": self.render_tex,
} }
return TexRenderer( self.texenv = Environment(loader=FileSystemLoader(os.path.join(
template="chordpro.tex",
encoding='utf8',
texenv=Environment(loader=FileSystemLoader(os.path.join(
os.path.abspath(pkg_resources.resource_filename(__name__, 'data')), os.path.abspath(pkg_resources.resource_filename(__name__, 'data')),
'latex' 'latex'
))), )))
return self.render_tex(context, self.cached['song'].content, template="chordpro.tex")
@contextfunction
def render_tex(self, context, content, template=None):
"""Render ``content`` as tex."""
if isinstance(context, dict):
context['content'] = content
else:
context.vars['content'] = content
if template is None:
template = content.template('tex')
return TexRenderer(
template=template,
encoding='utf8',
texenv=self.texenv,
).template.render(context) ).template.render(context)
SONG_PARSERS = { SONG_PARSERS = {

4
patacrep/songs/chordpro/data/latex/chordpro.tex

@ -18,8 +18,8 @@
\cover \cover
(* endif *) (* endif *)
(* for content in content *) (* for item in content *)
(* include content.template("tex") *) (( render(item) ))
(* endfor *) (* endfor *)
\endsong \endsong

2
patacrep/songs/chordpro/data/latex/content_line.tex

@ -1 +1 @@
(* for content in content.line *)(* include content.template("tex") *)(* endfor *) (* for item in content.line *)(( render(item) ))(* endfor *)

5
patacrep/songs/chordpro/data/latex/content_verse.tex

@ -1,7 +1,6 @@
\begin{(( content.type ))} \begin{(( content.type ))}
(* for content in content.lines *) (* for line in content.lines *)
(* include content.template("tex") *) (( render(line) ))
(* endfor *) (* endfor *)
\end{(( content.type ))} \end{(( content.type ))}

Loading…
Cancel
Save