leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.
328 stars 31 forks source link

SVG attributes helper #36

Closed kamichal closed 6 years ago

kamichal commented 6 years ago

Most of svg attributes have '-' (minus) character in name (e.g. 'stroke-width', 'fill-opacity', 'font-size', etc.) so its is impossible to define such attribute as keyword-argument in Doc.tag(*args, **kwargs) call, because of syntax errors. Instead of that, user has to use the alternative method to pass it as 'positional argument', creating a tuple consisting of attribute name and its value, which is at least very "unconfortable".

This fix is 100% compatible with yattag, but also allows to define problematic attributes with underscores instead of minus characters. E.g.: stag('rect', stroke_width=1.2) automatically translates to So far user would need to write: stag('rect', ('stroke-width', 1.2)). Best Regards, Michal

leforestier commented 6 years ago

Hi Michal,

thanks for your interest in yattag. I'm not too fond of your change. Attribute names with underscores are valid in XML. What if someone is using a "font_size" attribute and doesn't want it to be changed to "font-size"? This could be confusing. Actually, I find that even the "klass" shortcut I authored is not very elegant and I only keep it to avoid breaking people's code. If I were you I would just make a custom subclass (for my personal use, maybe I'd call it SVGDoc for example) of the SimpleDoc class and override the tag and stag methods so that they have the behavior you like.

kamichal commented 6 years ago

Thank you Benjamin for your quick reply. I see your point and I almost totally agree with you. Making it an option is mandatory. The "almost" comes from that I don't think any overloading is needed. My second commit makes all what's needed. I exposed two functions: add_svg_attributes() and reset_attr_substitutions(). By calling it user can toggle the functionality.

Regards, Michal

leforestier commented 6 years ago

Hi Michal,

sorry, I think I wasn't clear enough. Although your feature can be useful to people working with SVG, I'm not going to add attribute substitutions for a particular format. I think the best thing you could do is upload a python package on pypi (maybe call it "yattag-svg") with your subsitutions. This way people who work with SVG can use this particular package. That would be a pretty cool package actually. Everybody will be happy and I won't have to review code for every XML format that exists.

Again, I appreciate your contribution but I want to keep yattag as lightweight as possible.

kamichal commented 6 years ago

I see, Benjamin. Don't worry. Thank you.