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
# File 'lib/coradoc/document_builder.rb', line 13

def initialize
  @document = CoreModel::DocumentElement.new(
    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



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

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

#blockquote(text, attribution: nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/coradoc/document_builder.rb', line 59

def blockquote(text, attribution: nil)
  block = CoreModel::QuoteBlock.new(
    content: text,
    attribution: attribution
  )
  @current_context.children << block
  self
end

#bulleted_list(&block) ⇒ Object



91
92
93
# File 'lib/coradoc/document_builder.rb', line 91

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

#code(code_text, language: nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/coradoc/document_builder.rb', line 51

def code(code_text, language: nil)
  @current_context.children << CoreModel::SourceBlock.new(
    content: code_text,
    language: language
  )
  self
end

#hrObject



123
124
125
126
# File 'lib/coradoc/document_builder.rb', line 123

def hr
  @current_context.children << CoreModel::HorizontalRuleBlock.new
  self
end

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



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/coradoc/document_builder.rb', line 68

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



95
96
97
# File 'lib/coradoc/document_builder.rb', line 95

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

#paragraph(text) ⇒ Object



44
45
46
47
48
49
# File 'lib/coradoc/document_builder.rb', line 44

def paragraph(text)
  @current_context.children << CoreModel::ParagraphBlock.new(
    content: text
  )
  self
end

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



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

def section(title_text, level: 1, &block)
  new_section = CoreModel::SectionElement.new(
    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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/coradoc/document_builder.rb', line 106

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



128
129
130
131
132
133
# File 'lib/coradoc/document_builder.rb', line 128

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

#title(text) ⇒ Object



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

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

#to(format, **options) ⇒ Object



153
154
155
# File 'lib/coradoc/document_builder.rb', line 153

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

#to_asciidoc(**options) ⇒ Object



165
166
167
# File 'lib/coradoc/document_builder.rb', line 165

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

#to_coreObject



149
150
151
# File 'lib/coradoc/document_builder.rb', line 149

def to_core
  @document
end

#to_html(**options) ⇒ Object



157
158
159
# File 'lib/coradoc/document_builder.rb', line 157

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

#to_markdown(**options) ⇒ Object



161
162
163
# File 'lib/coradoc/document_builder.rb', line 161

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