Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7f5221d976
Fetching contributors…

Cannot retrieve contributors at this time

executable file 177 lines (134 sloc) 4.089 kb
#! /usr/bin/env perl
#
# Copyright (C) 2010-2012 Stefan Hornburg (Racke) <racke@linuxia.de>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
use strict;
use warnings;
use Getopt::Long;
use File::Temp qw(tempfile);
use Template::Flute;
use Template::Flute::Config;
use Template::Flute::Database::Rose;
use Template::Flute::Specification::XML;
use Template::Flute::HTML;
my (%opts, %input, %values);
# commandline options
$opts{value} = \%values;
my $whandler = $SIG{__WARN__};
$SIG{__WARN__} = sub {print STDERR "$0: @_";};
unless (GetOptions (\%opts, 'browser', 'combine', 'conf=s',
'iterator=s', 'value=s%', 'pdf=s')) {
exit 1;
}
$SIG{__WARN__} = $whandler;
# get input out of commandline arguments (key=value pairs)
for (@ARGV) {
if (/^(\w+)=(.*)$/) {
$input{$1} = $2;
}
}
unless (@ARGV) {
die "$0: Commandline arguments missing.\n";
}
if ($opts{combine}) {
unless ($opts{pdf}) {
die "$0: --combine option only valid together with --pdf=FILE\n";
}
require Template::Flute::PDF;
require Template::Flute::PDF::Import;
# combine PDF's from commandline
my ($pdf, $import);
$pdf = new Template::Flute::PDF (file => $opts{pdf},
);
$import = new Template::Flute::PDF::Import;
for my $file (@ARGV) {
$import->import(pdf => $pdf->{pdf}, file => $file);
}
$pdf->{pdf}->saveas();
exit 0;
}
my ($config_file, $config_vars, $spec_file, $template_file, $db_object,
%iterators);
if ($opts{conf}) {
$config_file = $opts{conf};
$config_vars = Template::Flute::Config::load($config_file);
if (exists $config_vars->{database}) {
# setup database object
$db_object = new Template::Flute::Database::Rose (%{$config_vars->{database}});
}
}
if ($opts{iterator}) {
my $name;
# shift name from iterator option
if ($opts{iterator} =~ s%^(\w+)=%%) {
$name = $1;
}
if ($opts{iterator} =~ m%^(.*?/)*(.+)\.json$%) {
# JSON iterator
unless (defined $name) {
$name = $2;
}
require Template::Flute::Iterator::JSON;
$iterators{$name} = Template::Flute::Iterator::JSON->new(file => $opts{iterator});
}
else {
die "$0: Unknown format for iterator: $opts{iterator}\n";
}
}
# parse specification file
my ($xml_spec, $xml_file, $spec);
$xml_file = shift;
$xml_spec = new Template::Flute::Specification::XML;
unless ($spec = $xml_spec->parse_file($xml_file)) {
die "$0: error parsing $xml_file: " . $xml_spec->error() . "\n";
}
# parse template
my ($html_file, $html_object, $list_object, $form_object);
$html_file = shift;
$html_object = new Template::Flute::HTML;
$html_object->parse_file($html_file, $spec);
for $list_object ($html_object->lists()) {
# seed and check input
$list_object->input(\%input);
}
for $form_object ($html_object->forms()) {
# seed and check input
$form_object->input(\%input);
}
# process template
my ($flute);
$flute = new Template::Flute (template => $html_object,
specification => $spec,
database => $db_object,
iterators => \%iterators,
values => \%values);
if ($opts{pdf}) {
require Template::Flute::PDF;
$flute->process();
my $pdf = new Template::Flute::PDF (template => $html_object);
$pdf->process($opts{pdf});
}
elsif ($opts{browser}) {
# store flute result in temporary file
my ($tmph, $tmpfile) = tempfile();
print $tmph $flute->process();
print "Using temporary file $tmpfile\n";
system('x-www-browser', $tmpfile);
}
else {
print $flute->process(), "\n";
}
Jump to Line
Something went wrong with that request. Please try again.