Class: Html2Odt::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/html2odt/document.rb

Constant Summary collapse

CONTENT_REGEX =
/<text:p[^>]*>{{content}}<\/text:p>/
INCH_TO_CM =
2.54
DPI =

The following value was determined by comparing the generated result with an image dropped into LibreOffice interactively. Though this might be related to the fact, that my screen has a native resolution of 114 dpi.

xhtml2odt uses 96 by default.

114.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template: Html2Odt::ODT_TEMPLATE, html: nil) ⇒ Document

Returns a new instance of Document.



17
18
19
20
21
22
23
# File 'lib/html2odt/document.rb', line 17

def initialize(template: Html2Odt::ODT_TEMPLATE, html: nil)
  @html     = html
  @template = template
  @base_uri = nil

  read_xmls
end

Instance Attribute Details

#authorObject

Document meta data



15
16
17
# File 'lib/html2odt/document.rb', line 15

def author
  @author
end

#image_location_mappingObject

Returns the value of attribute image_location_mapping.



12
13
14
# File 'lib/html2odt/document.rb', line 12

def image_location_mapping
  @image_location_mapping
end

#titleObject

Document meta data



15
16
17
# File 'lib/html2odt/document.rb', line 15

def title
  @title
end

Instance Method Details

#base_uriObject



49
50
51
# File 'lib/html2odt/document.rb', line 49

def base_uri
  @base_uri
end

#base_uri=(uri) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html2odt/document.rb', line 34

def base_uri=(uri)
  if uri.is_a? URI
    @base_uri = uri
  else
    @base_uri = URI::parse(uri)
  end

  unless @base_uri.is_a? URI::HTTP
    raise ArgumentError, "Invalid URI - Expecting http(s) scheme."
  end

rescue URI::InvalidURIError
  raise ArgumentError, "Invalid URI - #{$!.message}"
end

#content_xmlObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/html2odt/document.rb', line 53

def content_xml
  @content_xml ||= begin

    html = prepare_html

    xml = xslt_tranform(html, Html2Odt::XHTML2ODT_XSL)

    xml = xml.sub('<?xml version="1.0" encoding="utf-8"?>', '')
    xml = @tpl_content_xml.sub(CONTENT_REGEX) { xml }

    xml = xslt_tranform(xml, Html2Odt::XHTML2ODT_STYLES_XSL)

    xml
  end
end

#dataObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/html2odt/document.rb', line 131

def data
  @data ||= begin
    buffer = Zip::OutputStream.write_buffer do |output_stream|
      # Copy contents from template, while replacing content.xml and
      # styles.xml
      Zip::File.open(@template) do |file|
        file.each do |entry|
          next if entry.directory?

          entry.get_input_stream do |input_stream|
            data = case entry.name
            when "content.xml"
              content_xml
            when "meta.xml"
              meta_xml
            when "styles.xml"
              styles_xml
            when "META-INF/manifest.xml"
              manifest_xml
            else
              input_stream.sysread
            end

            if entry.name == "mimetype"
              # mimetype may not be compressed
              output_stream.put_next_entry(entry.name, nil, nil, Zlib::NO_COMPRESSION)
            else
              output_stream.put_next_entry(entry.name)
            end
            output_stream.write data
          end
        end
      end

      # Adding images found in the HTML sources
      (@images || []).each do |image|
        output_stream.put_next_entry(image.target)
        output_stream.write File.read(image.source)
      end
    end

    buffer.string
  end
end

#htmlObject



30
31
32
# File 'lib/html2odt/document.rb', line 30

def html
  @html
end

#html=(html) ⇒ Object



25
26
27
28
# File 'lib/html2odt/document.rb', line 25

def html=(html)
  reset
  @html = html
end

#manifest_xmlObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/html2odt/document.rb', line 73

def manifest_xml
  @manifest_xml ||= begin
    content_xml # trigger HTML parsing

    if @images.nil? or @images.empty?
      @tpl_manifest_xml
    else
      doc = Nokogiri::XML(@tpl_manifest_xml)

      @images.each do |image|
        entry = create_node(doc, "manifest:file-entry")
        entry["manifest:full-path"]  = image.target
        entry["manifest:media-type"] = image.mime_type

        doc.root.add_child entry
      end

      doc.to_xml
    end
  end
end

#meta_xmlObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/html2odt/document.rb', line 95

def meta_xml
  @meta_xml ||= begin
     doc = Nokogiri::XML(@tpl_meta_xml)

     meta = doc.at_xpath("office:document-meta/office:meta")

     meta.xpath("meta:generator").remove
     meta.add_child create_node(doc, "meta:generator", "html2odt.rb/#{Html2Odt::VERSION}")

     meta.xpath("meta:creation-date").remove
     meta.add_child create_node(doc, "meta:creation-date", Time.now.utc.iso8601)

     meta.xpath("dc:date").remove
     meta.add_child create_node(doc, "dc:date", Time.now.utc.iso8601)

     meta.xpath("meta:editing-duration").remove
     meta.add_child create_node(doc, "meta:editing-duration", "P0D")

     meta.xpath("meta:editing-cycles").remove
     meta.add_child create_node(doc, "meta:editing-cycles", "1")

     meta.xpath("meta:initial-creator").remove
     meta.add_child create_node(doc, "meta:initial-creator", author) if author

     meta.xpath("dc:creator").remove
     meta.add_child create_node(doc, "dc:creator", author) if author

     meta.xpath("dc:title").remove
     meta.add_child create_node(doc, "dc:title", title) if title

     meta.xpath("meta:document-statistic").remove

     doc.to_xml
  end
end

#styles_xmlObject



69
70
71
# File 'lib/html2odt/document.rb', line 69

def styles_xml
  @styles_xml ||= xslt_tranform(@tpl_styles_xml, Html2Odt::XHTML2ODT_STYLES_XSL)
end

#write_to(path) ⇒ Object



176
177
178
# File 'lib/html2odt/document.rb', line 176

def write_to(path)
  File.write(path, data)
end