@ -3,6 +3,7 @@
import logging
import operator
import os
import urllib
from jinja2 import Environment , FileSystemLoader , ChoiceLoader
from jinja2 import contextfunction
@ -31,6 +32,12 @@ class ChordproSong(Song):
output_language = None
_translation_map = { }
_translation_map_url = None
def __init__ ( self , * args , * * kwargs ) :
super ( ) . __init__ ( * args , * * kwargs )
if self . _translation_map_url is None :
self . _translation_map_url = self . _translation_map
def _parse ( self ) :
""" Parse content, and return the dictionary of song data. """
@ -52,6 +59,7 @@ class ChordproSong(Song):
' search_image ' : self . search_image ,
' search_partition ' : self . search_partition ,
' escape_specials ' : self . _escape_specials ,
' escape_url ' : self . _escape_url ,
} )
return filters
@ -86,13 +94,20 @@ class ChordproSong(Song):
context . vars [ ' content ' ] = content
return context . environment . get_template ( content . template ( ) ) . render ( context )
def _escape_specials ( self , content , chars ) :
def _escape_specials ( self , content , chars = None , * , translation_map = None ) :
if translation_map is None :
translation_map = self . _translation_map
if chars is None :
chars = translation_map . keys ( )
return str ( content ) . translate ( str . maketrans ( {
key : value
for key , value in self . _translation_map . items ( )
for key , value in translation_map . items ( )
if key in chars
} ) )
def _escape_url ( self , content ) :
return self . _escape_specials ( content , translation_map = self . _translation_map_url )
class Chordpro2HtmlSong ( ChordproSong ) :
""" Render chordpro song to html code """
@ -125,6 +140,13 @@ class Chordpro2LatexSong(ChordproSong):
' % ' : r ' \ % ' ,
' _ ' : r ' \ _ ' ,
}
_translation_map_url = {
" " : urllib . parse . quote ( " " ) ,
" { " : urllib . parse . quote ( " { " ) ,
" } " : urllib . parse . quote ( " } " ) ,
' % ' : r ' \ % ' ,
' # ' : r ' \ # ' ,
}
def search_file ( self , filename , extensions = None , * , datadirs = None ) :
_datadir , filename , _extension = self . search_datadir_file (
@ -191,6 +213,11 @@ class Chordpro2ChordproSong(ChordproSong):
' \\ ' : ' \\ \\ ' ,
' # ' : r ' \ # ' ,
}
_translation_map_url = {
' { ' : r ' \ { ' ,
' } ' : r ' \ } ' ,
' \\ ' : ' \\ \\ ' ,
}
def search_file ( self , filename , extensions = None , * , datadirs = None ) :
# pylint: disable=unused-variable