Class: Htmltoword::Document
- Inherits:
-
Object
- Object
- Htmltoword::Document
show all
- Extended by:
- HtmltowordHelper
- Defined in:
- lib/htmltoword/document.rb
Class Method Summary
collapse
Instance Method Summary
collapse
replace_values, template_file
Constructor Details
#initialize(template_path) ⇒ Document
Returns a new instance of Document.
56
57
58
59
|
# File 'lib/htmltoword/document.rb', line 56
def initialize(template_path)
@replaceable_files = {}
@template_path = template_path
end
|
Class Method Details
.create(content, template_name = nil, extras = false) ⇒ Object
6
7
8
9
10
11
|
# File 'lib/htmltoword/document.rb', line 6
def create(content, template_name = nil, = false)
template_name += extension if template_name && !template_name.end_with?(extension)
document = new(template_file(template_name))
document.replace_file(content, Document.doc_xml_file, )
document.generate
end
|
.create_and_save(content, file_path, template_name = nil, extras = false) ⇒ Object
13
14
15
16
17
|
# File 'lib/htmltoword/document.rb', line 13
def create_and_save(content, file_path, template_name = nil, = false)
File.open(file_path, "wb") do |out|
out << create(content, template_name, )
end
end
|
.create_with_content(template, content, set = nil, extras = false) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/htmltoword/document.rb', line 19
def create_with_content(template, content, set=nil, = false)
template += extension unless template.end_with?(extension)
content = replace_values(content, set) if set
document = new(template_file(template))
document.replace_file(content, Document.doc_xml_file, )
document.generate
end
|
.doc_xml_file ⇒ Object
31
32
33
|
# File 'lib/htmltoword/document.rb', line 31
def doc_xml_file
'word/document.xml'
end
|
.extension ⇒ Object
27
28
29
|
# File 'lib/htmltoword/document.rb', line 27
def extension
'.docx'
end
|
.numbering_xml_file ⇒ Object
35
36
37
|
# File 'lib/htmltoword/document.rb', line 35
def numbering_xml_file
'word/numbering.xml'
end
|
.numbering_xslt ⇒ Object
43
44
45
|
# File 'lib/htmltoword/document.rb', line 43
def numbering_xslt
File.join(Htmltoword.config.default_xslt_path, 'numbering.xslt')
end
|
.relations_xml_file ⇒ Object
39
40
41
|
# File 'lib/htmltoword/document.rb', line 39
def relations_xml_file
'word/_rels/document.xml.rels'
end
|
.relations_xslt ⇒ Object
47
48
49
|
# File 'lib/htmltoword/document.rb', line 47
def relations_xslt
File.join(Htmltoword.config.default_xslt_path, 'relations.xslt')
end
|
.xslt_template(extras = false) ⇒ Object
51
52
53
|
# File 'lib/htmltoword/document.rb', line 51
def xslt_template( = false)
File.join(Htmltoword.config.default_xslt_path, ( ? 'htmltoword.xslt' : 'base.xslt'))
end
|
Instance Method Details
#generate ⇒ Object
Generate a string representing the contents of a docx file.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/htmltoword/document.rb', line 64
def generate
Zip::File.open(@template_path) do |template_zip|
buffer = Zip::OutputStream.write_buffer do |out|
template_zip.each do |entry|
out.put_next_entry entry.name
if @replaceable_files[entry.name] && entry.name == Document.doc_xml_file
source = entry.get_input_stream.read.sub(/(<w:body>)((.|\n)*?)(<w:sectPr)/, "\\1#{@replaceable_files[entry.name]}\\4")
out.write(source)
elsif @replaceable_files[entry.name]
out.write(@replaceable_files[entry.name])
else
out.write(template_zip.read(entry.name))
end
end
end
buffer.string
end
end
|
#replace_file(html, file_name = Document.doc_xml_file, extras = false) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/htmltoword/document.rb', line 83
def replace_file(html, file_name = Document.doc_xml_file, = false)
html = html.presence || '<body></body>'
source = Nokogiri::HTML(html.gsub(/>\s+</, '><'))
transform_and_replace(source, Document.numbering_xslt, Document.numbering_xml_file)
transform_and_replace(source, Document.relations_xslt, Document.relations_xml_file)
cleaned_source = Nokogiri::XSLT(File.open(File.join(Htmltoword.config.default_xslt_path, 'inline_elements.xslt'))).transform(source)
transform_and_replace(cleaned_source, Document.xslt_template(), file_name, )
end
|