miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
841 stars 119 forks source link

Facilitate extending HTMLRenderer to add attributes #134

Closed not-my-profile closed 1 year ago

not-my-profile commented 2 years ago

This PR introduces optional attr parameters to some methods of the HTMLRenderer, allowing API users to easily add their own attributes without having to reimplement the logic of the renderer methods. (An example for that is provided in the docstring and I also added two test cases).

not-my-profile commented 2 years ago

I have an idea how to make this new API even nicer, but before I do, we should resolve #135.

not-my-profile commented 2 years ago

I updated the PR. Whereas previously attributes were either a dictionary or a string or None, now they are just dictionary or an empty tuple. The elegant part about this is that dict.update(()) doesn't change the dictionary (whereas with None this would require an extra if statement everywhere).

And this also addresses your concern that title should be overridable ... now all attributes can be overridden.

pbodnar commented 2 years ago

I updated the PR. Whereas previously attributes were either a dictionary or a string or None, now they are just dictionary or an empty tuple. The elegant part about this is that dict.update(()) doesn't change the dictionary (whereas with None this would require an extra if statement everywhere).

And this also addresses your concern that title should be overridable ... now all attributes can be overridden.

OK, this looks quite clean, yet maybe a bit hacky with the empty tuple trick (unless this is an idiomatic way in Python?). If there is no dramatic performance penalty from this solution, I would get along with it. :)

Anyway, please see my remarks and also if you would like to add some unit tests (e.g. to cover the possibility of attributes overriding), that would be great. Thank you.

pbodnar commented 2 years ago

Note: Don't hesitate to possibly just create a new commit this time.

legenderrys commented 1 year ago

Hello, I recently had the need to include html attributes onto a rendered HTML nodes.

I would like to propose an alternative solution that will not alter existing render methods.

I opened a PR#170 that demonstrates the idea.

Primary difference is how/where html attributes are stored and accessed. @pbodnar @not-my-profile