Class: Coradoc::DocumentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/document_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocumentBuilder

Returns a new instance of DocumentBuilder.



13
14
15
16
17
18
19
20
# File 'lib/coradoc/document_builder.rb', line 13

def initialize
  @document = CoreModel::StructuralElement.new(
    element_type: 'document',
    children: []
  )
  @current_context = @document
  @context_stack = []
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



5
6
7
# File 'lib/coradoc/document_builder.rb', line 5

def document
  @document
end

Class Method Details

.build(&block) ⇒ Object



7
8
9
10
11
# File 'lib/coradoc/document_builder.rb', line 7

def self.build(&block)
  builder = new
  builder.instance_eval(&block) if block_given?
  builder
end

Instance Method Details

#admonition(type, text) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/coradoc/document_builder.rb', line 145

def admonition(type, text)
  @current_context.children << CoreModel::AnnotationBlock.new(
    annotation_type: type.to_s,
    content: text
  )
  self
end

#blockquote(text, attribution: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/coradoc/document_builder.rb', line 64

def blockquote(text, attribution: nil)
  block = CoreModel::Block.new(
    element_type: 'block',
    delimiter_type: '____',
    content: text
  )
  block.('attribution', attribution) if attribution
  @current_context.children << block
  self
end

#bulleted_list(&block) ⇒ Object



98
99
100
# File 'lib/coradoc/document_builder.rb', line 98

def bulleted_list(&block)
  list(:unordered, &block)
end

#code(code_text, language: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/coradoc/document_builder.rb', line 54

def code(code_text, language: nil)
  @current_context.children << CoreModel::Block.new(
    element_type: 'block',
    delimiter_type: '----',
    content: code_text,
    language: language
  )
  self
end

#hrObject



130
131
132
133
134
135
# File 'lib/coradoc/document_builder.rb', line 130

def hr
  @current_context.children << CoreModel::Block.new(
    element_type: 'horizontal_rule'
  )
  self
end

#image(src, alt: '', title: nil) ⇒ Object



106
107
108
109
110
111
# File 'lib/coradoc/document_builder.rb', line 106

def image(src, alt: '', title: nil)
  img = CoreModel::Image.new(src: src, alt: alt)
  img.title = title if title
  @current_context.children << img
  self
end

#list(type = :unordered, &block) ⇒ Object Also known as: ordered_list, unordered_list



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/coradoc/document_builder.rb', line 75

def list(type = :unordered, &block)
  list_items = []
  list_type = type

  wrapper = Object.new
  wrapper.define_singleton_method(:item) do |text|
    marker = list_type == :ordered ? '1.' : '*'
    list_items << CoreModel::ListItem.new(content: text, marker: marker)
    wrapper
  end

  wrapper.instance_eval(&block) if block_given?

  @current_context.children << CoreModel::ListBlock.new(
    marker_type: type.to_s,
    items: list_items
  )
  self
end

#numbered_list(&block) ⇒ Object



102
103
104
# File 'lib/coradoc/document_builder.rb', line 102

def numbered_list(&block)
  list(:ordered, &block)
end

#paragraph(text) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/coradoc/document_builder.rb', line 46

def paragraph(text)
  @current_context.children << CoreModel::Block.new(
    element_type: 'paragraph',
    content: text
  )
  self
end

#section(title_text, level: 1, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coradoc/document_builder.rb', line 27

def section(title_text, level: 1, &block)
  new_section = CoreModel::StructuralElement.new(
    element_type: 'section',
    level: level,
    title: title_text,
    children: []
  )

  @current_context.children << new_section

  if block_given?
    push_context(new_section)
    instance_eval(&block)
    pop_context
  end

  self
end

#table(headers = [], rows = []) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/coradoc/document_builder.rb', line 113

def table(headers = [], rows = [])
  table_rows = []

  if headers.any?
    header_cells = headers.map { |h| CoreModel::TableCell.new(content: h, header: true) }
    table_rows << CoreModel::TableRow.new(cells: header_cells)
  end

  rows.each do |row|
    cells = row.map { |c| CoreModel::TableCell.new(content: c.to_s) }
    table_rows << CoreModel::TableRow.new(cells: cells)
  end

  @current_context.children << CoreModel::Table.new(rows: table_rows)
  self
end

#text(text_content) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/coradoc/document_builder.rb', line 137

def text(text_content)
  @current_context.children << CoreModel::Block.new(
    element_type: 'text',
    content: text_content
  )
  self
end

#title(text) ⇒ Object



22
23
24
25
# File 'lib/coradoc/document_builder.rb', line 22

def title(text)
  @document.title = text
  self
end

#to(format, **options) ⇒ Object



163
164
165
# File 'lib/coradoc/document_builder.rb', line 163

def to(format, **options)
  Coradoc.serialize(@document, to: format, **options)
end

#to_asciidoc(**options) ⇒ Object



175
176
177
# File 'lib/coradoc/document_builder.rb', line 175

def to_asciidoc(**options)
  to(:asciidoc, **options)
end

#to_coreObject



159
160
161
# File 'lib/coradoc/document_builder.rb', line 159

def to_core
  @document
end

#to_html(**options) ⇒ Object



167
168
169
# File 'lib/coradoc/document_builder.rb', line 167

def to_html(**options)
  to(:html, **options)
end

#to_markdown(**options) ⇒ Object



171
172
173
# File 'lib/coradoc/document_builder.rb', line 171

def to_markdown(**options)
  to(:markdown, **options)
end