浏览代码

Dump outline as xml in preperation for html TOC

Jakob Truelsen 15 年之前
父节点
当前提交
a1367d763c
共有 4 个文件被更改,包括 42 次插入27 次删除
  1. 29 23
      src/outline.cc
  2. 2 1
      src/outline.hh
  3. 1 2
      src/outline_p.hh
  4. 10 1
      src/pageconverter.cc

+ 29 - 23
src/outline.cc

@@ -74,32 +74,38 @@ void OutlinePrivate::outlineChildren(OutlineItem * item, QPrinter * printer, int
 	}
 	}
 }
 }
 
 
-/* dump outline */
-void OutlinePrivate::dumpOutlineChildren(OutlineItem * item, ofstream &dumpfile, int level) {
-	if (level){
-		dumpfile << "Level: " << level << "  ";
-		dumpfile << "Page: "  << item->page << "  ";
-		dumpfile << "Title: " << item->value.toUtf8().toPercentEncoding().data();
-		//dumpfile << item->anchor.toUtf8().toPercentEncoding() << " ";
-		dumpfile << "\n";
-	}
-	if (level + 1 <= settings.outlineDepth){
-		foreach (OutlineItem * i, item->children) {
-			dumpOutlineChildren(i, dumpfile, level + 1);
+QString escape(QString & str) {
+	return str.replace('&',"&amp;")
+		.replace('<',"&lt;")
+		.replace('>',"&gt;")
+		.replace('"',"&quot;")
+		.replace('\'',"&apos;");
+}
+
+void OutlinePrivate::dumpChildren(QTextStream & stream, const QList<OutlineItem *> & items, int level) const {
+	foreach(OutlineItem * item, items) {
+		for(int i=0; i < level; ++i) stream << "  ";
+		stream << "<item title=\"" << escape(item->value) << "\" page=\"" << item->page << "\"";
+
+		if (item->children.empty())
+			stream << "/>" << endl;
+		else {
+			stream << ">" << endl;
+			dumpChildren(stream, item->children, level+1);
+			for(int i=0; i < level; ++i) stream << "  ";
+			stream << "</item>" << endl;
 		}
 		}
 	}
 	}
 }
 }
 
 
-
-void Outline::dumpOutline() {
-	if (d->settings.dumpOutline.isEmpty()) return;
-	char * filename = d->settings.dumpOutline.toUtf8().data();
-	ofstream dumpfile(filename, ios::out);
-	dumpfile << "Pages: " << pageCount() << "\n";
-	foreach(OutlineItem * i, d->documentOutlines){
-		d->dumpOutlineChildren(i, dumpfile, 0);
-	}
-	dumpfile.close();
+void Outline::dump(QTextStream & stream, const QString & xsl) const {
+	stream << "<?xml version=\"1.0\" encoding=\"UTF-8-\"?>" << endl;
+	QString x = xsl;
+	if (!x.isEmpty())
+		stream << "<?xml-stylesheet type=\"text/xsl\" href=\"" << escape(x) << "\"?>" << endl;
+	stream << "<outline xmlns=\"http://code.google.com/p/wkhtmltopdf/outline\">" << endl;
+	d->dumpChildren(stream, d->documentOutlines, 1);
+	stream << "</outline>" << endl;
 }
 }
 
 
 
 
@@ -149,7 +155,7 @@ void Outline::addWebPage(const QString & name, QWebPrinter & wp, QWebFrame * fra
 		uint level = element.tagName().mid(1).toInt();
 		uint level = element.tagName().mid(1).toInt();
 		OutlineItem * item = new OutlineItem();
 		OutlineItem * item = new OutlineItem();
 		item->page = d->pageCount + i.key().first;
 		item->page = d->pageCount + i.key().first;
-		item->value = element.toPlainText().replace("\n", " ");
+		item->value = element.toPlainText().replace("\n", " ").trimmed();
 		item->element = element;
 		item->element = element;
 		item->display = ps.includeInOutline;
 		item->display = ps.includeInOutline;
 		item->anchor = QString("__WKANCHOR_")+QString::number(d->anchorCounter++,36);
 		item->anchor = QString("__WKANCHOR_")+QString::number(d->anchorCounter++,36);

+ 2 - 1
src/outline.hh

@@ -34,7 +34,8 @@ public:
 	void fillAnchors(int d, QHash<QString, QWebElement> & anchors);
 	void fillAnchors(int d, QHash<QString, QWebElement> & anchors);
 	int pageCount();
 	int pageCount();
 	void printOutline(QPrinter * printer);
 	void printOutline(QPrinter * printer);
-	void dumpOutline();
+
+	void dump(QTextStream & stream, const QString & xsl) const;
 private:
 private:
 	OutlinePrivate * d;
 	OutlinePrivate * d;
 	friend class TocPrinter;
 	friend class TocPrinter;

+ 1 - 2
src/outline_p.hh

@@ -16,7 +16,6 @@
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef __OUTLINE_P_HH
 #ifndef __OUTLINE_P_HH
 #define __OUTLINE_P_HH
 #define __OUTLINE_P_HH
-#include <fstream>
 #include "outline.hh"
 #include "outline.hh"
 #ifdef __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
 #ifdef __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
 namespace wkhtmltopdf {
 namespace wkhtmltopdf {
@@ -48,7 +47,7 @@ public:
 	void fillChildAnchors(OutlineItem * item, QHash<QString, QWebElement> & anchors);
 	void fillChildAnchors(OutlineItem * item, QHash<QString, QWebElement> & anchors);
 	void outlineChildren(OutlineItem * item, QPrinter * printer, int level);
 	void outlineChildren(OutlineItem * item, QPrinter * printer, int level);
 	void buildHFCache(OutlineItem * i, int level);
 	void buildHFCache(OutlineItem * i, int level);
-	void dumpOutlineChildren(OutlineItem * item, std::ofstream &dumpfile, int level);
+	void dumpChildren(QTextStream & stream, const QList<OutlineItem *> & items, int level) const;
 };
 };
 
 
 }
 }

+ 10 - 1
src/pageconverter.cc

@@ -610,7 +610,16 @@ void PageConverterPrivate::printPage(bool ok) {
 		}
 		}
  	}
  	}
 	outline->printOutline(printer);
 	outline->printOutline(printer);
-	outline->dumpOutline();
+
+
+	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, "");
+	}
+
  	painter->end();
  	painter->end();
 #endif
 #endif
 	if (settings.out == "-" && lout != "/dev/stdout") {
 	if (settings.out == "-" && lout != "/dev/stdout") {