module

Hwaro::Content::Processors::Wikilinks

Constants

DEF_TOKEN = (/(?m:^) {0,3}\[(?!\^)[^\]\n]+\]:[ \t]*<?(?<def>[^\s>]+)/).source
ESC_TOKEN = (/(?<esc>\\[^\n])/).source
FOOTNOTE_DEF_RE = /\A {0,3}\[\^[^\]]+\]:[^\n]/

A footnote definition starts a new block, as a list item does. Same shape preprocess_footnotes extracts (FOOTNOTE_DEF_RE there).

HREF_RE = /\bhref\s*=\s*["']([^"']*)["']/i
IMAGE_EXT_RE = /\.(?:png|jpe?g|gif|webp|svg|avif|bmp|ico|tiff?)\z/i
LINK_MATH_TOKEN_RE = Regex.new("#{VERBATIM_TOKENS}|#{TEX_TOKEN}|#{ESC_TOKEN}|#{WIKILINK_TOKEN}|#{URL_TOKEN}|#{DEF_TOKEN}")
LINK_TOKEN_RE = Regex.new("#{VERBATIM_TOKENS}|#{ESC_TOKEN}|#{WIKILINK_TOKEN}|#{URL_TOKEN}|#{DEF_TOKEN}")
QUOTE_MARKERS_RE = /\A(?: {0,3}>[ \t]?)*/

The blockquote markers a line opens with.

QUOTE_PREFIX_RE = /\A {0,3}> ?/

One blockquote marker, as TableParser strips it for a quoted table.

SIZE_RE = /\A(\d+)(?:x(\d+))?\z/
STANDALONE_LINE_RE = /\A(?: {0,3}>[ \t]?)*+(?: {0,3}\#{1,6}(?:[ \t]|\r?\n?\z)| {0,3}(?:=+|-+|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})[ \t]*\r?\n?\z)/

A line matched on its own, never joined to a chunk: an ATX heading, a setext underline or thematic break. Table rows are too (see walk), each row being its own block.

TEX_TOKEN = (/(?<tex>\\\((?s:.*?)\\\))/).source

\(…\) TeX math, kept verbatim with [markdown] math (the $ forms are stashed by MarkdownExtensions.protect_math). Precedes ESC_TOKEN, which would otherwise take its \(.

URL_TOKEN = (/\]\(\s*<?(?<url>[^\s)>]+)/).source

The other link forms backlinks count: a Markdown destination (](url, which rewrite also keeps verbatim, so a [[x]] inside a URL is never rewritten) and a link reference definition ([label]: url, not a footnote). HTML hrefs are read out of the tag token.

VERBATIM_TOKENS = [(/(?<code>`+)(?s:.*?)(?<!`)\k<code>(?!`)/).source, (/(?<comment><!--(?s:.*?)-->)/).source, "(?<tag>#{MarkdownExtensions::HTML_TAG_RE.source})", (/(?<autolink><[A-Za-z][A-Za-z0-9+.-]*:[^\s<>]*>)/).source].join('|')

Tokens kept verbatim, matched over one paragraph-sized chunk: a code span (which may cross a line break), an HTML comment, an HTML tag (Markd's grammar, so attribute values are never rewritten), an autolink (<https://x/[[y]]>) and a backslash escape (\[[x]] stays literal).

WIKILINK_TOKEN = (/(?<bang>!?)\[\[(?<inner>[^\[\]\n]+)\]\]/).source

Class methods

key(text : String) : String

Lookup key: NFC (a file name saved decomposed still matches a link typed composed) and lowercase.

Source

Instance methods

rewrite(content : String, source : Models::Page, index : Index, safe : Bool = false, misses : Array(Tuple(String, String)) | Nil = nil, warn : Bool = true, math : Bool = false) : String

Rewrite every wikilink in content (see the file comment). An unresolved one renders as a wikilink-missing span (plain text in safe mode), is warned about, and is appended to misses as {"[[…]]" source text, reason}.

Source

Nested types