Class: Coradoc::Input::Html::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/html/input/plugin.rb,
lib/coradoc/html/input/plugins/plateau.rb

Direct Known Subclasses

Plateau

Defined Under Namespace

Classes: Plateau

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugin

Returns a new instance of Plugin.



22
23
24
25
# File 'lib/coradoc/html/input/plugin.rb', line 22

def initialize
  @html_tree_hooks_pre = {}
  @html_tree_hooks_post = {}
end

Instance Attribute Details

#coremodel_treeObject

HTML Tree functionalities



38
39
40
# File 'lib/coradoc/html/input/plugin.rb', line 38

def coremodel_tree
  @coremodel_tree
end

#html_treeObject

HTML Tree functionalities



38
39
40
# File 'lib/coradoc/html/input/plugin.rb', line 38

def html_tree
  @html_tree
end

#output_stringObject

HTML Tree functionalities



38
39
40
# File 'lib/coradoc/html/input/plugin.rb', line 38

def output_string
  @output_string
end

Class Method Details

.newObject

Allow building plugins with a shorthand syntax: plugin = Coradoc::Html::Input::Plugin.new do

def name = "Test"

end



14
15
16
17
18
19
20
# File 'lib/coradoc/html/input/plugin.rb', line 14

def self.new(&)
  if self == Plugin
    Class.new(Plugin, &)
  else
    super
  end
end

Instance Method Details

#html_tree_add_hook_post(element, &block) ⇒ Object

Creates a hook to be called after converting an element to a CoreModel node.

proc |html_node, coremodel_node, state|

coremodel_node

end



99
100
101
# File 'lib/coradoc/html/input/plugin.rb', line 99

def html_tree_add_hook_post(element, &block)
  @html_tree_hooks_post[element] = block
end

#html_tree_add_hook_post_by_css(css, &block) ⇒ Object



103
104
105
106
107
# File 'lib/coradoc/html/input/plugin.rb', line 103

def html_tree_add_hook_post_by_css(css, &block)
  html_tree.css(css).each do |e|
    html_tree_add_hook_post(e, &block)
  end
end

#html_tree_add_hook_pre(element, &block) ⇒ Object

Creates a hook to be called instead of converting an element to a CoreModel node.

proc |html_node, state|

coremodel_node

end



83
84
85
# File 'lib/coradoc/html/input/plugin.rb', line 83

def html_tree_add_hook_pre(element, &block)
  @html_tree_hooks_pre[element] = block
end

#html_tree_add_hook_pre_by_css(css, &block) ⇒ Object



87
88
89
90
91
# File 'lib/coradoc/html/input/plugin.rb', line 87

def html_tree_add_hook_pre_by_css(css, &block)
  html_tree.css(css).each do |e|
    html_tree_add_hook_pre(e, &block)
  end
end

#html_tree_change_properties_by_css(css, properties) ⇒ Object



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

def html_tree_change_properties_by_css(css, properties)
  html_tree.css(css).each do |e|
    properties.each do |k, v|
      e[k.to_s] = v
    end
  end
end

#html_tree_change_tag_name_by_css(css, new_name) ⇒ Object



40
41
42
43
44
# File 'lib/coradoc/html/input/plugin.rb', line 40

def html_tree_change_tag_name_by_css(css, new_name)
  html_tree.css(css).each do |e|
    e.name = new_name
  end
end

#html_tree_previewObject



68
69
70
71
72
73
# File 'lib/coradoc/html/input/plugin.rb', line 68

def html_tree_preview
  Tempfile.open(%w[coradoc .html]) do |i|
    i << html_tree.to_html
    system 'chromium-browser', '--no-sandbox', i.path
  end
end

#html_tree_process_to_coremodel(tree, state = {}) ⇒ Object



64
65
66
# File 'lib/coradoc/html/input/plugin.rb', line 64

def html_tree_process_to_coremodel(tree, state = {})
  Coradoc::Html::Input::Converters.process_coradoc(tree, state)
end

#html_tree_remove_by_css(css) ⇒ Object



54
55
56
# File 'lib/coradoc/html/input/plugin.rb', line 54

def html_tree_remove_by_css(css)
  html_tree.css(css).each(&:remove)
end

#html_tree_replace_with_children_by_css(css) ⇒ Object



58
59
60
61
62
# File 'lib/coradoc/html/input/plugin.rb', line 58

def html_tree_replace_with_children_by_css(css)
  html_tree.css(css).each do |e|
    e.replace(e.children)
  end
end

#html_tree_run_hooks(node, state) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/coradoc/html/input/plugin.rb', line 109

def html_tree_run_hooks(node, state, &)
  hook_pre = @html_tree_hooks_pre[node]
  hook_post = @html_tree_hooks_post[node]

  coremodel = hook_pre.call(node, state) if hook_pre
  coremodel ||= yield node, state

  coremodel = hook_post.call(node, coremodel, state) if hook_post

  coremodel
end

#nameObject



27
28
29
# File 'lib/coradoc/html/input/plugin.rb', line 27

def name
  self.class.name
end

#postprocess_coremodel_treeObject



33
# File 'lib/coradoc/html/input/plugin.rb', line 33

def postprocess_coremodel_tree; end

#postprocess_output_stringObject



34
# File 'lib/coradoc/html/input/plugin.rb', line 34

def postprocess_output_string; end

#preprocess_html_treeObject

Default no-op hooks. Plugins override these as needed.



32
# File 'lib/coradoc/html/input/plugin.rb', line 32

def preprocess_html_tree; end