# A link or image token.
# This class is mainly used to factorize work between images and links.
abstract class TokenLinkOrImage
super Token
# Link adress
var link: nullable Text = null
# Link text
var name: nullable Text = null
# Is the link construct an abbreviation?
var is_abbrev = false
redef fun emit(v) do
var tmp = new FlatBuffer
var b = check_link(v, tmp, pos, self)
if b > 0 then
emit_hyper(v)
v.current_pos = b
else
v.addc char
end
end
# Emit the hyperlink as link or image.
private fun emit_hyper(v: MarkdownProcessor) is abstract
# Check if the link is a valid link.
private fun check_link(v: MarkdownProcessor, out: FlatBuffer, start: Int, token: Token): Int do
var md = v.current_text
if md == null then return -1
var pos
if token isa TokenLink then
pos = start + 1
else
pos = start + 2
end
var tmp = new FlatBuffer
pos = md.read_md_link_id(tmp, pos)
if pos < start then return -1
name = tmp
var old_pos = pos
pos += 1
pos = md.skip_spaces(pos)
if pos < start then
var tid = name.as(not null).write_to_string.to_lower
if v.link_refs.has_key(tid) then
var lr = v.link_refs[tid]
is_abbrev = lr.is_abbrev
link = lr.link
comment = lr.title
pos = old_pos
else
return -1
end
else if md[pos] == '(' then
pos += 1
pos = md.skip_spaces(pos)
if pos < start then return -1
tmp = new FlatBuffer
var use_lt = md[pos] == '<'
if use_lt then
pos = md.read_until(tmp, pos + 1, '>')
else
pos = md.read_md_link(tmp, pos)
end
if pos < start then return -1
if use_lt then pos += 1
link = tmp.write_to_string
if md[pos] == ' ' then
pos = md.skip_spaces(pos)
if pos > start and md[pos] == '"' then
pos += 1
tmp = new FlatBuffer
pos = md.read_until(tmp, pos, '"')
if pos < start then return -1
comment = tmp.write_to_string
pos += 1
pos = md.skip_spaces(pos)
if pos == -1 then return -1
end
end
if pos < start then return -1
if md[pos] != ')' then return -1
else if md[pos] == '[' then
pos += 1
tmp = new FlatBuffer
pos = md.read_raw_until(tmp, pos, ']')
if pos < start then return -1
var id
if tmp.length > 0 then
id = tmp
else
id = name
end
var tid = id.as(not null).write_to_string.to_lower
if v.link_refs.has_key(tid) then
var lr = v.link_refs[tid]
link = lr.link
comment = lr.title
end
else
var tid = name.as(not null).write_to_string.replace("\n", " ").to_lower
if v.link_refs.has_key(tid) then
var lr = v.link_refs[tid]
link = lr.link
comment = lr.title
pos = old_pos
else
return -1
end
end
if link == null then return -1
return pos
end
end
lib/markdown/markdown.nit:2069,1--2188,3