Class: Mdlint::Parser::InlineParser

Inherits:
Object
  • Object
show all
Defined in:
lib/mdlint/parser/inline_parser.rb,
sig/internal.rbs

Constant Summary collapse

ESCAPE_CHARS =
'!"#$%&\'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~'
ESCAPE_REGEXP =
/\\([#{Regexp.escape(ESCAPE_CHARS)}])/
BACKTICK_REGEXP =
/(`+)(.+?)\1(?!`)/
%r{<(([A-Za-z][A-Za-z0-9+.-]{1,31}):[^\s<>]+)>}
/<([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/
HTML_INLINE_REGEXP =
%r{</?[a-zA-Z][a-zA-Z0-9:-]*(?:\s+[^>]*)?/?>}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ InlineParser

Returns a new instance of InlineParser.

Parameters:

  • options (Object) (defaults to: {})


15
16
17
18
# File 'lib/mdlint/parser/inline_parser.rb', line 15

def initialize(options = {})
  @options = options
  @dialect = Dialect.resolve(options[:dialect])
end

Instance Method Details

#code_span_match(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/mdlint/parser/inline_parser.rb', line 423

def code_span_match(value)
  opening = value[/\A`+/]
  return unless opening

  delimiter = opening
  search = delimiter.length
  while (closing = value.index(delimiter, search))
    run = value[closing..].match(/\A`+/)[0].length
    if run == delimiter.length
      return { delimiter: delimiter, content: value[delimiter.length...closing], length: closing + delimiter.length }
    end
    search = closing + run
  end
  nil
end

#delimiter_close?(delimiter, before, after) ⇒ Boolean

Parameters:

  • delimiter (Object)
  • before (Object)
  • after (Object)

Returns:

  • (Boolean)


464
465
466
467
468
469
470
471
472
473
# File 'lib/mdlint/parser/inline_parser.rb', line 464

def delimiter_close?(delimiter, before, after)
  before_space = whitespace_character?(before)
  after_space = whitespace_character?(after)
  before_punctuation = punctuation_character?(before)
  after_punctuation = punctuation_character?(after)
  left_flanking = !after_space && (!after_punctuation || before_space || before_punctuation)
  right_flanking = !before_space && (!before_punctuation || after_space || after_punctuation)

  delimiter.start_with?("_") ? right_flanking && (!left_flanking || after_punctuation || after_space) : right_flanking
end

#delimiter_open?(delimiter, before, after) ⇒ Boolean

Parameters:

  • delimiter (Object)
  • before (Object)
  • after (Object)

Returns:

  • (Boolean)


453
454
455
456
457
458
459
460
461
462
# File 'lib/mdlint/parser/inline_parser.rb', line 453

def delimiter_open?(delimiter, before, after)
  before_space = whitespace_character?(before)
  after_space = whitespace_character?(after)
  before_punctuation = punctuation_character?(before)
  after_punctuation = punctuation_character?(after)
  left_flanking = !after_space && (!after_punctuation || before_space || before_punctuation)
  right_flanking = !before_space && (!before_punctuation || after_space || after_punctuation)

  delimiter.start_with?("_") ? left_flanking && (!right_flanking || before_punctuation || before_space) : left_flanking
end

#emphasis_match(value, delimiter, previous:, strong: false) ⇒ Object

Parameters:

  • value (Object)
  • delimiter (Object)
  • previous: (Object)
  • strong: (Boolean) (defaults to: false)

Returns:

  • (Object)


439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/mdlint/parser/inline_parser.rb', line 439

def emphasis_match(value, delimiter, previous:, strong: false)
  escaped = Regexp.escape(delimiter)
  match = value.match(/\A#{escaped}(?!\s)(.+?)(?<!\s)#{escaped}/m)
  return unless match

  opening_next = match[1][0]
  closing_previous = match[1][-1]
  after = value[match[0].length]
  return unless delimiter_open?(delimiter, previous, opening_next)
  return unless delimiter_close?(delimiter, closing_previous, after)

  match
end

#find_unescaped(value, character, start) ⇒ Object

Parameters:

  • value (Object)
  • character (Object)
  • start (Object)

Returns:

  • (Object)


413
414
415
416
417
418
419
420
421
# File 'lib/mdlint/parser/inline_parser.rb', line 413

def find_unescaped(value, character, start)
  index = start
  while index < value.length
    return index if value[index] == character && (index.zero? || value[index - 1] != "\\")

    index += 1
  end
  nil
end

#flush_text(buffer, tokens) ⇒ Object

Parameters:

  • buffer (Object)
  • tokens (Object)

Returns:

  • (Object)


483
484
485
486
487
# File 'lib/mdlint/parser/inline_parser.rb', line 483

def flush_text(buffer, tokens)
  return if buffer.empty?

  tokens << Token.new(type: :text, content: buffer)
end

Parameters:

  • value (Object)
  • image: (Boolean) (defaults to: false)

Returns:

  • (Object)


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/mdlint/parser/inline_parser.rb', line 322

def inline_link_match(value, image: false)
  prefix = image ? "![" : "["
  return unless value.start_with?(prefix)

  depth = 1
  index = prefix.length
  while index < value.length
    if value[index] == "\\"
      index += 2
      next
    end
    if value[index] == "`" && (code = code_span_match(value[index..]))
      index += code[:length]
      next
    end
    if value[index] == "["
      depth += 1
    elsif value[index] == "]"
      depth -= 1
      break if depth.zero?
    end
    index += 1
  end
  return unless depth.zero? && value[index + 1] == "("

  destination = parse_link_destination(value[(index + 2)..])
  return unless destination

  {
    text: value[prefix.length...index],
    url: destination[:url],
    title: destination[:title],
    length: index + 2 + destination[:length]
  }
end

#parse(content) ⇒ Object

Parameters:

  • content (Object)

Returns:

  • (Object)


20
21
22
23
24
# File 'lib/mdlint/parser/inline_parser.rb', line 20

def parse(content)
  tokens = []
  parse_inline(content, tokens)
  tokens
end

#parse_inline(content, tokens) ⇒ Object

Parameters:

  • content (Object)
  • tokens (Object)

Returns:

  • (Object)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/mdlint/parser/inline_parser.rb', line 26

def parse_inline(content, tokens)
  pos = 0
  text_buffer = ""

  while pos < content.length
    remaining = content[pos..]

    if (match = remaining.match(/\A\[\^([^\]]+)\]/))
      flush_text(text_buffer, tokens)
      text_buffer = ""
      tokens << Token.new(type: :footnote_ref, attrs: { label: match[1].downcase })
      pos += match[0].length
    elsif (match = remaining.match(/\A\$([^$\n]+)\$/))
      flush_text(text_buffer, tokens)
      text_buffer = ""
      tokens << Token.new(type: :math_inline, content: match[1])
      pos += match[0].length
    elsif remaining.start_with?("\\\n")
      flush_text(text_buffer, tokens)
      text_buffer = ""
      tokens << Token.new(type: :hardbreak, tag: "br")
      pos += 2
    elsif (match = remaining.match(/\A#{ESCAPE_REGEXP}/))
      flush_text(text_buffer, tokens)
      text_buffer = ""
      tokens << Token.new(type: :text, content: match[1])
      pos += match[0].length
    elsif remaining.start_with?("![")
      if (match = inline_link_match(remaining, image: true))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(
          type: :image,
          tag: "img",
          attrs: {
            src: match[:url],
            alt: match[:text],
            title: match[:title]
          }.compact,
          content: match[:text]
        )
        pos += match[:length]
      elsif (match = remaining.match(/\A!\[([^\]]*)\]\[([^\]]*)\]/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        label = match[2].empty? ? match[1] : match[2]
        tokens << Token.new(
          type: :image,
          tag: "img",
          attrs: { reference_label: label.downcase, reference_kind: :full, alt: match[1] },
          content: match[1],
          markup: "reference"
        )
        pos += match[0].length
      elsif (match = remaining.match(/\A!\[([^\]]+)\](?!\()/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(
          type: :image,
          tag: "img",
          attrs: { reference_label: match[1].downcase, reference_kind: :shortcut, alt: match[1] },
          content: match[1],
          markup: "reference"
        )
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("[")
      # Inline link: [text](url "title")
      if (match = inline_link_match(remaining))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(
          type: :link_open,
          tag: "a",
          nesting: 1,
          attrs: {
            href: match[:url],
            title: match[:title]
          }.compact
        )
        parse_inline(match[:text], tokens)
        tokens << Token.new(
          type: :link_close,
          tag: "a",
          nesting: -1
        )
        pos += match[:length]
      # Full reference link: [text][label]
      elsif (match = remaining.match(/\A\[([^\]]*)\]\[([^\]]*)\]/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        label = match[2].empty? ? match[1] : match[2]
        tokens << Token.new(
          type: :link_open,
          tag: "a",
          nesting: 1,
          attrs: { reference_label: label.downcase, reference_kind: :full },
          markup: "reference"
        )
        parse_inline(match[1], tokens)
        tokens << Token.new(
          type: :link_close,
          tag: "a",
          nesting: -1,
          markup: "reference"
        )
        pos += match[0].length
      # Shortcut reference link: [label]
      elsif (match = remaining.match(/\A\[([^\[\]]+)\](?!\(|\[)/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        label = match[1]
        tokens << Token.new(
          type: :link_open,
          tag: "a",
          nesting: 1,
          attrs: { reference_label: label.downcase, reference_kind: :shortcut },
          markup: "reference"
        )
        tokens << Token.new(type: :text, content: label)
        tokens << Token.new(
          type: :link_close,
          tag: "a",
          nesting: -1,
          markup: "reference"
        )
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("`")
      if (match = code_span_match(remaining))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        code_content = match[:content]
        code_content = code_content.gsub(/\r?\n/, " ")
        if code_content.start_with?(" ") && code_content.end_with?(" ") && !code_content.match?(/\A +\z/)
          code_content = code_content[1...-1]
        end
        tokens << Token.new(
          type: :code_inline,
          tag: "code",
          content: code_content,
          markup: match[:delimiter]
        )
        pos += match[:length]
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("**")
      if (match = emphasis_match(remaining, "**", previous: pos.zero? ? nil : content[pos - 1], strong: true))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(type: :strong_open, tag: "strong", nesting: 1, markup: "**")
        parse_inline(match[1], tokens)
        tokens << Token.new(type: :strong_close, tag: "strong", nesting: -1, markup: "**")
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("__")
      if (match = emphasis_match(remaining, "__", previous: pos.zero? ? nil : content[pos - 1], strong: true))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(type: :strong_open, tag: "strong", nesting: 1, markup: "__")
        parse_inline(match[1], tokens)
        tokens << Token.new(type: :strong_close, tag: "strong", nesting: -1, markup: "__")
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif @dialect.feature?(:strikethrough) && remaining.start_with?("~~")
      if (match = remaining.match(/\A~~(?!\s)(.+?)(?<!\s)~~/m))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(type: :s_open, tag: "del", nesting: 1, markup: "~~")
        parse_inline(match[1], tokens)
        tokens << Token.new(type: :s_close, tag: "del", nesting: -1, markup: "~~")
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("*")
      if (match = emphasis_match(remaining, "*", previous: pos.zero? ? nil : content[pos - 1]))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(type: :em_open, tag: "em", nesting: 1, markup: "*")
        parse_inline(match[1], tokens)
        tokens << Token.new(type: :em_close, tag: "em", nesting: -1, markup: "*")
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("_")
      if (match = emphasis_match(remaining, "_", previous: pos.zero? ? nil : content[pos - 1]))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(type: :em_open, tag: "em", nesting: 1, markup: "_")
        parse_inline(match[1], tokens)
        tokens << Token.new(type: :em_close, tag: "em", nesting: -1, markup: "_")
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif remaining.start_with?("<")
      if (match = remaining.match(/\A#{AUTOLINK_REGEXP}/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(
          type: :link_open,
          tag: "a",
          nesting: 1,
          attrs: { href: match[1] },
          markup: "autolink"
        )
        tokens << Token.new(type: :text, content: match[1])
        tokens << Token.new(
          type: :link_close,
          tag: "a",
          nesting: -1,
          markup: "autolink"
        )
        pos += match[0].length
      elsif (match = remaining.match(/\A#{EMAIL_AUTOLINK_REGEXP}/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(
          type: :link_open,
          tag: "a",
          nesting: 1,
          attrs: { href: "mailto:#{match[1]}" },
          markup: "autolink"
        )
        tokens << Token.new(type: :text, content: match[1])
        tokens << Token.new(
          type: :link_close,
          tag: "a",
          nesting: -1,
          markup: "autolink"
        )
        pos += match[0].length
      elsif (match = remaining.match(/\A#{HTML_INLINE_REGEXP}/))
        flush_text(text_buffer, tokens)
        text_buffer = ""
        tokens << Token.new(type: :html_inline, content: match[0])
        pos += match[0].length
      else
        text_buffer += remaining[0]
        pos += 1
      end
    elsif @dialect.feature?(:bare_autolinks) && (match = remaining.match(/\A((?:https?|ftp):\/\/[^\s<]+)/))
      flush_text(text_buffer, tokens)
      text_buffer = ""
      href = match[1].sub(/[.,!?;:]\z/, "")
      consumed = href.length
      tokens << Token.new(
        type: :link_open,
        tag: "a",
        nesting: 1,
        attrs: { href: href },
        markup: "autolink"
      )
      tokens << Token.new(type: :text, content: href)
      tokens << Token.new(type: :link_close, tag: "a", nesting: -1, markup: "autolink")
      pos += consumed
    elsif (match = remaining.match(/\A {2,}\n/))
      flush_text(text_buffer, tokens)
      text_buffer = ""
      tokens << Token.new(type: :hardbreak, tag: "br")
      pos += match[0].length
    elsif remaining[0] == "\n"
      flush_text(text_buffer, tokens)
      text_buffer = ""
      tokens << Token.new(type: :softbreak)
      pos += 1
    else
      text_buffer += remaining[0]
      pos += 1
    end
  end

  flush_text(text_buffer, tokens)
end

Parameters:

  • value (Object)

Returns:

  • (Object)


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/mdlint/parser/inline_parser.rb', line 358

def parse_link_destination(value)
  index = 0
  index += 1 while index < value.length && value[index].match?(/\s/)
  return { url: "", title: nil, length: index + 1 } if value[index] == ")"

  if value[index] == "<"
    closing = find_unescaped(value, ">", index + 1)
    return unless closing

    url = value[(index + 1)...closing]
    return if url.match?(/[\r\n]/)
    index = closing + 1
  else
    start = index
    depth = 0
    while index < value.length
      character = value[index]
      if character == "\\"
        index += 2
        next
      end
      if character == "("
        depth += 1
      elsif character == ")"
        break if depth.zero?

        depth -= 1
      elsif character.match?(/\s/) && depth.zero?
        break
      end
      index += 1
    end
    return if index == start

    url = value[start...index]
  end

  whitespace = index
  index += 1 while index < value.length && value[index].match?(/\s/)
  return { url: url, title: nil, length: index + 1 } if value[index] == ")"
  return unless index > whitespace

  title_start = value[index]
  title_end = { '"' => '"', "'" => "'", "(" => ")" }[title_start]
  return unless title_end

  closing = find_unescaped(value, title_end, index + 1)
  return unless closing
  remainder = closing + 1
  remainder += 1 while remainder < value.length && value[remainder].match?(/\s/)
  return unless value[remainder] == ")"

  { url: url, title: value[(index + 1)...closing], length: remainder + 1 }
end

#punctuation_character?(character) ⇒ Boolean

Parameters:

  • character (Object)

Returns:

  • (Boolean)


479
480
481
# File 'lib/mdlint/parser/inline_parser.rb', line 479

def punctuation_character?(character)
  !character.nil? && character.match?(/[[:punct:]]/u)
end

#whitespace_character?(character) ⇒ Boolean

Parameters:

  • character (Object)

Returns:

  • (Boolean)


475
476
477
# File 'lib/mdlint/parser/inline_parser.rb', line 475

def whitespace_character?(character)
  character.nil? || character.match?(/\s|\p{Space}/u)
end