diff --git a/patacrep/data/templates/songs/chordpro/latex/content_image b/patacrep/data/templates/songs/chordpro/latex/content_image index a567730d..e2007ad2 100644 --- a/patacrep/data/templates/songs/chordpro/latex/content_image +++ b/patacrep/data/templates/songs/chordpro/latex/content_image @@ -1,5 +1,5 @@ (* block image *) -(* set image = content.argument|search_image|path2posix *) +(* set image = content.filename|search_image|path2posix *) (* if image *) \image{(( image ))} (*- endif *) diff --git a/patacrep/data/templates/styles/patacrep.sty b/patacrep/data/templates/styles/patacrep.sty index 8b88af75..4fb4b075 100644 --- a/patacrep/data/templates/styles/patacrep.sty +++ b/patacrep/data/templates/styles/patacrep.sty @@ -7,6 +7,7 @@ \ProvidesPackage{patacrep}[2014/06/17 Patacrep Package, version 1] \RequirePackage{graphicx,xcolor} % +\RequirePackage[space]{grffile} \RequirePackage{epstopdf} % \RequirePackage{fancybox} \RequirePackage{xstring} diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 37dbae28..6b577870 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -405,6 +405,26 @@ class Define(Directive): def __str__(self): return None +class Image(Directive): + """An image + + .. attribute:: filename + The filename of the image. + .. attribute:: size + A tuple of ``(width, height)`` of the image. Both can be ``None``. + """ + + def __init__(self, filename, size=None): + self.filename = filename + if size is None: + self.size = (None, None) + else: + self.size = size + super().__init__("image", None) + + def __str__(self): + raise NotImplementedError("TODO4") + class Tab(AST): """Tablature""" diff --git a/patacrep/songs/chordpro/syntax.py b/patacrep/songs/chordpro/syntax.py index 9a7b9feb..08485962 100644 --- a/patacrep/songs/chordpro/syntax.py +++ b/patacrep/songs/chordpro/syntax.py @@ -2,6 +2,7 @@ import logging import re +import shlex import ply.yacc as yacc @@ -171,7 +172,43 @@ class ChordproParser(Parser): symbols[0] = ast.Error() return self._directives.append(define) - + elif keyword == "image": + splitted = shlex.split(argument) + if len(splitted) == 1: + symbols[0] = ast.Image(splitted[0]) + elif len(splitted) == 2: + match = re.compile( + r""" + ^ + size= + ((?P\d*\.\d+|\d+)(?P%|cm|em|pt))? + x + ((?P\d*\.\d+|\d+)(?P%|cm|em|pt))? + $ + """, + re.VERBOSE + ).match(splitted[1]) + if match is None: + self.error( + line=symbols.lexer.lineno, + message="TODO1", + ) + symbols[0] = ast.Error() + else: + groupdict = match.groupdict() + symbols[0] = ast.Image( + splitted[0], + ( + (groupdict['widthvalue'], groupdict['widthunit']), + (groupdict['heightvalue'], groupdict['heightunit']), + ), + ) + else: + self.error( + line=symbols.lexer.lineno, + message="TODO3", + ) + symbols[0] = ast.Error() else: directive = ast.Directive(keyword, argument) if directive.inline: diff --git a/test/test_book/syntax.tex.control b/test/test_book/syntax.tex.control index af7acb06..f4a8a46f 100644 --- a/test/test_book/syntax.tex.control +++ b/test/test_book/syntax.tex.control @@ -112,9 +112,13 @@ guitar, \endsong +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% songs/./musicnote.csg +TODO Test image sizes + \end{songs} -\end{document} \ No newline at end of file +\end{document} diff --git a/test/test_book/syntax_datadir/img/dices.png b/test/test_book/syntax_datadir/img/dices.png new file mode 100644 index 00000000..f088bf46 Binary files /dev/null and b/test/test_book/syntax_datadir/img/dices.png differ diff --git a/test/test_book/syntax_datadir/img/image with spaces.png b/test/test_book/syntax_datadir/img/image with spaces.png new file mode 100644 index 00000000..f088bf46 Binary files /dev/null and b/test/test_book/syntax_datadir/img/image with spaces.png differ diff --git a/test/test_book/syntax_datadir/songs/images.csg b/test/test_book/syntax_datadir/songs/images.csg new file mode 100644 index 00000000..1c220bd6 --- /dev/null +++ b/test/test_book/syntax_datadir/songs/images.csg @@ -0,0 +1,24 @@ +{title : Test of image sizes} + +{image: dices.png} +{image: "image with spaces.png"} + +{image: dices.png size=20%x} +{image: "image with spaces.png" size=x20%} +{image: dices.png size=20%x10%} + +{image: dices.png size=20%x} +{image: "image with spaces.png" size=x20%} +{image: dices.png size=20%x10%} + +{image: dices.png size=2cmx} +{image: "image with spaces.png" size=x2cm} +{image: dices.png size=2cmx1cm} + +{image: dices.png size=2emx} +{image: "image with spaces.png" size=x2em} +{image: dices.png size=2emx1em} + +{image: dices.png size=50ptx} +{image: "image with spaces.png" size=x50pt} +{image: dices.png size=50ptx100pt} diff --git a/test/test_song/image.csg b/test/test_song/image.csg new file mode 100644 index 00000000..3467a163 --- /dev/null +++ b/test/test_song/image.csg @@ -0,0 +1,22 @@ +{image: dices.png} +{image: "image with spaces.png"} + +{image: dices.png size=20%x} +{image: "image with spaces.png" size=x20%} +{image: dices.png size=20%x10%} + +{image: dices.png size=20%x} +{image: "image with spaces.png" size=x20%} +{image: dices.png size=20%x10%} + +{image: dices.png size=2cmx} +{image: "image with spaces.png" size=x2cm} +{image: dices.png size=2cmx1cm} + +{image: dices.png size=2emx} +{image: "image with spaces.png" size=x2em} +{image: dices.png size=2emx1em} + +{image: dices.png size=50ptx} +{image: "image with spaces.png" size=x50pt} +{image: dices.png size=50ptx100pt} diff --git a/test/test_song/image.csg.source b/test/test_song/image.csg.source new file mode 100644 index 00000000..0c656573 --- /dev/null +++ b/test/test_song/image.csg.source @@ -0,0 +1,18 @@ +{image: dices.png} +{image: "image with spaces.png"} + +{image: dices.png size=20%x} +{image: "image with spaces.png" size=x20%} +{image: dices.png size=20%x10%} + +{image: dices.png size=2cmx} +{image: "image with spaces.png" size=x2cm} +{image: dices.png size=2cmx1cm} + +{image: dices.png size=2emx} +{image: "image with spaces.png" size=x2em} +{image: dices.png size=2emx1em} + +{image: dices.png size=50ptx} +{image: "image with spaces.png" size=x50pt} +{image: dices.png size=50ptx100pt} diff --git a/test/test_song/image.tsg b/test/test_song/image.tsg new file mode 100644 index 00000000..1333ed77 --- /dev/null +++ b/test/test_song/image.tsg @@ -0,0 +1 @@ +TODO