Class: Docbook::Mirror::Node
- Inherits:
-
Object
- Object
- Docbook::Mirror::Node
- Defined in:
- lib/docbook/mirror/node.rb
Direct Known Subclasses
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
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
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
-
#initialize(type: nil, attrs: {}, content: [], marks: []) ⇒ Node
constructor
A new instance of Node.
- #text_content ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/docbook/mirror/node.rb', line 35 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 only if the subclass defines its own if klass != Node && klass.singleton_class.method_defined?(:from_h, false) 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
#text_content ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/docbook/mirror/node.rb', line 57 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
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/docbook/mirror/node.rb', line 17 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(&:to_h) end if content && !content.empty? result["content"] = content.map(&:to_h) end result end |
#to_json(**options) ⇒ Object
31 32 33 |
# File 'lib/docbook/mirror/node.rb', line 31 def to_json(**) to_h.to_json() end |