Class: Coradoc::Input::Html::HtmlConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/html/input/html_converter.rb

Overview

HTML to CoreModel converter

This class handles the conversion of HTML documents to CoreModel. It does NOT handle serialization to any specific output format. For serialization, use Coradoc.serialize(coremodel, to: :format)

Examples:

Basic usage - get CoreModel

coremodel = HtmlConverter.to_core_model(html_string)

Serialize to AsciiDoc

coremodel = HtmlConverter.to_core_model(html_string)
adoc_text = Coradoc.serialize(coremodel, to: :asciidoc)

Class Method Summary collapse

Class Method Details

.prepare_plugin_instances(options) ⇒ Object



75
76
77
# File 'lib/coradoc/html/input/html_converter.rb', line 75

def self.prepare_plugin_instances(options)
  options[:plugin_instances] || Html.config.plugins.map(&:new)
end

.to_core_model(input, options = {}) ⇒ Coradoc::CoreModel::Base

Convert HTML to CoreModel

Parameters:

  • input (String, Nokogiri::XML::Document, Nokogiri::XML::Node)

    HTML input

  • options (Hash) (defaults to: {})

    Conversion options

Returns:

  • (Coradoc::CoreModel::Base)

    CoreModel document



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/coradoc/html/input/html_converter.rb', line 25

def self.to_core_model(input, options = {})
  Input::Html.config.with(options) do
    plugin_instances = prepare_plugin_instances(options)

    root = track_time 'Loading input HTML document' do
      case input
      when String
        Nokogiri::HTML(input).root
      when Nokogiri::XML::Document
        input.root
      when Nokogiri::XML::Node
        input
      end
    end

    return nil unless root

    plugin_instances.each do |plugin|
      plugin.html_tree = root
      track_time "Preprocessing document with #{plugin.name} plugin" do
        plugin.preprocess_html_tree
      end
      root = plugin.html_tree
    end

    coremodel = track_time 'Converting input document tree to CoreModel' do
      Converters.process_coradoc(
        root,
        plugin_instances: plugin_instances
      )
    end

    coremodel = track_time 'Post-process CoreModel tree' do
      Postprocessor.process(coremodel)
    end

    plugin_instances.each do |plugin|
      plugin.coremodel_tree = coremodel
      track_time "Postprocessing CoreModel tree with #{plugin.name} plugin" do
        plugin.postprocess_coremodel_tree
      end
      coremodel = plugin.coremodel_tree
    end

    options[:plugin_instances] = plugin_instances unless options.frozen?

    coremodel
  end
end

.track_time(task) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/coradoc/html/input/html_converter.rb', line 80

def self.track_time(task)
  if Input::Html.config.track_time
    warn ('  ' * @track_time_indentation) + "* #{task} is starting..."
    @track_time_indentation += 1
    t0 = Time.now
    ret = yield
    time_elapsed = Time.now - t0
    @track_time_indentation -= 1
    warn ('  ' * @track_time_indentation) +
         "* #{task} took #{time_elapsed.round(3)} seconds"
    ret
  else
    yield
  end
end