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"
27 #include <kconfiggroup.h>
28 #include <kstandarddirs.h>
31 #include <QTextStream>
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." ;
58 QFile
f( mainTemplate
);
59 if ( !f
.open( QIODevice::ReadOnly
) ) {
60 kWarning() << "Unable to open main template file '" << mainTemplate
67 enum State
{ IDLE
, SINGLELINE
, MULTILINE
};
72 while( !( line
= ts
.readLine() ).isNull() ) {
75 if ( !line
.isEmpty() && !line
.startsWith( '#' ) ) {
76 int pos
= line
.indexOf( "<<" );
79 symbol
= line
.left( pos
).trimmed();
80 endMarker
= line
.mid( pos
+ 2 ).trimmed();
83 symbol
= line
.trimmed();
88 mSymbols
.insert( symbol
, line
);
92 if ( line
.startsWith( endMarker
) ) {
93 mSymbols
.insert( symbol
, value
);
101 kError() << "Formatter::readTemplates(): Illegal state: "
102 << static_cast<int>(state
) << endl
;
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();
117 QStringList requiredSymbols
;
118 requiredSymbols
<< "HEADER" << "FOOTER";
121 QStringList::ConstIterator it2
;
122 for( it2
= requiredSymbols
.constBegin(); it2
!= requiredSymbols
.constEnd(); ++it2
) {
123 if ( !mSymbols
.contains( *it2
) ) {
125 kError() << "Symbol '" << *it2
<< "' is missing from main template file."
130 if ( success
) mHasTemplate
= true;
135 QString
Formatter::header( const QString
&title
)
138 if ( mHasTemplate
) {
139 s
= mSymbols
[ "HEADER" ];
140 s
.replace( "--TITLE:--", title
);
142 s
= QLatin1String("<html><head><title>") + title
+ QLatin1String("</title></head>\n<body>\n");
147 QString
Formatter::footer()
149 if ( mHasTemplate
) {
150 return mSymbols
[ "FOOTER" ];
152 return QLatin1String("</body></html>");
156 QString
Formatter::separator()
158 // return "<table width=100%><tr><td bgcolor=\"#7B8962\"> "
159 // "</td></tr></table>";
163 QString
Formatter::docTitle( const QString
&title
)
165 return QLatin1String("<h3><font color=\"red\">") + title
+ QLatin1String("</font></h3>");
168 QString
Formatter::sectionHeader( const QString
§ion
)
170 return QLatin1String("<h2><font color=\"blue\">") + section
+ QLatin1String("</font></h2>");
173 QString
Formatter::processResult( const QString
&data
)
177 enum { Header
, BodyTag
, Body
, Footer
};
181 for( int i
= 0; i
< data
.length(); ++i
) {
185 if ( c
== QLatin1Char('<') && data
.mid( i
, 5 ).toLower() == QLatin1String("<body") ) {
191 if ( c
== '>' ) state
= Body
;
194 if ( c
== QLatin1Char('<') && data
.mid( i
, 7 ).toLower() == QLatin1String("</body>") ) {
208 if ( state
== Header
) return data
;
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>");