Class: Utopia::Project::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/project/document.rb

Overview

Represents a Markdown document with optional source code cross-references.

Direct Known Subclasses

ReleasesDocument

Instance Method Summary collapse

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.



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/utopia/project/document.rb', line 162

def code_node(content, language = nil)
	if language
		node = inline_html_node(
			"<code class=\"language-#{language}\">#{XRB::Strings.to_html(content)}</code>"
		)
	else
		node = Markly::Node.new(:code)
		node.string_content = content
		return node
	end
	
	return node
end

#first_childObject

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.



119
120
121
122
123
# File 'lib/utopia/project/document.rb', line 119

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.



128
129
130
131
132
# File 'lib/utopia/project/document.rb', line 128

def inline_html_node(content)
	node = Markly::Node.new(:inline_html)
	node.string_content = content
	return node
end

Build a link node around a child node.



148
149
150
151
152
153
154
155
156
# File 'lib/utopia/project/document.rb', line 148

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.



109
110
111
112
113
# File 'lib/utopia/project/document.rb', line 109

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

#rootObject

Parse and resolve the document root.



31
32
33
# File 'lib/utopia/project/document.rb', line 31

def root
	@root ||= resolve(Markly.parse(@text, extensions: [:table]))
end

#text_node(content) ⇒ Object

Build a text node.



137
138
139
140
141
# File 'lib/utopia/project/document.rb', line 137

def text_node(content)
	node = Markly::Node.new(:text)
	node.string_content = content
	return node
end

#titleObject

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
# File 'lib/utopia/project/document.rb', line 101

def to_html(node = self.root, **options)
	renderer = Renderer.new(ids: true, flags: Markly::UNSAFE, **options)
	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(**options)
	self.root.to_markdown(**options)
end