Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
catcombo committed Feb 28, 2021
2 parents 5d22525 + c0f2e0c commit 41a6604
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 98 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ convert("[Winston Smith|~accountid:internal-id] woke up with the word 'Shakespea
|------|----------|
|`*strong*`|`**strong**`|
|`_emphasis_`|Not converted (the same syntax)|
|`??citation??`|Not converted|
|`??citation??`|`<q>citation</q>`|
|`-deleted-`|`~~deleted~~`|
|`+inserted+`|`inserted`|
|`^superscript^`|Not converted|
|`~subscript~`|Not converted|
|`^superscript^`|`<sup>superscript</sup>`|
|`~subscript~`|`<sub>subscript</sub>`|
|`{{monospaced}}`|`` `monospaced` ``|
|`bq. Some block quoted text`|`> Some block quoted text`|
|`{quote}Content to be quoted{quote}`|`> Content to be quoted`|
|`{color:red}red text!{color}`|`<font color="red">red text!<font>`|
|`{color:red}red text!{color}`|`<font color="red">red text!</font>`|

## Text Breaks

Expand Down Expand Up @@ -260,12 +260,18 @@ preformatted piece of text
<td>

```
{panel:title=My Title}
Some text with a title
{panel}
Some text
{panel}
```
</td>
<td>Not supported</td>
<td>

```
> **My Title**
> Some text with a title
```
</td>
</tr>
<tr>
<td>
Expand Down
7 changes: 0 additions & 7 deletions jira2markdown/expressions.py

This file was deleted.

34 changes: 33 additions & 1 deletion jira2markdown/markup/advanced.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pyparsing import Combine, FollowedBy, Optional, ParseResults, ParserElement, QuotedString, SkipTo, Word, alphanums
from pyparsing import Combine, FollowedBy, Forward, Group, Literal, OneOrMore, Optional, ParseResults, ParserElement, \
QuotedString, SkipTo, Suppress, Word, alphanums, alphas


class Noformat:
Expand Down Expand Up @@ -30,3 +31,34 @@ def expr(self) -> ParserElement:
+ SkipTo("{code}").setResultsName("text")
+ "{code}",
).setParseAction(self.action)


class Panel:
def __init__(self, markup: Forward):
self.markup = markup

def action(self, tokens: ParseResults) -> str:
text = self.markup.transformString(tokens.text.strip())

for param, value in tokens.get("params", []):
if param.lower() == "title":
text = f"**{value}**\n{text}"

return "\n".join([f"> {line.lstrip()}" for line in text.splitlines()])

@property
def expr(self) -> ParserElement:
PARAM = Word(alphas) \
+ Suppress("=") \
+ SkipTo(Literal("|") | Literal("}")) \
+ Optional("|").suppress()

return Combine(
"{panel"
+ Optional(
":" + OneOrMore(Group(PARAM), stopOn="}").setResultsName("params"),
)
+ "}"
+ SkipTo("{panel}").setResultsName("text")
+ "{panel}",
).setParseAction(self.action)
7 changes: 4 additions & 3 deletions jira2markdown/markup/images.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pyparsing import Combine, Optional, ParseResults, ParserElement, SkipTo, Word, printables
import re

from jira2markdown.tokens import NotUnicodeAlphaNum
from pyparsing import Combine, Optional, ParseResults, ParserElement, PrecededBy, Regex, SkipTo, StringStart, Word, \
printables


class Image:
Expand All @@ -9,7 +10,7 @@ def action(self, tokens: ParseResults) -> str:

@property
def expr(self) -> ParserElement:
return NotUnicodeAlphaNum() + Combine(
return (StringStart() | PrecededBy(Regex(r"\W", flags=re.UNICODE), retreat=1)) + Combine(
"!"
+ Word(printables + " ", min=3, excludeChars="|!").setResultsName("url")
+ Optional("|")
Expand Down
14 changes: 10 additions & 4 deletions jira2markdown/markup/links.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pyparsing import CaselessLiteral, Combine, Forward, Optional, ParseResults, ParserElement, SkipTo, Suppress,\
Word, alphanums
from string import punctuation

from pyparsing import CaselessLiteral, Char, Combine, FollowedBy, Forward, Optional, ParseResults, ParserElement, \
PrecededBy, SkipTo, StringEnd, StringStart, Suppress, White, Word, alphanums


class MailTo:
Expand Down Expand Up @@ -59,7 +61,7 @@ def action(self, tokens: ParseResults) -> str:

@property
def expr(self) -> ParserElement:
return Combine(
MENTION = Combine(
"["
+ Optional(
SkipTo("|", failOn="]") + Suppress("|"),
Expand All @@ -69,4 +71,8 @@ def expr(self) -> ParserElement:
+ Optional(CaselessLiteral("accountid:"))
+ Word(alphanums + ":-").setResultsName("accountid")
+ "]",
).setParseAction(self.action)
)
return (StringStart() | Optional(PrecededBy(White(), retreat=1), default=" ")) \
+ MENTION.setParseAction(self.action) \
+ (StringEnd()
| Optional(FollowedBy(White() | Char(punctuation, excludeChars="[") | MENTION), default=" "))
16 changes: 8 additions & 8 deletions jira2markdown/markup/tables.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pyparsing import Forward, Group, LineEnd, LineStart, Literal, OneOrMore, Optional, ParseResults, ParserElement, \
SkipTo, StringEnd, ZeroOrMore
from pyparsing import Combine, Forward, Group, LineEnd, LineStart, Literal, OneOrMore, Optional, ParseResults, \
ParserElement, SkipTo, StringEnd, StringStart, White, ZeroOrMore

from jira2markdown.expressions import StepBack
from jira2markdown.markup.images import Image
from jira2markdown.markup.links import Link, MailTo, Mention

Expand Down Expand Up @@ -32,20 +31,21 @@ def action(self, tokens: ParseResults) -> str:
# Insert header delimiter after the first row
output.insert(1, "|" + "-|" * max(max_columns_count, 1))

return "\n" + "\n".join(output) + "\n"
return "\n".join(output) + "\n"

@property
def expr(self) -> ParserElement:
NL = LineEnd().suppress()
SEP = (Literal("||") | Literal("|")).suppress()
ROW_BREAK = NL + SEP | NL + NL | StringEnd()
IGNORE = StepBack([Link(self.markup).expr, MailTo().expr, Image().expr, Mention({}).expr])
IGNORE = Link(self.markup).expr | MailTo().expr | Image().expr | Mention({}).expr

ROW = SEP + ZeroOrMore(
SkipTo(SEP, ignore=IGNORE, failOn=ROW_BREAK) + Optional(SEP),
SkipTo(SEP | ROW_BREAK, ignore=IGNORE) + Optional(SEP),
stopOn=ROW_BREAK | NL + ~SEP,
)

return Optional(LineEnd(), default="\n") \
EMPTY_LINE = Combine("\n" + White(" \t", min=0) + "\n")
return ((StringStart() + Optional("\n")) ^ Optional(EMPTY_LINE, default="\n")) \
+ OneOrMore(LineStart() + Group(ROW) + NL).setParseAction(self.action) \
+ Optional(LineEnd(), default="\n")
+ (StringEnd() | Optional(LineEnd(), default="\n"))
67 changes: 63 additions & 4 deletions jira2markdown/markup/text_effects.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re

from pyparsing import CaselessLiteral, Char, Combine, Forward, LineEnd, Literal, Optional, ParseResults, \
ParserElement, QuotedString, SkipTo, StringStart, Suppress, White, Word, WordEnd, WordStart, alphanums, alphas, \
hexnums, nums, replaceWith
ParserElement, PrecededBy, QuotedString, Regex, SkipTo, StringStart, Suppress, White, Word, WordEnd, WordStart, \
alphanums, alphas, hexnums, nums, replaceWith

from jira2markdown.tokens import NotUnicodeAlphaNum
from jira2markdown.markup.links import Attachment, Mention


class Bold:
Expand All @@ -16,7 +18,7 @@ def action(self, tokens: ParseResults) -> str:
def expr(self) -> ParserElement:
TOKEN = Suppress("*")
IGNORE = White() + TOKEN | Color(self.markup).expr
return NotUnicodeAlphaNum() + Combine(
return (StringStart() | PrecededBy(Regex(r"\W", flags=re.UNICODE), retreat=1)) + Combine(
TOKEN
+ (~White() & ~TOKEN)
+ SkipTo(TOKEN, ignore=IGNORE, failOn=LineEnd())
Expand Down Expand Up @@ -63,6 +65,63 @@ def expr(self) -> ParserElement:
).setParseAction(self.action) + WordEnd()


class InlineQuote:
def __init__(self, markup: Forward):
self.markup = markup

def action(self, tokens: ParseResults) -> str:
return "<q>" + self.markup.transformString(tokens[0]) + "</q>"

@property
def expr(self) -> ParserElement:
TOKEN = Suppress("??")
IGNORE = White() + TOKEN | Color(self.markup).expr
return WordStart() + Combine(
TOKEN
+ ~White()
+ SkipTo(TOKEN, ignore=IGNORE, failOn="\n")
+ TOKEN,
).setParseAction(self.action) + WordEnd()


class Superscript:
def __init__(self, markup: Forward):
self.markup = markup

def action(self, tokens: ParseResults) -> str:
return "<sup>" + self.markup.transformString(tokens[0]) + "</sup>"

@property
def expr(self) -> ParserElement:
TOKEN = Suppress("^")
IGNORE = White() + TOKEN | Color(self.markup).expr | Attachment().expr
return WordStart() + Combine(
TOKEN
+ ~White()
+ SkipTo(TOKEN, ignore=IGNORE, failOn="\n")
+ TOKEN,
).setParseAction(self.action) + WordEnd()


class Subscript:
def __init__(self, markup: Forward):
self.markup = markup

def action(self, tokens: ParseResults) -> str:
return "<sub>" + self.markup.transformString(tokens[0]) + "</sub>"

@property
def expr(self) -> ParserElement:
TOKEN = Suppress("~")
IGNORE = White() + TOKEN | Color(self.markup).expr | Mention({}).expr
return WordStart() + Combine(
TOKEN
+ ~White()
+ SkipTo(TOKEN, ignore=IGNORE, failOn="\n")
+ TOKEN,
).setParseAction(self.action) + WordEnd()


class Color:
def __init__(self, markup: Forward):
self.markup = markup
Expand Down
10 changes: 7 additions & 3 deletions jira2markdown/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from pyparsing import Forward, ParserElement

from jira2markdown.markup.advanced import Code, Noformat
from jira2markdown.markup.advanced import Code, Noformat, Panel
from jira2markdown.markup.headings import Headings
from jira2markdown.markup.images import Image
from jira2markdown.markup.links import Attachment, Link, MailTo, Mention
from jira2markdown.markup.lists import OrderedList, UnorderedList
from jira2markdown.markup.tables import Table
from jira2markdown.markup.text_breaks import LineBreak, Mdash, Ndash, Ruler
from jira2markdown.markup.text_effects import BlockQuote, Bold, Color, EscSpecialChars, Monospaced, Quote, \
Strikethrough, Underline
from jira2markdown.markup.text_effects import BlockQuote, Bold, Color, EscSpecialChars, InlineQuote, Monospaced, \
Quote, Strikethrough, Subscript, Superscript, Underline

ParserElement.setDefaultWhitespaceChars(" \t")

Expand All @@ -33,12 +33,16 @@ def convert(text: str, usernames: Optional[dict] = None) -> str:
Headings().expr | \
Quote().expr | \
BlockQuote(markup).expr | \
Panel(markup).expr | \
Bold(markup).expr | \
Ndash().expr | \
Mdash().expr | \
Ruler().expr | \
Strikethrough(markup).expr | \
Underline(markup).expr | \
InlineQuote(markup).expr | \
Superscript(markup).expr | \
Subscript(markup).expr | \
Color(markup).expr | \
LineBreak().expr | \
EscSpecialChars().expr
Expand Down
25 changes: 0 additions & 25 deletions jira2markdown/tokens.py

This file was deleted.

12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jira2markdown"
version = "0.1.7"
version = "0.1.8"
description = "Convert text from JIRA markup to Markdown using parsing expression grammars"
authors = ["Evgeniy Krysanov <[email protected]>"]
readme = "README.md"
Expand Down
Loading

0 comments on commit 41a6604

Please sign in to comment.