Class: MarkdownIt::RulesBlock::StateBlock

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, md, env, tokens) ⇒ StateBlock




12
13
14
15
16
17
18
19
20
21
22
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
76
77
78
79
80
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 12

def initialize(src, md, env, tokens)
  @src = src

  # link to parser instance
  @md  = md
  @env = env

  #--- Internal state variables

  @tokens = tokens

  @bMarks = []  # line begin offsets for fast jumps
  @eMarks = []  # line end offsets for fast jumps
  @tShift = []  # indent for each line

  # block parser variables
  @blkIndent  = 0       # required block content indent (for example, if we are in list)
  @line       = 0       # line index in src
  @lineMax    = 0       # lines count
  @tight      = false   # loose/tight mode for lists
  @parentType = 'root'  # if `list`, block parser stops on two newlines
  @ddIndent   = -1      # indent of the current dd block (-1 if there isn't any)

  @level = 0

  # renderer
  @result = ''

  # Create caches
  # Generate markers.
  s            = @src
  indent       = 0
  indent_found = false

  start = pos = indent = 0
  len = s.length
  start.upto(len - 1) do |pos|
  # !!!!!!
  # for (start = pos = indent = 0, len = s.length pos < len pos++) {
    ch = s.charCodeAt(pos)

    if !indent_found
      if ch == 0x20  # space
        indent += 1
        next
      else
        indent_found = true
      end
    end

    if ch == 0x0A || pos == (len - 1)
      pos += 1 if ch != 0x0A
      @bMarks.push(start)
      @eMarks.push(pos)
      @tShift.push(indent)

      indent_found = false
      indent       = 0
      start        = pos + 1
    end
  end

  # Push fake entry to simplify cache bounds checks
  @bMarks.push(s.length)
  @eMarks.push(s.length)
  @tShift.push(0)

  @lineMax = @bMarks.length - 1 # don't count last fake line
end

Instance Attribute Details

#blkIndentObject

Returns the value of attribute blkIndent.



8
9
10
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 8

def blkIndent
  @blkIndent
end

#bMarksObject

Returns the value of attribute bMarks.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def bMarks
  @bMarks
end

#ddIndentObject

Returns the value of attribute ddIndent.



8
9
10
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 8

def ddIndent
  @ddIndent
end

#eMarksObject

Returns the value of attribute eMarks.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def eMarks
  @eMarks
end

#envObject

Returns the value of attribute env.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def env
  @env
end

#levelObject

Returns the value of attribute level.



9
10
11
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 9

def level
  @level
end

#lineObject

Returns the value of attribute line.



8
9
10
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 8

def line
  @line
end

#lineMaxObject

Returns the value of attribute lineMax.



8
9
10
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 8

def lineMax
  @lineMax
end

#mdObject

Returns the value of attribute md.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def md
  @md
end

#parentTypeObject

Returns the value of attribute parentType.



8
9
10
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 8

def parentType
  @parentType
end

#resultObject

Returns the value of attribute result.



9
10
11
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 9

def result
  @result
end

#srcObject

Returns the value of attribute src.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def src
  @src
end

#tightObject

Returns the value of attribute tight.



8
9
10
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 8

def tight
  @tight
end

#tokensObject

Returns the value of attribute tokens.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def tokens
  @tokens
end

#tShiftObject

Returns the value of attribute tShift.



7
8
9
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 7

def tShift
  @tShift
end

Instance Method Details

#getLines(line_begin, line_end, indent, keepLastLF) ⇒ Object

cut lines range from source.




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
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 145

def getLines(line_begin, line_end, indent, keepLastLF)
  line = line_begin

  return '' if line_begin >= line_end

  # Opt: don't use push queue for single line
  if (line + 1) == line_end
    first = @bMarks[line] + [@tShift[line], indent].min
    last = keepLastLF ? @bMarks[line_end] : @eMarks[line_end - 1]
    return @src.slice(first...last)
  end

  queue = Array.new(line_end - line_begin)

  i = 0
  while line < line_end
    shift = @tShift[line]
    shift = indent if shift > indent
    shift = 0 if shift < 0

    first = @bMarks[line] + shift

    if line + 1 < line_end || keepLastLF
      # No need for bounds check because we have fake entry on tail.
      last = @eMarks[line] + 1
    else
      last = @eMarks[line]
    end

    queue[i] = @src.slice(first...last)
    line += 1
    i    += 1
  end

  return queue.join('')
end

#isEmpty(line) ⇒ Object




97
98
99
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 97

def isEmpty(line)
  return @bMarks[line] + @tShift[line] >= @eMarks[line]
end

#push(type, tag, nesting) ⇒ Object

Push new token to “stream”.




84
85
86
87
88
89
90
91
92
93
94
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 84

def push(type, tag, nesting)
  token       = Token.new(type, tag, nesting)
  token.block = true

  @level -= 1 if nesting < 0
  token.level = @level
  @level += 1 if nesting > 0

  @tokens.push(token)
  return token
end

#skipChars(pos, code) ⇒ Object

Skip char codes from given position




123
124
125
126
127
128
129
130
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 123

def skipChars(pos, code)
  max = @src.length
  while pos < max
    break if (@src.charCodeAt(pos) != code)
    pos += 1
  end
  return pos
end

#skipCharsBack(pos, code, min) ⇒ Object

Skip char codes reverse from given position - 1




134
135
136
137
138
139
140
141
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 134

def skipCharsBack(pos, code, min)
  return pos if pos <= min

  while (pos > min)
    return (pos + 1) if code != @src.charCodeAt(pos -= 1)
  end
  return pos
end

#skipEmptyLines(from) ⇒ Object




102
103
104
105
106
107
108
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 102

def skipEmptyLines(from)
  while from < @lineMax
    break if (@bMarks[from] + @tShift[from] < @eMarks[from])
    from += 1
  end
  return from
end

#skipSpaces(pos) ⇒ Object

Skip spaces from given position.




112
113
114
115
116
117
118
119
# File 'lib/motion-markdown-it/rules_block/state_block.rb', line 112

def skipSpaces(pos)
  max = @src.length
  while pos < max
    break if @src.charCodeAt(pos) != 0x20 # space
    pos += 1
  end
  return pos
end