From 4aed2c8219774f5d797760606b8489a92ddc5163 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- khelpcenter/formatter.cpp | 222 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 khelpcenter/formatter.cpp (limited to 'khelpcenter/formatter.cpp') diff --git a/khelpcenter/formatter.cpp b/khelpcenter/formatter.cpp new file mode 100644 index 000000000..e31c9d433 --- /dev/null +++ b/khelpcenter/formatter.cpp @@ -0,0 +1,222 @@ +/* + This file is part of KHelpcenter. + + Copyright (c) 2003 Cornelius Schumacher + + 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. + + 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, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "formatter.h" + +#include +#include +#include +#include +#include + +#include +#include + +using namespace KHC; + +Formatter::Formatter() + : mHasTemplate( false ) +{ +} + +Formatter:: ~Formatter() +{ +} + +bool Formatter::readTemplates() +{ + KConfig *cfg = KGlobal::config(); + cfg->setGroup( "Templates" ); + QString mainTemplate = cfg->readEntry( "MainTemplate" ); + + if ( mainTemplate.isEmpty() ) { + mainTemplate = locate( "appdata", "maintemplate" ); + } + + if ( mainTemplate.isEmpty() ) { + kdWarning() << "Main template file name is empty." << endl; + return false; + } + + QFile f( mainTemplate ); + if ( !f.open( IO_ReadOnly ) ) { + kdWarning() << "Unable to open main template file '" << mainTemplate + << "'." << endl; + return false; + } + + QTextStream ts( &f ); + QString line; + enum State { IDLE, SINGLELINE, MULTILINE }; + State state = IDLE; + QString symbol; + QString endMarker; + QString value; + while( !( line = ts.readLine() ).isNull() ) { + switch ( state ) { + case IDLE: + if ( !line.isEmpty() && !line.startsWith( "#" ) ) { + int pos = line.find( "<<" ); + if ( pos >= 0 ) { + state = MULTILINE; + symbol = line.left( pos ).stripWhiteSpace(); + endMarker = line.mid( pos + 2 ).stripWhiteSpace(); + } else { + state = SINGLELINE; + symbol = line.stripWhiteSpace(); + } + } + break; + case SINGLELINE: + mSymbols.insert( symbol, line ); + state = IDLE; + break; + case MULTILINE: + if ( line.startsWith( endMarker ) ) { + mSymbols.insert( symbol, value ); + value = ""; + state = IDLE; + } else { + value += line + '\n'; + } + break; + default: + kdError() << "Formatter::readTemplates(): Illegal state: " + << state << endl; + break; + } + } + + f.close(); + +#if 0 + QMap::ConstIterator it; + for( it = mSymbols.begin(); it != mSymbols.end(); ++it ) { + kdDebug() << "KEY: " << it.key() << endl; + kdDebug() << "VALUE: " << it.data() << endl; + } +#endif + + QStringList requiredSymbols; + requiredSymbols << "HEADER" << "FOOTER"; + + bool success = true; + QStringList::ConstIterator it2; + for( it2 = requiredSymbols.begin(); it2 != requiredSymbols.end(); ++it2 ) { + if ( !mSymbols.contains( *it2 ) ) { + success = false; + kdError() << "Symbol '" << *it2 << "' is missing from main template file." + << endl; + } + } + + if ( success ) mHasTemplate = true; + + return success; +} + +QString Formatter::header( const QString &title ) +{ + QString s; + if ( mHasTemplate ) { + s = mSymbols[ "HEADER" ]; + s.replace( "--TITLE:--", title ); + } else { + s = "" + title + "\n\n"; + } + return s; +} + +QString Formatter::footer() +{ + if ( mHasTemplate ) { + return mSymbols[ "FOOTER" ]; + } else { + return ""; + } +} + +QString Formatter::separator() +{ +// return "
 " +// "
"; + return "
"; +} + +QString Formatter::docTitle( const QString &title ) +{ + return "

" + title + "

"; +} + +QString Formatter::sectionHeader( const QString §ion ) +{ + return "

" + section + "

"; +} + +QString Formatter::processResult( const QString &data ) +{ + QString result; + + enum { Header, BodyTag, Body, Footer }; + + int state = Header; + + for( uint i = 0; i < data.length(); ++i ) { + QChar c = data[i]; + switch ( state ) { + case Header: + if ( c == '<' && data.mid( i, 5 ).lower() == "' ) state = Body; + break; + case Body: + if ( c == '<' && data.mid( i, 7 ).lower() == "" ) { + state = Footer; + } else { + result.append( c ); + } + break; + case Footer: + break; + default: + result.append( c ); + break; + } + } + + if ( state == Header ) return data; + else return result; +} + +QString Formatter::paragraph( const QString &str ) +{ + return "

" + str + "

"; +} + +QString Formatter::title( const QString &title ) +{ + return "

" + title + "

"; +} + +// vim:ts=2:sw=2:et -- cgit v1.2.1