From 6c29053a6190dc45d726ed5ffc85048cfb9796fb Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 5 May 2016 18:57:58 +0200 Subject: [PATCH] [WIP] Error works, and chordpro to chordpro image conversion works [CI SKIP] --- .../songs/chordpro/chordpro/content_image | 2 +- patacrep/songs/chordpro/__init__.py | 13 +++++++ patacrep/songs/chordpro/ast.py | 3 -- patacrep/songs/chordpro/syntax.py | 4 +- test/test_song/image with spaces.png | 0 test/test_song/image.csg | 38 ++++++++++++------- test/test_song/image.csg.source | 33 ++++++++-------- test/test_song/image.png | 0 8 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 test/test_song/image with spaces.png create mode 100644 test/test_song/image.png diff --git a/patacrep/data/templates/songs/chordpro/chordpro/content_image b/patacrep/data/templates/songs/chordpro/chordpro/content_image index 4f9bf06c..7398eec3 100644 --- a/patacrep/data/templates/songs/chordpro/chordpro/content_image +++ b/patacrep/data/templates/songs/chordpro/chordpro/content_image @@ -1 +1 @@ -{image: (( content.argument|search_image ))} +{image: "(( content.filename|search_image ))" ((content.size|render_size))} diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index 6f1ca655..07ca17f8 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -60,6 +60,7 @@ class ChordproSong(Song): 'search_partition': self.search_partition, 'escape_specials': self._escape_specials, 'escape_url': self._escape_url, + 'render_size': self._render_size, }) return filters @@ -94,6 +95,18 @@ class ChordproSong(Song): context.vars['content'] = content return context.environment.get_template(content.template()).render(context) + @staticmethod + def _render_size(size): + if size == (None, None): + return "" + text = "size=" + if size[0] != (None, None): + text += "".join(size[0]) + text += "x" + if size[1] != (None, None): + text += "".join(size[1]) + return text + def _escape_specials(self, content, chars=None, *, translation_map=None): if translation_map is None: translation_map = self._translation_map diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 6b577870..61298b39 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -422,9 +422,6 @@ class Image(Directive): 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 08485962..63322d52 100644 --- a/patacrep/songs/chordpro/syntax.py +++ b/patacrep/songs/chordpro/syntax.py @@ -191,7 +191,7 @@ class ChordproParser(Parser): if match is None: self.error( line=symbols.lexer.lineno, - message="TODO1", + message="Cannot parse image size '{}'.".format(splitted[1]), ) symbols[0] = ast.Error() else: @@ -206,7 +206,7 @@ class ChordproParser(Parser): else: self.error( line=symbols.lexer.lineno, - message="TODO3", + message="Too many arguments to 'image' directive.", ) symbols[0] = ast.Error() else: diff --git a/test/test_song/image with spaces.png b/test/test_song/image with spaces.png new file mode 100644 index 00000000..e69de29b diff --git a/test/test_song/image.csg b/test/test_song/image.csg index 3467a163..47db149a 100644 --- a/test/test_song/image.csg +++ b/test/test_song/image.csg @@ -1,22 +1,34 @@ -{image: dices.png} -{image: "image with spaces.png"} +{image: image.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.png size=20%x} {image: "image with spaces.png" size=x20%} -{image: dices.png size=20%x10%} +{image: image.png size=20%x10%} + -{image: dices.png size=2cmx} +{image: image.png size=2cmx} {image: "image with spaces.png" size=x2cm} -{image: dices.png size=2cmx1cm} +{image: image.png size=2cmx1cm} + -{image: dices.png size=2emx} +{image: image.png size=2emx} {image: "image with spaces.png" size=x2em} -{image: dices.png size=2emx1em} +{image: image.png size=2emx1em} -{image: dices.png size=50ptx} + +{image: image.png size=50ptx} {image: "image with spaces.png" size=x50pt} -{image: dices.png size=50ptx100pt} +{image: image.png size=50ptx100pt} + + +{image: image.png size=2.5cmx} +{image: "image with spaces.png" size=x2.5cm} +{image: image.png size=2.5cmx1.5cm} + + +{image: image.png size=3cmx10%} +{image: "image with spaces.png" size=10%x3cm} +{image: image.png size=x} + + diff --git a/test/test_song/image.csg.source b/test/test_song/image.csg.source index dc5c9e32..6cde5921 100644 --- a/test/test_song/image.csg.source +++ b/test/test_song/image.csg.source @@ -1,30 +1,31 @@ -{image: dices.png} +{image: image.png} {image: "image with spaces.png"} -{image: dices.png size=20%x } +{image: image.png size=20%x } {image: "image with spaces.png" size=x20%} -{image: dices.png size=20%x10%} +{image: image.png size=20%x10%} -{image: dices.png size=2cmx} +{image: image.png size=2cmx} {image: "image with spaces.png" size=x2cm} -{image: dices.png size=2cmx1cm} +{image: image.png size=2cmx1cm} -{image: dices.png size=2emx} +{image: image.png size=2emx} {image: "image with spaces.png" size=x2em} -{image: dices.png size=2emx1em} +{image: image.png size=2emx1em} -{image: dices.png size=50ptx} +{image: image.png size=50ptx} {image: "image with spaces.png" size=x50pt} -{image: dices.png size=50ptx100pt} +{image: image.png size=50ptx100pt} -{image: dices.png size=2.5cmx} +{image: image.png size=2.5cmx} {image: "image with spaces.png" size=x2.5cm} -{image: dices.png size=2.5cmx1.5cm} +{image: image.png size=2.5cmx1.5cm} -{image: dices.png size=3cmx10%} +{image: image.png size=3cmx10%} {image: "image with spaces.png" size=10%x3cm} -{image: dices.png size=x} +{image: image.png size=x} -{image: dices.png error=foo} -{image: "image with spaces.png" dices.png} -{image: dices.png size=2exx3km} +{image: image.png error=foo} +{image: "image with spaces.png" image.png} +{image: "image with spaces.png" image.png image.png} +{image: image.png size=2exx3km} diff --git a/test/test_song/image.png b/test/test_song/image.png new file mode 100644 index 00000000..e69de29b