forked from sandrods/odf-report
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow nested images inside tables and sections (sandrods#103)
* move image handling to a proper class * testing images * bin - scripts for debugging * store images data in Image class * working on tables & sections * image specs
- Loading branch information
Showing
38 changed files
with
737 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ doc | |
test/result | ||
spec/result | ||
Gemfile.lock | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'launchy' | ||
|
||
arg = ARGV[0] | ||
dir = File.basename(arg, File.extname(arg)) | ||
|
||
%x( rm -rf #{dir}; unzip -d #{dir} #{arg} ) | ||
|
||
Launchy.open dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'launchy' | ||
require "nokogiri" | ||
require "zip" | ||
|
||
xml = "" | ||
|
||
Zip::File.open(ARGV[0]) do |zip| | ||
content = zip.get_entry('content.xml').get_input_stream.read | ||
xml = Nokogiri::XML(content).to_xml | ||
end | ||
|
||
filename = File.join(Dir.mktmpdir, "#{File.basename(ARGV[0])}.result.xml") | ||
|
||
File.open(filename, 'w') { |f| f.write xml } | ||
|
||
Launchy.open filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module ODFReport | ||
class DataSource | ||
|
||
attr_reader :value | ||
|
||
def initialize(opts, &block) | ||
@value = opts[:value] || opts[:collection] | ||
@data_field = opts[:data_field] || opts[:collection_field] || opts[:name] | ||
@block = block | ||
end | ||
|
||
def set_source(record) | ||
@value = extract_value_from_item(record) | ||
end | ||
|
||
def each(&block) | ||
@value.each(&block) | ||
end | ||
|
||
def empty? | ||
@value.nil? || @value.empty? | ||
end | ||
|
||
private | ||
|
||
def extract_value_from_item(record) | ||
|
||
if @block | ||
@block.call(record) | ||
|
||
elsif record.is_a?(Hash) | ||
key = @data_field | ||
record[key] || record[key.to_s.downcase] || record[key.to_s.upcase] || record[key.to_s.downcase.to_sym] | ||
|
||
elsif @data_field.is_a?(Array) | ||
execute_methods_on_item(record) | ||
|
||
elsif @data_field.is_a?(Hash) && record.respond_to?(@data_field.keys[0]) | ||
record.send(@data_field.keys[0], @data_field.values[0]) | ||
|
||
elsif record.respond_to?(@data_field) | ||
record.send(@data_field) | ||
|
||
else | ||
raise "Can't find [#{@data_field.to_s}] in this #{record.class}" | ||
|
||
end | ||
|
||
end | ||
|
||
def execute_methods_on_item(record) | ||
tmp = record.dup | ||
@data_field.each do |f| | ||
if f.is_a?(Hash) | ||
tmp = tmp.send(f.keys[0], f.values[0]) | ||
else | ||
tmp = tmp.send(f) | ||
end | ||
end | ||
tmp | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module ODFReport | ||
class Image < Field | ||
|
||
IMAGE_DIR_NAME = "Pictures" | ||
|
||
attr_reader :files | ||
|
||
def initialize(opts, &block) | ||
@files = [] | ||
super | ||
end | ||
|
||
def replace!(doc, data_item = nil) | ||
|
||
frame = doc.xpath("//draw:frame[@draw:name='#{@name}']").first | ||
image = doc.xpath("//draw:frame[@draw:name='#{@name}']/draw:image").first | ||
|
||
return unless image | ||
|
||
file = @data_source.value | ||
|
||
image.attribute('href').content = File.join(IMAGE_DIR_NAME, File.basename(file)) | ||
frame.attribute('name').content = SecureRandom.uuid | ||
|
||
@files << file | ||
end | ||
|
||
def self.include_image_file(zip_file, image_file) | ||
return unless image_file | ||
|
||
href = File.join(IMAGE_DIR_NAME, File.basename(image_file)) | ||
|
||
zip_file.update_file(href, File.read(image_file)) | ||
end | ||
|
||
def self.include_manifest_entry(content, image_file) | ||
return unless image_file | ||
|
||
return unless root_node = content.at("//manifest:manifest") | ||
|
||
href = File.join(IMAGE_DIR_NAME, File.basename(image_file)) | ||
|
||
entry = content.create_element('manifest:file-entry') | ||
entry['manifest:full-path'] = href | ||
entry['manifest:media-type'] = MIME::Types.type_for(href)[0].content_type | ||
|
||
root_node.add_child entry | ||
|
||
end | ||
|
||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module ODFReport | ||
class Nestable | ||
|
||
def initialize(opts) | ||
@name = opts[:name] | ||
|
||
@data_source = DataSource.new(opts) | ||
|
||
@fields = [] | ||
@texts = [] | ||
@tables = [] | ||
@sections = [] | ||
@images = [] | ||
|
||
end | ||
|
||
def set_source(data_item) | ||
@data_source.set_source(data_item) | ||
self | ||
end | ||
|
||
def add_field(name, data_field=nil, &block) | ||
opts = { name: name, data_field: data_field } | ||
@fields << Field.new(opts, &block) | ||
end | ||
alias_method :add_column, :add_field | ||
|
||
def add_text(name, data_field=nil, &block) | ||
opts = {name: name, data_field: data_field} | ||
@texts << Text.new(opts, &block) | ||
end | ||
|
||
def add_image(name, data_field=nil, &block) | ||
opts = {name: name, data_field: data_field} | ||
@images << Image.new(opts, &block) | ||
end | ||
|
||
def add_table(table_name, collection_field, opts={}) | ||
opts.merge!(name: table_name, collection_field: collection_field) | ||
tab = Table.new(opts) | ||
@tables << tab | ||
|
||
yield(tab) | ||
end | ||
|
||
def add_section(section_name, collection_field, opts={}) | ||
opts.merge!(name: section_name, collection_field: collection_field) | ||
sec = Section.new(opts) | ||
@sections << sec | ||
|
||
yield(sec) | ||
end | ||
|
||
def all_images | ||
(@images.map(&:files) + @sections.map(&:all_images) + @tables.map(&:all_images)).flatten | ||
end | ||
|
||
def wrap_with_ns(node) | ||
<<-XML | ||
<root xmlns:draw="a" xmlns:xlink="b" xmlns:text="c" xmlns:table="d">#{node.to_xml}</root> | ||
XML | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.