瀏覽代碼

Add an option to dump the default toc stylesheet

Jakob Truelsen 15 年之前
父節點
當前提交
893f7ae368
共有 6 個文件被更改,包括 126 次插入19 次删除
  1. 14 3
      src/arguments.cc
  2. 12 7
      src/pageconverter.cc
  3. 2 0
      src/pageconverter.hh
  4. 3 0
      src/tocprinter.hh
  5. 80 0
      src/tocstylesheet.cc
  6. 15 9
      wkhtmltopdf.pro

+ 14 - 3
src/arguments.cc

@@ -15,6 +15,8 @@
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "commandlineparser_p.hh"
+#include <QFile>
+#include "pageconverter.hh"
 #include <qglobal.h>
 
 /*!
@@ -357,6 +359,17 @@ struct HelpFunc {
 	}
 };
 
+
+struct DefaultTocFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p, Page &) {
+		QFile file;
+		file.open(0, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
+		QTextStream stream(&file);
+		wkhtmltopdf::dumbDefaultTOCStyleSheet(stream);
+		exit(0);
+	}
+};
+
 /*!
   Lambda: Call the man method
 */
@@ -517,8 +530,6 @@ CommandLineParserPrivate::CommandLineParserPrivate(Global & s, QList<Page> & ps)
  	addarg("page-width", 0, "Page width", new UnitRealSetter(s.size.width,"unitreal"));
 
 // 	addarg("book",'b',"Set the options one would usually set when printing a book", new Caller<BookFunc>());
-// 	addarg("cover",0,"Use html document as cover. It will be inserted before the toc with no headers and footers",new QStrSetter(s.cover,"url",""));
-	
 	addarg("cookie-jar", 0, "Read and write cookies from and to the supplied cookie jar file", new QStrSetter(s.cookieJar, "path") );
 
 
@@ -537,7 +548,7 @@ CommandLineParserPrivate::CommandLineParserPrivate(Global & s, QList<Page> & ps)
  	addarg("no-outline",0,"Do not put an outline into the pdf", new ConstSetter<bool>(s.outline,false));
  	addarg("outline-depth",0,"Set the depth of the outline", new IntSetter(s.outlineDepth,"level"));
  	addarg("dump-outline",0,"Dump the outline to a file",new QStrSetter(s.dumpOutline,"file"));
-
+	addarg("dump-default-toc-xsl",0,"Dump the default TOC xsl style sheet to stdout", new Caller<DefaultTocFunc>());
 
 	section("Page options");
 	mode(page);

+ 12 - 7
src/pageconverter.cc

@@ -36,6 +36,16 @@ using namespace wkhtmltopdf::settings;
 
 QMap<QWebPage *, PageObject *> PageObject::webPageToObject;
 
+
+struct StreamDumper {
+	QFile out;
+	QTextStream stream;
+
+	StreamDumper(const QString & path): out(path), stream(&out) {
+		out.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
+		stream.setCodec("UTF-8");
+	}
+};
  
 /*!
   \file pageconverter.hh
@@ -585,7 +595,6 @@ void PageConverterPrivate::printPage(bool ok) {
 								elm.attribute("readonly") == "readonly");
 						}
 					}
-
 					for(QHash<QString, QWebElement>::iterator i=myAnchors[p+1].begin();
 						i != myAnchors[p+1].end(); ++i) {
 						QRectF r = wp.elementLocation(i.value()).second;
@@ -611,13 +620,9 @@ void PageConverterPrivate::printPage(bool ok) {
  	}
 	outline->printOutline(printer);
 
-
 	if (!settings.dumpOutline.isEmpty()) {
-		QFile out(settings.dumpOutline);
-		out.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
-		QTextStream stream(&out);
-		stream.setCodec("UTF-8");
-		outline->dump(stream, "");
+		StreamDumper sd(settings.dumpOutline);
+		outline->dump(sd.stream, "");
 	}
 
  	painter->end();

+ 2 - 0
src/pageconverter.hh

@@ -20,6 +20,8 @@
 #include <QObject>
 namespace wkhtmltopdf {
 
+void dumbDefaultTOCStyleSheet(QTextStream & stream);
+
 class PageConverterPrivate;
 class PageConverter: public QObject {
 	Q_OBJECT

+ 3 - 0
src/tocprinter.hh

@@ -21,6 +21,8 @@
 #include <QPainter>
 #include <QPrinter>
 
+namespace wkhtmltopdf {
+
 class TocPrinterPrivate;
 
 class TocPrinter {
@@ -34,5 +36,6 @@ private:
 	TocPrinterPrivate * d;
 };
 
+}
 #endif //__EXTENSIVE_WKHTMLTOPDF_QT_HACK__
 #endif //__TOCPRINTER_HH

+ 80 - 0
src/tocstylesheet.cc

@@ -0,0 +1,80 @@
+// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
+// vi:set ts=4 sts=4 sw=4 noet :
+// This file is part of wkhtmltopdf.
+//
+// wkhtmltopdf 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.
+//
+// wkhtmltopdf 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 wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
+#include "outline_p.hh"
+#include <QTextOStream>
+
+namespace wkhtmltopdf {
+
+void dumbDefaultTOCStyleSheet(QTextStream & stream) {
+    stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl
+		   << "<xsl:stylesheet version=\"1.0\"" << endl
+		   << "	        xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"" << endl
+		   << "                xmlns:outline=\"http://code.google.com/p/wkhtmltopdf/outline\"" << endl
+		   << "                xmlns=\"http://www.w3.org/1999/xhtml\">" << endl
+		   << "  <xsl:template match=\"outline:outline\">" << endl
+		   << "    <html>" << endl
+		   << "      <head>" << endl
+		   << "        <title><xsl:value-of select=\"@title\" /></title>" << endl
+		   << "        <style>" << endl
+		   << "          h1 {" << endl
+		   << "            text-align: center;" << endl
+		   << "            font-size: 20px;" << endl
+		   << "            font-family: ariel;" << endl
+		   << "          }" << endl
+		   << "          div {border-bottom: 1px dashed rgb(200,200,200);}" << endl
+		   << "          span {float: right;}" << endl
+		   << "          li {list-style: none;}" << endl
+		   << "          ul {" << endl
+		   << "            font-size: 20px;" << endl
+		   << "            font-family: ariel;" << endl
+		   << "          }" << endl
+		   << "          ul ul {font-size: 80%; }" << endl
+		   << "          ul {padding-left: 0em;}" << endl
+		   << "          ul ul {padding-left: 1em;}" << endl
+		   << "          a {text-decoration:none; color: black;}" << endl
+		   << "        </style>" << endl
+		   << "      </head>" << endl
+		   << "      <body>" << endl
+		   << "        <h1><xsl:value-of select=\"@title\" /></h1>" << endl
+		   << "        <ul><xsl:apply-templates select=\"outline:item\"/></ul>" << endl
+		   << "      </body>" << endl
+		   << "    </html>" << endl
+		   << "  </xsl:template>" << endl
+		   << "  <xsl:template match=\"outline:item\">" << endl
+		   << "    <li>" << endl
+		   << "      <div>" << endl
+		   << "        <a>" << endl
+		   << "          <xsl:if test=\"@link\">" << endl
+		   << "            <xsl:attribute name=\"href\"><xsl:value-of select=\"@link\"/></xsl:attribute>" << endl
+		   << "          </xsl:if>" << endl
+		   << "          <xsl:if test=\"@backLink\">" << endl
+		   << "            <xsl:attribute name=\"name\"><xsl:value-of select=\"@backLink\"/></xsl:attribute>" << endl
+		   << "          </xsl:if>" << endl
+		   << "          <xsl:value-of select=\"@title\" /> " << endl
+		   << "        </a>" << endl
+		   << "        <span> <xsl:value-of select=\"@page\" /> </span>" << endl
+		   << "      </div>" << endl
+		   << "      <ul>" << endl
+		   << "        <xsl:apply-templates select=\"outline:item\"/>" << endl
+		   << "      </ul>" << endl
+		   << "    </li>" << endl
+		   << "  </xsl:template>" << endl
+		   << "</xsl:stylesheet>" << endl;
+}
+
+}
+

+ 15 - 9
wkhtmltopdf.pro

@@ -40,7 +40,7 @@ readme.target=README
 readme.commands=./wkhtmltopdf --readme > README
 readme.depends=wkhtmltopdf
 
-QMAKE_EXTRA_UNIX_TARGETS += readme
+QMAKE_EXTRA_TARGETS += readme
 
 unix {
     man.target=wkhtmltopdf.1.gz
@@ -52,7 +52,7 @@ unix {
     manins.files=wkhtmltopdf.1.gz
     manins.path=$$INSTALLBASE/share/man/man1
 
-    QMAKE_EXTRA_UNIX_TARGETS += manins man
+    QMAKE_EXTRA_TARGETS += manins man
     INSTALLS += manins
 }
 
@@ -66,12 +66,18 @@ target.path=$$INSTALLBASE/bin
 
 QT += webkit network
 
-# Input
-HEADERS += src/pageconverter_p.hh src/pageconverter.hh \
-           src/multipageloader_p.hh src/multipageloader.hh src/progressfeedback.hh
+#Libaray part
+HEADERS += src/pageconverter.hh src/pageconverter_p.hh \
+           src/multipageloader_p.hh src/multipageloader.hh
+
+SOURCES += src/tempfile.cc src/settings.cc src/pageconverter.cc \
+           src/multipageloader.cc src/outline.cc src/tocstylesheet.cc
+
+#Application part
+
+HEADERS += src/progressfeedback.hh
 
 SOURCES += src/wkhtmltopdf.cc src/arguments.cc src/commandlineparser.cc \
-           src/docparts.cc src/outputter.cc src/manoutputter.cc src/settings.cc \
-           src/htmloutputter.cc src/textoutputter.cc src/tempfile.cc \
-           src/multipageloader.cc src/pageconverter.cc src/progressfeedback.cc \
-           src/outline.cc src/tocprinter.cc
+           src/docparts.cc src/outputter.cc src/manoutputter.cc \
+           src/htmloutputter.cc src/textoutputter.cc src/progressfeedback.cc
+