Debian Codegraph @

examples/odf2pod

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # $Id : odf2pod 0.4 2010-01-11 JMG$
4 #-----------------------------------------------------------------------------
5
6 =head1 NAME
7
8 odf2pod - POD generation from an OpenDocument file
9
10 =head1 SYNOPSIS
11
12 Usage : odf2pod <filename>
13
14 =head1 DESCRIPTION
15
16 This demo script exports the content of a given OpenDocument file
17 to POD on the standard output. In the present form, it's quite limited
18 and not flexible, in order to remain easily readable. It should be
19 considered as an example of text extraction using OpenOffice::OODoc
20 and not as the 'definitive' odf2pod filter, knowing that complex
21 document structures are not properly rendered.
22
23 Before extraction, some transformations are done in the document
24 in order to make it more convenient for a POD presentation. Some
25 pieces of metadata (title, subject, description), if defined, are
26 reported in the beginning of the POD. The footnotes are removed from
27 the content and reported in a special section at the end.
28
29 This script needs Text::Wrapper (that is not necessarily required
30 by the OpenOffice::OODoc installation). To implement more sophisicated
31 presentation rules, you could use Text::Format instead.
32
33 =cut
34
35 use OpenOffice::OODoc 2.101;
36 use Text::Wrapper;
37
38 #-----------------------------------------------------------------------------
39 my $meta; # will be the metadata object
40 my $doc; # will be the document content object
41 #-----------------------------------------------------------------------------
42 # text output utilities (using Text::Wrapper)
43
44 my $paragraph_wrapper;
45 my $list_wrapper;
46
47 sub BEGIN # wrappers initialisation
48 {
49 # It's just an example; in a real application, the formatting rules
50 # should be more flexibles and variables according to the style
51 # of each source text element
52 # Here, we prepair two kinds of wrappers, in order to have
53 # a larger left margin for item lists than for ordinary paragraphs
54 $paragraph_wrapper = Text::Wrapper->new
55 (
56 columns => 76,
57 par_start => ' ',
58 body_start => ' '
59 );
60 $list_wrapper = Text::Wrapper->new
61 (
62 columns => 76,
63 par_start => ' ',
64 body_start => ' '
65 );
66 }
67
68 sub heading_output
69 {
70 my ($level, $text) = @_;
71 $text && print "=head$level\t$text\n\n";
72 }
73
74 # output the content according to the type of text object
75 sub content_output
76 {
77 my $element = shift; # it's an ODF text object (not a flat string)
78 my $text = $doc->getText($element);
79
80 # choose an output format according to the type
81 if ($element->isItemList)
82 {
83 print $list_wrapper->wrap($text) . "\n";
84 }
85 # we use the paragraph output rule for any element
86 # that is not a list
87 else
88 {
89 print $paragraph_wrapper->wrap($text) . "\n";
90 }
91 # in a more specialised script, we could select another
92 # alternative wrapper according to the style (using the
93 # getStyle() method of OpenOffice::OODoc::Text)
94 }
95
96 #-----------------------------------------------------------------------------
97
98 # initialise the ODF file object
99 my $ooarchive = odfContainer($ARGV[0])
100 or die "No regular ODF file\n";
101
102 # extract the metadata
103 $meta = odfMeta(container => $ooarchive)
104 or warn "This file has not standard ODF properties. Looks strange.\n";
105
106 # extract the content
107 $doc = odfDocument(container => $ooarchive, part => 'content')
108 or die "No standard ODF content ! I give up !\n";
109
110 # attempt to use some metadata to begin the output
111 if ($meta)
112 {
113 my $title = $meta->title;
114 if ($title)
115 {
116 heading_output(1, "NAME");
117 print $paragraph_wrapper->wrap($title) . "\n";
118 }
119 my $subject = $meta->subject;
120 if ($subject)
121 {
122 heading_output(1, "SUBJECT");
123 print $paragraph_wrapper->wrap($subject) . "\n";
124 }
125 my $description = $meta->description;
126 if ($description)
127 {
128 heading_output(1, "DESCRIPTION");
129 print $paragraph_wrapper->wrap($description) . "\n";
130 }
131 # we could dump other metadata here...
132 }
133
134 # the strange 2 next lines prevent the getText() method of
135 # OpenOffice::OODoc::Text (see the corresponding man page) from using
136 # its default tags for spans and footnotes
137 delete $doc->{'delimiters'}->{'text:span'};
138 delete $doc->{'delimiters'}->{'text:footnote-body'};
139
140 # here we select the tab as field separator for table field output
141 # (the default is ";" as for CSV output)
142 $doc->{'field_separator'} = "\t";
143
144 # in the next sequence, we will extract all the footnotes, store them for
145 # later processing and remove them from the content
146 my @notes = $doc->getFootnoteList;
147 $doc->removeElement($_) for @notes;
148
149 # get the full list of text objects (without the previously removed footnotes)
150 my @content = $doc->getTextElementList;
151
152 # if the first text element is not a heading, we create a leading
153 # heading here, using the title or an arbitrary name
154 heading_output(1, $meta->title || "INTRODUCTION")
155 unless ($content[0]->isHeading);
156 foreach my $element (@content)
157 {
158 my $level = $doc->getLevel($element); # get the hierarchical level
159 if ($level) # if an element has a 'level', it's a heading
160 {
161 heading_output($level, $doc->getText($element));
162 }
163 else
164 {
165 content_output($element);
166 }
167 }
168
169 # all the document body is processed
170
171 if (@notes)
172 {
173 # OK, we have some footnotes in store
174 # create a special section
175 heading_output(1, "NOTES");
176 my $count = 0;
177 while (@notes)
178 {
179 $count++;
180 my $element = shift @notes;
181 my $text = "[$count] " . $doc->getText($element);
182 print $paragraph_wrapper->wrap($text) . "\n";
183 }
184 }
185
186 # end of POD output
187 print "=cut\n";
188
189 exit;
190
191 #-----------------------------------------------------------------------------