Class: Utopia::Project::Document
- Inherits:
-
Object
- Object
- Utopia::Project::Document
- Defined in:
- lib/utopia/project/document.rb
Overview
Represents a Markdown document with optional source code cross-references.
Direct Known Subclasses
Instance Method Summary collapse
-
#code_node(content, language = nil) ⇒ Object
Build an inline code node.
-
#first_child ⇒ Object
Get the first node in the document.
-
#html_node(content, type = :html) ⇒ Object
Build an HTML block node.
-
#initialize(text, base = nil, definition: nil, default_language: nil) ⇒ Document
constructor
Initialize a document from Markdown text.
-
#inline_html_node(content) ⇒ Object
Build an inline HTML node.
-
#link_node(title, url, child) ⇒ Object
Build a link node around a child node.
-
#paragraph_node(child) ⇒ Object
Wrap a node in a paragraph.
-
#replace_section(name, children: false) ⇒ Object
Remove a named section and yield its heading for replacement.
-
#root ⇒ Object
Parse and resolve the document root.
-
#text_node(content) ⇒ Object
Build a text node.
-
#title ⇒ Object
Extract the leading heading as the document title.
-
#to_html(node = self.root, **options) ⇒ Object
Render a document node as HTML.
-
#to_markdown(**options) ⇒ Object
Render the document as Markdown.
Constructor Details
#initialize(text, base = nil, definition: nil, default_language: nil) ⇒ Document
Initialize a document from Markdown text.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/utopia/project/document.rb', line 18 def initialize(text, base = nil, definition: nil, default_language: nil) @text = text @base = base @index = base&.index @definition = definition @default_language = default_language @root = nil end |
Instance Method Details
#code_node(content, language = nil) ⇒ Object
Build an inline code node.
167 168 169 170 171 172 173 |
# File 'lib/utopia/project/document.rb', line 167 def code_node(content, language = nil) node = Markly::Node.new(:code) node.string_content = content node.code_info = language if language return node end |
#first_child ⇒ Object
Get the first node in the document.
47 48 49 |
# File 'lib/utopia/project/document.rb', line 47 def first_child self.root.first_child end |
#html_node(content, type = :html) ⇒ Object
Build an HTML block node.
124 125 126 127 128 |
# File 'lib/utopia/project/document.rb', line 124 def html_node(content, type = :html) node = Markly::Node.new(:html) node.string_content = content return node end |
#inline_html_node(content) ⇒ Object
Build an inline HTML node.
133 134 135 136 137 |
# File 'lib/utopia/project/document.rb', line 133 def inline_html_node(content) node = Markly::Node.new(:inline_html) node.string_content = content return node end |
#link_node(title, url, child) ⇒ Object
Build a link node around a child node.
153 154 155 156 157 158 159 160 161 |
# File 'lib/utopia/project/document.rb', line 153 def link_node(title, url, child) node = Markly::Node.new(:link) node.title = title node.url = url.to_s node.append_child(child) return node end |
#paragraph_node(child) ⇒ Object
Wrap a node in a paragraph.
114 115 116 117 118 |
# File 'lib/utopia/project/document.rb', line 114 def paragraph_node(child) node = Markly::Node.new(:paragraph) node.append_child(child) return node end |
#replace_section(name, children: false) ⇒ Object
Remove a named section and yield its heading for replacement.
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 89 90 |
# File 'lib/utopia/project/document.rb', line 56 def replace_section(name, children: false) child = self.first_child while child if child.type == :header header = child # We found the matched header: if header.first_child.to_plaintext.include?(name) # Now subsequent children: current = header.next # Delete everything in the section until we encounter another header: while current if current.type == :header # If we are removing all children, keep on going until we reach a header of the same level or higher: if children break if current.header_level <= header.header_level else break end end current_next = current.next current.delete current = current_next end return yield(header) end end child = child.next end end |
#root ⇒ Object
Parse and resolve the document root.
31 32 33 |
# File 'lib/utopia/project/document.rb', line 31 def root @root ||= resolve(Markly.parse(@text, flags: Markly::INLINE_CODE_INFO, extensions: [:table])) end |
#text_node(content) ⇒ Object
Build a text node.
142 143 144 145 146 |
# File 'lib/utopia/project/document.rb', line 142 def text_node(content) node = Markly::Node.new(:text) node.string_content = content return node end |
#title ⇒ Object
Extract the leading heading as the document title.
37 38 39 40 41 42 43 |
# File 'lib/utopia/project/document.rb', line 37 def title child = self.root.first_child if child && child.type == :header return child.first_child.to_plaintext end end |
#to_html(node = self.root, **options) ⇒ Object
Render a document node as HTML.
101 102 103 104 105 106 107 108 109 |
# File 'lib/utopia/project/document.rb', line 101 def to_html(node = self.root, **) renderer = Renderer.new( inline_code_resolver: (@index ? method(:reference_node) : nil), ids: true, flags: Markly::UNSAFE, ** ) XRB::Markup.raw(renderer.render(node)) end |
#to_markdown(**options) ⇒ Object
Render the document as Markdown.
94 95 96 |
# File 'lib/utopia/project/document.rb', line 94 def to_markdown(**) self.root.to_markdown(**) end |