-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow setting image sizes #218
Changes from 8 commits
8cfe6be
7c476a7
b85a88a
6c29053
29e0ac9
acd13c8
b20cc79
5fb090a
5f05d8c
60f6060
f4e1243
346f630
cbe34b9
99bee51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{image: (( content.argument|search_image ))} | ||
{image: "(( content.filename|search_image ))" ((content.size|render_size))} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
(* block image *) | ||
(* set image = content.argument|search_image|path2posix *) | ||
(* set image = content.filename|search_image|path2posix *) | ||
(* if image *) | ||
\image{(( image ))} | ||
\image[(( content.size|render_size ))]{(( image ))} | ||
(*- endif *) | ||
(*- endblock *) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import logging | ||
import re | ||
import shlex | ||
|
||
import ply.yacc as yacc | ||
|
||
|
@@ -124,10 +125,62 @@ def _parse_define(groups): | |
fingers=fingers, | ||
) | ||
|
||
def _parse_image_size(self, argument, *, lineno): | ||
if argument is None: | ||
return None | ||
if argument.startswith("size="): | ||
match = re.compile( | ||
r""" | ||
^ | ||
size= | ||
((?P<widthvalue>\d*\.\d+|\d+)(?P<widthunit>%|cm|em|pt))? | ||
x | ||
((?P<heightvalue>\d*\.\d+|\d+)(?P<heightunit>%|cm|em|pt))? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idée spontanée (mais pas forcément réfléchie) : est-ce que le r"""
^
size=
((?P<heightvalue>\d*\.\d+|\d+)(?P<heightunit>%|cm|em|pt))?
(x(?P<heightvalue>\d*\.\d+|\d+)(?P<heightunit>%|cm|em|pt))?
""" Ainsi Autre proposition (totalement indépendante) : remplacer le En revanche, si seule la hauteur est personnalisée, ça donne There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pour le Pour remplacer le |
||
$ | ||
""", | ||
re.VERBOSE | ||
).match(argument) | ||
if match is None: | ||
self.error( | ||
line=lineno, | ||
message="Cannot parse image size '{}'.".format(argument), | ||
) | ||
return None | ||
groupdict = match.groupdict() | ||
return ( | ||
'size', | ||
(groupdict['widthvalue'], groupdict['widthunit']), | ||
(groupdict['heightvalue'], groupdict['heightunit']), | ||
) | ||
elif argument.startswith("scale="): | ||
match = re.compile( | ||
r""" | ||
^ | ||
scale= | ||
(?P<scale>\d*\.\d+|\d+) | ||
$ | ||
""", | ||
re.VERBOSE | ||
).match(argument) | ||
if match is None: | ||
self.error( | ||
line=lineno, | ||
message="Cannot parse image size '{}'.".format(argument), | ||
) | ||
return None | ||
return ('scale', match.groupdict()['scale']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Est-ce qu'il y a une différence entre : Si il n'y en a pas, est-ce que cela pourrait être un motif pour garder seulement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En fait, Comme expliqué plus haut, on ne peut pas définir une largeur ou hauteur relative (mais seulement une échelle globale relative), parce que cela autoriserait Du coup, ça me fait penser que le |
||
self.error( | ||
line=lineno, | ||
message="Cannot parse image size '{}'.".format(argument), | ||
) | ||
return None | ||
|
||
|
||
def p_directive(self, symbols): | ||
"""directive : LBRACE KEYWORD directive_next RBRACE | ||
| LBRACE SPACE KEYWORD directive_next RBRACE | ||
""" | ||
# pylint: disable=too-many-branches | ||
if len(symbols) == 5: | ||
keyword = symbols[2] | ||
argument = symbols[3] | ||
|
@@ -171,7 +224,29 @@ def p_directive(self, symbols): | |
symbols[0] = ast.Error() | ||
return | ||
self._directives.append(define) | ||
|
||
elif keyword == "image": | ||
splitted = shlex.split(argument) | ||
if len(splitted) < 1: | ||
self.error( | ||
line=symbols.lexer.lineno, | ||
message="Missing filename for image directive", | ||
) | ||
symbols[0] = ast.Error() | ||
else: | ||
if len(splitted) > 2: | ||
self.error( | ||
line=symbols.lexer.lineno, | ||
message=( | ||
"Ignoring extra arguments for image directive: " + | ||
" ".join(['"{}"'.format(arg) for arg in splitted[2:]]) | ||
), | ||
) | ||
if len(splitted) == 1: | ||
splitted.append(None) | ||
symbols[0] = ast.Image( | ||
splitted[0], | ||
self._parse_image_size(splitted[1], lineno=symbols.lexer.lineno), | ||
) | ||
else: | ||
directive = ast.Directive(keyword, argument) | ||
if directive.inline: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{image: image.png} | ||
{image: "image with spaces.png"} | ||
|
||
{image: image.png scale=.2 } | ||
{image: "image with spaces.png" scale=.20} | ||
{image: image.png scale=1.2} | ||
|
||
{image: image.png size=2cmx} | ||
{image: "image with spaces.png" size=x2cm} | ||
{image: image.png size=2cmx1cm} | ||
|
||
{image: image.png size=2emx} | ||
{image: "image with spaces.png" size=x2em} | ||
{image: image.png size=2emx1em} | ||
|
||
{image: image.png size=50ptx} | ||
{image: "image with spaces.png" size=x50pt} | ||
{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=3cmx10pt} | ||
{image: "image with spaces.png" size=10ptx3cm} | ||
{image: image.png size=x} | ||
|
||
{image: image.png error=foo} | ||
{image: "image with spaces.png" not_a_size} | ||
{image: "image with spaces.png" too many arguments} | ||
{image: image.png size=2exx3km} | ||
{image: } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Serais-ce un reste de développement ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non, un TODO pour #109. Mais c'est mal supposer que le rendu html utilisera aussi ce filtre jinja2. Corrigé : 5f05d8c.