Class: Coradoc::DocumentManipulator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ DocumentManipulator

Returns a new instance of DocumentManipulator.



7
8
9
10
11
12
13
14
# File 'lib/coradoc/document_manipulator.rb', line 7

def initialize(document)
  unless document.is_a?(Coradoc::CoreModel::Base)
    raise ArgumentError,
          "Expected CoreModel::Base, got #{document.class}"
  end

  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

Instance Method Details

#add_metadata(metadata) ⇒ Object



74
75
76
77
78
79
# File 'lib/coradoc/document_manipulator.rb', line 74

def ()
  .each do |key, value|
    @document.(key.to_s, value.to_s)
  end
  self
end

#add_toc(levels: 3, position: :top) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/coradoc/document_manipulator.rb', line 48

def add_toc(levels: 3, position: :top)
  sections = collect_sections(@document, max_level: levels)
  toc = CoreModel::TocGenerator.generate(sections)

  toc_element = CoreModel::Block.new(element_type: 'toc', content: toc)
  case position
  when :top
    @document.children = [toc_element] + @document.children
  when :bottom
    @document.children = @document.children + [toc_element]
  end

  self
end

#cloneObject



111
112
113
# File 'lib/coradoc/document_manipulator.rb', line 111

def clone
  DocumentManipulator.new(deep_clone(@document))
end

#query(selector) ⇒ Object



16
17
18
# File 'lib/coradoc/document_manipulator.rb', line 16

def query(selector)
  Coradoc::Query.query(@document, selector).to_a
end

#remove_elements(element_type) ⇒ Object



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

def remove_elements(element_type)
  Visitor::Transformer.new do |element|
    next unless element.is_a?(CoreModel::StructuralElement) && element.children

    element.children.reject! do |child|
      match_element_type?(child, element_type)
    end
  end.visit(@document)
  self
end

#select_sections(level: nil, title: nil) ⇒ Object



20
21
22
23
# File 'lib/coradoc/document_manipulator.rb', line 20

def select_sections(level: nil, title: nil)
  filtered = filter_sections(@document, level: level, title: title)
  DocumentManipulator.new(filtered)
end

#set_id(id) ⇒ Object



86
87
88
89
# File 'lib/coradoc/document_manipulator.rb', line 86

def set_id(id)
  @document.id = id
  self
end

#set_title(title) ⇒ Object



81
82
83
84
# File 'lib/coradoc/document_manipulator.rb', line 81

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

#to(format, **options) ⇒ Object



103
104
105
# File 'lib/coradoc/document_manipulator.rb', line 103

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

#to_asciidoc(**options) ⇒ Object



99
100
101
# File 'lib/coradoc/document_manipulator.rb', line 99

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

#to_coreObject



107
108
109
# File 'lib/coradoc/document_manipulator.rb', line 107

def to_core
  @document
end

#to_html(**options) ⇒ Object



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

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

#to_markdown(**options) ⇒ Object



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

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

#transform_headingsObject



39
40
41
42
43
44
45
46
# File 'lib/coradoc/document_manipulator.rb', line 39

def transform_headings
  return self unless block_given?

  Visitor::Transformer.new do |element|
    element.title = yield(element.title) if element.is_a?(CoreModel::StructuralElement) && element.title.is_a?(String)
  end.visit(@document)
  self
end

#transform_textObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/coradoc/document_manipulator.rb', line 25

def transform_text
  return self unless block_given?

  Visitor::Transformer.new do |element|
    case element
    when CoreModel::InlineElement
      element.content = yield(element.content) if element.content.is_a?(String)
    when CoreModel::Block
      element.content = yield(element.content) if element.content.is_a?(String)
    end
  end.visit(@document)
  self
end