/*************************************************************************** * Copyright (C) 2006 by * * Jason Kivlighn (jkivlighn@gmail.com) * * * * 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 #include #include #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::iterator stream_it = fileMap.find( (*cat_it).name ); (**stream_it) << "

"; (**stream_it) << TQString(""); (**stream_it) << HTMLExporter::createContent(*recipe_it); (**stream_it) << TQString("[ Top ]"); (**stream_it) << TQString("[ Back ]"); (**stream_it) << "

"; } } 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+"

Krecipes Recipes

"+catLinks+"
"; } TQString HTMLBookExporter::createFooter() { TQMap::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 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::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(""); (*stream) << "

"<<(*cat_it).name<<"

"; stream_it = fileMap.insert((*cat_it).name,stream); } (**stream_it) << TQString("[
" + (*recipe_it).title + " ]"); } } if ( !categoriesUsed.empty() ) { //only keep the relevant category structure removeIfUnused( categoriesUsed, m_categories ); xml << "
    \n"; writeCategoryStructure( xml, m_categories ); xml << "
\n"; } } bool HTMLBookExporter::removeIfUnused( const TQValueList &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
  • \n\t\t"+TQStyleSheet::escape( categoryTree->category.name ).replace("\"",""") + "\n"; } for ( CategoryTree * child_it = categoryTree->firstChild(); child_it; child_it = child_it->nextSibling() ) { if ( categoryTree->parent() != 0 ) xml << "
      \n"; writeCategoryStructure( xml, child_it ); if ( categoryTree->parent() != 0 ) xml << "
    \n"; } if ( categoryTree->category.id != -1 ) xml << "\t
  • \n"; } }