Class: MarkdownIt::RulesBlock::HtmlBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-markdown-it/rules_block/html_block.rb

Constant Summary collapse

HTML_OPEN_CLOSE_TAG_RE =
MarkdownIt::Common::HtmlRe::HTML_OPEN_CLOSE_TAG_RE
HTML_SEQUENCES =

An array of opening and corresponding closing sequences for html tags, last argument defines whether it can terminate a paragraph or not

[
  [ /^<(script|pre|style)(?=(\s|>|$))/i, /<\/(script|pre|style)>/i, true ],
  [ /^<!--/,        /-->/,   true ],
  [ /^<\?/,         /\?>/,   true ],
  [ /^<![A-Z]/,     />/,     true ],
  [ /^<!\[CDATA\[/, /\]\]>/, true ],
  [ Regexp.new('^</?(' + MarkdownIt::HTML_BLOCKS.join('|') + ')(?=(\\s|/?>|$))', 'i'), /^$/, true ],
  [ Regexp.new(HTML_OPEN_CLOSE_TAG_RE.source + '\\s*$'),  /^$/, false ]
]

Class Method Summary collapse

Class Method Details

.html_block(state, startLine, endLine, silent) ⇒ Object




23
24
25
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
# File 'lib/motion-markdown-it/rules_block/html_block.rb', line 23

def self.html_block(state, startLine, endLine, silent)
  pos   = state.bMarks[startLine] + state.tShift[startLine]
  max   = state.eMarks[startLine]

  # if it's indented more than 3 spaces, it should be a code block
  return false if state.sCount[startLine] - state.blkIndent >= 4

  return false if !state.md.options[:html]
  return false if state.src.charCodeAt(pos) != 0x3C    # <

  lineText = state.src.slice(pos...max)

  i = 0
  while i < HTML_SEQUENCES.length
    break if HTML_SEQUENCES[i][0].match(lineText)
    i += 1
  end

  return false if i == HTML_SEQUENCES.length

  if silent
    # true if this sequence can be a terminator, false otherwise
    return HTML_SEQUENCES[i][2]
  end

  nextLine = startLine + 1

  # If we are here - we detected HTML block.
  # Let's roll down till block end.
  if !HTML_SEQUENCES[i][1].match(lineText)
    while nextLine < endLine
      break if state.sCount[nextLine] < state.blkIndent

      pos = state.bMarks[nextLine] + state.tShift[nextLine]
      max = state.eMarks[nextLine]
      lineText = state.src.slice(pos...max)

      if HTML_SEQUENCES[i][1].match(lineText)
        nextLine += 1 if lineText.length != 0
        break
      end
      nextLine += 1
    end
  end

  state.line = nextLine

  token         = state.push('html_block', '', 0)
  token.map     = [ startLine, nextLine ]
  token.content = state.getLines(startLine, nextLine, state.blkIndent, true)

  return true
end