Class: Uniword::HtmlImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/html_importer.rb

Overview

Imports HTML content and converts it to OOXML paragraphs or documents.

Examples:

Import simple HTML

paragraphs = Uniword::HtmlImporter.import('<p>Hello World</p>')

Import with new instance

importer = Uniword::HtmlImporter.new('<p>Hello</p>')
doc = importer.to_document

Class method import

paragraphs = Uniword::HtmlImporter.import('<p>Hello</p>')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html = nil) ⇒ HtmlImporter

Create a new HtmlImporter instance

Parameters:

  • html (String) (defaults to: nil)

    HTML content to import



22
23
24
# File 'lib/uniword/html_importer.rb', line 22

def initialize(html = nil)
  @html = html
end

Instance Attribute Details

#htmlString (readonly)

Returns The HTML content being imported.

Returns:

  • (String)

    The HTML content being imported



17
18
19
# File 'lib/uniword/html_importer.rb', line 17

def html
  @html
end

Class Method Details

.import(html) ⇒ Array<Uniword::Wordprocessingml::Paragraph>

Class method: Import HTML content

Parameters:

  • html (String)

    HTML content

Returns:



51
52
53
# File 'lib/uniword/html_importer.rb', line 51

def self.import(html)
  new.import(html)
end

Instance Method Details

#import(html = nil) ⇒ Array<Uniword::Wordprocessingml::Paragraph>

Import HTML content and convert to OOXML paragraphs

Parameters:

  • html (String) (defaults to: nil)

    HTML content (overrides instance html if provided)

Returns:



30
31
32
33
# File 'lib/uniword/html_importer.rb', line 30

def import(html = nil)
  content = html || @html
  Transformation::HtmlToOoxmlConverter.html_to_paragraphs(content)
end

#to_documentUniword::Wordprocessingml::DocumentRoot

Convert imported HTML to a DocumentRoot



38
39
40
41
42
43
44
45
# File 'lib/uniword/html_importer.rb', line 38

def to_document
  doc = Wordprocessingml::DocumentRoot.new
  paragraphs = import
  paragraphs.each do |para|
    doc.body.paragraphs << para
  end
  doc
end