Skip to content

Commit

Permalink
Migrate to pyparsing 3 (#12)
Browse files Browse the repository at this point in the history
* chore(poetry): bump pyparsing to version 3 and Python to 3.7
* feat: rename methods and arguments names according to new pyparsing naming convention
* feat: switch from ParserElement to Token for ListIndent
* feat: update parsing rules
* chore(poetry): bump project version to 0.3
  • Loading branch information
catcombo authored Dec 6, 2022
1 parent e917170 commit 8146051
Show file tree
Hide file tree
Showing 15 changed files with 272 additions and 353 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.7

- name: Install Poetry
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Prerequisites

- Python 3.6+
- Python 3.7+

# Installation

Expand Down
18 changes: 9 additions & 9 deletions jira2markdown/markup/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def action(self, tokens: ParseResults) -> str:
@property
def expr(self) -> ParserElement:
return Combine(
Literal("{noformat") + ... + Literal("}") + SkipTo("{noformat}").setResultsName("text") + "{noformat}",
).setParseAction(self.action)
Literal("{noformat") + ... + Literal("}") + SkipTo("{noformat}").set_results_name("text") + "{noformat}",
).set_parse_action(self.action)


class Code(AbstractMarkup):
Expand All @@ -40,13 +40,13 @@ def expr(self) -> ParserElement:
return Combine(
"{code"
+ Optional(
":" + Word(alphanums + "#+").setResultsName("lang") + FollowedBy(Literal("}") | Literal("|")),
":" + Word(alphanums + "#+").set_results_name("lang") + FollowedBy(Literal("}") | Literal("|")),
)
+ ...
+ "}"
+ SkipTo("{code}").setResultsName("text")
+ SkipTo("{code}").set_results_name("text")
+ "{code}",
).setParseAction(self.action)
).set_parse_action(self.action)


class Panel(AbstractMarkup):
Expand All @@ -58,7 +58,7 @@ def action(self, tokens: ParseResults) -> str:
else:
prefix = ""

text = self.markup.transformString("\n".join([line.lstrip() for line in tokens.text.strip().splitlines()]))
text = self.markup.transform_string("\n".join([line.lstrip() for line in tokens.text.strip().splitlines()]))
return prefix + "\n".join([f"> {line}" for line in text.splitlines()])

@property
Expand All @@ -68,9 +68,9 @@ def expr(self) -> ParserElement:
return Combine(
"{panel"
+ Optional(
":" + OneOrMore(Group(PARAM), stopOn="}").setResultsName("params"),
":" + OneOrMore(Group(PARAM), stop_on="}").set_results_name("params"),
)
+ "}"
+ SkipTo("{panel}").setResultsName("text")
+ SkipTo("{panel}").set_results_name("text")
+ "{panel}",
).setParseAction(self.action)
).set_parse_action(self.action)
18 changes: 11 additions & 7 deletions jira2markdown/markup/headings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyparsing import Combine, LineEnd, ParserElement, ParseResults, SkipTo, StringEnd, StringStart, Word
from pyparsing import Combine, LineEnd, LineStart, Optional, ParserElement, ParseResults, SkipTo, StringEnd, White, Word

from jira2markdown.markup.base import AbstractMarkup

Expand All @@ -7,12 +7,16 @@ class Headings(AbstractMarkup):
is_inline_element = False

def action(self, tokens: ParseResults) -> str:
return "#" * int(tokens.level[1]) + " " + self.inline_markup.transformString(tokens.text)
return "#" * int(tokens.level[1]) + " " + self.inline_markup.transform_string(tokens.text)

@property
def expr(self) -> ParserElement:
return ("\n" | StringStart()) + Combine(
Word("h", "123456", exact=2).setResultsName("level")
+ ". "
+ SkipTo(LineEnd() | StringEnd()).setResultsName("text"),
).setParseAction(self.action)
return (
LineStart()
+ Optional(White())
+ Combine(
Word("h", "123456", exact=2).set_results_name("level")
+ ". "
+ SkipTo(LineEnd() | StringEnd()).set_results_name("text"),
).set_parse_action(self.action)
)
6 changes: 3 additions & 3 deletions jira2markdown/markup/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def action(self, tokens: ParseResults) -> str:
def expr(self) -> ParserElement:
return (StringStart() | PrecededBy(Regex(r"\W", flags=re.UNICODE), retreat=1)) + Combine(
"!"
+ Word(printables + " ", min=3, excludeChars="|!").setResultsName("url")
+ Word(printables + " ", min=3, exclude_chars="|!").set_results_name("url")
+ Optional("|")
+ SkipTo("!", failOn="\n")
+ SkipTo("!", fail_on="\n")
+ "!",
).setParseAction(self.action)
).set_parse_action(self.action)
33 changes: 18 additions & 15 deletions jira2markdown/markup/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class MailTo(AbstractMarkup):
def action(self, tokens: ParseResults) -> str:
alias = self.markup.transformString(getattr(tokens, "alias", ""))
alias = self.markup.transform_string(getattr(tokens, "alias", ""))
email = tokens.email

if (alias == email) or (len(alias.strip()) == 0):
Expand All @@ -35,37 +35,37 @@ def action(self, tokens: ParseResults) -> str:
def expr(self) -> ParserElement:
return Combine(
"["
+ Optional(SkipTo("|", failOn="]").setResultsName("alias") + "|")
+ Optional(SkipTo("|", fail_on="]").set_results_name("alias") + "|")
+ "mailto:"
+ Word(alphanums + "@.-_").setResultsName("email")
+ Word(alphanums + "@.-_").set_results_name("email")
+ "]",
).setParseAction(self.action)
).set_parse_action(self.action)


class Link(AbstractMarkup):
URL_PREFIXES = ["http", "ftp"]

def action(self, tokens: ParseResults) -> str:
alias = self.markup.transformString(getattr(tokens, "alias", ""))
alias = self.markup.transform_string(getattr(tokens, "alias", ""))
url = tokens.url

if url.lower().startswith("www."):
url = f"https://{url}"

if not any(map(url.lower().startswith, self.URL_PREFIXES)):
url = self.markup.transformString(url)
return fr"[{alias}\|{url}]" if alias else f"[{url}]"
url = self.markup.transform_string(url)
return rf"[{alias}\|{url}]" if alias else f"[{url}]"

return f"[{alias}]({url})" if len(alias) > 0 else f"<{url}>"

@property
def expr(self) -> ParserElement:
return Combine(
"["
+ Optional(SkipTo("|", failOn="]").setResultsName("alias") + "|")
+ SkipTo("]").setResultsName("url")
+ Optional(SkipTo("|", fail_on="]").set_results_name("alias") + "|")
+ SkipTo("]").set_results_name("url")
+ "]",
).setParseAction(self.action)
).set_parse_action(self.action)


class Attachment(AbstractMarkup):
Expand All @@ -74,7 +74,7 @@ def action(self, tokens: ParseResults) -> str:

@property
def expr(self) -> ParserElement:
return Combine("[^" + SkipTo("]").setResultsName("filename") + "]").setParseAction(self.action)
return Combine("[^" + SkipTo("]").set_results_name("filename") + "]").set_parse_action(self.action)


class Mention(AbstractMarkup):
Expand All @@ -87,16 +87,19 @@ def expr(self) -> ParserElement:
MENTION = Combine(
"["
+ Optional(
SkipTo("|", failOn="]") + Suppress("|"),
SkipTo("|", fail_on="]") + Suppress("|"),
default="",
)
+ "~"
+ Optional(CaselessLiteral("accountid:"))
+ Word(alphanums + ":-").setResultsName("accountid")
+ Word(alphanums + ":-").set_results_name("accountid")
+ "]",
)
return (
(StringStart() | Optional(PrecededBy(White(), retreat=1), default=" "))
+ MENTION.setParseAction(self.action)
+ (StringEnd() | Optional(FollowedBy(White() | Char(punctuation, excludeChars="[") | MENTION), default=" "))
+ MENTION.set_parse_action(self.action)
+ (
StringEnd()
| Optional(FollowedBy(White() | Char(punctuation, exclude_chars="[") | MENTION), default=" ")
)
)
12 changes: 6 additions & 6 deletions jira2markdown/markup/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ParseResults,
SkipTo,
StringEnd,
Token,
White,
)

Expand All @@ -27,11 +28,10 @@ def reset(self):
self.indent = 0


class ListIndent(ParserElement):
def __init__(self, indent_state: ListIndentState, tokens: str, *args, **kwargs):
super().__init__(*args, **kwargs)
class ListIndent(Token):
def __init__(self, indent_state: ListIndentState, tokens: str):
super().__init__()

self.name = "ListIndent"
self.indent_state = indent_state
self.tokens = tokens

Expand Down Expand Up @@ -75,7 +75,7 @@ def action(self, tokens: ParseResults) -> str:

line_padding = " " * count
item_padding = " " * (count - self.indent) + self.bullet + " "
text = self.markup.transformString(text).splitlines() or [""]
text = self.markup.transform_string(text).splitlines() or [""]

result.append(
"\n".join([item_padding + line if i == 0 else line_padding + line for i, line in enumerate(text)]),
Expand All @@ -97,7 +97,7 @@ def expr(self) -> ParserElement:
+ Optional(NL),
)

return OneOrMore(ROW, stopOn=LIST_BREAK).setParseAction(self.action)
return OneOrMore(ROW, stop_on=LIST_BREAK).set_parse_action(self.action)


class UnorderedList(List):
Expand Down
17 changes: 9 additions & 8 deletions jira2markdown/markup/tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from pyparsing import (
Combine,
Group,
LineEnd,
LineStart,
Expand All @@ -8,10 +9,10 @@
Optional,
ParserElement,
ParseResults,
Regex,
SkipTo,
StringEnd,
StringStart,
White,
ZeroOrMore,
)

Expand All @@ -34,7 +35,7 @@ def action(self, tokens: ParseResults) -> str:
+ "|".join(
map(
lambda cell: cell.replace("\n", "<br>"),
map(self.markup.transformString, row),
map(self.markup.transform_string, row),
),
)
+ "|"
Expand Down Expand Up @@ -63,12 +64,12 @@ def expr(self) -> ParserElement:

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

EMPTY_LINE = Combine("\n" + White(" \t", min=0) + "\n")
EMPTY_LINE = LineStart() + Optional(Regex(r"[ \t]+", flags=re.UNICODE)) + LineEnd()
return (
((StringStart() + Optional("\n")) ^ Optional(EMPTY_LINE, default="\n"))
+ OneOrMore(LineStart() + Group(ROW) + NL).setParseAction(self.action)
+ (StringEnd() | Optional(LineEnd(), default="\n"))
(StringStart() ^ Optional(EMPTY_LINE, default="\n"))
+ OneOrMore(LineStart() + Group(ROW) + NL).set_parse_action(self.action)
+ (StringEnd() | Optional(EMPTY_LINE, default="\n"))
)
14 changes: 8 additions & 6 deletions jira2markdown/markup/text_breaks.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from pyparsing import Keyword, LineEnd, ParserElement, StringStart, WordEnd, WordStart, replaceWith
from pyparsing import Keyword, LineEnd, LineStart, Optional, ParserElement, White, WordEnd, WordStart, replaceWith

from jira2markdown.markup.base import AbstractMarkup


class LineBreak(AbstractMarkup):
@property
def expr(self) -> ParserElement:
return Keyword("\\\\", identChars="\\").setParseAction(replaceWith("\n"))
return Keyword("\\\\", ident_chars="\\").set_parse_action(replaceWith("\n"))


class Ndash(AbstractMarkup):
@property
def expr(self) -> ParserElement:
return WordStart() + Keyword("--", identChars="-").setParseAction(replaceWith("–")) + WordEnd()
return WordStart() + Keyword("--", ident_chars="-").set_parse_action(replaceWith("–")) + WordEnd()


class Mdash(AbstractMarkup):
@property
def expr(self) -> ParserElement:
return WordStart() + Keyword("---", identChars="-").setParseAction(replaceWith("—")) + WordEnd()
return WordStart() + Keyword("---", ident_chars="-").set_parse_action(replaceWith("—")) + WordEnd()


class Ruler(AbstractMarkup):
Expand All @@ -29,7 +29,9 @@ def expr(self) -> ParserElement:
# Text with dashed below it turns into a heading. To prevent this
# add a line break before the dashes.
return (
("\n" | StringStart() | LineBreak(**self.init_kwargs).expr)
+ Keyword("----", identChars="-").setParseAction(replaceWith("\n----"))
(LineStart() | LineBreak(**self.init_kwargs).expr)
+ (Optional(White()) + Keyword("----", ident_chars="-") + Optional(White())).set_parse_action(
replaceWith("\n----"),
)
+ LineEnd()
)
Loading

0 comments on commit 8146051

Please sign in to comment.