Class: MarkdownIt::Token

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, tag, nesting) ⇒ Token

new Token(type, tag, nesting)

Create new token and fill passed properties.




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
81
82
83
84
85
86
87
88
# File 'lib/motion-markdown-it/token.rb', line 13

def initialize(type, tag, nesting)
  #  * Token#type -> String
  #  *
  #  * Type of the token (string, e.g. "paragraph_open")
  @type     = type

   # * Token#tag -> String
   # *
   # * html tag name, e.g. "p"
  @tag      = tag

   # * Token#attrs -> Array
   # *
   # * Html attributes. Format: `[ [ name1, value1 ], [ name2, value2 ] ]`
  @attrs    = nil

   # * Token#map -> Array
   # *
   # * Source map info. Format: `[ line_begin, line_end ]`
  @map      = nil

   # * Token#nesting -> Number
   # *
   # * Level change (number in {-1, 0, 1} set), where:
   # *
   # * -  `1` means the tag is opening
   # * -  `0` means the tag is self-closing
   # * - `-1` means the tag is closing
  @nesting  = nesting

   # * Token#level -> Number
   # *
   # * nesting level, the same as `state.level`
  @level    = 0

   # * Token#children -> Array
   # *
   # * An array of child nodes (inline and img tokens)
  @children = nil

   # * Token#content -> String
   # *
   # * In a case of self-closing tag (code, html, fence, etc.),
   # * it has contents of this tag.
  @content  = ''

   # * Token#markup -> String
   # *
   # * '*' or '_' for emphasis, fence string for fence, etc.
  @markup   = ''

   # * Token#info -> String
   # *
   # * Additional information:
   # *
   # * - Info string for "fence" tokens
   # * - The value "auto" for autolink "link_open" and "link_close" tokens
  @info     = ''

   # * Token#meta -> Object
   # *
   # * A place for plugins to store an arbitrary data
  @meta     = nil

   # * Token#block -> Boolean
   # *
   # * True for block-level tokens, false for inline tokens.
   # * Used in renderer to calculate line breaks
  @block    = false

   # * Token#hidden -> Boolean
   # *
   # * If it's true, ignore this element when rendering. Used for tight lists
   # * to hide paragraphs.
  @hidden   = false
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def attrs
  @attrs
end

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def children
  @children
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#hiddenObject

Returns the value of attribute hidden.



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

def hidden
  @hidden
end

#infoObject

Returns the value of attribute info.



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

def info
  @info
end

#levelObject

Returns the value of attribute level.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def level
  @level
end

#mapObject

Returns the value of attribute map.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def map
  @map
end

#markupObject

Returns the value of attribute markup.



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

def markup
  @markup
end

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#nestingObject

Returns the value of attribute nesting.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def nesting
  @nesting
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/motion-markdown-it/token.rb', line 6

def type
  @type
end

Instance Method Details

#attrGet(name) ⇒ Object

Token.attrGet(name)

Get the value of attribute ‘name`, or null if it does not exist.




137
138
139
140
141
142
143
144
145
146
# File 'lib/motion-markdown-it/token.rb', line 137

def attrGet(name)
  idx    = attrIndex(name)
  value  = nil

  if idx >= 0
    value = @attrs[idx][1]
  end

  return value
end

#attrIndex(name) ⇒ Object

  • Token.attrIndex(name) -> Number

*

  • Search attribute index by name.




95
96
97
98
99
100
101
102
103
104
# File 'lib/motion-markdown-it/token.rb', line 95

def attrIndex(name)
  return -1 if !@attrs

  attrs = @attrs

  attrs.each_with_index do |attr_, index|
    return index if attr_[0] == name
  end
  return -1
end

#attrJoin(name, value) ⇒ Object

Token.attrJoin(name, value)

Join value to existing attribute via space. Or create new attribute if not exists. Useful to operate with token classes.




153
154
155
156
157
158
159
160
161
# File 'lib/motion-markdown-it/token.rb', line 153

def attrJoin(name, value)
  idx = attrIndex(name)

  if idx < 0
    attrPush([ name, value ])
  else
    @attrs[idx][1] = @attrs[idx][1] + ' ' + value
  end
end

#attrPush(attrData) ⇒ Object

  • Token.attrPush(attrData)

*

  • Add ‘[ name, value ]` attribute to list. Init attrs if necessary




110
111
112
113
114
115
116
# File 'lib/motion-markdown-it/token.rb', line 110

def attrPush(attrData)
  if @attrs
    @attrs.push(attrData)
  else
    @attrs = [ attrData ]
  end
end

#attrSet(name, value) ⇒ Object

Token.attrSet(name, value)

Set ‘name` attribute to `value`. Override old value if exists.




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

def attrSet(name, value)
  idx      = attrIndex(name)
  attrData = [ name, value ]

  if idx < 0
    attrPush(attrData)
  else
    @attrs[idx] = attrData
  end
end

#to_jsonObject




164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/motion-markdown-it/token.rb', line 164

def to_json
  {
    type: @type,
    tag: @tag,
    attrs: @attrs,
    map: @map,
    nesting: @nesting,
    level: @level,
    children: @children.nil? ? nil : @children.each {|t| t.to_json},
    content: @content,
    markup: @markup,
    info: @info,
    meta: @meta,
    block: @block,
    hidden: @hidden
  }
end