Skip to content

Commit 00e9e3c

Browse files
author
Cristina Matonte
committed
Small refactoring
Change the way of building paths, separate helper methods from Document class
1 parent bdfbdc1 commit 00e9e3c

File tree

4 files changed

+82
-56
lines changed

4 files changed

+82
-56
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ spec/reports
1515
test/tmp
1616
test/version_tmp
1717
tmp
18+
.idea/*

lib/htmltoword.rb

+46-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# encoding: UTF-8
22
require "htmltoword/version"
3+
require "htmltoword/htmltoword_helper"
34
require "action_controller"
45
require "action_view"
56
require "nokogiri"
@@ -9,21 +10,57 @@ module Htmltoword
910
def self.root
1011
File.expand_path '../..', __FILE__
1112
end
13+
def self.templates_path
14+
File.join root, "templates"
15+
end
1216

1317
class Document
18+
1419
DOC_XML_FILE = "word/document.xml"
1520
BASIC_PATH = ::Htmltoword.root
16-
TEMPLATES_PATH = File.join BASIC_PATH, "templates"
17-
REL_PATH = "/tmp"
21+
SAVING_REL_PATH = "public/tmp"
22+
BASIC_REL_URL = "/tmp"
1823
FILE_EXTENSION = ".docx"
1924
XSLT_TEMPLATE = File.join(BASIC_PATH, 'xslt', 'html_to_wordml.xslt')
2025

21-
def initialize(path)
26+
class << self
27+
include HtmltowordHelper
28+
29+
def create content, file_name
30+
word_file = new(template_file, file_name)
31+
word_file.replace_file content
32+
word_file.save
33+
word_file.rel_url
34+
end
35+
36+
def create_with_content template, file_name, content, set=nil
37+
word_file = new(template_file("#{template}#{FILE_EXTENSION}"), file_name)
38+
content = replace_values(content, set) if set
39+
word_file.replace_file content
40+
word_file.save
41+
word_file.rel_url
42+
end
43+
end
44+
45+
def initialize(template_path, file_name)
46+
@file_name = "#{file_name}#{FILE_EXTENSION}"
2247
@replaceable_files = {}
23-
@template_zip = Zip::ZipFile.open(path)
48+
@template_zip = Zip::ZipFile.open(template_path)
49+
end
50+
51+
def file_name
52+
@file_name
2453
end
2554

26-
def save_to(path)
55+
#
56+
# It creates missing folders if needed, creates a new zip/word file on the
57+
# specified location, copies all the files from the template word document
58+
# and replace the content of the ones to be replaced.
59+
#
60+
# path: Could be an absolute or relative path, by default it uses a
61+
# relative path defined on <code>rel_path</code>
62+
#
63+
def save(path=rel_path)
2764
FileUtils.mkdir_p File.dirname(path)
2865
Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |out|
2966
@template_zip.each do |entry|
@@ -39,67 +76,20 @@ def save_to(path)
3976
@template_zip.close
4077
end
4178

42-
def replace_body content
43-
xml = @template_zip.read(DOC_XML_FILE).force_encoding("UTF-8")
44-
content.gsub!("\n", "")
45-
xml.gsub!(/<w:body>.*<\/w:body>/, "<w:body>#{content.gsub("\n", "")}</w:body>")
46-
@replaceable_files[DOC_XML_FILE] = xml
47-
end
48-
4979
def replace_file html, file_name=DOC_XML_FILE
5080
source = Nokogiri::HTML(html.gsub(/>\s+</, "><"))
5181
xslt = Nokogiri::XSLT( File.read(XSLT_TEMPLATE) )
5282
source = xslt.transform( source ) unless (source/"/html").blank?
5383
@replaceable_files[file_name] = source.to_s
5484
end
5585

56-
def self.template_file template
57-
default_path = File.join(TEMPLATES_PATH, "template#{FILE_EXTENSION}")
58-
template_path = File.join(TEMPLATES_PATH, "#{template}#{FILE_EXTENSION}")
59-
File.exist?(template_path) ? template_path : default_path
60-
end
61-
62-
def self.create content, file_name
63-
word_file = new(template_file("overview"))
64-
word_file.replace_file content
65-
relative_path = File.join REL_PATH, "#{file_name}#{FILE_EXTENSION}"
66-
word_file.save_to File.join("public", relative_path)
67-
relative_path
86+
def rel_path
87+
File.join(SAVING_REL_PATH, file_name)
6888
end
6989

70-
def self.create_with_content template, file_name, content, set=nil
71-
word_file = new(template_file(template))
72-
content = replace_values(content, set) if set
73-
word_file.replace_file content
74-
relative_path = File.join REL_PATH, "#{file_name}#{FILE_EXTENSION}"
75-
word_file.save_to File.join("public", relative_path)
76-
relative_path
90+
def rel_url
91+
File.join(BASIC_REL_URL, file_name)
7792
end
7893

79-
private
80-
def self.replace_values content, set
81-
doc = Nokogiri::HTML(content)
82-
set.each_pair do |key, value|
83-
fields = (doc/"//span[@data-id='#{key}']")
84-
fields.each do |f|
85-
date_format = f.attr("date-format") || "long"
86-
data_transform = f.attr("data-transform")
87-
if value.is_a? Hash
88-
view = ActionView::Base.new(ActionController::Base.view_paths, {})
89-
final_value = view.render "partials/answer_table", answer: value
90-
fragment = doc.root.parse(final_value).first
91-
new_node = doc.root.add_child(fragment)
92-
f.parent.replace new_node
93-
elsif value.is_a? Time
94-
f.content = I18n.l(value.to_date, format: date_format.to_sym)
95-
elsif data_transform == "capitalized"
96-
f.content = value.mb_chars.capitalize rescue value
97-
else
98-
f.content = value
99-
end
100-
end
101-
end
102-
doc.to_s
103-
end
10494
end
10595
end

lib/htmltoword/htmltoword_helper.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Htmltoword
2+
module HtmltowordHelper
3+
4+
def template_file template_file_name=nil
5+
default_path = File.join(::Htmltoword.templates_path, "default.docx")
6+
template_path = template_file_name.present? ? File.join(::Htmltoword.templates_path, template_file_name) : ""
7+
File.exist?(template_path) ? template_path : default_path
8+
end
9+
10+
def replace_values content, set
11+
doc = Nokogiri::HTML(content)
12+
set.each_pair do |key, value|
13+
fields = (doc/"//span[@data-id='#{key}']")
14+
fields.each do |f|
15+
date_format = f.attr("date-format") || "long"
16+
data_transform = f.attr("data-transform")
17+
if value.is_a? Hash
18+
view = ActionView::Base.new(ActionController::Base.view_paths, {})
19+
final_value = view.render "partials/answer_table", answer: value
20+
fragment = doc.root.parse(final_value).first
21+
new_node = doc.root.add_child(fragment)
22+
f.parent.replace new_node
23+
elsif value.is_a? Time
24+
f.content = I18n.l(value.to_date, format: date_format.to_sym)
25+
elsif data_transform == "capitalized"
26+
f.content = value.mb_chars.capitalize rescue value
27+
else
28+
f.content = value
29+
end
30+
end
31+
end
32+
doc.to_s
33+
end
34+
end
35+
end
File renamed without changes.

0 commit comments

Comments
 (0)