Class: Coradoc::ReverseAdoc::HtmlConverter
- Inherits:
-
Object
- Object
- Coradoc::ReverseAdoc::HtmlConverter
- Defined in:
- lib/coradoc/reverse_adoc/html_converter.rb
Class Method Summary collapse
- .convert(input, options = {}) ⇒ Object
- .convert_single_coradoc_to_adoc(_file, coradoc, plugin_instances) ⇒ Object
- .to_coradoc(input, options = {}) ⇒ Object
- .track_time(task) ⇒ Object
Class Method Details
.convert(input, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/coradoc/reverse_adoc/html_converter.rb', line 92 def self.convert(input, = {}) ReverseAdoc.config.with() do plugin_instances = Coradoc::ReverseAdoc.config.plugins.map(&:new) = .merge(plugin_instances: plugin_instances) coradoc = to_coradoc(input, ) 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, plugin_instances)] end end else convert_single_coradoc_to_adoc(nil, coradoc, plugin_instances) end end end |
.convert_single_coradoc_to_adoc(_file, coradoc, plugin_instances) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/coradoc/reverse_adoc/html_converter.rb', line 112 def self.convert_single_coradoc_to_adoc(_file, coradoc, plugin_instances) result = track_time "Converting Coradoc tree into Asciidoc" do Coradoc::Generator.gen_adoc(coradoc) end result = track_time "Cleaning up the result" do ReverseAdoc.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 |
.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 |
# File 'lib/coradoc/reverse_adoc/html_converter.rb', line 42 def self.to_coradoc(input, = {}) plugin_instances = .delete(:plugin_instances) ReverseAdoc.config.with() do plugin_instances ||= Coradoc::ReverseAdoc.config.plugins.map(&:new) 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 coradoc end end |
.track_time(task) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/coradoc/reverse_adoc/html_converter.rb', line 132 def self.track_time(task) if ReverseAdoc.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 |