diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index c46a0880..b2b328eb 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -3,6 +3,7 @@ from jinja2 import Environment, FileSystemLoader, contextfunction, ChoiceLoader import jinja2 import logging +import operator import os from pkg_resources import resource_filename @@ -14,6 +15,10 @@ from patacrep.latex import lang2babel LOGGER = logging.getLogger(__name__) +def sort_directive_argument(directives): + """Sort directives by their argument.""" + return sorted(directives, key=operator.attrgetter("argument")) + class ChordproSong(Song): """Chordpro song parser""" # pylint: disable=abstract-method @@ -54,6 +59,7 @@ class ChordproSong(Song): jinjaenv.filters['search_image'] = self.search_image jinjaenv.filters['search_partition'] = self.search_partition jinjaenv.filters['lang2babel'] = lang2babel + jinjaenv.filters['sortargs'] = sort_directive_argument try: return Renderer( diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 9213ed37..4fe49001 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -194,6 +194,7 @@ class Song(AST): "artist": "add_author", "key": "add_key", "define": "add_cumulative", + "tag": "add_cumulative", } def __init__(self, filename): diff --git a/patacrep/songs/chordpro/data/chordpro/song_header b/patacrep/songs/chordpro/data/chordpro/song_header index 2ffb1d30..60583433 100644 --- a/patacrep/songs/chordpro/data/chordpro/song_header +++ b/patacrep/songs/chordpro/data/chordpro/song_header @@ -14,16 +14,20 @@ (*- for author in authors -*) {artist: (( author[1] )) (( author[0] ))} -(* endfor *) +(* endfor -*) -(*- for key in ['album', 'copyright', 'tag'] *) +(*- for key in ['album', 'copyright'] *) (* if key in metadata -*) {(( key )): (( metadata[key] ))} (* endif *) (* endfor *) (* if 'cov' in metadata -*) {(( 'cov' )): (( metadata['cov'].argument|search_image ))} -(* endif *) +(* endif -*) + +(* for tag in metadata.get("tag", [])|sortargs -*) + {tag: (( tag.argument ))} +(* endfor -*) (*- for key in metadata.morekeys -*) {key: (( key.keyword )): (( key.argument ))} diff --git a/patacrep/songs/chordpro/data/latex/song b/patacrep/songs/chordpro/data/latex/song index 6f026c79..57714de1 100644 --- a/patacrep/songs/chordpro/data/latex/song +++ b/patacrep/songs/chordpro/data/latex/song @@ -22,7 +22,7 @@ (* endif *) (* endfor *) }, - (* for key in ['album', 'copyright', 'tag'] *) + (* for key in ['album', 'copyright'] *) (* if key in metadata *) (( key ))={(( metadata[key] ))}, (* endif *) diff --git a/test/test_chordpro/tags.sgc b/test/test_chordpro/tags.sgc new file mode 100644 index 00000000..555948c9 --- /dev/null +++ b/test/test_chordpro/tags.sgc @@ -0,0 +1,5 @@ +{lang: en} +{title: Tag test} +{tag: a third tag} +{tag: another} +{tag: one tag} diff --git a/test/test_chordpro/tags.source b/test/test_chordpro/tags.source new file mode 100644 index 00000000..5408dae0 --- /dev/null +++ b/test/test_chordpro/tags.source @@ -0,0 +1,4 @@ +{title: Tag test} +{tag: one tag} +{tag: another} +{tag: a third tag} diff --git a/test/test_chordpro/tags.tex b/test/test_chordpro/tags.tex new file mode 100644 index 00000000..e27a462a --- /dev/null +++ b/test/test_chordpro/tags.tex @@ -0,0 +1,10 @@ +\selectlanguage{english} + +\beginsong{Tag test}[ + by={ + }, +] + + + +\endsong