Converting text from jira to markdown

Przemysław Markiewicz

I'm trying to convert text from Jira to markdown, but I run into a problem when trying to convert links and text color:

  • {color:red}text in red{color} to <span style="color:red">text in red</span>
  • [Google|http://google.com] to [Google](http://google.com)

The problem with color is that i want to leave color variable (this only would work for red color).

Here is my code, it works, but it's probably not the best way to solve the problem:

import re

conversion_dict = {
    r"\]": ")",
    r"\|": "](",
    r"{color:red}": "<span style=\"color:red\">",
    r"{color}": "</span>"
}


def format_text_from_jira(comment_body):
    for pattern in conversion_dict:
        comment_body = re.sub(pattern, conversion_dict[pattern], comment_body)
    return comment_body

Does anyone know better solution?

The fourth bird

For the color specific you can use 3 capture groups matching color with a backreference to color, a negated character class to match any char except the curly's and a non greedy match to match until the first occurrence of the matching closing part.

{(color):([^{}]+)}(.*?){\1}

Regex demo

And use the 3 capture groups in the replacement.

import re

regex = r"{(color):([^{}]+)}(.*?){\1}"
s = "{color:red}text in red{color}"
subst = '<span style="\\1:\\2\">\\3</span>)'
result = re.sub(regex, r'<span style="\1:\2">\3</span>', s)
print(result)

Output

<span style="color:red">text in red</span>

For the link you can use

\[([^][|]+)\|([^][]+)]

Regex demo

import re

regex = r"\[([^][|]+)\|([^][]+)]"
s = "[Google|http://google.com]"
subst = '<span style="\\1:\\2\">\\3</span>)'
result = re.sub(regex, r'[\1](\2)', s)
print(result)

Output

[Google](http://google.com)

Or you might make the link part a bit more specific

\[([^][|]+)\|(https?://[^][]+)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting markdown text into React components

Error in converting from markdown to editor in flask

Create Jira ticket with markdown

How to force image to text when converting markdown to pdf using pandoc

Converting from binary to text in python

Set margin size when converting from Markdown to PDF with pandoc

pandoc: add html events such as onload when converting from markdown

Change font family and size when converting from Markdown to Word?

Pandoc: Change font family to sans while converting from Markdown to HTML

Save HTML from clipboard as markdown text

How to get the text from an AttributedString initialized with markdown?

R markdown: report values from dataframe as text

Inline text in Markdown - adding results from code

Parsing text from API and converting to object

How to prevent GMail from converting text to URL?

Reading from a text file, parsing it, then converting it to a csv

Converting date from text to number format

Converting from text field string to double

Converting xml text from webpage to json

Converting value from datagridview to a text box

Converting from bytes to French text in Python

Converting specific sheet and column from text to number

converting code from Tkinter TEXT to Tkinter LABEL

Converting from Gödel code to text

Converting from Text file to class objects

Stopping Pandoc from escaping single quotes when converting from HTML to Markdown

converting call to equation in r markdown

Add proper syntax name to code blocks when converting from HTML to Markdown with Pandoc

Extracting URL and anchor text from Markdown using Python