2
This file is part of the Grantlee template system.
4
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2 of the Licence, or (at your option) any later version.
11
This library 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 GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
21
#include "mediawikimarkupbuilder.h"
23
using namespace Grantlee;
25
MediaWikiMarkupBuilder::MediaWikiMarkupBuilder()
29
MediaWikiMarkupBuilder::~MediaWikiMarkupBuilder()
33
void MediaWikiMarkupBuilder::beginStrong()
35
m_text.append( "'''" );
37
void MediaWikiMarkupBuilder::endStrong()
39
m_text.append( "'''" );
41
void MediaWikiMarkupBuilder::beginEmph()
43
m_text.append( "''" );
45
void MediaWikiMarkupBuilder::endEmph()
47
m_text.append( "''" );
49
void MediaWikiMarkupBuilder::beginUnderline()
51
m_text.append( "<u>" );
53
void MediaWikiMarkupBuilder::endUnderline()
55
m_text.append( "</u>" );
57
void MediaWikiMarkupBuilder::beginStrikeout()
59
m_text.append( "<s>" );
61
void MediaWikiMarkupBuilder::endStrikeout()
63
m_text.append( "</s>" );
66
void MediaWikiMarkupBuilder::endParagraph()
68
m_text.append( "\n" );
70
void MediaWikiMarkupBuilder::addNewline()
72
m_text.append( "\n" );
75
void MediaWikiMarkupBuilder::beginAnchor( const QString &href, const QString &name )
78
m_text.append( QString( "[%1 " ).arg( href ) );
80
void MediaWikiMarkupBuilder::endAnchor()
85
void MediaWikiMarkupBuilder::beginHeader( int level )
89
m_text.append( "= " );
92
m_text.append( "== " );
95
m_text.append( "=== " );
98
m_text.append( "==== " );
101
m_text.append( "===== " );
104
m_text.append( "====== " );
111
void MediaWikiMarkupBuilder::endHeader( int level )
115
m_text.append( " =\n" );
118
m_text.append( " ==\n" );
121
m_text.append( " ===\n" );
124
m_text.append( " ====\n" );
127
m_text.append( " =====\n" );
130
m_text.append( " ======\n" );
137
void MediaWikiMarkupBuilder::beginList( QTextListFormat::Style type )
139
currentListItemStyles.append( type );
141
case QTextListFormat::ListDisc:
142
case QTextListFormat::ListCircle:
143
case QTextListFormat::ListSquare:
144
case QTextListFormat::ListDecimal:
145
case QTextListFormat::ListLowerAlpha:
146
case QTextListFormat::ListUpperAlpha:
147
m_text.append( "\n" );
154
void MediaWikiMarkupBuilder::endList()
156
m_text.append( "\n" );
157
currentListItemStyles.removeLast();
160
void MediaWikiMarkupBuilder::beginListItem()
162
switch ( currentListItemStyles.last() ) {
163
case QTextListFormat::ListDisc:
164
case QTextListFormat::ListCircle:
165
case QTextListFormat::ListSquare:
166
m_text.append( "* " ); // Unordered lists are all disc type in MediaWikiMarkup.
168
case QTextListFormat::ListDecimal:
169
case QTextListFormat::ListLowerAlpha:
170
case QTextListFormat::ListUpperAlpha:
171
m_text.append( "# " );
178
void MediaWikiMarkupBuilder::endListItem()
180
m_text.append( "\n" );
183
void MediaWikiMarkupBuilder::appendLiteralText( const QString &text )
185
m_text.append( escape( text ) );
188
const QString MediaWikiMarkupBuilder::escape( const QString &s )
190
if ( s.contains( "<" ) ) { // TODO: This could contain more. "''" and "[" for example
191
return QString( "<nowiki>" + s + "</nowiki>" );
196
QString MediaWikiMarkupBuilder::getResult()
198
QString ret = m_text;