Sha256: bb0d05d3d31e889e0661831b3205a5295bb6e4316d9461a936f17ee36c4bb14c

Contents?: true

Size: 998 Bytes

Versions: 4

Compression:

Stored size: 998 Bytes

Contents

module DocxTools
  class PartList

    # the stored list of parts
    attr_accessor :parts

    # parse the content type entries out of the given document
    def initialize(document, content_types)
      self.parts = {}

      content_types.map!(&method(:expand_type))
      document.get('[Content_Types].xml').xpath(content_types.join(' | ')).each do |tag|
        filename = tag['PartName'].split('/', 2)[1]
        parts[filename] = document.get(filename)
      end
    end

    # yield each part to the block
    def each_part(&block)
      parts.values.each(&block)
    end

    # get the requested part
    def get(filename)
      parts[filename]
    end

    # true if this part list has extracted this part from the document
    def has?(filename)
      parts.key?(filename)
    end

    private

      def expand_type(content_type)
        "xmlns:Types/xmlns:Override[@ContentType='application/vnd.openxmlformats-officedocument.wordprocessingml.#{content_type}+xml']"
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
docx_tools-0.0.4 lib/docx_tools/part_list.rb
docx_tools-0.0.3 lib/docx_tools/part_list.rb
docx_tools-0.0.2 lib/docx_tools/part_list.rb
docx_tools-0.0.1 lib/docx_tools/part_list.rb