[Jan Gerber] fix unicode filenames in wikibooks2epub (from https://code.launchpad...
[objavi2.git] / perl / ObjaviContrib.pm
blob1c66398b157477b9c10dd0aeaa1ae5729f22f660
1 # Plugin for TWiki Enterprise Collaboration Platform, http://TWiki.org/
3 # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it
4 # Copyright (C) 2001-2006 Peter Thoeny, peter@thoeny.org
5 # and TWiki Contributors. All Rights Reserved. TWiki Contributors
6 # are listed in the AUTHORS file in the root of this distribution.
7 # NOTE: Please extend that file, not this notice.
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version. For
13 # more details read LICENSE in the root of this distribution.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 # For licensing info read LICENSE file in the TWiki root.
20 # change the package name and $pluginName!!!
22 package TWiki::Contrib::ObjaviContrib;
24 # Always use strict to enforce variable scoping
25 use strict;
27 require TWiki::Func; # The plugins API
28 require TWiki::Plugins; # For the API version
30 # $VERSION is referred to by TWiki, and is the only global variable that
31 # *must* exist in this package.
32 use vars qw( $VERSION $RELEASE $SHORTDESCRIPTION $debug $pluginName $NO_PREFS_IN_TOPIC );
34 use File::Temp qw/ tempfile /;
35 use List::Compare::Functional qw( get_unique );
37 # This should always be $Rev$ so that TWiki can determine the checked-in
38 # status of the plugin. It is used by the build automation tools, so
39 # you should leave it alone.
40 $VERSION = '$Rev$';
42 # This is a free-form string you can use to "name" your own plugin version.
43 # It is *not* used by the build automation tools, but is reported as part
44 # of the version number in PLUGINDESCRIPTIONS.
45 $RELEASE = 'TWiki-4.2';
47 # Short description of this plugin
48 # One line description, is shown in the %TWIKIWEB%.TextFormattingRules topic:
49 $SHORTDESCRIPTION = 'Empty Plugin used as a template for new Plugins';
51 # You must set $NO_PREFS_IN_TOPIC to 0 if you want your plugin to use preferences
52 # stored in the plugin topic. This default is required for compatibility with
53 # older plugins, but imposes a significant performance penalty, and
54 # is not recommended. Instead, use $TWiki::cfg entries set in LocalSite.cfg, or
55 # if you want the users to be able to change settings, then use standard TWiki
56 # preferences that can be defined in your Main.TWikiPreferences and overridden
57 # at the web and topic level.
58 $NO_PREFS_IN_TOPIC = 1;
60 # Name of this Plugin, only used in this module
61 $pluginName = 'ObjaviContrib';
64 use Data::Dumper;
66 =pod
68 ---++ initPlugin($topic, $web, $user, $installWeb) -> $boolean
69 * =$topic= - the name of the topic in the current CGI query
70 * =$web= - the name of the web in the current CGI query
71 * =$user= - the login name of the user
72 * =$installWeb= - the name of the web the plugin is installed in
74 REQUIRED
76 Called to initialise the plugin. If everything is OK, should return
77 a non-zero value. On non-fatal failure, should write a message
78 using TWiki::Func::writeWarning and return 0. In this case
79 %FAILEDPLUGINS% will indicate which plugins failed.
81 In the case of a catastrophic failure that will prevent the whole
82 installation from working safely, this handler may use 'die', which
83 will be trapped and reported in the browser.
85 You may also call =TWiki::Func::registerTagHandler= here to register
86 a function to handle variables that have standard TWiki syntax - for example,
87 =%MYTAG{"my param" myarg="My Arg"}%. You can also override internal
88 TWiki variable handling functions this way, though this practice is unsupported
89 and highly dangerous!
91 __Note:__ Please align variables names with the Plugin name, e.g. if
92 your Plugin is called FooBarPlugin, name variables FOOBAR and/or
93 FOOBARSOMETHING. This avoids namespace issues.
96 =cut
98 sub initPlugin {
99 my( $topic, $web, $user, $installWeb ) = @_;
101 # check for Plugins.pm versions
102 if( $TWiki::Plugins::VERSION < 1.026 ) {
103 TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" );
104 return 0;
107 # my $setting = $TWiki::cfg{Plugins}{EmptyPlugin}{ExampleSetting} || 0;
108 # $debug = $TWiki::cfg{Plugins}{EmptyPlugin}{Debug} || 0;
111 ## TWiki::Func::registerRESTHandler('example', \&restExample);
113 return 1;
116 =pod
118 ---++ restExample($session) -> $text
120 This is an example of a sub to be called by the =rest= script. The parameter is:
121 * =$session= - The TWiki object associated to this session.
123 Additional parameters can be recovered via de query object in the $session.
125 For more information, check TWiki:TWiki.TWikiScripts#rest
127 *Since:* TWiki::Plugins::VERSION 1.1
129 =cut
131 sub restExample {
132 my ($session) = @_;
133 return "This is an example of a REST invocation\n\n";
137 sub handleRequest {
138 my ($webName, $userName, $thePathInfo, $query) = @_;
140 my $htmlData = <<END;
141 <html>
142 <head>
143 <title>OBJAVI!</title>
144 <meta http-equiv="Content-Type" content="text/html; charset=en_GB" />
145 <meta name="robots" content="noindex" />
146 <link rel="icon" href="http://www.flossmanuals.net/pub/TWiki//FlossSkin2/fl2.ico" type="image/x-icon" /> <link rel="shortcut icon" href="http://www.flossmanuals.net/pub/TWiki/FlossSkin2/fl2.ico" type="image/x-icon" />
147 <link rel=StyleSheet href="http://www.flossmanuals.net/pub/TWiki/FlossSkin2/typography_cover.css" type="text/css" media="screen"/>
148 <style type="text/css">
149 img {
150 display:block;
152 * {margin:0}
153 </style>
154 <style>
155 body
157 margin:0;
158 padding:0
160 #header
162 position:absolute;
163 top:23px;
164 width:100%;
165 background-image:
166 url('http://www.flossmanuals.net/pub/TWiki/FlossSkin2/header_bg.gif');
167 background-position: 600px 0px; /* this places the orange background at
168 an offset, making sure it doesn't underlap the left side of the gif */
169 background-repeat: no-repeat;
171 #header_imagemap
173 margin-left: 352px;
174 border: 0px
176 h1 {
177 color:#ff7f00;
179 h2 {
180 color:#ff7f00;
182 h3 {
183 color:#000000;
185 </style>
186 </head><body background="http://www.flossmanuals.net/pub/TWiki/FlossSkin2/background.gif" style="margin:0;color:#000000;text-decoration:none;">
187 <div id="header"><img id="header_imagemap" width="465" height="95" alt="FlossManuals menu" src='http://www.flossmanuals.net/pub/TWiki/FlossSkin2/objavib_entireheader4.gif' border=0 usemap='#map'></div>
188 </div>
189 <div style="position:absolute;left:270px;top:130px;">
190 <table cellpadding="0" cellspacing="0" summary="" style="table-layout:fixed;width:730;border: 5px solid #666666;padding-right: 0px;padding-left: 0px;padding-bottom: 0px;padding-top: 0px;margin-left : 10px;margin-top:10;background:#FFF7F0;">
191 <tr>
192 <td width=100%>
193 <div class="ds-contentcontainer">
194 <div style="margin-left:1.2em;">
195 <br>
196 <img src="http://www.flossmanuals.net/pub/TWiki/FlossSkin2/objavib.gif">
198 This is the BETA publisher for FLOSS Manuals. Using OBJAVI you can export your manual to print ready source for upload and sale on Lulu.com
199 </p><p>
200 All design is managed by CSS. You can alter the CSS in the below text box and the PDF will be formatted accordingly. </p><p>This site is for testing only.
201 </p>
203 <br>
204 <br>
205 Choose Manual :
206 <form action="" method="POST">
208 <select name="webName">
211 my @websList = TWiki::Func::getListOfWebs("user, public");
213 my @websExclude = split /,/, TWiki::Func::getPreferencesValue( "REMIXWEBEXCLUDE", "Main");
215 foreach my $web (get_unique([\@websList, \@websExclude ])) {
216 $htmlData .= '<option value="'.$web.'">'.$web.'</option>';
220 $htmlData .= <<END;
221 </select>
222 <br/>
223 <br/>
224 Text for Title page :<br>
225 <input type="text" name="title" />
226 <br>
227 <br>
228 Header Text :<br>
229 <input type="text" name="header" />
230 <br><br>
231 License :
232 <br>
233 <input type="text" name="license" value="GPL"/>
234 <br><br>
235 ISBN Number (optional) :
236 <br>
237 <input type="text" name="isbn" />
238 <br>
239 <br>
241 <br>
242 <textarea name="css" cols="60" rows="20">
245 open(DT, "</var/www/floss/pub/TWiki/Pisa/fm-book.css");
246 while(<DT>) {
247 $htmlData .= $_;
249 close(DT);
251 $htmlData .= <<END;
252 </textarea><br/>
253 <button>OBJAVI!</button>
254 </form>
255 </div></td></tr></table>
256 </body>
257 </html>
261 print "Content-Type: text/html\r\n\r\n";
262 print $htmlData;
266 sub _showHeading {
267 my ($web, $num_heading, @chapters) = @_;
269 my $htmlData = "";
271 $htmlData .= "</div></fmsection><p></p>\n";
273 my $n = 0;
274 foreach my $a (@chapters) {
275 my $topicData = TWiki::Func::readTopicText($web, $a);
277 $topicData = TWiki::Func::expandCommonVariables($topicData, "neki topic", $web);
278 $topicData = TWiki::Func::renderText($topicData, $web);
279 my $s = '';
281 foreach my $line (split(/\n/,$topicData)) {
282 # if($line =~ /<h1>(.+)<\/h1>/) {
283 if($line =~ /<h1>(.+)/) {
284 $s .= '<h1><span class="fminitial">'.($num_heading+$n).'.</span> '.$1.'';
285 $s .= "<br><br>";
286 $s .= "\n";
287 $n += 1;
288 } else {
289 $line =~ s/%META:\w+{.*?}%//gs;
290 $line =~ s/src=\"/src=\"\/var\/www/gs;
291 $s .= $line."\n";
295 my $filename;
296 my $filename2;
298 (undef, $filename) = tempfile("/tmp/objaviXXXXX", UNLINK => 0);
299 (undef, $filename2) = tempfile("/tmp/objaviXXXXX", UNLINK => 0);
301 open(DT, ">$filename");
302 print DT $s;
303 close(DT);
305 my @args = ('/usr/bin/tidy', "-q", "-f", "/dev/null", "-o", "$filename2", "$filename");
306 system(@args);
308 my $s2 = "";
310 open(DT, "<$filename2");
311 while(<DT>) {
312 $s2 .= $_;
314 close(DT);
316 unlink($filename);
317 unlink($filename2);
319 # if($s2 =~ /.*\<body\>(.+)\<\/body\>.*/) {
320 # $htmlData .= $1;
321 # } else {
322 # $htmlData .= $s2;
325 $htmlData .= $s;
327 # $n += 1;
330 return $htmlData;
333 sub handlePublish {
334 my ($webName, $userName, $thePathInfo, $query) = @_;
336 my $htmlData = "";
338 #print "Content-type: text/plain\r\n\r\n";
339 print "Content-type: application/pdf\r\nContent-Disposition: attachment; filename=\"fm.pdf\"\r\n\r\n";
341 my $web = $query->param("webName");
342 my $customCss =$query->param("css");
343 my $customTitle =$query->param("title");
344 my $customHeader =$query->param("header");
345 my $customISBN =$query->param("isbn");
346 my $customLicense =$query->param("license");
348 my $tocData = TWiki::Func::readAttachment( $web, "_index", "TOC.txt");
349 my $current_heading = 1;
350 my $num_heading = 0;
352 my @lines = split /\n/, $tocData;
353 my @chapters;
355 for(my $i = 0; $i < (@lines/3); $i++) {
356 my $topicName = $lines[$i*3+1];
357 my $description = $lines[$i*3+2];
358 my $topicID = $lines[$i*3];
360 if($topicID eq "0") {
362 if($current_heading+$num_heading != 1) {
363 $htmlData .= _showHeading($web, $current_heading, @chapters);
364 @chapters = ();
366 $current_heading = $current_heading + $num_heading;
370 # $current_heading = $current_heading + $num_heading;
372 $num_heading = 0;
374 $htmlData .= "<fmsection>\n";
375 $htmlData .= '<h0>'.$description."</h0>\n";
376 $htmlData .= '<div class="fmtoc">';
377 $htmlData .= "\n";
379 } elsif($topicID eq "1") {
380 push @chapters, $topicName;
381 $htmlData .= ($current_heading+$num_heading).'. '.$description.'<br/>';
382 $htmlData .= "\n";
383 $num_heading += 1;
387 $htmlData .= _showHeading($web, $current_heading, @chapters);
389 my $tmpl = TWiki::Func::readTemplate("basic", "pisa");
391 $tmpl =~ s/%%BODY%%/$htmlData/gs;
392 $tmpl =~ s/%%STYLE%%/$customCss/gs;
393 $tmpl =~ s/%%CUSTOMHEADER%%/$customHeader/gs;
394 $tmpl =~ s/%%CUSTOMTITLE%%/$customTitle/gs;
395 $tmpl =~ s/%%CUSTOMISBN%%/$customISBN/gs;
396 $tmpl =~ s/%%CUSTOMLICENSE%%/$customLicense/gs;
399 $tmpl = TWiki::Func::expandCommonVariables($tmpl, "neki topic", $web);
400 $tmpl = TWiki::Func::renderText($tmpl, $web);
402 # my $willchange=$TWiki::cfg{"InstallDirectory"}";
403 my $willchange = "/var/www/floss";
404 my (undef, $filename) = tempfile("$willchange/pub/TWiki/Pisa/objaviXXXXX", UNLINK => 0);
406 open(FH, ">$filename");
408 print FH $tmpl;
410 close(FH);
412 my @args = ('/var/www/floss/pub/TWiki/Pisa/objavi.sh', $filename);
413 #my @args = ('/usr/bin/python2.4', 'pisa/pisa.py', "-x", $filename, "-" );
414 #chdir("$willchange/pub/TWiki/Pisa/");
415 system(@args) or print STDERR "couldn't exec pisa: $!";
416 # disabled for debugging - lf
417 # unlink($filename);
419 #00:43 < fileneed> cd /var/www/floss_clean/pub/TWiki/Pisa
420 #00:43 < fileneed> python2.4 pisa-3.0.19-fm/pisa.py -x index.xhtml