-
Notifications
You must be signed in to change notification settings - Fork 146
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
Added convert_all func #163
base: develop
Are you sure you want to change the base?
Conversation
@Divyesh06 - your code implements What is the use case you have in mind for this? |
@chrispy-snps I just listed the example of text-align. So let's say I want a custom markdown syntax for aligning text. I will find it hard to do it without the With Of course, this is just an example. But I think it can be used for all kinds of custom markdown syntax that require some CSS style to be added. |
@Divyesh06 - at the point where your Can you give me a specific example of what would be passed as input to your formatting function, and what the function would return? You used the example of |
Yes, I already mentioned that in the usage. Simply putting, it works just like other convert functions like Let me further explain the example here. First of all, we are just creating a custom markdown converter class which extends the class CustomMarkdownConverter(MarkdownConverter):
def convert_all(self,el,text, convert_as_inline):
alignment = ""
if 'style' in el.attrs and 'text-align' in el.attrs['style']:
style = el.attrs['style']
if "text-align" in style:
alignment = style.split("text-align:")[1].split(";")[0].strip()
if alignment:
return f"[align={alignment}]{text}[/align]"
return text
converter = CustomMarkdownConverter() Now if I pass a string that has different HTML tags with
The output I get will be
See how I didn't need to write separate logic for Without class CustomMarkdownConverter(MarkdownConverter):
def convert_p():
... #Logic for converting text align for p tag
def convert_b():
... #Logic for converting text align for b tag
def convert_h1():
... #Logic for converting text align for h1 tag
#And this goes on forever |
Thanks @Divyesh06, I understand now. Here are my thoughts:
Here is my suggestion for how the inner loop might look: if not children_only:
if self.preprocess_convert_fn: # <----
text = self.preprocess_convert_fn(node, text, convert_as_inline) # <----
convert_fn = getattr(self, 'convert_%s' % node.name, None)
if convert_fn and self.should_convert_tag(node.name):
text = convert_fn(node, text, convert_as_inline)
if self.postprocess_convert_fn: # <----
text = self.postprocess_convert_fn(node, text, convert_as_inline) # <---- |
@chrispy-snps Makes sense. I will implement those changes |
Make it possible for custom markdown classes to apply a particular rule to all tags.
Usage
Example - Handling Text Align