Debian Codegraph @

examples/odfbuild

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # $Id : odfbuild 0.2 2008-05-04 JMG$
4 #-----------------------------------------------------------------------------
5
6 =head1 NAME
7
8 odfbuild - OpenDocument file creation utility
9
10 =head1 SYNOPSIS
11
12 odfbuild filename.odt
13
14 odfbuild filename.odt --title "My Document" --subject "Test"
15
16 odfbuild filename.ods --class spreadsheet --source "data.csv" --tablesize "8x16"
17
18 cat data.txt | odfbuild filename.odt -
19
20 =head1 OPTIONS
21
22 --class Document class (text, spreadsheet, drawing, presentation)
23 Default: text
24 --opendocument (no value). If this option is on, the document will be in
25 OpenDocument format. Without this option, the format will
26 be selected according to the general configuration of the
27 OpenOffice::OODoc installation.
28 --creator The author of the document. Default: the current user's
29 login name.
30 --date Creation date. Default is current local time.
31 If provided, must be in ISO-8601 format
32 (YYYY-MM-DDTHH:MM:SS)
33 --description The description (abstract) of the document. Default: none.
34 --force (no value). If this option is on, any existing file with
35 the same path as the target file will be replaced. Without
36 this option, the program will fail if the target exists.
37 --generator Software signature to be stored in the file (not visible
38 for the end user).
39 Default: "Genicorp OpenOffice::OODoc <version>"
40 --keywords A list of comma-separated keywords. Default: none.
41 --source A text file, to be used as the content of the document.
42 If the document class is 'text', each line is loaded as a new
43 paragraph with the standard style. If the document class is
44 'spreadsheet', the file is processed as CSV data and loaded
45 in one sheet. If the document class is neither 'text' nor
46 'spreadsheet', the file is not processed.
47 If source = '-', or if a '-' argument is provided, the data
48 file is read through the standard input.
49 --subject The subject of the document. Default: none.
50 --tablename The name of the sheet to be created if the document class is
51 'spreadsheet' and if a data file is provided. Default: the name
52 of the data file, or "Unnamed Sheet" if the data is read from
53 the standard input.
54 --tablesize The size of the sheet to be created if the document class is
55 'spreadsheet' and if a data file is provided, in 'HxW' format
56 where H is the number of lines and W the number of columns.
57 Default: '16x8'
58 --title The title of the document. Default: "Untitled".
59 --readable_XML (no value). For debugging only. If this option is on, the XML
60 content of the target file is indented, in order to be later
61 edited.
62
63 =cut
64
65 #----------------------------------------------------------------------------------------
66
67 use OpenOffice::OODoc 2.101;
68 use Getopt::Long;
69
70 my $INPUT = undef;
71 my $input = undef;
72
73 my $generator = 'OpenOffice::OODoc ' . $OpenOffice::OODoc::VERSION;
74 my $title = "Generated document";
75 my $description = "This file has been created with $generator";
76
77 my $targetfile = $ARGV[0]
78 or die "Usage: oobuild <filename> [--options]\n";
79
80 GetOptions
81 (
82 '' => \(my $stdin = undef),
83 'class=s' => \(my $class = 'text'),
84 'date=s' => \(my $date = odfLocaltime),
85 'generator=s' => \$generator,
86 'title=s' => \$title,
87 'subject=s' => \(my $subject = ''),
88 'description=s' => \$description,
89 'keywords=s' => \(my $keywords = ''),
90 'creator=s' => \(my $creator = scalar getpwuid($<)),
91 'source=s' => \(my $source = undef),
92 'tablesize=s' => \(my $tablesize = '16x8'),
93 'tablename=s' => \(my $tablename = undef),
94 'force' => \(my $force = undef),
95 'opendocument' => \(my $odf = undef),
96 'readable_XML' => \(my $rxml = undef)
97 );
98
99 if ( -e $targetfile && ! defined $force)
100 {
101 die "File $targetfile exists. I don't create it.\n" .
102 "Use --force to replace it.\n";
103 }
104 my $odf_flag = $odf ? 'on' : undef;
105 my $rxml_flag = $rxml ? 'on' : undef;
106
107 my $archive = odfContainer
108 (
109 $targetfile,
110 create => $class,
111 opendocument => $odf_flag
112 )
113 or die "File creation failure\n";
114
115 #----------------------------------------------------------------------------------------
116
117 my $meta = odfMeta(container => $archive, readable_XML => $rxml_flag);
118 $meta->creation_date($date);
119 $meta->date($date);
120 $meta->generator($generator);
121 $meta->initial_creator($creator);
122 $meta->creator($creator);
123 $meta->title($title);
124 $meta->subject($subject);
125 $meta->description($description);
126 $meta->keywords(split ',', $keywords) if $keywords;
127
128 #----------------------------------------------------------------------------------------
129
130 if ($stdin || $source)
131 {
132 my $INPUT = undef;
133 if ($stdin || ($source eq '-'))
134 {
135 $INPUT = *STDIN;
136 $source = '-';
137 }
138 else
139 {
140 if ( -e $source && -r $source )
141 {
142 open(SOURCE, "<", $source);
143 $INPUT = *SOURCE;
144 }
145 }
146 my $content = odfDocument
147 (container => $archive, readable_XML => $rxml_flag);
148 if ($class eq 'text')
149 {
150 my $first_para = $content->getParagraph(0);
151 while (my $para = <$INPUT>)
152 {
153 $content->appendParagraph(text => $para);
154 }
155 $content->removeElement($first_para) if $first_para;
156 }
157 elsif ($class eq 'spreadsheet')
158 {
159 my ($cols, $lns) = split 'x', $tablesize;
160 unless ($tablename)
161 {
162 if ($source gt '-')
163 {
164 $tablename = $source;
165 }
166 else
167 {
168 $tablename = 'Unnamed Sheet';
169 }
170 }
171 my $first_sheet = $content->getTable(0);
172 my $sheet = $content->appendTable($tablename, $cols, $lns);
173 $content->removeElement($first_sheet) if $first_sheet;
174 ROW: for (my $i = 0; my $record = <$INPUT>; $i++)
175 {
176 last ROW unless $record;
177 chomp $record;
178 my @data = split ';', $record;
179 my $row = $content->getTableRow($sheet, $i);
180 CELL: for (my $j = 0; my $value = shift @data; $j++)
181 {
182 last CELL unless defined $value;
183 $content->cellValue($row, $j, $value);
184 }
185 }
186 }
187 else
188 {
189 warn "Source text loading not allowed for this class\n";
190 }
191 close $INPUT unless ($source eq '-');
192 }
193
194 #----------------------------------------------------------------------------------------
195
196 $archive->save;
197 exit;
198
199 #----------------------------------------------------------------------------------------