Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / khelpcenter / formatter.cpp
blobb9f05174f366d6ef8a410379d9347dd4f80349e2
1 /*
2 This file is part of KHelpcenter.
4 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "formatter.h"
23 #include <klocale.h>
24 #include <kglobal.h>
25 #include <kdebug.h>
26 #include <kconfig.h>
27 #include <kconfiggroup.h>
28 #include <kstandarddirs.h>
30 #include <QFile>
31 #include <QTextStream>
33 using namespace KHC;
35 Formatter::Formatter()
36 : mHasTemplate( false )
40 Formatter:: ~Formatter()
44 bool Formatter::readTemplates()
46 KConfigGroup cfg(KGlobal::config(), "Templates");
47 QString mainTemplate = cfg.readEntry( "MainTemplate" );
49 if ( mainTemplate.isEmpty() ) {
50 mainTemplate = KStandardDirs::locate( "appdata", "maintemplate" );
53 if ( mainTemplate.isEmpty() ) {
54 kWarning() << "Main template file name is empty." ;
55 return false;
58 QFile f( mainTemplate );
59 if ( !f.open( QIODevice::ReadOnly ) ) {
60 kWarning() << "Unable to open main template file '" << mainTemplate
61 << "'." << endl;
62 return false;
65 QTextStream ts( &f );
66 QString line;
67 enum State { IDLE, SINGLELINE, MULTILINE };
68 State state = IDLE;
69 QString symbol;
70 QString endMarker;
71 QString value;
72 while( !( line = ts.readLine() ).isNull() ) {
73 switch ( state ) {
74 case IDLE:
75 if ( !line.isEmpty() && !line.startsWith( '#' ) ) {
76 int pos = line.indexOf( "<<" );
77 if ( pos >= 0 ) {
78 state = MULTILINE;
79 symbol = line.left( pos ).trimmed();
80 endMarker = line.mid( pos + 2 ).trimmed();
81 } else {
82 state = SINGLELINE;
83 symbol = line.trimmed();
86 break;
87 case SINGLELINE:
88 mSymbols.insert( symbol, line );
89 state = IDLE;
90 break;
91 case MULTILINE:
92 if ( line.startsWith( endMarker ) ) {
93 mSymbols.insert( symbol, value );
94 value = "";
95 state = IDLE;
96 } else {
97 value += line + '\n';
99 break;
100 default:
101 kError() << "Formatter::readTemplates(): Illegal state: "
102 << static_cast<int>(state) << endl;
103 break;
107 f.close();
109 #if 0
110 QMap<QString,QString>::ConstIterator it;
111 for( it = mSymbols.begin(); it != mSymbols.end(); ++it ) {
112 kDebug() << "KEY: " << it.key();
113 kDebug() << "VALUE: " << it.data();
115 #endif
117 QStringList requiredSymbols;
118 requiredSymbols << "HEADER" << "FOOTER";
120 bool success = true;
121 QStringList::ConstIterator it2;
122 for( it2 = requiredSymbols.begin(); it2 != requiredSymbols.end(); ++it2 ) {
123 if ( !mSymbols.contains( *it2 ) ) {
124 success = false;
125 kError() << "Symbol '" << *it2 << "' is missing from main template file."
126 << endl;
130 if ( success ) mHasTemplate = true;
132 return success;
135 QString Formatter::header( const QString &title )
137 QString s;
138 if ( mHasTemplate ) {
139 s = mSymbols[ "HEADER" ];
140 s.replace( "--TITLE:--", title );
141 } else {
142 s = QLatin1String("<html><head><title>") + title + QLatin1String("</title></head>\n<body>\n");
144 return s;
147 QString Formatter::footer()
149 if ( mHasTemplate ) {
150 return mSymbols[ "FOOTER" ];
151 } else {
152 return QLatin1String("</body></html>");
156 QString Formatter::separator()
158 // return "<table width=100%><tr><td bgcolor=\"#7B8962\">&nbsp;"
159 // "</td></tr></table>";
160 return "<hr>";
163 QString Formatter::docTitle( const QString &title )
165 return QLatin1String("<h3><font color=\"red\">") + title + QLatin1String("</font></h3>");
168 QString Formatter::sectionHeader( const QString &section )
170 return QLatin1String("<h2><font color=\"blue\">") + section + QLatin1String("</font></h2>");
173 QString Formatter::processResult( const QString &data )
175 QString result;
177 enum { Header, BodyTag, Body, Footer };
179 int state = Header;
181 for( int i = 0; i < data.length(); ++i ) {
182 QChar c = data[i];
183 switch ( state ) {
184 case Header:
185 if ( c == QLatin1Char('<') && data.mid( i, 5 ).toLower() == QLatin1String("<body") ) {
186 state = BodyTag;
187 i += 4;
189 break;
190 case BodyTag:
191 if ( c == '>' ) state = Body;
192 break;
193 case Body:
194 if ( c == QLatin1Char('<') && data.mid( i, 7 ).toLower() == QLatin1String("</body>") ) {
195 state = Footer;
196 } else {
197 result.append( c );
199 break;
200 case Footer:
201 break;
202 default:
203 result.append( c );
204 break;
208 if ( state == Header ) return data;
209 else return result;
212 QString Formatter::paragraph( const QString &str )
214 return QLatin1String("<p>") + str + QLatin1String("</p>");
217 QString Formatter::title( const QString &title )
219 return QLatin1String("<h2>") + title + QLatin1String("</h2>");
222 // vim:ts=2:sw=2:et