From dfc7762931b7b0e350e99f2799de42eda02e2753 Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 17 Apr 2016 08:27:47 +0200 Subject: [PATCH] [WIP] Special characters: Song generation works. Songbook generation remains to implement --- .../templates/songs/chordpro/latex/content_word | 2 +- patacrep/data/templates/songs/chordpro/latex/song | 8 ++++---- patacrep/songs/chordpro/__init__.py | 12 ++++++++++++ test/test_song/special.csg.source | 2 +- .../special_datadir/songs/special.csg | 14 ++++++++------ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/patacrep/data/templates/songs/chordpro/latex/content_word b/patacrep/data/templates/songs/chordpro/latex/content_word index d9dd7a30..ebe4d86f 100644 --- a/patacrep/data/templates/songs/chordpro/latex/content_word +++ b/patacrep/data/templates/songs/chordpro/latex/content_word @@ -1 +1 @@ -(( content.value )) +(( content.value|escape_specials('{}&#_^%~$\\') )) diff --git a/patacrep/data/templates/songs/chordpro/latex/song b/patacrep/data/templates/songs/chordpro/latex/song index cb29cc29..d2cbe830 100644 --- a/patacrep/data/templates/songs/chordpro/latex/song +++ b/patacrep/data/templates/songs/chordpro/latex/song @@ -8,7 +8,7 @@ \beginsong{ (*- for title in titles -*) - (( title )) + (( title|escape_specials('{}&#_^%~$\\') )) (*- if not loop.last -*) \\ (* endif *) @@ -16,7 +16,7 @@ }[ by={ (* for author in authors *) - (( author[1] )) (( author[0] )) + (( author[1]|escape_specials('{}&#_^%~$\\') )) (( author[0]|escape_specials('{}&#_^%~$\\') )) (*- if not loop.last -*) , (* endif *) @@ -24,7 +24,7 @@ }, (* for key in ['album', 'copyright'] *) (* if key in metadata *) - (( key ))={(( metadata[key] ))}, + (( key ))={(( metadata[key]|escape_specials('{}&#_^%~$\\') ))}, (* endif *) (* endfor *) (* if 'cover' in metadata *) @@ -36,7 +36,7 @@ (* endblock *) (* endif *) (* for key in metadata.morekeys *) - (( key.keyword ))={(( key.argument ))}, + (( key.keyword ))={(( key.argument|escape_specials('{}&#_^%~$\\') ))}, (* endfor *) ] diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index c63a07b8..4318cd6a 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -113,6 +113,18 @@ class Chordpro2LatexSong(ChordproSong): """Render chordpro song to latex code""" output_language = "latex" + _translation_map = { + '{': r'\{', + '}': r'\}', + '\\': r'\textbackslash{}', + '^': r'\textasciicircum{}', + '~': r'\textasciitilde{}', + '#': r'\#', + '&': r'\&', + '$': r'\$', + '%': r'\%', + '_': r'\_', + } def search_file(self, filename, extensions=None, *, datadirs=None): _datadir, filename, _extension = self.search_datadir_file( diff --git a/test/test_song/special.csg.source b/test/test_song/special.csg.source index 128cfa98..cb8d7c9d 100644 --- a/test/test_song/special.csg.source +++ b/test/test_song/special.csg.source @@ -1,7 +1,7 @@ -TODO url ? {title: & $ % # _ \} \{ ~ ^ \\} {artist: & $ % # _ \} \{ ~ ^ \\} {album: & $ % # _ \} \{ ~ ^ \\} +{url: & $ % # _ \} \{ ~ ^ \\} & $ % \# _ \} \{ ~ ^ \\ diff --git a/test/test_songbook/special_datadir/songs/special.csg b/test/test_songbook/special_datadir/songs/special.csg index 29caee7a..cb8d7c9d 100644 --- a/test/test_songbook/special_datadir/songs/special.csg +++ b/test/test_songbook/special_datadir/songs/special.csg @@ -1,8 +1,10 @@ -{title : & % $ \# _ \} \{ ~ ^ \\ } -{by: & % $ \# _ }\ \{ ~ ^ \\ } +{title: & $ % # _ \} \{ ~ ^ \\} +{artist: & $ % # _ \} \{ ~ ^ \\} +{album: & $ % # _ \} \{ ~ ^ \\} +{url: & $ % # _ \} \{ ~ ^ \\} -& % $ \# _ }\ \{ ~ ^ \\ +& $ % \# _ \} \{ ~ ^ \\ -{soc} - & % $ \# _ }\ \{ ~ ^ \\ -{eoc} +{start_of_chorus} +& $ % \# _ \} \{ ~ ^ \\ +{end_of_chorus}