Class: Html2rss::Selectors::PostProcessors::HtmlToMarkdown

Inherits:
Base
  • Object
show all
Defined in:
lib/html2rss/selectors/post_processors/html_to_markdown.rb

Overview

Returns HTML code as Markdown formatted String. Before converting to markdown, the HTML is sanitized with SanitizeHtml. Imagine this HTML structure:

<section>
  Lorem <b>ipsum</b> dolor...
  <iframe src="https://evil.corp/miner"></iframe>
  <script>alert();</script>
</section>

YAML usage example:

selectors:
 description:
   selector: section
   extractor: html
   post_process:
     name: html_to_markdown

Would return:

'Lorem **ipsum** dolor'

Constant Summary collapse

DESCRIPTION =

JSON Schema description exported via schema_doc.

'Sanitize HTML then convert it to Markdown (via ReverseMarkdown).'
EXAMPLES =

Example post-process objects for JSON Schema examples.

[
  { 'name' => 'html_to_markdown' }
].freeze

Instance Attribute Summary

Attributes inherited from Base

#context, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

assert_type, expect_options, #initialize

Constructor Details

This class inherits a constructor from Html2rss::Selectors::PostProcessors::Base

Class Method Details

.schema_docHash{Symbol => Object}

Returns JSON Schema fragment for this post-processor.

Returns:

  • (Hash{Symbol => Object})

    JSON Schema fragment for this post-processor



40
# File 'lib/html2rss/selectors/post_processors/html_to_markdown.rb', line 40

def self.schema_doc = SchemaDoc.for_post_processor(name: :html_to_markdown, klass: self)

.validate_args!(value, context) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)

    extracted selector value

  • context (Selectors::Context)

    post-processor context



45
46
47
# File 'lib/html2rss/selectors/post_processors/html_to_markdown.rb', line 45

def self.validate_args!(value, context)
  assert_type value, String, :value, context:
end

Instance Method Details

#getString

Returns formatted in Markdown.

Returns:

  • (String)

    formatted in Markdown



51
52
53
54
55
# File 'lib/html2rss/selectors/post_processors/html_to_markdown.rb', line 51

def get
  sanitized_value = SanitizeHtml.new(value, context).get

  ReverseMarkdown.convert(sanitized_value)
end