Class: Markdownator::Converters::Html

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

Overview

Converts HTML into Markdown using reverse_markdown (Nokogiri-backed).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_title(html) ⇒ Object



22
23
24
25
26
# File 'lib/markdownator/converters/html.rb', line 22

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.



17
18
19
20
# File 'lib/markdownator/converters/html.rb', line 17

def self.html_to_markdown(html)
  Markdownator.require_optional("reverse_markdown", feature: "HTML conversion")
  ReverseMarkdown.convert(html, unknown_tags: :bypass, github_flavored: true).strip
end

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/markdownator/converters/html.rb', line 7

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



11
12
13
14
# File 'lib/markdownator/converters/html.rb', line 11

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