Class: Coradoc::Input::Html::HtmlConverter
- Inherits:
-
Object
- Object
- Coradoc::Input::Html::HtmlConverter
- 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)
Class Method Summary collapse
- .prepare_plugin_instances(options) ⇒ Object
-
.to_core_model(input, options = {}) ⇒ Coradoc::CoreModel::Base
Convert HTML to CoreModel.
- .track_time(task) ⇒ Object
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() [:plugin_instances] || Html.config.plugins.map(&:new) end |
.to_core_model(input, options = {}) ⇒ Coradoc::CoreModel::Base
Convert HTML to CoreModel
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, = {}) Input::Html.config.with() do plugin_instances = prepare_plugin_instances() 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 [:plugin_instances] = plugin_instances unless .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 |