Class: Markdownator::Converters::Html

Inherits:
Base
  • Object
show all
Defined in:
lib/markdownator/converters/html.rb

Overview

Converts HTML into Markdown by walking the Nokogiri node tree.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_title(html) ⇒ Object



26
27
28
29
30
# File 'lib/markdownator/converters/html.rb', line 26

def self.extract_title(html)
  Markdownator.require_optional("nokogiri", feature: "HTML conversion")
  title = Nokogiri::HTML(html).at_css("title")&.text&.strip
  title unless title.nil? || title.empty?
end

.html_to_markdown(html) ⇒ Object

Shared so other container converters (EPUB) can reuse HTML conversion.



19
20
21
22
23
24
# File 'lib/markdownator/converters/html.rb', line 19

def self.html_to_markdown(html)
  Markdownator.require_optional("nokogiri", feature: "HTML conversion")
  doc = Nokogiri::HTML(html)
  root = doc.at_css("body") || doc.root || doc
  Renderers::HtmlRenderer.new.render(root)
end

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/markdownator/converters/html.rb', line 9

def accepts?(_io, stream_info)
  matches?(stream_info, extensions: %w[html htm], mimetypes: %w[text/html application/xhtml+xml])
end

#convert(io, stream_info, **_options) ⇒ Object



13
14
15
16
# File 'lib/markdownator/converters/html.rb', line 13

def convert(io, stream_info, **_options)
  html = read_all(io, stream_info)
  Result.new(markdown: self.class.html_to_markdown(html), title: self.class.extract_title(html))
end