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}
{columns : 2}
{ title : Greensleeves}
{ title : Un sous titre}
{subtitle: Test of the chordpro format}
{artist: Traditionnel}
{artist: Prénom Nom}
{cover : traditionnel }

32
patacrep/songs/chordpro/__init__.py

@ -1,6 +1,6 @@
"""Chordpro parser"""
from jinja2 import Environment, FileSystemLoader
from jinja2 import Environment, FileSystemLoader, contextfunction
import pkg_resources
import os
@ -12,8 +12,12 @@ from patacrep.templates import TexRenderer
class ChordproSong(Song):
"""Chordpros song parser."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.texenv = None
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:
song = parse_song(song.read(), self.fullpath)
self.authors = song.authors
@ -33,15 +37,27 @@ class ChordproSong(Song):
"authors": ", ".join(["{} {}".format(name[1], name[0]) for name in self.authors]),
"metadata": self.data,
"beginsong": self.cached['song'].meta_beginsong(),
"content": self.cached['song'].content,
"render": self.render_tex,
}
self.texenv = Environment(loader=FileSystemLoader(os.path.join(
os.path.abspath(pkg_resources.resource_filename(__name__, 'data')),
'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="chordpro.tex",
template=template,
encoding='utf8',
texenv=Environment(loader=FileSystemLoader(os.path.join(
os.path.abspath(pkg_resources.resource_filename(__name__, 'data')),
'latex'
))),
texenv=self.texenv,
).template.render(context)
SONG_PARSERS = {

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

@ -18,8 +18,8 @@
\cover
(* endif *)
(* for content in content *)
(* include content.template("tex") *)
(* for item in content *)
(( render(item) ))
(* endfor *)
\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 ))}
(* for content in content.lines *)
(* include content.template("tex") *)
(* for line in content.lines *)
(( render(line) ))
(* endfor *)
\end{(( content.type ))}

Loading…
Cancel
Save