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



76
77
78
79
80
81
# File 'lib/coradoc/document_manipulator.rb', line 76

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

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



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

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



113
114
115
# File 'lib/coradoc/document_manipulator.rb', line 113

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



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

def remove_elements(element_type)
  Visitor::Transformer.new do |element|
    next unless element.respond_to?(:children) && 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



88
89
90
91
# File 'lib/coradoc/document_manipulator.rb', line 88

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

#set_title(title) ⇒ Object



83
84
85
86
# File 'lib/coradoc/document_manipulator.rb', line 83

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

#to(format, **options) ⇒ Object



105
106
107
# File 'lib/coradoc/document_manipulator.rb', line 105

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

#to_asciidoc(**options) ⇒ Object



101
102
103
# File 'lib/coradoc/document_manipulator.rb', line 101

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

#to_coreObject



109
110
111
# File 'lib/coradoc/document_manipulator.rb', line 109

def to_core
  @document
end

#to_html(**options) ⇒ Object



93
94
95
# File 'lib/coradoc/document_manipulator.rb', line 93

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

#to_markdown(**options) ⇒ Object



97
98
99
# File 'lib/coradoc/document_manipulator.rb', line 97

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

#transform_headings(&block) ⇒ Object



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

def transform_headings(&block)
  return self unless block_given?

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

#transform_text(&block) ⇒ Object



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

def transform_text(&block)
  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