-
Notifications
You must be signed in to change notification settings - Fork 75
✨ 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
Changes from 1 commit
9935a20
bb9b840
523278b
e38bbbd
b07d778
4308e3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 | ||||||
|
||||||
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"\+-|\.\.|\?\?\?\?|!!!!|,,|--" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It's a bit more performant to pre-compile, then use e.g. |
||||||
|
||||||
# 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)\)" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
same as above |
||||||
|
||||||
SCOPED_ABBR = { | ||||||
"c": "©", | ||||||
"r": "®", | ||||||
"p": "§", | ||||||
"tm": "™" | ||||||
} | ||||||
|
||||||
|
||||||
def replaceFn(match: re.Match): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,19 @@ | |
<p>(bad)</p> | ||
. | ||
|
||
copyright (Lower) | ||
. | ||
(c) | ||
. | ||
<p>©</p> | ||
. | ||
|
||
copyright (Upper) | ||
. | ||
(C) | ||
. | ||
<p>©</p> | ||
. | ||
|
||
copyright | ||
. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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. 😄