Skip to content

Commit

Permalink
fixup! fix(jira2markdown): process windows line breaks as a Unix line…
Browse files Browse the repository at this point in the history
… breaks
  • Loading branch information
catcombo committed Oct 2, 2024
1 parent 5ed8b03 commit 6252a47
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
5 changes: 3 additions & 2 deletions jira2markdown/markup/headings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pyparsing import Combine, LineEnd, LineStart, Optional, ParserElement, ParseResults, SkipTo, StringEnd, White, Word
from pyparsing import Combine, LineStart, Optional, ParserElement, ParseResults, SkipTo, StringEnd, White, Word

from jira2markdown.markup.base import AbstractMarkup
from jira2markdown.tokens import UniversalLineEnd


class Headings(AbstractMarkup):
Expand All @@ -17,6 +18,6 @@ def expr(self) -> ParserElement:
+ Combine(
Word("h", "123456", exact=2).set_results_name("level")
+ ". "
+ SkipTo(LineEnd() | StringEnd()).set_results_name("text"),
+ SkipTo(UniversalLineEnd() | StringEnd()).set_results_name("text"),
).set_parse_action(self.action)
)
4 changes: 2 additions & 2 deletions jira2markdown/markup/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pyparsing import (
Char,
Combine,
LineEnd,
LineStart,
Literal,
MatchFirst,
Expand All @@ -20,6 +19,7 @@
from jira2markdown.markup.advanced import Panel
from jira2markdown.markup.base import AbstractMarkup
from jira2markdown.markup.text_effects import BlockQuote, Color
from jira2markdown.tokens import UniversalLineEnd


class ListIndentState:
Expand Down Expand Up @@ -90,7 +90,7 @@ def action(self, tokens: ParseResults) -> str:
@property
def expr(self) -> ParserElement:
WHITESPACE = Regex(r"[ \t]+", flags=re.UNICODE)
EOL = LineEnd()
EOL = UniversalLineEnd()
LIST_BREAK = EOL + Optional(WHITESPACE) + EOL | StringEnd()
IGNORE = BlockQuote(**self.init_kwargs).expr | Panel(**self.init_kwargs).expr | Color(**self.init_kwargs).expr
ROW = (
Expand Down
6 changes: 3 additions & 3 deletions jira2markdown/markup/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from pyparsing import (
Group,
LineEnd,
LineStart,
Literal,
OneOrMore,
Expand All @@ -19,6 +18,7 @@
from jira2markdown.markup.base import AbstractMarkup
from jira2markdown.markup.images import Image
from jira2markdown.markup.links import Link, MailTo, Mention
from jira2markdown.tokens import UniversalLineEnd


class Table(AbstractMarkup):
Expand Down Expand Up @@ -52,7 +52,7 @@ def action(self, tokens: ParseResults) -> str:

@property
def expr(self) -> ParserElement:
NL = LineEnd().suppress()
NL = UniversalLineEnd().suppress()
SEP = (Literal("||") | Literal("|")).suppress()
ROW_BREAK = NL + SEP | NL + NL | StringEnd()
IGNORE = (
Expand All @@ -67,7 +67,7 @@ def expr(self) -> ParserElement:
stop_on=ROW_BREAK | NL + ~SEP,
)

EMPTY_LINE = LineStart() + Optional(Regex(r"[ \t]+", flags=re.UNICODE)) + LineEnd()
EMPTY_LINE = LineStart() + Optional(Regex(r"[ \t]+", flags=re.UNICODE)) + UniversalLineEnd()
return (
(StringStart() ^ Optional(EMPTY_LINE, default="\n"))
+ OneOrMore(LineStart() + Group(ROW) + NL).set_parse_action(self.action)
Expand Down
5 changes: 3 additions & 2 deletions jira2markdown/markup/text_breaks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pyparsing import Keyword, LineEnd, LineStart, Optional, ParserElement, White, WordEnd, WordStart, replaceWith
from pyparsing import Keyword, LineStart, Optional, ParserElement, White, WordEnd, WordStart, replaceWith

from jira2markdown.markup.base import AbstractMarkup
from jira2markdown.tokens import UniversalLineEnd


class LineBreak(AbstractMarkup):
Expand Down Expand Up @@ -33,5 +34,5 @@ def expr(self) -> ParserElement:
+ (Optional(White()) + Keyword("----", ident_chars="-") + Optional(White())).set_parse_action(
replaceWith("\n----"),
)
+ LineEnd()
+ UniversalLineEnd()
)
4 changes: 2 additions & 2 deletions jira2markdown/markup/text_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Char,
Combine,
FollowedBy,
LineEnd,
LineStart,
Literal,
OneOrMore,
Expand All @@ -30,6 +29,7 @@
from jira2markdown.markup.base import AbstractMarkup
from jira2markdown.markup.images import Image
from jira2markdown.markup.links import Attachment, Link, Mention
from jira2markdown.tokens import UniversalLineEnd


class QuotedElement(AbstractMarkup):
Expand Down Expand Up @@ -159,7 +159,7 @@ def action(self, tokens: ParseResults) -> str:

@property
def expr(self) -> ParserElement:
NL = LineEnd()
NL = UniversalLineEnd()
EMPTY_LINE = LineStart() + Optional(Regex(r"[ \t]+", flags=re.UNICODE)) + NL
ROW = (
LineStart()
Expand Down
1 change: 0 additions & 1 deletion jira2markdown/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ def convert(text: str, usernames: Optional[dict] = None, elements: Optional[Mark
inline_markup << elements.expr(inline_markup, markup, usernames, filter(lambda e: e.is_inline_element, elements))
markup << elements.expr(inline_markup, markup, usernames, elements)

text = text.replace("\r\n", "\n")
return markup.transform_string(text)
20 changes: 20 additions & 0 deletions jira2markdown/tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pyparsing import LineEnd, ParseException, ParseImplReturnType


class UniversalLineEnd(LineEnd):
def __init__(self):
super().__init__()
self.whiteChars.discard("\r\n")

def parseImpl(self, instring, loc, do_actions=True) -> ParseImplReturnType:
if loc < len(instring):
if instring.startswith("\r\n", loc):
return loc + 2, "\n"
elif instring[loc] in ("\n", "\r"):
return loc + 1, "\n"
else:
raise ParseException(instring, loc, self.errmsg, self)
elif loc == len(instring):
return loc + 1, []
else:
raise ParseException(instring, loc, self.errmsg, self)

0 comments on commit 6252a47

Please sign in to comment.