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.



36
37
38
39
40
41
42
43
44
45
# File 'lib/commonmarker/node.rb', line 36

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

#respond_to?(name, include_all = false) ⇒ Boolean

Public: Whether this node responds to the given method.

name - A Symbol or String naming the method. include_all - A Boolean indicating whether to consider private methods.

Returns a Boolean.

Returns:

  • (Boolean)


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

def respond_to?(name, include_all = false)
  return false if node_supports?(name.to_sym) == false

  super
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)


68
69
70
71
72
73
74
75
# File 'lib/commonmarker/node.rb', line 68

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(render: opts[:render], parse: opts[:parse], extension: opts[:extension], 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)


53
54
55
56
57
58
59
60
# File 'lib/commonmarker/node.rb', line 53

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(render: opts[:render], parse: opts[:parse], extension: opts[:extension], 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:



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

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

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