Skip to content
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

Examples of overriding various markup to "plain text"? #16

Closed
eddyg opened this issue Feb 2, 2023 · 2 comments
Closed

Examples of overriding various markup to "plain text"? #16

eddyg opened this issue Feb 2, 2023 · 2 comments

Comments

@eddyg
Copy link

eddyg commented Feb 2, 2023

First, thanks for a great library! 🎉

I'd like to be able to convert more elements to "plain text", for example, when it sees {color:red}Red Text{color}, I'd like the conversion to only have Red Text.

I'm not familiar with pyparsing, so I'm wondering if you have suggestions on the "best" way to override the various classes in the markup directory.

Perhaps you can provide some examples of how to convert some of the various classes of markup to "just text"?

@catcombo
Copy link
Owner

catcombo commented Feb 3, 2023

Hi @eddyg !

Thank you for your kind words!

You can use the element parser class you want to change the behavior of as a base class. Then you need to update ether action method (if exists) or expr method. In the case of the color tag, the code may look like this:

from pyparsing import ParseResults

from jira2markdown import convert
from jira2markdown.elements import MarkupElements
from jira2markdown.markup.text_effects import Color


class CustomColor(Color):
    def action(self, tokens: ParseResults) -> str:
        return self.inline_markup.transform_string(tokens.text)


elements = MarkupElements()
elements.replace(Color, CustomColor)

print(convert("{color:red}Red Text{color}", elements=elements))

It's hard to provide a universal solution. So if you have any more questions, feel free to ask.

@eddyg
Copy link
Author

eddyg commented Feb 3, 2023

Ah, fantastic!

Thanks for the example — I had been able to override some simple inline examples by just setting QUOTE_CHAR and END_QUOTE_CHAR in the custom classes, but wasn't sure what was needed for a more complex token. Your example helps a lot.

I'll close this for now. Thanks again! 👍

@eddyg eddyg closed this as completed Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants