Class: Coradoc::Document

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

Returns a new instance of Document.



63
64
65
66
67
68
69
# File 'lib/coradoc/document.rb', line 63

def initialize(options = {})
  @document_attributes = options.fetch(:document_attributes,
                                       Coradoc::Element::DocumentAttributes.new)
  @header = options.fetch(:header, Coradoc::Element::Header.new(""))
  @sections = options.fetch(:sections, [])
  self
end

Instance Attribute Details

#document_attributesObject

Returns the value of attribute document_attributes.



61
62
63
# File 'lib/coradoc/document.rb', line 61

def document_attributes
  @document_attributes
end

#headerObject

Returns the value of attribute header.



61
62
63
# File 'lib/coradoc/document.rb', line 61

def header
  @header
end

#sectionsObject

Returns the value of attribute sections.



61
62
63
# File 'lib/coradoc/document.rb', line 61

def sections
  @sections
end

Class Method Details

.from_adoc(filename) ⇒ Object



32
33
34
35
# File 'lib/coradoc/document.rb', line 32

def from_adoc(filename)
  ast = Coradoc::Parser.parse(filename)
  Coradoc::Transformer.transform(ast)
end

.from_ast(elements) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coradoc/document.rb', line 37

def from_ast(elements)
  @sections = []

  elements.each do |element|
    case element
    when Coradoc::Element::DocumentAttributes
      @document_attributes = element

    when Coradoc::Element::Header
      @header = element

    when Coradoc::Element::Section
      @sections << element
    end
  end

  new(
    document_attributes: @document_attributes,
    header: @header,
    sections: @sections,
  )
end

Instance Method Details

#to_adocObject



71
72
73
74
75
# File 'lib/coradoc/document.rb', line 71

def to_adoc
  Coradoc::Generator.gen_adoc(@header) +
    Coradoc::Generator.gen_adoc(@document_attributes) +
    Coradoc::Generator.gen_adoc(@sections)
end