From f732fecf6354a7fbe4687327a34fd16eea256fc7 Mon Sep 17 00:00:00 2001 From: Sandro Duarte Date: Fri, 22 Mar 2024 14:28:54 -0300 Subject: [PATCH] standardrb --fix --- lib/odf-report.rb | 32 ++++---- lib/odf-report/data_source.rb | 16 ++-- lib/odf-report/field.rb | 11 +-- lib/odf-report/image.rb | 17 ++-- lib/odf-report/nestable.rb | 31 ++++---- lib/odf-report/parser/default.rb | 132 +++++++++++++++---------------- lib/odf-report/report.rb | 124 ++++++++++++++--------------- lib/odf-report/section.rb | 21 ++--- lib/odf-report/table.rb | 25 +++--- lib/odf-report/template.rb | 14 +--- lib/odf-report/text.rb | 14 +--- 11 files changed, 189 insertions(+), 248 deletions(-) diff --git a/lib/odf-report.rb b/lib/odf-report.rb index f2ec389..aadb8f5 100644 --- a/lib/odf-report.rb +++ b/lib/odf-report.rb @@ -1,18 +1,18 @@ -require 'rubygems' -require 'zip' -require 'fileutils' -require 'nokogiri' -require 'mime/types' -require 'securerandom' +require "rubygems" +require "zip" +require "fileutils" +require "nokogiri" +require "mime/types" +require "securerandom" -require File.expand_path('../odf-report/parser/default', __FILE__) +require File.expand_path("../odf-report/parser/default", __FILE__) -require File.expand_path('../odf-report/data_source', __FILE__) -require File.expand_path('../odf-report/field', __FILE__) -require File.expand_path('../odf-report/text', __FILE__) -require File.expand_path('../odf-report/template', __FILE__) -require File.expand_path('../odf-report/image', __FILE__) -require File.expand_path('../odf-report/nestable', __FILE__) -require File.expand_path('../odf-report/section', __FILE__) -require File.expand_path('../odf-report/table', __FILE__) -require File.expand_path('../odf-report/report', __FILE__) +require File.expand_path("../odf-report/data_source", __FILE__) +require File.expand_path("../odf-report/field", __FILE__) +require File.expand_path("../odf-report/text", __FILE__) +require File.expand_path("../odf-report/template", __FILE__) +require File.expand_path("../odf-report/image", __FILE__) +require File.expand_path("../odf-report/nestable", __FILE__) +require File.expand_path("../odf-report/section", __FILE__) +require File.expand_path("../odf-report/table", __FILE__) +require File.expand_path("../odf-report/report", __FILE__) diff --git a/lib/odf-report/data_source.rb b/lib/odf-report/data_source.rb index e9d8286..029b94b 100644 --- a/lib/odf-report/data_source.rb +++ b/lib/odf-report/data_source.rb @@ -1,12 +1,11 @@ module ODFReport class DataSource - attr_reader :value def initialize(opts, &block) - @value = opts[:value] || opts[:collection] + @value = opts[:value] || opts[:collection] @data_field = opts[:data_field] || opts[:collection_field] || opts[:name] - @block = block + @block = block end def set_source(record) @@ -25,7 +24,6 @@ def empty? private def extract_value_from_item(record) - if @block @block.call(record) @@ -43,23 +41,21 @@ def extract_value_from_item(record) record.send(@data_field) else - raise "Can't find [#{@data_field.to_s}] in this #{record.class}" + raise "Can't find [#{@data_field}] 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]) + tmp = if f.is_a?(Hash) + tmp.send(f.keys[0], f.values[0]) else - tmp = tmp.send(f) + tmp.send(f) end end tmp end - end end diff --git a/lib/odf-report/field.rb b/lib/odf-report/field.rb index c991ec0..c1f9463 100644 --- a/lib/odf-report/field.rb +++ b/lib/odf-report/field.rb @@ -1,6 +1,5 @@ module ODFReport class Field - DELIMITERS = %w([ ]) def initialize(opts, &block) @@ -14,16 +13,14 @@ def set_source(record) end def replace!(content, data_item = nil) - txt = content.inner_html if txt.gsub!(to_placeholder, sanitize(@data_source.value)) content.inner_html = txt end - end - private + private def to_placeholder if DELIMITERS.is_a?(Array) @@ -35,11 +32,10 @@ def to_placeholder def sanitize(txt) txt = html_escape(txt) - txt = odf_linebreak(txt) - txt + odf_linebreak(txt) end - HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' } + HTML_ESCAPE = {"&" => "&", ">" => ">", "<" => "<", '"' => """} def html_escape(s) return "" unless s @@ -50,6 +46,5 @@ def odf_linebreak(s) return "" unless s s.to_s.gsub("\n", "") end - end end diff --git a/lib/odf-report/image.rb b/lib/odf-report/image.rb index 3c760d0..5a77782 100644 --- a/lib/odf-report/image.rb +++ b/lib/odf-report/image.rb @@ -1,6 +1,5 @@ module ODFReport class Image < Field - IMAGE_DIR_NAME = "Pictures" attr_reader :files @@ -11,7 +10,6 @@ def initialize(opts, &block) 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 @@ -20,14 +18,13 @@ def replace!(doc, data_item = nil) file = @data_source.value if file - image.attribute('href').content = File.join(IMAGE_DIR_NAME, File.basename(file)) - frame.attribute('name').content = SecureRandom.uuid + image.attribute("href").content = File.join(IMAGE_DIR_NAME, File.basename(file)) + frame.attribute("name").content = SecureRandom.uuid @files << file else frame.remove end - end def self.include_image_file(zip_file, image_file) @@ -41,17 +38,15 @@ def self.include_image_file(zip_file, image_file) def self.include_manifest_entry(content, image_file) return unless image_file - return unless root_node = content.at("//manifest:manifest") + 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 + 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 diff --git a/lib/odf-report/nestable.rb b/lib/odf-report/nestable.rb index f6c3546..6f97dd9 100644 --- a/lib/odf-report/nestable.rb +++ b/lib/odf-report/nestable.rb @@ -1,17 +1,15 @@ module ODFReport class Nestable - def initialize(opts) @name = opts[:name] @data_source = DataSource.new(opts) - @fields = [] - @texts = [] - @tables = [] + @fields = [] + @texts = [] + @tables = [] @sections = [] - @images = [] - + @images = [] end def set_source(data_item) @@ -19,32 +17,36 @@ def set_source(data_item) self end - def add_field(name, data_field=nil, &block) - opts = { name: name, data_field: data_field } + 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) + 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) + 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) + def add_table(table_name, collection_field, opts = {}) + opts[:name] = table_name + opts[: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) + def add_section(section_name, collection_field, opts = {}) + opts[:name] = section_name + opts[:collection_field] = collection_field + sec = Section.new(opts) @sections << sec @@ -60,6 +62,5 @@ def wrap_with_ns(node) #{node.to_xml} XML end - end end diff --git a/lib/odf-report/parser/default.rb b/lib/odf-report/parser/default.rb index ae63732..e5b437e 100644 --- a/lib/odf-report/parser/default.rb +++ b/lib/odf-report/parser/default.rb @@ -1,90 +1,82 @@ module ODFReport + module Parser + # Default HTML parser + # + # sample HTML + # + #

first paragraph

+ #

second paragraph

+ #
+ #

first quote paragraph

+ #

first quote paragraph

+ #

first quote paragraph

+ #
+ #

third paragraph

+ # + #

fourth paragraph

+ #

fifth paragraph

+ #

sixth paragraph

+ # + + class Default + attr_accessor :paragraphs + + def initialize(text, template_node) + @text = text + @paragraphs = [] + @template_node = template_node + + parse + end -module Parser - - - # Default HTML parser - # - # sample HTML - # - #

first paragraph

- #

second paragraph

- #
- #

first quote paragraph

- #

first quote paragraph

- #

first quote paragraph

- #
- #

third paragraph

- # - #

fourth paragraph

- #

fifth paragraph

- #

sixth paragraph

- # - - class Default - - attr_accessor :paragraphs - - def initialize(text, template_node) - @text = text - @paragraphs = [] - @template_node = template_node - - parse - end - - def parse - html = Nokogiri::HTML5.fragment(@text) + def parse + html = Nokogiri::HTML5.fragment(@text) - html.css("p", "h1", "h2").each do |p| - style = check_style(p) - text = parse_formatting(p.inner_html) + html.css("p", "h1", "h2").each do |p| + style = check_style(p) + text = parse_formatting(p.inner_html) - add_paragraph(text, style) + add_paragraph(text, style) + end end - end - def add_paragraph(text, style) + def add_paragraph(text, style) + node = @template_node.dup - node = @template_node.dup + node["text:style-name"] = style if style + node.children = text - node['text:style-name'] = style if style - node.children = text + @paragraphs << node + end - @paragraphs << node - end + private - private + def parse_formatting(text) + text.strip! + text.gsub!(/(.+?)<\/strong>/) { "#{$1}" } + text.gsub!(/(.+?)<\/em>/) { "#{$1}" } + text.gsub!(/(.+?)<\/u>/) { "#{$1}" } + text.gsub!(//, "") + text.delete!("\n") + text + end - def parse_formatting(text) - text.strip! - text.gsub!(/(.+?)<\/strong>/) { "#{$1}<\/text:span>" } - text.gsub!(/(.+?)<\/em>/) { "#{$1}<\/text:span>" } - text.gsub!(/(.+?)<\/u>/) { "#{$1}<\/text:span>" } - text.gsub!(//, "") - text.gsub!("\n", "") - text - end + def check_style(node) + style = nil - def check_style(node) - style = nil + if /h\d/i.match?(node.name) + style = "title" - if node.name =~ /h\d/i - style = "title" + elsif node.parent && node.parent.name == "blockquote" + style = "quote" - elsif node.parent && node.parent.name == "blockquote" - style = "quote" + elsif /margin/.match?(node["style"]) + style = "quote" - elsif node['style'] =~ /margin/ - style = "quote" + end + style end - - style end - end - -end - end diff --git a/lib/odf-report/report.rb b/lib/odf-report/report.rb index 7208fc0..b129cf3 100644 --- a/lib/odf-report/report.rb +++ b/lib/odf-report/report.rb @@ -1,92 +1,84 @@ module ODFReport + class Report + def initialize(template_name = nil, io: nil) + @template = ODFReport::Template.new(template_name, io: io) -class Report + @texts = [] + @fields = [] + @tables = [] + @sections = [] - def initialize(template_name = nil, io: nil) + @images = [] - @template = ODFReport::Template.new(template_name, io: io) - - @texts = [] - @fields = [] - @tables = [] - @sections = [] - - @images = [] - - yield(self) if block_given? - - end + yield(self) if block_given? + end - def add_field(field_tag, value='') - opts = {:name => field_tag, :value => value} - field = Field.new(opts) - @fields << field - end + def add_field(field_tag, value = "") + opts = {name: field_tag, value: value} + field = Field.new(opts) + @fields << field + end - def add_text(field_tag, value='') - opts = {:name => field_tag, :value => value} - text = Text.new(opts) - @texts << text - end + def add_text(field_tag, value = "") + opts = {name: field_tag, value: value} + text = Text.new(opts) + @texts << text + end - def add_table(table_name, collection, opts={}) - opts.merge!(:name => table_name, :collection => collection) - tab = Table.new(opts) - @tables << tab + def add_table(table_name, collection, opts = {}) + opts[:name] = table_name + opts[:collection] = collection - yield(tab) - end + tab = Table.new(opts) + @tables << tab - def add_section(section_name, collection, opts={}) - opts.merge!(:name => section_name, :collection => collection) - sec = Section.new(opts) - @sections << sec + yield(tab) + end - yield(sec) - end + def add_section(section_name, collection, opts = {}) + opts[:name] = section_name + opts[:collection] = collection - def add_image(image_name, value=nil) - opts = {:name => image_name, :value => value} - image = Image.new(opts) - @images << image - end + sec = Section.new(opts) + @sections << sec - def generate(dest = nil) + yield(sec) + end - @template.update_content do |file| + def add_image(image_name, value = nil) + opts = {name: image_name, value: value} + image = Image.new(opts) + @images << image + end - file.update_files do |doc| + def generate(dest = nil) + @template.update_content do |file| + file.update_files do |doc| + @sections.each { |c| c.replace!(doc) } + @tables.each { |c| c.replace!(doc) } - @sections.each { |c| c.replace!(doc) } - @tables.each { |c| c.replace!(doc) } + @texts.each { |c| c.replace!(doc) } + @fields.each { |c| c.replace!(doc) } - @texts.each { |c| c.replace!(doc) } - @fields.each { |c| c.replace!(doc) } + @images.each { |c| c.replace!(doc) } + end - @images.each { |c| c.replace!(doc) } + all_images.each { |i| Image.include_image_file(file, i) } + file.update_manifest do |content| + all_images.each { |i| Image.include_manifest_entry(content, i) } + end end - all_images.each { |i| Image.include_image_file(file, i) } - - file.update_manifest do |content| - all_images.each { |i| Image.include_manifest_entry(content, i) } + if dest + File.binwrite(dest, @template.data) + else + @template.data end - end - if dest - File.open(dest, "wb") { |f| f.write(@template.data) } - else - @template.data + def all_images + @all_images ||= (@images.map(&:files) + @sections.map(&:all_images) + @tables.map(&:all_images)).flatten.uniq end - - end - - def all_images - @all_images ||= (@images.map(&:files) + @sections.map(&:all_images) + @tables.map(&:all_images)).flatten.uniq end - -end - end diff --git a/lib/odf-report/section.rb b/lib/odf-report/section.rb index 8920b23..1e725e1 100644 --- a/lib/odf-report/section.rb +++ b/lib/odf-report/section.rb @@ -1,29 +1,24 @@ module ODFReport class Section < Nestable - def replace!(doc) - return unless find_section_node(doc) @data_source.each do |record| - new_section = deep_clone(@section_node) - @tables.each { |t| t.set_source(record).replace!(new_section) } - @sections.each { |s| s.set_source(record).replace!(new_section) } - @texts.each { |t| t.set_source(record).replace!(new_section) } - @fields.each { |f| f.set_source(record).replace!(new_section) } - @images.each { |i| i.set_source(record).replace!(new_section) } + @tables.each { |t| t.set_source(record).replace!(new_section) } + @sections.each { |s| s.set_source(record).replace!(new_section) } + @texts.each { |t| t.set_source(record).replace!(new_section) } + @fields.each { |f| f.set_source(record).replace!(new_section) } + @images.each { |i| i.set_source(record).replace!(new_section) } @section_node.before(new_section.to_xml) - end @section_node.remove - end # replace_section - private + private def find_section_node(doc) @section_node = doc.at_xpath("//text:section[@text:name='#{@name}']") @@ -31,9 +26,7 @@ def find_section_node(doc) def deep_clone(node) Nokogiri::XML(wrap_with_ns(node)).at_xpath("//text:section") - .tap { |n| n.attribute('name').content = SecureRandom.uuid } - + .tap { |n| n.attribute("name").content = SecureRandom.uuid } end - end end diff --git a/lib/odf-report/table.rb b/lib/odf-report/table.rb index 98e988f..9b510f6 100644 --- a/lib/odf-report/table.rb +++ b/lib/odf-report/table.rb @@ -1,16 +1,15 @@ module ODFReport class Table < Nestable - def initialize(opts) super(opts) @template_rows = [] - @header = opts[:header] || false - @skip_if_empty = opts[:skip_if_empty] || false + @header = opts[:header] || false + @skip_if_empty = opts[:skip_if_empty] || false end def replace!(doc) - return unless table = find_table_node(doc) + return unless (table = find_table_node(doc)) @template_rows = table.xpath("table:table-row") @@ -22,26 +21,23 @@ def replace!(doc) end @data_source.each do |record| - new_node = get_next_row - @tables.each { |n| n.set_source(record).replace!(new_node) } - @sections.each { |n| n.set_source(record).replace!(new_node) } - @texts.each { |n| n.set_source(record).replace!(new_node) } - @fields.each { |n| n.set_source(record).replace!(new_node) } - @images.each { |n| n.set_source(record).replace!(new_node) } + @tables.each { |n| n.set_source(record).replace!(new_node) } + @sections.each { |n| n.set_source(record).replace!(new_node) } + @texts.each { |n| n.set_source(record).replace!(new_node) } + @fields.each { |n| n.set_source(record).replace!(new_node) } + @images.each { |n| n.set_source(record).replace!(new_node) } table.add_child(new_node.to_xml) - end @template_rows.each_with_index do |r, i| r.remove if (get_start_node..template_length) === i end - end # replace - private + private def get_next_row if @template_rows.size == 1 @@ -59,7 +55,7 @@ def get_next_row end end - return deep_clone(ret) + deep_clone(ret) end def get_start_node @@ -77,6 +73,5 @@ def find_table_node(doc) def deep_clone(node) Nokogiri::XML(wrap_with_ns(node)).at_xpath("//table:table-row") end - end end diff --git a/lib/odf-report/template.rb b/lib/odf-report/template.rb index 15bc0b6..2dc8c55 100644 --- a/lib/odf-report/template.rb +++ b/lib/odf-report/template.rb @@ -1,7 +1,6 @@ module ODFReport class Template - - CONTENT_FILES = ['content.xml', 'styles.xml'] + CONTENT_FILES = ["content.xml", "styles.xml"] MANIFEST_FILE = "META-INF/manifest.xml" attr_accessor :output_stream @@ -22,13 +21,10 @@ def update_content end def update_files(&block) - get_template_entries.each do |entry| - next if entry.directory? entry.get_input_stream do |is| - data = is.sysread if CONTENT_FILES.include?(entry.name) @@ -36,25 +32,20 @@ def update_files(&block) end update_file(entry.name, data) unless entry.name == MANIFEST_FILE - end end - end def update_manifest(&block) entry = get_template_entries.find_entry(MANIFEST_FILE) entry.get_input_stream do |is| - data = is.sysread process_entry(data, &block) update_file(MANIFEST_FILE, data) - end - end def data @@ -69,13 +60,11 @@ def update_file(name, data) private def get_template_entries - if @template Zip::File.open(@template) else Zip::File.open_buffer(@io.force_encoding("ASCII-8BIT")) end - end def process_entry(entry) @@ -83,6 +72,5 @@ def process_entry(entry) yield doc entry.replace(doc.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)) end - end end diff --git a/lib/odf-report/text.rb b/lib/odf-report/text.rb index 8c8eed4..0394156 100644 --- a/lib/odf-report/text.rb +++ b/lib/odf-report/text.rb @@ -1,12 +1,9 @@ module ODFReport - class Text < Field - attr_accessor :parser def replace!(doc, data_item = nil) - - return unless node = find_text_node(doc) + return unless (node = find_text_node(doc)) @parser = Parser::Default.new(@data_source.value, node) @@ -15,7 +12,6 @@ def replace!(doc, data_item = nil) end node.remove - end private @@ -24,10 +20,10 @@ def find_text_node(doc) texts = doc.xpath(".//text:p[text()='#{to_placeholder}']") if texts.empty? texts = doc.xpath(".//text:p/text:span[text()='#{to_placeholder}']") - if texts.empty? - texts = nil + texts = if texts.empty? + nil else - texts = texts.first.parent + texts.first.parent end else texts = texts.first @@ -35,7 +31,5 @@ def find_text_node(doc) texts end - end - end