Class: Commonmarker::Node

Inherits:
Object
  • Object
show all
Includes:
Inspect, Enumerable
Defined in:
lib/commonmarker/node.rb,
lib/commonmarker/node/ast.rb,
lib/commonmarker/node/inspect.rb

Defined Under Namespace

Modules: Inspect Classes: Ast

Constant Summary

Constants included from Inspect

Inspect::PP_INDENT_SIZE

Instance Method Summary collapse

Methods included from Inspect

#inspect, #pretty_print

Instance Method Details

#eachObject

Public: Iterate over the children (if any) of the current pointer.



24
25
26
27
28
29
30
31
32
33
# File 'lib/commonmarker/node.rb', line 24

def each
  return enum_for(:each) unless block_given?

  child = first_child
  while child
    next_child = child.next_sibling
    yield child
    child = next_child
  end
end

#to_commonmark(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS) ⇒ Object

Public: Convert the node to a CommonMark string.

options - A Symbol or of Symbols indicating the render options plugins - A Hash of additional plugins.

Returns a String.

Raises:

  • (TypeError)


56
57
58
59
60
61
62
63
# File 'lib/commonmarker/node.rb', line 56

def to_commonmark(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
  raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)

  opts = Config.process_options(options)
  plugins = Config.process_plugins(plugins)

  node_to_commonmark(options: opts, plugins: plugins).force_encoding("utf-8")
end

#to_html(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS) ⇒ Object

Public: Converts a node to an HTML string.

options - A Hash of render, parse, and extension options to transform the text. plugins - A Hash of additional plugins.

Returns a String of HTML.

Raises:

  • (TypeError)


41
42
43
44
45
46
47
48
# File 'lib/commonmarker/node.rb', line 41

def to_html(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
  raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)

  opts = Config.process_options(options)
  plugins = Config.process_plugins(plugins)

  node_to_html(options: opts, plugins: plugins).force_encoding("utf-8")
end

#walk {|_self| ... } ⇒ Object

Public: An iterator that “walks the tree,” descending into children recursively.

blk - A Proc representing the action to take for each child

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
# File 'lib/commonmarker/node.rb', line 14

def walk(&block)
  return enum_for(:walk) unless block

  yield self
  each do |child|
    child.walk(&block)
  end
end