Class: Coradoc::Input::HTML::HtmlConverter

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

Class Method Summary collapse

Class Method Details

.cleanup_result(result, options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/coradoc/input/html/html_converter.rb', line 121

def self.cleanup_result(result, options)
  Input::HTML.config.with(options) do
    plugin_instances = prepare_plugin_instances(options)

    result = track_time "Cleaning up the result" do
      Input::HTML.cleaner.tidy(result)
    end
    plugin_instances.each do |plugin|
      if plugin.respond_to?(:postprocess_asciidoc_string)
        plugin.asciidoc_string = result
        track_time "Postprocessing AsciiDoc string with #{plugin.name} plugin" do
          plugin.postprocess_asciidoc_string
        end
        result = plugin.asciidoc_string
      end
    end

    result
  end
end

.convert(input, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/coradoc/input/html/html_converter.rb', line 93

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

    coradoc = to_coradoc(input, options)

    if coradoc.is_a?(Hash)
      coradoc.to_h do |file, tree|
        track_time "Converting file #{file || 'main'}" do
          [file,
           convert_single_coradoc_to_adoc(file, tree, options)]
        end
      end
    else
      convert_single_coradoc_to_adoc(nil, coradoc, options)
    end
  end
end

.convert_single_coradoc_to_adoc(_file, coradoc, options) ⇒ Object



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

def self.convert_single_coradoc_to_adoc(_file, coradoc, options)
  plugin_instances = prepare_plugin_instances(options)

  result = track_time "Converting Coradoc tree into Asciidoc" do
    Coradoc::Generator.gen_adoc(coradoc)
  end
  cleanup_result(result, options)
end

.prepare_plugin_instances(options) ⇒ Object



142
143
144
# File 'lib/coradoc/input/html/html_converter.rb', line 142

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

.to_coradoc(input, options = {}) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/coradoc/input/html/html_converter.rb', line 42

def self.to_coradoc(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 "" unless root

    plugin_instances.each do |plugin|
      plugin.html_tree = root
      if plugin.respond_to?(:preprocess_html_tree)
        track_time "Preprocessing document with #{plugin.name} plugin" do
          plugin.preprocess_html_tree
        end
      end
      root = plugin.html_tree
    end

    coradoc = track_time "Converting input document tree to Coradoc tree" do
      Converters.process_coradoc(root, plugin_instances: plugin_instances)
    end

    coradoc = track_time "Post-process Coradoc tree" do
      Postprocessor.process(coradoc)
    end

    plugin_instances.each do |plugin|
      if plugin.respond_to?(:postprocess_coradoc_tree)
        plugin.coradoc_tree = coradoc
        track_time "Postprocessing Coradoc tree with #{plugin.name} plugin" do
          plugin.postprocess_coradoc_tree
        end
        coradoc = plugin.coradoc_tree
      end
    end

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

    coradoc
  end
end

.track_time(task) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/coradoc/input/html/html_converter.rb', line 147

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