From d712a8060133a21c217d56738b53755141a1c342 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 13 Mar 2016 17:46:24 +0100 Subject: [PATCH] Support repeat inline directive --- .../songs/chordpro/chordpro/content_meta | 2 +- .../songs/chordpro/latex/content_meta | 2 +- .../songs/chordpro/latex/content_repeat | 1 + patacrep/songs/chordpro/ast.py | 31 +++++++++++++++++++ patacrep/songs/chordpro/syntax.py | 13 ++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 patacrep/data/templates/songs/chordpro/latex/content_repeat diff --git a/patacrep/data/templates/songs/chordpro/chordpro/content_meta b/patacrep/data/templates/songs/chordpro/chordpro/content_meta index a4015066..7af09db0 100644 --- a/patacrep/data/templates/songs/chordpro/chordpro/content_meta +++ b/patacrep/data/templates/songs/chordpro/chordpro/content_meta @@ -1 +1 @@ -{meta: (( content.argument[0] )):(( content.argument[2] ))} \ No newline at end of file +{meta: (( content.argument[0] )):(( content.argument[2] ))} diff --git a/patacrep/data/templates/songs/chordpro/latex/content_meta b/patacrep/data/templates/songs/chordpro/latex/content_meta index 919f95a7..ff5d49d7 100644 --- a/patacrep/data/templates/songs/chordpro/latex/content_meta +++ b/patacrep/data/templates/songs/chordpro/latex/content_meta @@ -1 +1 @@ -\metacrep{((- content.argument[0] -))}{((- content.argument[2] -))} \ No newline at end of file +\metacrep{((- content.argument[0] -))}{((- content.argument[2] -))} diff --git a/patacrep/data/templates/songs/chordpro/latex/content_repeat b/patacrep/data/templates/songs/chordpro/latex/content_repeat new file mode 100644 index 00000000..5939f45c --- /dev/null +++ b/patacrep/data/templates/songs/chordpro/latex/content_repeat @@ -0,0 +1 @@ +\rep{((- content -))} \ No newline at end of file diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 2bbddaf1..380d1437 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -46,6 +46,12 @@ INLINE_DIRECTIVES = { "meta", } +#: List of properties that are to be displayed in the flow of the song (not as +#: metadata at the beginning or end of song. +VERSE_DIRECTIVES = { + "repeat", + } + #: Some directive have alternative names. For instance `{title: Foo}` and `{t: #: Foo}` are equivalent. DIRECTIVE_SHORTCUTS = { @@ -376,6 +382,31 @@ class Directive(AST): """Return `True` iff `self` is an inline directive.""" return self.keyword in INLINE_DIRECTIVES +class VerseDirective(Directive): + """A verse directive (can be inside some text)""" + + def __init__(self, keyword, argument=None): + if keyword not in VERSE_DIRECTIVES: + #TODO Raise better exception + raise Exception + super().__init__(keyword, argument) + + @property + def _template(self): + """Name of the template to use to render this keyword. + + This only applies if ``self.inline == True`` + """ + return self.keyword + + def __str__(self): + return str(self.argument) + + @property + def inline(self): + """Return `True` iff `self` is an inline directive.""" + return True + class Define(Directive): """A chord definition. diff --git a/patacrep/songs/chordpro/syntax.py b/patacrep/songs/chordpro/syntax.py index a1e3f6e6..b44d0334 100644 --- a/patacrep/songs/chordpro/syntax.py +++ b/patacrep/songs/chordpro/syntax.py @@ -251,6 +251,18 @@ class ChordproParser(Parser): else: self._directives.append(directive) + def p_verse_directive(self, symbols): + """verse_directive : LBRACE KEYWORD directive_next RBRACE + | LBRACE SPACE KEYWORD directive_next RBRACE + """ + if len(symbols) == 5: + keyword = symbols[2] + argument = symbols[3] + else: + keyword = symbols[3] + argument = symbols[4] + + symbols[0] = ast.VerseDirective(keyword, argument) @staticmethod def p_directive_next(symbols): @@ -310,6 +322,7 @@ class ChordproParser(Parser): | space line_next | chord line_next | echo line_next + | verse_directive line_next | empty """ if len(symbols) == 2: