2005-11-06 15:44:40 +00:00
# Copyright (C) 2005 Fletcher T. Penney <fletcher@freeshell.org>
#
# 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.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307 USA
#
2015-03-29 23:27:00 +02:00
use strict ;
2015-03-27 03:01:01 +02:00
2014-08-21 22:23:23 +02:00
AddModuleDescription ( 'pdf.pl' , 'PDF Module' ) ;
2005-11-06 15:46:00 +00:00
2015-04-11 23:41:33 +03:00
* PdfOldDoBrowseRequest = \ & DoBrowseRequest ;
* DoBrowseRequest = \ & PdfDoBrowseRequest ;
2005-11-06 15:44:40 +00:00
2015-04-10 13:31:28 +03:00
our ( $ q , % Page , $ OpenPageName , $ ModuleDir , $ ScriptName , $ SiteName ) ;
our ( @ NoLinkToPdf , $ pdfDirectory , $ pdfProcessCommand , $ tempBaseDirectory ) ; # TODO use CamelCase (first capital letter) for public vars
2005-11-06 15:44:40 +00:00
# These variables must be configured properly!
2005-11-06 16:35:15 +00:00
$ pdfProcessCommand = "/path/to/your/pdflatexscript"
unless defined $ pdfProcessCommand ;
2005-11-06 15:44:40 +00:00
# Note - this script will vary from machine to machine
# The key is to set up pdflatex to use the appropriate texmf folder
# Also, I recommend running three times, if you use indexing, etc
# You can also try to get latexmk to work (I could not)
# My file:
# #!/bin/sh
#
# # Use MultiMarkdown to automagically convert a text file to html and pdf
#
# IFS=' '
2015-03-27 03:01:01 +02:00
#
2005-11-06 15:44:40 +00:00
# export TEXINPUTS=/arpa/af/f/fletcher/texmf//:
2015-03-27 03:01:01 +02:00
#
2005-11-06 15:44:40 +00:00
# for filename in "$@"
# do
2005-11-06 16:35:15 +00:00
#
# # Use XSLT to process XHTML to LaTeX
# /usr/pkg/bin/xsltproc /www/af/f/fletcher/wiki/wikidb/modules/Markdown/xhtml2article.xslt "$filename" > "${filename%.*}.tex"
2015-03-27 03:01:01 +02:00
#
2005-11-06 15:44:40 +00:00
# /usr/pkg/bin/pdflatex "$filename"
# /usr/pkg/bin/pdflatex "$filename"
# /usr/pkg/bin/pdflatex "$filename"
#
# done
$ tempBaseDirectory = "$ModuleDir/Markdown/temp"
unless defined $ tempBaseDirectory ;
$ pdfDirectory = "/path/to/your/pdf/directory"
unless defined $ pdfDirectory ;
# Do not need to change these
2015-03-27 03:01:01 +02:00
my $ tempDirectory = "" ;
2005-11-06 15:44:40 +00:00
sub PdfDoBrowseRequest {
my $ id = GetId ( ) ;
$ id = FreeToNormal ( $ id ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
if ( GetParam ( 'pdf' , '' ) ) {
2006-01-08 20:48:00 +00:00
# Strip `.pdf` if present
# This does cause problems if you have a page name
# that ends in `.pdf`...
2015-03-27 03:01:01 +02:00
2006-01-08 20:48:00 +00:00
$ id =~ s/\.pdf$// ;
2005-11-06 15:44:40 +00:00
# Isolate ourselves
local % Page ;
local $ OpenPageName = '' ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
OpenPage ( $ id ) ;
2015-03-27 03:01:01 +02:00
# Create a working directory
2005-11-06 15:44:40 +00:00
CreateTempDirectory ( $ id ) ;
# Isolate our output
outputHTML ( $ id ) ;
2015-03-29 23:27:00 +02:00
createPDF ( $ id ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
if ( - f "$pdfDirectory/$id.pdf" ) {
# Remove working directory/lockfile
system ( "/bin/rm -rf \"$tempDirectory\"" ) ;
# pdf in place, redirect browser to download
2006-01-08 20:48:00 +00:00
my % headers = ( - uri = > "$ScriptName/pdf/$id.pdf" ) ;
2005-11-06 15:44:40 +00:00
print $ q - > redirect ( % headers ) ;
} else {
# Something happened - pdf not in place
# Leave lockfile to prevent the hard-headed from
# killing your server, and for debugging
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
ReportError ( Ts ( 'There was an error generating the pdf for %s. Please report this to webmaster, but do not try to download again as it will not work.' , $ id ) ) ;
}
} else {
& PdfOldDoBrowseRequest ( ) ;
}
}
# Create an HTML file with just the content of the page body
sub outputHTML {
2015-03-29 23:27:00 +02:00
my ( $ id ) = @ _ ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
local * STDERR ;
2015-05-02 03:19:25 +03:00
open ( STDERR , '>' , '/dev/null' ) ;
2005-11-06 15:44:40 +00:00
# Fix Markdown
2015-05-17 03:35:35 +03:00
my $ result = ToString sub { print PageHtml ( $ id ) } ;
2005-11-06 15:44:40 +00:00
2015-05-02 03:19:25 +03:00
open ( my $ FILE , '>' , "$tempDirectory/temp.html" ) or ReportError ( Ts ( 'Cannot write %s' , "temp.html" ) ) ;
2015-03-27 03:01:01 +02:00
2015-05-02 03:19:25 +03:00
print $ FILE qq{ <?xml version="1.0" encoding="UTF-8" ?>
2005-11-06 15:44:40 +00:00
<html>
<head> } ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
# Create meta-data (you can customize this)
2015-05-02 03:19:25 +03:00
print $ FILE qq{ <title>$OpenPageName</title> } ;
print $ FILE qq{ <meta name="author" content="$SiteName"/> } ;
print $ FILE qq{ <meta name="copyright" content="2005. This work is licensed under a Creative Commons License: http://creativecommons.org/licenses/by-sa/2.5/"/> } ;
print $ FILE qq{ <meta name="XMP" content="CCAttributionShareAlike"/> } ;
print $ FILE "</head><body>" ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
# Output the body and close the file
2015-05-02 03:19:25 +03:00
print $ FILE $ result ;
print $ FILE "\n</body>\n</html>\n" ;
close $ FILE ;
2005-11-06 15:44:40 +00:00
}
# Run a series of steps to convert XHTML to pdf
# You may have to change this based on your system
sub createPDF {
2015-03-29 23:27:00 +02:00
my $ id = shift ;
2005-11-06 15:44:40 +00:00
local * STDOUT ;
2015-05-02 03:19:25 +03:00
open ( STDOUT , '>' , '/dev/null' ) ;
2005-11-06 15:44:40 +00:00
local * STDERR ;
2015-05-02 03:19:25 +03:00
open ( STDERR , '>' , '/dev/null' ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
# Run latex script and copy pdf to final location
2005-11-06 16:35:15 +00:00
system ( "cd \"$tempDirectory\"; \"$pdfProcessCommand\" temp.html > /dev/null; /bin/cp temp.pdf \"$pdfDirectory/$id.pdf\" " ) ;
2005-11-06 15:44:40 +00:00
}
# If we save a new version of a file, we want to delete the old pdf
# To save wasted time, don't recreate it until called for
2015-04-11 23:41:33 +03:00
* PdfOldDoPost = \ & DoPost ;
* DoPost = \ & PdfNewDoPost ;
2005-11-06 15:44:40 +00:00
sub PdfNewDoPost {
my $ id = FreeToNormal ( shift ) ;
unlink ( "$pdfDirectory/$id.pdf" ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
PdfOldDoPost ( $ id ) ;
}
sub CreateTempDirectory {
my ( $ id ) = @ _ ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
$ tempDirectory = "$tempBaseDirectory/$id" ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
# Create the general directory if it doesn't exist
CreateDir ( $ tempBaseDirectory ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
# Now, create a temp directory for this page
# If it exists, then someone else is generating pdf - give error message
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
if ( - d $ tempDirectory ) {
# Someone else is creating this pdf
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
ReportError ( Ts ( 'Someone else is generating a pdf for %s. Please wait a minute and then try again.' , $ id ) ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
}
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
CreateDir ( $ tempDirectory ) ;
}
# Fix Wiki Links - they have to be fully qualified
2015-04-11 23:41:33 +03:00
* PdfOldCreateWikiLink = \ & CreateWikiLink ;
* CreateWikiLink = \ & PdfNewCreateWikiLink ;
2005-11-06 15:44:40 +00:00
sub PdfNewCreateWikiLink {
my $ title = shift ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
my $ rawlink = PdfOldCreateWikiLink ( $ title ) ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
if ( $ rawlink =~ /http\:\/\// ) {
return $ rawlink ;
} else {
$ rawlink =~ s/\((.*)\)/($ScriptName\/$1)/ ;
2015-03-27 03:01:01 +02:00
2005-11-06 15:44:40 +00:00
return $ rawlink ;
}
2005-11-06 18:54:15 +00:00
}
2015-04-11 23:41:33 +03:00
* PdfOldGetFooterLinks = \ & GetFooterLinks ;
* GetFooterLinks = \ & PdfNewGetFooterLinks ;
2005-11-06 18:54:15 +00:00
sub PdfNewGetFooterLinks {
my ( $ id , $ rev ) = @ _ ;
2015-03-27 03:01:01 +02:00
my $ result = PdfOldGetFooterLinks ( $ id , $ rev ) ;
2005-11-06 18:54:15 +00:00
2015-03-27 03:01:01 +02:00
push ( @ NoLinkToPdf , "" ) ;
2005-11-06 18:54:15 +00:00
foreach my $ page ( @ NoLinkToPdf ) {
if ( $ id =~ /^$page$/ ) {
return $ result ;
}
}
2015-03-27 03:01:01 +02:00
2006-01-08 20:48:00 +00:00
return $ result . "<br/>" . ScriptLink ( "pdf/$id.pdf" , T ( 'Download this page as PDF' ) ) ;
2012-04-19 02:18:45 +02:00
}