Class: Uniword::Transformation::MhtmlMetadataBuilder Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Builds metadata, document properties, and file parts for MHTML output.

Extracted from OoxmlToMhtmlConverter for separation of responsibilities.

Instance Method Summary collapse

Constructor Details

#initialize(document, core_properties, relationships, document_name) ⇒ MhtmlMetadataBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MhtmlMetadataBuilder.



11
12
13
14
15
16
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 11

def initialize(document, core_properties, relationships, document_name)
  @document = document
  @core_properties = core_properties
  @relationships = relationships
  @provided_document_name = document_name
end

Instance Method Details

#build_custom_document_propertiesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build o:CustomDocumentProperties for AssetID and other custom fields



139
140
141
142
143
144
145
146
147
148
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 139

def build_custom_document_properties
  asset_id = custom_property("AssetID")
  return "" unless asset_id

  <<~XML
    <o:CustomDocumentProperties xmlns:o="urn:schemas-microsoft-com:office:office">
     <o:AssetID dt:dt="string">#{escape_xml(asset_id)}</o:AssetID>
    </o:CustomDocumentProperties>
  XML
end

#build_document_propertiesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build document properties from core properties



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 48

def build_document_properties
  props = Uniword::Mhtml::Metadata::DocumentProperties.new
  cp = core_properties

  if cp
    props.author = cp.creator.to_s if cp.respond_to?(:creator) && cp.creator
    props.created = cp.created.value.to_s if cp.respond_to?(:created) && cp.created
    props.last_author = cp.last_modified_by.to_s if cp.respond_to?(:last_modified_by) && cp.last_modified_by
    props.last_saved = cp.modified.value.to_s if cp.respond_to?(:modified) && cp.modified
    props.pages = cp.pages.to_s if cp.respond_to?(:pages) && cp.pages
    props.words = cp.words.to_s if cp.respond_to?(:words) && cp.words
    props.characters = cp.characters.to_s if cp.respond_to?(:characters) && cp.characters
    props.application = "Microsoft Word"
  end

  props
end

#build_filelist_partObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build filelist.xml part



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 67

def build_filelist_part
  image_entries = if @document.image_parts && !@document.image_parts.empty?
                    @document.image_parts.map do |_r_id, image_data|
                      %(<o:File HRef="#{image_data[:target]}"/>)
                    end.join("\n            ")
                  else
                    ""
                  end

  filelist_xml = <<~XML
    <xml xmlns:o="urn:schemas-microsoft-com:office:office">
      <o:MainFile HRef="../#{document_name}.htm"/>
      <o:File HRef="filelist.xml"/>
      #{image_entries}
    </xml>
  XML

  part = Uniword::Mhtml::XmlPart.new
  part.content_type = 'text/xml; charset="utf-8"'
  part.content_transfer_encoding = "quoted-printable"
  part.raw_content = filelist_xml
  part.content_location = "file:///C:/D057922B/#{document_name}.fld/filelist.xml"

  part
end

#build_image_partsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build image parts from document.image_parts



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 94

def build_image_parts
  return [] unless @document.image_parts && !@document.image_parts.empty?

  @document.image_parts.map do |_r_id, image_data|
    part = Uniword::Mhtml::ImagePart.new
    part.content_type = image_data[:content_type] || "image/png"
    part.content_transfer_encoding = "base64"
    part.raw_content = image_data[:data]
    part.content_location = "file:///C:/D057922B/#{document_name}.fld/#{image_data[:target]}"
    part
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build link tags for the head



161
162
163
164
165
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 161

def build_link_tags
  <<~LINK
    <link rel=File-List href="#{document_name}.fld/filelist.xml">
  LINK
end

#build_meta_tagsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build meta tags for the head



151
152
153
154
155
156
157
158
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 151

def build_meta_tags
  <<~META
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <meta name=ProgId content=Word.Document>
    <meta name=Generator content="Microsoft Word 15">
    <meta name=Originator content="Microsoft Word 15">
  META
end

#build_metadata_commentsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build the o:DocumentProperties content lines



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 108

def 
  props = core_properties
  return "" unless props

  author = props.respond_to?(:creator) ? props.creator : nil
  last_author = props.respond_to?(:last_modified_by) ? props.last_modified_by : nil
  revision = props.respond_to?(:revision) ? props.revision : nil
  created = props.respond_to?(:created) && props.created ? props.created.value : nil
  last_saved = props.respond_to?(:modified) && props.modified ? props.modified.value : nil

  stats = calculate_document_stats

  doc_props = []
  doc_props << " <o:Author>#{escape_xml(author)}</o:Author>" if author
  doc_props << " <o:LastAuthor>#{escape_xml(last_author)}</o:LastAuthor>" if last_author
  doc_props << " <o:Revision>#{escape_xml(revision)}</o:Revision>" if revision
  doc_props << " <o:TotalTime>0</o:TotalTime>"
  doc_props << " <o:Created>#{escape_xml(created)}</o:Created>" if created
  doc_props << " <o:LastSaved>#{escape_xml(last_saved)}</o:LastSaved>" if last_saved
  doc_props << " <o:Pages>#{stats[:pages]}</o:Pages>"
  doc_props << " <o:Words>#{stats[:words]}</o:Words>"
  doc_props << " <o:Characters>#{stats[:characters]}</o:Characters>"
  doc_props << " <o:Lines>#{stats[:lines]}</o:Lines>"
  doc_props << " <o:Paragraphs>#{stats[:paragraphs]}</o:Paragraphs>"
  doc_props << " <o:CharactersWithSpaces>#{stats[:characters_with_spaces]}</o:CharactersWithSpaces>"
  doc_props << " <o:Version>16.00</o:Version>"

  doc_props.join("\n")
end

#core_propertiesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the core properties to use (provided or from document)



19
20
21
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 19

def core_properties
  @core_properties || @document.core_properties
end

#document_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get document name - use provided name, or extract from relationships, or default



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/uniword/transformation/mhtml_metadata_builder.rb', line 25

def document_name
  @document_name ||= @provided_document_name || begin
    if @relationships
      rel = @relationships.relationships.find do |r|
        r.target.to_s.include?("document.xml")
      end
      if rel
        target = rel.target
        parts = target.split("/")
        return parts.last.sub(/\.xml$/, "") if parts.last
      end
    end
    location = @document.document_rels&.relationships&.first&.target || "document"
    File.basename(location, ".*")
  rescue StandardError => e
    Uniword.logger&.debug do
      "Falling back to 'document' name: #{e.message}"
    end
    "document"
  end
end