Skip to content

✨ NEW: Add simple typographic replacements #59

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

Merged
merged 6 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions markdown_it/parser_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

from .ruler import Ruler
from .rules_core.state_core import StateCore
from .rules_core import normalize, block, inline
from .rules_core import normalize, block, inline, replace

# TODO linkify, replacements, smartquotes
_rules = [
["normalize", normalize],
["block", block],
["inline", inline],
# [ 'linkify', require('./rules_core/linkify') ],
# [ 'replacements', require('./rules_core/replacements') ],
['replacements', replace],
# [ 'smartquotes', require('./rules_core/smartquotes') ]
]

Expand Down
1 change: 1 addition & 0 deletions markdown_it/rules_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .normalize import normalize # noqa: F401
from .block import block # noqa: F401
from .inline import inline # noqa: F401
from .replacements import replace # noqa: F401
107 changes: 0 additions & 107 deletions markdown_it/rules_core/replacements.js

This file was deleted.

100 changes: 100 additions & 0 deletions markdown_it/rules_core/replacements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Simple typographic replacements

(c) (C) → ©
(tm) (TM) → ™
(r) (R) → ®
+- → ±
(p) (P) -> §
... → … (also ?.... → ?.., !.... → !..)
???????? → ???, !!!!! → !!!, `,,` → `,`
-- → –, --- → —
"""
import logging
import re
from typing import List
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from typing import List
from typing import List, Match

Copy link
Contributor Author

@tsutsu3 tsutsu3 Oct 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

I was just in trouble. 😄


from .state_core import StateCore
from ..token import Token

LOGGER = logging.getLogger(__name__)

# TODO:
# - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
# - miltiplication 2 x 4 -> 2 × 4

RARE_RE = r"\+-|\.\.|\?\?\?\?|!!!!|,,|--"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARE_RE = r"\+-|\.\.|\?\?\?\?|!!!!|,,|--"
RARE_RE = re.compile(r"\+-|\.\.|\?\?\?\?|!!!!|,,|--")

It's a bit more performant to pre-compile, then use e.g. RARE_RE.search


# Workaround for phantomjs - need regex without /g flag,
# or root check will fail every second time
# SCOPED_ABBR_TEST_RE = r"\((c|tm|r|p)\)"

SCOPED_ABBR_RE = r"\((c|tm|r|p)\)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SCOPED_ABBR_RE = r"\((c|tm|r|p)\)"
SCOPED_ABBR_RE = re.compile(r"\((c|tm|r|p)\)", flags=re.IGNORECASE)

same as above


SCOPED_ABBR = {
"c": "©",
"r": "®",
"p": "§",
"tm": "™"
}


def replaceFn(match: re.Match):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def replaceFn(match: re.Match):
def replaceFn(match: Match):

return SCOPED_ABBR[match.group(1).lower()]


def replace_scoped(inlineTokens: List[Token]):
inside_autolink = 0

for token in inlineTokens:
if token.type == "text" and not inside_autolink:
token.content = re.sub(SCOPED_ABBR_RE, replaceFn, token.content, flags=re.IGNORECASE)

if token.type == "link_open" and token.info == "auto":
inside_autolink -= 1

if token.type == "link_close" and token.info == "auto":
inside_autolink += 1


def replace_rare(inlineTokens: List[Token]):
inside_autolink = 0

for token in inlineTokens:
if token.type == "text" and not inside_autolink:
if re.search(RARE_RE, token.content):
token.content = re.sub(r"\+-", "±", token.content)
# .., ..., ....... -> …
# but ?..... & !..... -> ?.. & !..
token.content = re.sub(r"\.{2,}", "…", token.content)
token.content = re.sub(r"([?!])…", "\\1..", token.content)
token.content = re.sub(r"([?!]){4,}", "\\1\\1\\1", token.content)
token.content = re.sub(r",{2,}", ",", token.content)
# em-dash
token.content = re.sub(r"(^|[^-])---(?=[^-]|$)",
"\\1\u2014", token.content, flags=re.MULTILINE)
# en-dash
token.content = re.sub(r"(^|\s)--(?=\s|$)", "\\1\u2013",
token.content, flags=re.MULTILINE)
token.content = re.sub(r"(^|[^-\s])--(?=[^-\s]|$)",
"\\1\u2013", token.content, flags=re.MULTILINE)

if token.type == "link_open" and token.info == "auto":
inside_autolink -= 1

if token.type == "link_close" and token.info == "auto":
inside_autolink += 1


def replace(state: StateCore):
if not state.md.options.typographer:
return

for token in state.tokens:
if token.type != "inline":
continue

if re.search(SCOPED_ABBR_RE, token.content, flags=re.IGNORECASE):
replace_scoped(token.children)

if re.search(RARE_RE, token.content):
replace_rare(token.children)
2 changes: 1 addition & 1 deletion tests/test_api/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_get_rules():
md = MarkdownIt("zero")
# print(md.get_all_rules())
assert md.get_all_rules() == {
"core": ["normalize", "block", "inline"],
"core": ["normalize", "block", "inline", "replacements"],
"block": [
"table",
"code",
Expand Down
13 changes: 13 additions & 0 deletions tests/test_port/fixtures/typographer.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
<p>(bad)</p>
.

copyright (Lower)
.
(c)
.
<p>©</p>
.

copyright (Upper)
.
(C)
.
<p>©</p>
.

copyright
.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_port/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


@pytest.mark.parametrize(
"line,title,input,expected", read_fixture_file(FIXTURE_PATH.joinpath("typographer.md"))
)
def test_typographer(line, title, input, expected):
md = MarkdownIt().enable("replacements")
md.options["typographer"] = True
text = md.render(input)
assert text.rstrip() == expected.rstrip()


@pytest.mark.parametrize(
"line,title,input,expected", read_fixture_file(FIXTURE_PATH.joinpath("tables.md"))
)
Expand Down