Add either a link or image, or a plan [
character, to block's children.
If there is a matching delimiter, remove it from the delimiter stack.
# Try match close bracket against an opening delimiter stack
#
# Add either a link or image, or a plan `[` character, to block's children.
# If there is a matching delimiter, remove it from the delimiter stack.
private fun parse_close_bracket: Bool do
advance 1
var start_index = index
var start_column = column
# Get previous `[` or `![`
var opener = last_bracket
if opener == null then
# no matching opener, just return a literal
append_text("]")
return true
end
if not opener.allowed then
# matching opener but it's not allowed, juste return a literal
append_text("]")
remove_last_bracket
return true
end
# check to see if we have a link or image
var dest: nullable Couple[nullable String, Bool] = null
var title = null
var is_link_or_image = false
# maybe an inline link like `[foo](\uri "title")`
if peek == '(' then
advance 1
spnl
dest = parse_link_destination
if dest.first != null then
spnl
# title needs a whitespace before
if input.substring(index - 1, 1).has(re_whitespace) then
title = parse_link_title
spnl
end
if peek == ')' then
advance 1
is_link_or_image = true
else
index = start_index
column = start_column
end
end
end
# maybe a reference link like `[foo][bar]`, `[foo][]` or `[foo]`
if not is_link_or_image then
# see if there's a link label like `[bar]` or `[]`
var before_label = index
var label_length = parse_link_label
advance label_length
var ref = null
if label_length > 2 then
ref = input.substring(before_label, label_length)
else if not opener.bracket_after then
# If the second label is empty `[foo][]` or missing `[foo]`, then the first label
# is the reference.
# But it can only be a reference when there's no (unescaped) bracket in it.
# If there is, we don't even need to try to lookup the reference.
ref = input.substring(opener.index, start_index - opener.index)
end
if ref != null then
var nref = ref.normalize_reference
if reference_map.has_key(nref) then
var link = reference_map[nref]
dest = new Couple[nullable String, Bool](link.destination, false)
title = link.title
is_link_or_image = true
end
end
end
if is_link_or_image then
# If we got here, open is a potential opener
var link_or_image: MdLinkOrImage
if opener.is_image then
link_or_image = new MdImage(new MdLocation(line, opener.column, line, column - 1), dest.as(not null).first or else "", title)
else
link_or_image = new MdLink(new MdLocation(line, opener.column, line, column - 1), dest.as(not null).first or else "", title)
end
link_or_image.has_brackets = dest.as(not null).second
var node = opener.node.next
while node != null do
var next = node.next
link_or_image.append_child(node)
node = next
end
append_node(link_or_image)
# Process delimiters such as emphasis inside a link/image
process_delimiters(opener.prev_delimiter)
merge_child_text_nodes(link_or_image)
# We don't need the corresponding text node anymore, we turned it into a node
opener.node.unlink
remove_last_bracket
# Links within links are not allowed
# We found this link, so there can be no other link around it.
if not opener.is_image then
var bracket = last_bracket
while bracket != null do
if not bracket.is_image then
# disallow link opener
bracket.allowed = false
end
bracket = bracket.prev
end
end
return true
end
if not is_link_or_image then
if parse_wikilink then return true
end
# no link or image
append_text("]")
remove_last_bracket
index = start_index
column = start_column
return true
end
lib/markdown2/markdown_inline_parsing.nit:496,2--625,4