Class: Docbook::Mirror::Node
- Inherits:
-
Object
- Object
- Docbook::Mirror::Node
- Defined in:
- lib/docbook/mirror/node.rb,
lib/docbook/mirror/node/base.rb,
lib/docbook/mirror/node/text.rb,
lib/docbook/mirror/node/block.rb
Defined Under Namespace
Classes: Acknowledgements, Admonition, Appendix, Article, BiblioEntry, Bibliography, Blockquote, BulletList, Callout, CalloutList, Caption, Chapter, CodeBlock, Colophon, Dedication, DefinitionDescription, DefinitionList, DefinitionTerm, Document, Equation, Figure, FootnoteEntry, FootnoteMarker, Footnotes, GlossDef, GlossEntry, GlossSee, GlossSeeAlso, GlossTerm, Glossary, Heading, Image, IndexBlock, IndexDiv, IndexEntry, ListItem, OrderedList, Paragraph, Part, Preface, Procedure, RefEntry, RefSection, Reference, Section, Set, Sidebar, SimPara, Step, SubSteps, Table, TableBody, TableCell, TableHead, TableRow, Text, Topic
Constant Summary collapse
- PM_TYPE =
"node"- NODES =
Hash.new
- NODE_TYPES =
{}.freeze
Instance Attribute Summary collapse
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#content ⇒ Object
Returns the value of attribute content.
-
#marks ⇒ Object
Returns the value of attribute marks.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#find_all(node_type) ⇒ Object
Find all nodes of given type.
-
#find_first(node_type) ⇒ Object
Find first node of given type.
-
#initialize(type: nil, attrs: {}, content: [], marks: []) ⇒ Node
constructor
A new instance of Node.
-
#text_content ⇒ Object
Get text content.
-
#to_h ⇒ Object
(also: #to_hash)
Convert to hash for JSON serialization.
- #to_json(**options) ⇒ Object
Constructor Details
#initialize(type: nil, attrs: {}, content: [], marks: []) ⇒ Node
Returns a new instance of Node.
10 11 12 13 14 15 |
# File 'lib/docbook/mirror/node.rb', line 10 def initialize(type: nil, attrs: {}, content: [], marks: []) @type = type || self.class::PM_TYPE @attrs = attrs || {} @content = content || [] @marks = marks || [] end |
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
8 9 10 |
# File 'lib/docbook/mirror/node.rb', line 8 def attrs @attrs end |
#content ⇒ Object
Returns the value of attribute content.
8 9 10 |
# File 'lib/docbook/mirror/node.rb', line 8 def content @content end |
#marks ⇒ Object
Returns the value of attribute marks.
8 9 10 |
# File 'lib/docbook/mirror/node.rb', line 8 def marks @marks end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/docbook/mirror/node.rb', line 8 def type @type end |
Class Method Details
.from_h(hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/docbook/mirror/node.rb', line 37 def self.from_h(hash) return nil unless hash type = hash["type"] attrs = hash["attrs"] || {} content = hash["content"] || [] marks = hash["marks"] || [] klass = NODES[type] || Node # Use class-specific from_h if available if klass.respond_to?(:from_h) && klass != Node klass.from_h(hash) else klass.new( attrs: attrs.transform_keys(&:to_sym), content: content.map { |c| Node.from_h(c) }, marks: marks.map { |m| Docbook::Mirror::Mark.from_h(m) }, ) end end |
Instance Method Details
#find_all(node_type) ⇒ Object
Find all nodes of given type
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/docbook/mirror/node/base.rb', line 74 def find_all(node_type) results = [] results << self if type == node_type content&.each do |child| next unless child.is_a?(Node) child_results = child.find_all(node_type) results.concat(child_results) if child_results end results end |
#find_first(node_type) ⇒ Object
Find first node of given type
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/docbook/mirror/node/base.rb', line 61 def find_first(node_type) return self if type == node_type content&.each do |child| next unless child.is_a?(Node) result = child.find_first(node_type) return result if result end nil end |
#text_content ⇒ Object
Get text content
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/docbook/mirror/node/base.rb', line 89 def text_content return "" unless content content.map do |item| if item.is_a?(Node) item.text_content else (item.is_a?(String) ? item : "") end end.join end |
#to_h ⇒ Object Also known as: to_hash
Convert to hash for JSON serialization
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/docbook/mirror/node/base.rb', line 18 def to_h result = { "type" => type } result["attrs"] = attrs.transform_keys(&:to_s) if attrs && !attrs.empty? if marks && !marks.empty? result["marks"] = marks.map { |m| m.respond_to?(:to_h) ? m.to_h : m } end if content && !content.empty? result["content"] = content.map do |i| i.respond_to?(:to_h) ? i.to_h : i end end result end |
#to_json(**options) ⇒ Object
33 34 35 |
# File 'lib/docbook/mirror/node.rb', line 33 def to_json(**) to_h.to_json() end |