Hi,
I am trying to write into ODT files using Ruby and ERB. I have the below content in odt doc file:
Hi <%= name%>,
I am doing good.
Thanks <%= another_name%>.
Now, I am unzipping the odt file, and using "content.xml" file to process it using ERB. But the problem is all <% syntac is getting encoded while reading so what I want to do is not happening. Any idea how to solve this issue and achieve my goal?
My code:
class Odt
require 'zip'
INPUT_FILE = Rails.root.join('tmp/odt/test.odt')
OUTPUT_DIR = Rails.root.join('tmp/odt/output')
def unzip_file (file = INPUT_FILE, destination = OUTPUT_DIR)
Zip::File.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
end
def parse
unzip_file
file = OUTPUT_DIR.join('content.xml')
new_file = File.rename(file, OUTPUT_DIR.join('content.xml.erb'))
a = ActionController::Base.new
a.append_view_path OUTPUT_DIR
content = a.render_to_string template: 'content', locals: { :name => "Arup", :another_name => 'Anil' }, layout: false
puts content
# File.rename(OUTPUT_DIR.join('content.xml.erb'), OUTPUT_DIR.join('content.xml'))
# doc = Nokogiri::XML(content)
# # ... make changes to doc ...
# File.write(OUTPUT_DIR.join('content.xml'), doc.to_xml)
···
#
# z = ZipFileGenerator.new(OUTPUT_DIR, OUTPUT_DIR.join('final.odt'))
# z.write
end
end
The output of the content I put inside this gist:
https://gist.github.com/aruprakshit/ec371ff52d9889e7a5c34e15c6129c6a . Not pasting here because it will make this email ugly.
Regards,
Arup Rakshit
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Arup
Rakshit
Now, I am unzipping the odt file, and using "content.xml" file to process it
using ERB. But the problem is all <% syntac is getting encoded while reading
so what I want to do is not happening. Any idea how to solve this issue and
achieve my goal?
Perhaps I have misunderstood, but I think that the problem you are going to have is that while Nokogiri can certainly output XML, that does not mean that the XML it outputs is compatible with ODF.
You need to know what structure ODF expects in the contents.xml file. You can find that here:
I'm afraid that it's quite complicated.
Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>
Hello Andy,
Thanks for your reply.
My problem is my erb code inside the XML is not being replaced by the variables I passed as `locals`.
<%= name%> and <%= another_name%> should be replaced by the value of `name` and `another_name`. Because that is what I tried to do by:
content = a.render_to_string template: 'content', locals: { :name => "Arup", :another_name => 'Anil' }, layout: false
Regards,
Arup Rakshit
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Arup
Rakshit
Now, I am unzipping the odt file, and using "content.xml" file to process it
using ERB. But the problem is all <% syntac is getting encoded while reading
so what I want to do is not happening. Any idea how to solve this issue and
achieve my goal?
Perhaps I have misunderstood, but I think that the problem you are going to have is that while Nokogiri can certainly output XML, that does not mean that the XML it outputs is compatible with ODF.
You need to know what structure ODF expects in the contents.xml file. You can find that here:
I'm afraid that it's quite complicated.
Click here to view Company Information and Confidentiality Notice.<Email Disclaimer | James Hall & Co.
···
On Wednesday, 2 November 2016 2:44 PM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote: