Browse Source

[WIP] Escaping characters when generating chordpro files works.

pull/213/head
Louis 9 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 *)
(*- 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'] *)

15
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

4
test/test_song/latex.csg

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

Loading…
Cancel
Save