Class: Metanorma::Standoc::LinkProtectPreprocessor

Inherits:
Asciidoctor::Extensions::Preprocessor
  • Object
show all
Defined in:
lib/metanorma/standoc/macros.rb

Overview

refer github.com/asciidoctor/asciidoctor/blob/main/lib/asciidoctor/substitutors.rb Not using TreeProcessor because that is still too close to inline expressions being processed on access (e.g. titles)

Constant Summary collapse

PASS_INLINE_MACROS =
%w(pass pass-format identifier std-link stem)
.join("|").freeze
PASS_INLINE_MACRO_STR =
"(\n  \\\\b(?<![-\\\\\\\\])                        # word-separator, no hyphen or backslash\n  (?:                                    # don't capture these!\n    (?:\#{PASS_INLINE_MACROS}):[^\\\\s\\\\[]* | # macro name, :, second key. OR:\n    span:uri \\\\b [^\\\\s\\\\[]*              # span:uri, third key\n  )\n  \\\\[.*?(?<!\\\\\\\\)\\\\]                     # [ ... ] not preceded by \\\\\n)\n".freeze
PASS_INLINE_MACRO_RX =
/#{PASS_INLINE_MACRO_STR}/xo
InlineLinkRx =

InlineLinkRx = %r((^|link:|#CG_BLANK|&lt;|[>();“‘])(\?(?:https?|file|ftp|irc)://)(?:([^s]+)[(|#CC_ALL*?[^\])]|([^s<]*([^s,.?!<)]))))m

%r((^|(?<![-\\])\blink:(?!\+)|\p{Blank}|&lt;|[<>\(\)\[\];"'])((?:https?|file|ftp|irc)://)(?:([^\s\[\]]+)(?:(\[(|.*?[^\\])\])|([^\s\[\]<]*([^\s,.?!\[\]<\)])))))m
InlineLinkMacroRx1 =

InlineLinkMacroRx = /\?(?:link|(mailto)):(|[^:s[^s[]*)[(|#CC_ALL*?)]/m

"(\\\\\\\\?\\\\b(?<!-)                  # optional backslash, no hyphen, word boundary\n  (?:link|mailto):)              # link: or mailto:\n(?!\\\\+)                          # no link:+ passthrough\n(|[^:\\\\s\\\\[][^\\\\s\\\\[]*)          # link: ... up to [\n(\\\\[(|.*?[^\\\\\\\\])\\\\])            # [ ... ], no ]\n".freeze
InlineLinkMacroRx =
/#{InlineLinkMacroRx1}/x

Instance Method Summary collapse

Instance Method Details



119
120
121
122
123
124
125
# File 'lib/metanorma/standoc/macros.rb', line 119

def inlinelink(text)
  text.include?("://") or return text
  /^\[.*\]\s*$/.match?(text) and return text
  pass_inline_split(text) do |x|
    inlinelink_escape(x)
  end.join
end


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/metanorma/standoc/macros.rb', line 127

def inlinelink_escape(text)
  text.gsub(InlineLinkRx) do
    body, suffix = $4.nil? ? [$3 + $6, "[]"] : [$3, ""]
    p = $1 and s = $2 and b = $4
    if p == "link:" then "#{p}++#{s}#{body}++#{b}#{suffix}"
    elsif p == "<"
      "#{p}link:++#{s}#{body.sub(/>$/, '')}++#{b}#{suffix}>"
    else "#{p}link:++#{s}#{body}++#{b}#{suffix}"
    end
  end
end

#inlinelinkmacro(text) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/metanorma/standoc/macros.rb', line 149

def inlinelinkmacro(text)
  (text.include?("[") &&
    ((text.include? "link:") || (text.include? "ilto:"))) or return text
  pass_inline_split(text) do |x|
    x.gsub(InlineLinkMacroRx) do
      "#{$1}++#{$2}++#{$3}"
    end
  end.join
end

#pass_inline_split(text) ⇒ Object



109
110
111
112
113
# File 'lib/metanorma/standoc/macros.rb', line 109

def pass_inline_split(text)
  text.split(PASS_INLINE_MACRO_RX).each.map do |x|
    PASS_INLINE_MACRO_RX.match?(x) ? x : yield(x)
  end
end

#process(document, reader) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/metanorma/standoc/macros.rb', line 84

def process(document, reader)
  p = Metanorma::Utils::LineStatus.new
  lines = reader.lines.map do |t|
    p.process(t)
    !p.pass && t.include?(":") and t = inlinelinkmacro(inlinelink(t))
    t
  end
  ::Asciidoctor::PreprocessorReader.new document, lines
end