-
Notifications
You must be signed in to change notification settings - Fork 80
✨ NEW: Add linkify #78
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
chrisjsewell
merged 14 commits into
executablebooks:master
from
tsutsu3:feature/5/linkify
Dec 13, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d71b1f8
Add linkify
tsutsu3 1ecaa09
Change implementation to exclude 'linkify-it-py' from dependency pack…
tsutsu3 1f15019
Fix mypy error
tsutsu3 667fc2e
Add dummy linkify tests
tsutsu3 82d6709
Comment out unnecessary methods
tsutsu3 711a9c2
Improve when linkify enabled but not installed
tsutsu3 e0d4b67
Add multi lines tests
tsutsu3 4a97b09
Fix bug caused by a misalignment of index 'i'
tsutsu3 3672c67
Change exception type and fix typo
tsutsu3 577b5c6
Merge branch 'master' into feature/5/linkify
tsutsu3 f4458ef
Improve while loop index
tsutsu3 efcac96
Merge branch 'master' into feature/5/linkify
chrisjsewell 258b35e
Update setup.py
chrisjsewell 994c0ad
Merge branch 'master' into feature/5/linkify
chrisjsewell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import re | ||
|
||
from ..common.utils import arrayReplaceAt | ||
from ..common.normalize_url import normalizeLinkText, normalizeLink, validateLink | ||
from .state_core import StateCore | ||
from ..token import Token | ||
|
||
|
||
LINK_OPEN_RE = re.compile(r"^<a[>\s]", flags=re.IGNORECASE) | ||
LINK_CLOSE_RE = re.compile(r"^</a\s*>", flags=re.IGNORECASE) | ||
|
||
HTTP_RE = re.compile(r"^http://") | ||
MAILTO_RE = re.compile(r"^mailto:") | ||
TEST_MAILTO_RE = re.compile(r"^mailto:", flags=re.IGNORECASE) | ||
|
||
|
||
def isLinkOpen(string: str): | ||
return LINK_OPEN_RE.search(string) | ||
|
||
|
||
def isLinkClose(string: str): | ||
return LINK_CLOSE_RE.search(string) | ||
|
||
|
||
def linkify(state: StateCore): | ||
blockTokens = state.tokens | ||
|
||
if not state.md.options.linkify: | ||
return | ||
|
||
if not state.md.linkify: | ||
raise ModuleNotFoundError("Linkify enabled but not installed.") | ||
|
||
for j in range(len(blockTokens)): | ||
if blockTokens[j].type != "inline" or not state.md.linkify.pretest( | ||
blockTokens[j].content | ||
): | ||
continue | ||
|
||
tokens = blockTokens[j].children | ||
|
||
htmlLinkLevel = 0 | ||
|
||
# We scan from the end, to keep position when new tags added. | ||
# Use reversed logic in links start/end match | ||
assert tokens is not None | ||
i = len(tokens) | ||
while i >= 1: | ||
i -= 1 | ||
assert isinstance(tokens, list) | ||
currentToken = tokens[i] | ||
|
||
# Skip content of markdown links | ||
if currentToken.type == "link_close": | ||
i -= 1 | ||
while ( | ||
tokens[i].level != currentToken.level | ||
and tokens[i].type != "link_open" | ||
): | ||
i -= 1 | ||
continue | ||
|
||
# Skip content of html tag links | ||
if currentToken.type == "html_inline": | ||
if isLinkOpen(currentToken.content) and htmlLinkLevel > 0: | ||
htmlLinkLevel -= 1 | ||
if isLinkClose(currentToken.content): | ||
htmlLinkLevel += 1 | ||
if htmlLinkLevel > 0: | ||
continue | ||
|
||
if currentToken.type == "text" and state.md.linkify.test( | ||
currentToken.content | ||
): | ||
text = currentToken.content | ||
links = state.md.linkify.match(text) | ||
|
||
# Now split string to nodes | ||
nodes = [] | ||
level = currentToken.level | ||
lastPos = 0 | ||
|
||
for ln in range(len(links)): | ||
url = links[ln].url | ||
fullUrl = normalizeLink(url) | ||
if not validateLink(fullUrl): | ||
continue | ||
|
||
urlText = links[ln].text | ||
|
||
# Linkifier might send raw hostnames like "example.com", where url | ||
# starts with domain name. So we prepend http:// in those cases, | ||
# and remove it afterwards. | ||
if not links[ln].schema: | ||
urlText = HTTP_RE.sub( | ||
"", normalizeLinkText("http://" + urlText) | ||
) | ||
elif links[ln].schema == "mailto:" and TEST_MAILTO_RE.search( | ||
urlText | ||
): | ||
urlText = MAILTO_RE.sub( | ||
"", normalizeLinkText("mailto:" + urlText) | ||
) | ||
else: | ||
urlText = normalizeLinkText(urlText) | ||
|
||
pos = links[ln].index | ||
|
||
if pos > lastPos: | ||
token = Token("text", "", 0) | ||
token.content = text[lastPos:pos] | ||
token.level = level | ||
nodes.append(token) | ||
|
||
token = Token("link_open", "a", 1) | ||
token.attrs = [["href", fullUrl]] | ||
token.level = level + 1 | ||
token.markup = "linkify" | ||
token.info = "auto" | ||
nodes.append(token) | ||
|
||
token = Token("text", "", 0) | ||
token.content = urlText | ||
token.level = level | ||
nodes.append(token) | ||
|
||
token = Token("link_close", "a", -1) | ||
token.level = level - 1 | ||
token.markup = "linkify" | ||
token.info = "auto" | ||
nodes.append(token) | ||
|
||
lastPos = links[ln].last_index | ||
|
||
if lastPos < len(text): | ||
token = Token("text", "", 0) | ||
token.content = text[lastPos:] | ||
token.level = level | ||
nodes.append(token) | ||
|
||
blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.