Browse Source

[WIP] Escaping characters when generating chordpro files works.

pull/213/head
Louis 8 years ago
parent
commit
83ed9ddf71
  1. 2
      patacrep/data/templates/songs/chordpro/chordpro/content_word
  2. 8
      patacrep/data/templates/songs/chordpro/chordpro/song_header
  3. 15
      patacrep/songs/chordpro/__init__.py
  4. 4
      test/test_song/latex.csg

2
patacrep/data/templates/songs/chordpro/chordpro/content_word

@ -1 +1 @@
(( content.value )) (( content.value|escape_specials('{}\\#') ))

8
patacrep/data/templates/songs/chordpro/chordpro/song_header

@ -9,16 +9,16 @@
(* endif *) (* endif *)
(*- for title in titles -*) (*- for title in titles -*)
{title: (( title ))} {title: (( title|escape_specials('{}\\') ))}
(* endfor -*) (* endfor -*)
(*- for author in authors -*) (*- for author in authors -*)
{artist: (( author[1] )) (( author[0] ))} {artist: (( author[1]|escape_specials('{}\\') )) (( author[0]|escape_specials('{}\\') ))}
(* endfor -*) (* endfor -*)
(*- for key in ['album', 'copyright'] *) (*- for key in ['album', 'copyright'] *)
(* if key in metadata -*) (* if key in metadata -*)
{(( key )): (( metadata[key] ))} {(( key )): (( metadata[key]|escape_specials('{}\\') ))}
(* endif *) (* endif *)
(* endfor *) (* endfor *)
(* if 'cover' in metadata -*) (* if 'cover' in metadata -*)
@ -30,7 +30,7 @@
(* endfor -*) (* endfor -*)
(*- for key in metadata.morekeys -*) (*- for key in metadata.morekeys -*)
{key: (( key.keyword )): (( key.argument ))} {key: (( key.keyword )): (( key.argument|escape_specials('{}\\') ))}
(* endfor *) (* endfor *)
(*- for chord in metadata['define'] *) (*- for chord in metadata['define'] *)

15
patacrep/songs/chordpro/__init__.py

@ -30,6 +30,7 @@ class ChordproSong(Song):
# pylint: disable=abstract-method # pylint: disable=abstract-method
output_language = None output_language = None
_translation_map = {}
def _parse(self): def _parse(self):
"""Parse content, and return the dictionary of song data.""" """Parse content, and return the dictionary of song data."""
@ -50,6 +51,7 @@ class ChordproSong(Song):
filters.update({ filters.update({
'search_image': self.search_image, 'search_image': self.search_image,
'search_partition': self.search_partition, 'search_partition': self.search_partition,
'escape_specials': self._escape_specials,
}) })
return filters return filters
@ -84,6 +86,13 @@ class ChordproSong(Song):
context.vars['content'] = content context.vars['content'] = content
return context.environment.get_template(content.template()).render(context) 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): class Chordpro2HtmlSong(ChordproSong):
"""Render chordpro song to html code""" """Render chordpro song to html code"""
@ -164,6 +173,12 @@ class Chordpro2ChordproSong(ChordproSong):
"""Render chordpro song to chordpro code""" """Render chordpro song to chordpro code"""
output_language = "chordpro" output_language = "chordpro"
_translation_map = {
'{': r'\{',
'}': r'\}',
'\\': '\\\\',
'#': r'\#',
}
def search_file(self, filename, extensions=None, *, datadirs=None): def search_file(self, filename, extensions=None, *, datadirs=None):
# pylint: disable=unused-variable # pylint: disable=unused-variable

4
test/test_song/latex.csg

@ -1,10 +1,12 @@
{lang: en} {lang: en}
{title: & $ % # _ \} \{ ~ ^ \\} {title: & $ % # _ \} \{ ~ ^ \\}
{artist: & $ % # _ \} \{ ~ ^ \\} {artist: & $ % # _ \} \{ ~ ^ \\}
{album: & $ % # _ \} \{ ~ ^ \\} {album: & $ % # _ \} \{ ~ ^ \\}
& $ % \# _ \} \{ ~ ^ \\ & $ % \# _ \} \{ ~ ^ \\
{start_of_chorus} {start_of_chorus}
& $ % \# _ \} \{ ~ ^ \\ & $ % \# _ \} \{ ~ ^ \\
{end_of_chorus} {end_of_chorus}

Loading…
Cancel
Save