-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Markdown: allow to end URL with balanced parenthesis #18321
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -1246,15 +1246,44 @@ proc isUrl(p: RstParser, i: int): bool = | |
p.tok[i+3].kind == tkWord and | ||
p.tok[i].symbol in ["http", "https", "ftp", "telnet", "file"] | ||
|
||
proc checkParen(token: Token, parensStack: var seq[char]): bool {.inline.} = | ||
## Returns `true` iff `token` is a closing parenthesis for some | ||
## previous opening parenthesis saved in `parensStack`. | ||
## This is according Markdown balanced parentheses rule | ||
## (https://spec.commonmark.org/0.29/#link-destination) | ||
## to allow links like | ||
## https://en.wikipedia.org/wiki/APL_(programming_language), | ||
## we use it for RST also. | ||
result = false | ||
if token.kind == tkPunct: | ||
let c = token.symbol[0] | ||
if c in {'(', '[', '{'}: # push | ||
parensStack.add c | ||
elif c in {')', ']', '}'}: # try pop | ||
if parensStack.len > 0: | ||
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. Pointless 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. removed, thanks. |
||
# a case like ([) inside a link is allowed and [ is discarded: | ||
for i in countdown(parensStack.len - 1, 0): | ||
if (parensStack[i] == '(' and c == ')' or | ||
parensStack[i] == '[' and c == ']' or | ||
parensStack[i] == '{' and c == '}'): | ||
parensStack.setLen i | ||
result = true | ||
break | ||
|
||
proc parseUrl(p: var RstParser): PRstNode = | ||
## https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#standalone-hyperlinks | ||
result = newRstNode(rnStandaloneHyperlink) | ||
var lastIdx = p.idx | ||
var closedParenIdx = p.idx - 1 # for balanced parens rule | ||
var parensStack: seq[char] | ||
while p.tok[lastIdx].kind in {tkWord, tkPunct, tkOther}: | ||
let isClosing = checkParen(p.tok[lastIdx], parensStack) | ||
if isClosing: | ||
closedParenIdx = lastIdx | ||
inc lastIdx | ||
dec lastIdx | ||
# standalone URL can not end with punctuation in RST | ||
while lastIdx >= p.idx and p.tok[lastIdx].kind == tkPunct and | ||
while lastIdx > closedParenIdx and p.tok[lastIdx].kind == tkPunct and | ||
p.tok[lastIdx].symbol != "/": | ||
dec lastIdx | ||
var s = "" | ||
|
@@ -1393,11 +1422,15 @@ proc parseMarkdownLink(p: var RstParser; father: PRstNode): bool = | |
var desc, link = "" | ||
var i = p.idx | ||
|
||
var parensStack: seq[char] | ||
template parse(endToken, dest) = | ||
parensStack.setLen 0 | ||
inc i # skip begin token | ||
while true: | ||
if p.tok[i].kind in {tkEof, tkIndent}: return false | ||
if p.tok[i].symbol == endToken: break | ||
let isClosing = checkParen(p.tok[i], parensStack) | ||
if p.tok[i].symbol == endToken and not isClosing: | ||
break | ||
dest.add p.tok[i].symbol | ||
inc i | ||
inc i # skip end token | ||
|
This file contains 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
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.
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.
completely optional: can you split this into a more general reusable proc and then call it, and then in future work we can move this reusable proc to strbasics or strutils? (similar to https://dlang.org/library/std/algorithm/searching/balanced_parens.html, and same functionality as what you have)
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.
The idea of
balancedParens
is very different: it check whether parens are balanced or not.checkParen
does not always forbid unbalanced parens: e.g.([)
and(])
are legal, andcheckParen
will return 3 valuesFalse,False,True
in both cases for 3 tokens.