#!/usr/bin/perl
######################
#
# Meganizer - Media Organzier is a media management software
# Copyright (C) 2010 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
#
# 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 3 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, see .
#
######################
use strict;
use XML::DOM;
use Getopt::Std;
our($opt_h, $opt_k);
getopts('hk');
# /usr/bin/plastex --split-level 2 manual
# theme=plain - Seperating Look&Feel and Content completely
#
my $cmdPDFLatex = "/usr/bin/pdflatex";
my $cmdPlastex = "/usr/bin/plastex";
# --theme=plain: don't use an css theme
# --split-level: create an own html file until sections (chapter is 0 and subsection is 2)
# --toc-depth: level which is shown at the table of contents (3 means subsection [different from split-levels])
# --toc-non-files: include also those entries into the toc, which does not have an own html file
my $cmdPlastexArgs = "--theme=plain --split-level 1 --toc-depth 3 --toc-non-files";
my $dirLatex = "./latex";
my $dirPDF = "./pdf";
my $dirQtAssistant = "./qtassistant";
my $dirHtml = "./html";
my $dirTmp = "/tmp/manuals";
# map keywords to titles (can be chapter, section, subsection, subsubsection)
# keywords --> title string
my %mapKeywords;
$mapKeywords{'Edit Dialog'} = "Edit data around Media";
$mapKeywords{'Setupwizard'} = "Configure Meganizer";
# $mapKeywords{'Add Medium'} = "Add new Media";
$mapKeywords{'Settings'} = "Configure Meganizer";
$mapKeywords{'Connect Search'} = "Using external Datasources (like Amazon)";
$mapKeywords{'External MimeTypes'} = "Handling of external MimeTypes";
$mapKeywords{'DBBackup'} = "Backup and restore the Database";
$mapKeywords{'Edit Medium'} = "Edit Media";
$mapKeywords{'MINs Manufacturers'} = "Manufacturers with different media types and related to MINs";
if($opt_h){
# print Help!
showHelp();
}
# check if all needed apps are there
my $error = 0;
if(! -e $cmdPDFLatex){
$error = 1;
print "Could not find PDFLatex at: ". $cmdPDFLatex ."\n";
}
if(! -e $cmdPlastex){
$error = 1;
print "Could not find plasTex at: ". $cmdPlastex ."\n";
}
if($error > 0){
exit 0;
}
print "Search source files at ". $dirLatex ."\n";
opendir(DIRLATEX, $dirLatex) || die "Could not open directory ". $!;
my @files;
while(my $file = readdir(DIRLATEX)){
if($file =~ m/([\w\W]+)\.tex$/){
push(@files, $1);
}
}
close DIRLATEX;
my $laenge = @files;
if($laenge < 1){
exit 1;
}
print " Found ". $laenge ." tex-files\n";
if(! -d $dirTmp ){
print "Create ". $dirTmp ." - ";
system("mkdir -p ". $dirTmp);
if(! -d $dirTmp){
print "FAILED\n";
exit 1;
}
print "OK\n";
}
if(!$opt_k){
print "Creating PDF Manuals\n";
foreach my $file (@files) {
print " ". $file ." - ";
# cleanup output before creating the new one
my $outFile = $dirPDF ."/". $file .".pdf";
system("rm ". $outFile ." > /dev/null 2>&1");
my $exec = $cmdPDFLatex ." -halt-on-error -output-directory=". $dirTmp ." ". $dirLatex ."/". $file ." > /dev/null";
system($exec);
# execute it a second time, to get cross references right
system($exec);
system("cp ". $dirTmp ."/". $file .".pdf ". $outFile ." > /dev/null 2>&1");
if(! -e $outFile){
print "FAILED\n";
exit 1;
}
print "OK\n";
}
}
print "Creating HTML Manuals\n";
foreach my $file (@files) {
print " ". $file ." - ";
my $outDir = $dirHtml ."/". $file;
if(! -d $outDir){
system("mkdir -p ". $outDir);
if(! -d $outDir){
print "FAILED\n Could not create dir ". $outDir ."\n";
exit 1;
}
}
# cleanup outdir before producing new output
system("rm ". $outDir ."/*.* > /dev/null 2>&1");
system("rm ". $outDir ."/*images/ > /dev/null 2>&1");
system($cmdPlastex ." ". $cmdPlastexArgs ." -d ". $dirTmp ." ". $dirLatex ."/". $file ." > /dev/null 2>&1");
system("cp ". $dirTmp ."/*.html ". $outDir ." > /dev/null 2>&1");
system("cp -a ". $dirTmp ."/images ". $outDir ." > /dev/null 2>&1");
system("cp -a ". $dirTmp ."/icons ". $outDir ." > /dev/null 2>&1");
system("cp -a ". $dirTmp ."/styles ". $outDir ." > /dev/null 2>&1");
# cleanup paux files
system("rm ./*.paux");
if(! -e $outDir ."/index.html"){
print "FAILED\n";
exit 1;
}
print "OK\n";
}
if($opt_k){
print "Generate keyword mapping\n";
}
else{
print " Generate keyword mapping - ";
}
my %mapKeywordSite = buildKeywordPageList($dirHtml, \@files);
if($opt_k){
foreach my $key (sort(keys(%mapKeywords))){
print $key ." --> ". $mapKeywords{$key} ." --> ". $mapKeywordSite{$key} ."\n";
}
exit 1;
}
elsif(scalar(keys(%mapKeywords)) != scalar(keys(%mapKeywordSite))){
print "FAILED (In: ". scalar(keys(%mapKeywords)) ." Out:". scalar(keys(%mapKeywordSite)) .")\n";
}
else{
print "OK (Indexes: ". scalar(keys(%mapKeywordSite)) .")\n";
}
print "Creating QtAssistant help collection project files (qhcp)\n";
print " Generate Qt help project (qhp)\n";
foreach my $file (@files) {
print " ". $file ."\n";
print " Generate table of contents - ";
my $toc = generateTOC($dirHtml ."/". $file ."/index.html");
if($toc eq ""){
print "FAILED\n";
exit 1;
}
print "OK\n";
# we put it into the correspsonding html directory,
# to get it independent from path to html directory
my $outFile = $dirHtml ."/". $file ."/". $file .".qhp";
print " Generate qhp file - ";
my $result = generateQhp($outFile, $toc, \%mapKeywordSite);
if($result ne ""){
print "FAILED (". $result .")\n";
exit 1;
}
print "OK\n";
print " Generate qhcp file - ";
my @inFiles = (".". $outFile);
$outFile = $dirQtAssistant ."/". $file .".qhcp";
my $result = generateQhcp($outFile, \@inFiles,
"qthelp://de.engsas.meganizer/meganizer/index.html",
"../". $dirLatex ."/about.txt",
"../../../../src/meganizer/icons/128x128/meganizer.png"
);
if($result ne ""){
print "FAILED (". $result .")\n";
exit 1;
}
print "OK\n";
}
exit 0;
# print "Copying application icon into image folder\n";
# system("cp ../../../src/meganizer/icons/128x128/meganizer.png ./". $fileLatex ."/icons/");
#
# print "Copying about.txt\n";
# system("/bin/touch ./". $fileLatex ."/about.txt");
sub generateTOC {
my $indexHtml = shift;
my $fileList = shift || 0;
# reading file content
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile($indexHtml);
# parsing index.html
foreach my $node ($doc->getElementsByTagName("div")){
my $class = $node->getAttribute("class");
# this is the toc part
if($class eq "contents document-contents"){
foreach my $elem ($node->getChildNodes){
if($elem->getNodeType == XML::DOM::ELEMENT_NODE){
# this ul contains the whole toc
if($elem->getTagName eq "ul"){
my $result = "";
foreach my $li ($elem->getChildNodes){
if($li->getNodeType == XML::DOM::ELEMENT_NODE){
if($li->getTagName eq "li"){
$result .= parseTOCLayer($li, 4, $fileList);
}
}
}
return $result;
}
}
}
}
}
return "";
}
sub parseTOCLayer {
my $elem = shift;
my $level = shift || 0;
my $fileList = shift || 0;
my $result = "";
my $foundChild = 0;
foreach my $node ($elem->getChildNodes){
if($node->getNodeType == XML::DOM::ELEMENT_NODE){
if($node->getTagName eq "a"){
if($fileList > 0){
$result .= "|". $node->getAttribute("href");
}
else{
for(my $i = 0; $i < $level; $i++){
$result .= " ";
}
$result .= "getFirstChild->getData ."\"";
$result .= " ref=\"./". $node->getAttribute("href") ."\">";
}
}
if($node->getTagName eq "ul"){
foreach my $child ($node->getChildNodes){
if($child->getNodeType == XML::DOM::ELEMENT_NODE){
if($child->getTagName eq "li"){
# only add it on first child
if(($foundChild < 1) && ($fileList < 1)){
$result .= "\n";
}
$result .= parseTOCLayer($child, $level + 1, $fileList);
$foundChild = 1;
}
}
}
}
}
}
if(($fileList == 0) && ($result ne "")){
if($foundChild > 0){
for(my $i = 0; $i < $level; $i++){
$result .= " ";
}
}
$result .= "\n";
}
return $result;
}
sub generateQhp {
my $outFile = shift;
my $toc = shift;
my %indexes = %{(shift)};
if($toc eq ""){
return "TOC should not be empty!";
}
open OUTPUT, ">:utf8", $outFile || return "Can not open file ". $outFile .": ". $!;
# printing header
print OUTPUT "\n";
print OUTPUT "\n";
print OUTPUT " de.engsas.meganizer\n";
print OUTPUT " meganizer\n";
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT $toc;
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " \n";
foreach my $key (keys(%indexes)){
print OUTPUT " \n";
}
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " ./*.html\n";
print OUTPUT " ./images/*.*\n";
# print OUTPUT " ./icons/*.*\n";
# print OUTPUT " ./styles/*.css\n";
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT "\n";
close OUTPUT;
return "";
}
sub generateQhcp {
my $outFile = shift;
my @inFiles = @{(shift)};
my $startPage = shift;
my $aboutFile = shift;
my $iconFile = shift;
my $laenge = @inFiles;
if($laenge < 1){
return "At least one qhp file is needed!";
}
open OUTPUT, ">:utf8", $outFile || return "Could not open ". $outFile;
print OUTPUT "\n";
print OUTPUT "\n";
print OUTPUT " \n";
print OUTPUT " Meganizer - Media Organizer\n";
print OUTPUT " ". $iconFile ."\n";
print OUTPUT " EngSaS/Meganizer\n";
print OUTPUT " ". $startPage ."\n";
print OUTPUT " \n";
print OUTPUT " About Meganizer\n";
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " ". $aboutFile ."\n";
print OUTPUT " ". $iconFile ."\n";
print OUTPUT " \n";
print OUTPUT " false\n";
print OUTPUT " false\n";
print OUTPUT " false\n";
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " \n";
foreach my $inFile (@inFiles){
if($inFile =~ m/([\w\d\s\-_]+)\.qhp$/){
print OUTPUT " ". $inFile ."\n";
print OUTPUT " \n";
}
}
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT " \n";
foreach my $inFile (@inFiles){
if($inFile =~ m/([\w\d\s\-_]+)\.qhp$/){
print OUTPUT " ". $1 .".qch\n";
}
}
print OUTPUT " \n";
print OUTPUT " \n";
print OUTPUT "\n";
close OTUPUT;
return "";
}
sub buildKeywordPageList {
my $dirHtml = shift;
my @inFiles = @{(shift)};
my %mapKeywordSite;
# chossing english version
my $inputFile = "";
foreach my $inFile (@inFiles){
if($inFile =~ m/_en$/){
$inputFile = $inFile;
last;
}
if(index($inFile, "_") < 0){
$inputFile = $inFile;
last;
}
}
$dirHtml .= "/". $inputFile;
my $indexHtml = $dirHtml ."/index.html";
if(! -e $indexHtml){
return %mapKeywordSite;
}
# getting a list with all referenced html-files
my $toc = generateTOC($indexHtml);
if($toc eq ""){
return %mapKeywordSite;
}
$toc =~ s/\\n//g;
$toc =~ s/([\s]{2,})//g;
foreach my $key (keys(%mapKeywords)){
# \Q start comment out special characters
# \E end comment out special characters
if($toc =~ m/\Q$mapKeywords{$key}\E" ref="([^"]+)/){
$mapKeywordSite{$key} = $1;
}
}
return %mapKeywordSite;
}
sub showHelp {
print "manual2html [-h, -k]\n\n";
print "This script create pdf and html manual out of the latex files.\n";
print "It also creates some Qt Help files, which are needed to run QtAssistant.\n\n";
print "Usage:\n";
print " -h: Shows this help\n";
print " -k: Show all known keywords\n";
exit 1;
}