From 83ed9ddf71744de3c3fd89a3442bfb80f4ba0099 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 15 Apr 2016 10:05:04 +0200 Subject: [PATCH] [WIP] Escaping characters when generating chordpro files works. --- .../songs/chordpro/chordpro/content_word | 2 +- .../templates/songs/chordpro/chordpro/song_header | 8 ++++---- patacrep/songs/chordpro/__init__.py | 15 +++++++++++++++ test/test_song/latex.csg | 4 +++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/patacrep/data/templates/songs/chordpro/chordpro/content_word b/patacrep/data/templates/songs/chordpro/chordpro/content_word index d9dd7a30..e90a86e9 100644 --- a/patacrep/data/templates/songs/chordpro/chordpro/content_word +++ b/patacrep/data/templates/songs/chordpro/chordpro/content_word @@ -1 +1 @@ -(( content.value )) +(( content.value|escape_specials('{}\\#') )) diff --git a/patacrep/data/templates/songs/chordpro/chordpro/song_header b/patacrep/data/templates/songs/chordpro/chordpro/song_header index 56ad6a3a..fe9feb48 100644 --- a/patacrep/data/templates/songs/chordpro/chordpro/song_header +++ b/patacrep/data/templates/songs/chordpro/chordpro/song_header @@ -9,16 +9,16 @@ (* endif *) (*- for title in titles -*) - {title: (( title ))} + {title: (( title|escape_specials('{}\\') ))} (* endfor -*) (*- for author in authors -*) - {artist: (( author[1] )) (( author[0] ))} + {artist: (( author[1]|escape_specials('{}\\') )) (( author[0]|escape_specials('{}\\') ))} (* endfor -*) (*- for key in ['album', 'copyright'] *) (* if key in metadata -*) - {(( key )): (( metadata[key] ))} + {(( key )): (( metadata[key]|escape_specials('{}\\') ))} (* endif *) (* endfor *) (* if 'cover' in metadata -*) @@ -30,7 +30,7 @@ (* endfor -*) (*- for key in metadata.morekeys -*) - {key: (( key.keyword )): (( key.argument ))} + {key: (( key.keyword )): (( key.argument|escape_specials('{}\\') ))} (* endfor *) (*- for chord in metadata['define'] *) diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index a7fd3e3b..c63a07b8 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -30,6 +30,7 @@ class ChordproSong(Song): # pylint: disable=abstract-method output_language = None + _translation_map = {} def _parse(self): """Parse content, and return the dictionary of song data.""" @@ -50,6 +51,7 @@ class ChordproSong(Song): filters.update({ 'search_image': self.search_image, 'search_partition': self.search_partition, + 'escape_specials': self._escape_specials, }) return filters @@ -84,6 +86,13 @@ class ChordproSong(Song): context.vars['content'] = content return context.environment.get_template(content.template()).render(context) + def _escape_specials(self, content, chars): + return str(content).translate(str.maketrans({ + key: value + for key, value in self._translation_map.items() + if key in chars + })) + class Chordpro2HtmlSong(ChordproSong): """Render chordpro song to html code""" @@ -164,6 +173,12 @@ class Chordpro2ChordproSong(ChordproSong): """Render chordpro song to chordpro code""" output_language = "chordpro" + _translation_map = { + '{': r'\{', + '}': r'\}', + '\\': '\\\\', + '#': r'\#', + } def search_file(self, filename, extensions=None, *, datadirs=None): # pylint: disable=unused-variable diff --git a/test/test_song/latex.csg b/test/test_song/latex.csg index 7fd9cf9f..c26ff044 100644 --- a/test/test_song/latex.csg +++ b/test/test_song/latex.csg @@ -1,10 +1,12 @@ {lang: en} {title: & $ % # _ \} \{ ~ ^ \\} -{artist: & $ % # _ \} \{ ~ ^ \\} +{artist: & $ % # _ \} \{ ~ ^ \\} {album: & $ % # _ \} \{ ~ ^ \\} + & $ % \# _ \} \{ ~ ^ \\ + {start_of_chorus} & $ % \# _ \} \{ ~ ^ \\ {end_of_chorus}