Class: Utopia::Project::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(text, base = nil, definition: nil, default_language: nil) ⇒ Document

Returns a new instance of Document.



11
12
13
14
15
16
17
18
19
20
# File 'lib/utopia/project/document.rb', line 11

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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/utopia/project/document.rb', line 104

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

#first_childObject



34
35
36
# File 'lib/utopia/project/document.rb', line 34

def first_child
	self.root.first_child
end

#html_node(content, type = :html) ⇒ Object



76
77
78
79
80
# File 'lib/utopia/project/document.rb', line 76

def html_node(content, type = :html)
	node = Markly::Node.new(:html)
	node.string_content = content
	return node
end

#inline_html_node(content) ⇒ Object



82
83
84
85
86
# File 'lib/utopia/project/document.rb', line 82

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


94
95
96
97
98
99
100
101
102
# File 'lib/utopia/project/document.rb', line 94

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



70
71
72
73
74
# File 'lib/utopia/project/document.rb', line 70

def paragraph_node(child)
	node = Markly::Node.new(:paragraph)
	node.append_child(child)
	return node
end

#replace_section(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/utopia/project/document.rb', line 38

def replace_section(name)
	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 && current.type != :header
					current_next = current.next
					current.delete
					current = current_next
				end
				
				return yield(header)
			end
		end
		
		child = child.next
	end
end

#rootObject



22
23
24
# File 'lib/utopia/project/document.rb', line 22

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

#text_node(content) ⇒ Object



88
89
90
91
92
# File 'lib/utopia/project/document.rb', line 88

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

#titleObject



26
27
28
29
30
31
32
# File 'lib/utopia/project/document.rb', line 26

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



65
66
67
68
# File 'lib/utopia/project/document.rb', line 65

def to_html(node = self.root, **options)
	renderer = Renderer.new(ids: true, flags: Markly::UNSAFE, **options)
	Trenni::MarkupString.raw(renderer.render(node))
end