From 915b73a4991fb1670bce45ecd577c840c48917d7 Mon Sep 17 00:00:00 2001 From: Michael Witrant Date: Sun, 31 Mar 2013 20:32:49 +0200 Subject: [PATCH] accept template as option --- extract_amendments.rb | 14 ++++++++------ server.rb | 8 ++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/extract_amendments.rb b/extract_amendments.rb index d084e7b..c092c2f 100755 --- a/extract_amendments.rb +++ b/extract_amendments.rb @@ -147,9 +147,10 @@ class AmendmentExtractor end debug "rendering amendments" - template = ERB.new File.read('template.erb'), nil, '-' + template_text = options[:template] || File.read('template.erb') + template = ERB.new template_text, 4, '-' - erb_binding = OpenStruct.new(amendments: amendments).instance_eval { binding } + erb_binding = OpenStruct.new(amendments: amendments).instance_eval { binding }.taint output = template.result(erb_binding) end end @@ -157,10 +158,11 @@ end if $0 == __FILE__ options = {} - extra_args = cli '--xml-dump' => lambda { |path| options[:xml_dump_path] = path }, - '-d --debug' => lambda { $DEBUG = true }, - '-1 --one' => lambda { options[:parse_only_one] = true }, - '-n --number' => lambda { |num| options[:parse_only_num] = num } + extra_args = cli '--xml-dump' => lambda { |path| options[:xml_dump_path] = path }, + '-d --debug' => lambda { $DEBUG = true }, + '-1 --one' => lambda { options[:parse_only_one] = true }, + '-n --number' => lambda { |num| options[:parse_only_num] = num }, + '-t --template' => lambda { |file| options[:template] = File.read(file) } opendocument_path = extra_args.first raise "usage: #$0 " unless opendocument_path diff --git a/server.rb b/server.rb index 8a356e0..ad04ee6 100755 --- a/server.rb +++ b/server.rb @@ -12,7 +12,7 @@ get '/' do end post '/extract' do - result = AmendmentExtractor.new.extract(params['file'][:tempfile].path) + result = AmendmentExtractor.new.extract(params['file'][:tempfile].path, template: params['template']) haml :extract, locals: {result: result} end @@ -39,9 +39,13 @@ __END__ %label.control-label{:for => "file"} ODT File .controls %input#file{type: "file", name: "file"} + .control-group + %label.control-label{:for => "template"} Template + .controls + %textarea.input-block-level#template{rows: 20, name: "template"}= params[:template] || File.read('template.erb') .control-group .controls %button.btn.btn-primary{:type => "submit"} Extract @@ extract -%textarea{rows: 20, style: 'width: 100%'}= Rack::Utils.escape_html(result) +%textarea.input-block-level{rows: 20}= Rack::Utils.escape_html(result) -- GitLab