summaryrefslogtreecommitdiffstats
path: root/src/exporters/htmlbookexporter.cpp
blob: 93440a57c64489d71b7e28f101731a1400684de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/***************************************************************************
*   Copyright (C) 2006 by                                                 *
*   Jason Kivlighn ([email protected])                                  *
*                                                                         *
*   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.                                   *
***************************************************************************/

#include "htmlbookexporter.h"

#include <tqfile.h>
#include <tqstylesheet.h>

#include <kdebug.h>

#include "backends/recipedb.h"
#include "datablocks/categorytree.h"

HTMLBookExporter::HTMLBookExporter( CategoryTree *categories, const TQString& basedir, const TQString &format ) :
		HTMLExporter( basedir+"/index", format ), m_categories(categories), m_basedir(basedir)
{
}

HTMLBookExporter::~HTMLBookExporter()
{
}

int HTMLBookExporter::headerFlags() const
{
	return RecipeDB::Categories | RecipeDB::Title;
}

TQString HTMLBookExporter::createContent( const RecipeList& recipes )
{
	RecipeList::const_iterator recipe_it;
	for ( recipe_it = recipes.begin(); recipe_it != recipes.end(); ++recipe_it ) {
		for ( ElementList::const_iterator cat_it = ( *recipe_it ).categoryList.begin(); cat_it != ( *recipe_it ).categoryList.end(); ++cat_it ) {
			TQMap<TQString,TQTextStream*>::iterator stream_it = fileMap.find( (*cat_it).name );
			(**stream_it) << "<br /><br />";
			(**stream_it) << TQString("<a name=\""+(*recipe_it).title+"\" />");
			(**stream_it) << HTMLExporter::createContent(*recipe_it);
			(**stream_it) << TQString("[ <a href=\"#top\">Top</a> ]");
			(**stream_it) << TQString("[ <a href=\"index.html\">Back</a> ]");
			(**stream_it) << "<br /><br />";
		}
	}

	return TQString::null;
}

TQString HTMLBookExporter::createHeader( const RecipeList &list )
{
	TQString output = HTMLExporter::createHeader(list);

	TQString catLinks;
	TQTextStream catLinksStream(&catLinks,IO_WriteOnly);
	createCategoryStructure(catLinksStream,list);

	return output+"<h1>Krecipes Recipes</h1><div>"+catLinks+"</li></ul></div>";
}

TQString HTMLBookExporter::createFooter()
{
	TQMap<TQString,TQTextStream*>::const_iterator it;
	for ( it = fileMap.begin(); it != fileMap.end(); ++it ) {
		(**it) << HTMLExporter::createFooter();

		(*it)->device()->close();

		//does it matter the order of deletion here?
		TQIODevice *file = (*it)->device();
		delete *it;
		delete file;
	}

	TQString output = HTMLExporter::createFooter();
	return output;
}

void HTMLBookExporter::createCategoryStructure( TQTextStream &xml, const RecipeList &recipes )
{
	TQValueList<int> categoriesUsed;
	for ( RecipeList::const_iterator recipe_it = recipes.begin(); recipe_it != recipes.end(); ++recipe_it ) {
		for ( ElementList::const_iterator cat_it = ( *recipe_it ).categoryList.begin(); cat_it != ( *recipe_it ).categoryList.end(); ++cat_it ) {
			TQMap<TQString,TQTextStream*>::iterator stream_it = fileMap.find( (*cat_it).name );
			if ( categoriesUsed.find( ( *cat_it ).id ) == categoriesUsed.end() ) {
				categoriesUsed << ( *cat_it ).id;

				TQString catPageName = m_basedir+"/"+escape((*cat_it).name)+".html";
				TQFile *catPage = new TQFile( catPageName );
				catPage->open( IO_WriteOnly );
				TQTextStream *stream = new TQTextStream( catPage );
				stream->setEncoding( TQTextStream::UnicodeUTF8 );
				(*stream) << HTMLExporter::createHeader(recipes);
				(*stream) << TQString("<a name=\"top\" />");
				(*stream) << "<h1>"<<(*cat_it).name<<"</h1>";

				stream_it = fileMap.insert((*cat_it).name,stream);
			}
			(**stream_it) << TQString("[ <a href=\"#" + (*recipe_it).title + "\">" + (*recipe_it).title + "</a> ]");
		}
	}

	if ( !categoriesUsed.empty() ) {
		//only keep the relevant category structure
		removeIfUnused( categoriesUsed, m_categories );

		xml << "<ul>\n";
		writeCategoryStructure( xml, m_categories );
		xml << "</ul>\n";
	}
}

bool HTMLBookExporter::removeIfUnused( const TQValueList<int> &cat_ids, CategoryTree *parent, bool parent_should_show )
{
	for ( CategoryTree * it = parent->firstChild(); it; it = it->nextSibling() ) {
		if ( cat_ids.find( it->category.id ) != cat_ids.end() ) {
			parent_should_show = true;
			removeIfUnused( cat_ids, it, true ); //still recurse, but doesn't affect 'parent'
		}
		else {
			bool result = removeIfUnused( cat_ids, it );
			if ( parent_should_show == false )
				parent_should_show = result;
		}
	}

	if ( !parent_should_show && parent->category.id != -1 ) {
		//FIXME: CategoryTree is broken when deleting items
		//delete parent;

		parent->category.id = -2; //temporary workaround
	}

	return parent_should_show;
}

void HTMLBookExporter::writeCategoryStructure( TQTextStream &xml, const CategoryTree *categoryTree )
{
	if ( categoryTree->category.id != -2 ) {
		if ( categoryTree->category.id != -1 ) {
			TQString catPageName = TQStyleSheet::escape(categoryTree->category.name)+".html";

			xml << "\t<li>\n\t\t<a href=\""+catPageName+"\">"+TQStyleSheet::escape( categoryTree->category.name ).replace("\"","&quot;") + "</a>\n";
		}
	
		for ( CategoryTree * child_it = categoryTree->firstChild(); child_it; child_it = child_it->nextSibling() ) {
			if ( categoryTree->parent() != 0 )
				xml << "<ul>\n";
			writeCategoryStructure( xml, child_it );
			if ( categoryTree->parent() != 0 )
				xml << "</ul>\n";
		}
	
		if ( categoryTree->category.id != -1 )
			xml << "\t</li>\n";
	}
}