~ubuntu-branches/ubuntu/precise/grantlee/precise

« back to all changes in this revision

Viewing changes to gui/mediawikimarkupbuilder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
 
 
19
*/
 
20
 
 
21
#include "mediawikimarkupbuilder.h"
 
22
 
 
23
using namespace Grantlee;
 
24
 
 
25
MediaWikiMarkupBuilder::MediaWikiMarkupBuilder()
 
26
{
 
27
}
 
28
 
 
29
MediaWikiMarkupBuilder::~MediaWikiMarkupBuilder()
 
30
{
 
31
}
 
32
 
 
33
void MediaWikiMarkupBuilder::beginStrong()
 
34
{
 
35
  m_text.append( "'''" );
 
36
}
 
37
void MediaWikiMarkupBuilder::endStrong()
 
38
{
 
39
  m_text.append( "'''" );
 
40
}
 
41
void MediaWikiMarkupBuilder::beginEmph()
 
42
{
 
43
  m_text.append( "''" );
 
44
}
 
45
void MediaWikiMarkupBuilder::endEmph()
 
46
{
 
47
  m_text.append( "''" );
 
48
}
 
49
void MediaWikiMarkupBuilder::beginUnderline()
 
50
{
 
51
  m_text.append( "<u>" );
 
52
}
 
53
void MediaWikiMarkupBuilder::endUnderline()
 
54
{
 
55
  m_text.append( "</u>" );
 
56
}
 
57
void MediaWikiMarkupBuilder::beginStrikeout()
 
58
{
 
59
  m_text.append( "<s>" );
 
60
}
 
61
void MediaWikiMarkupBuilder::endStrikeout()
 
62
{
 
63
  m_text.append( "</s>" );
 
64
}
 
65
 
 
66
void MediaWikiMarkupBuilder::endParagraph()
 
67
{
 
68
  m_text.append( "\n" );
 
69
}
 
70
void MediaWikiMarkupBuilder::addNewline()
 
71
{
 
72
  m_text.append( "\n" );
 
73
}
 
74
 
 
75
void MediaWikiMarkupBuilder::beginAnchor( const QString &href, const QString &name )
 
76
{
 
77
  Q_UNUSED( name );
 
78
  m_text.append( QString( "[%1 " ).arg( href ) );
 
79
}
 
80
void MediaWikiMarkupBuilder::endAnchor()
 
81
{
 
82
  m_text.append( "]" );
 
83
}
 
84
 
 
85
void MediaWikiMarkupBuilder::beginHeader( int level )
 
86
{
 
87
  switch ( level ) {
 
88
  case 1:
 
89
    m_text.append( "= " );
 
90
    break;
 
91
  case 2:
 
92
    m_text.append( "== " );
 
93
    break;
 
94
  case 3:
 
95
    m_text.append( "=== " );
 
96
    break;
 
97
  case 4:
 
98
    m_text.append( "==== " );
 
99
    break;
 
100
  case 5:
 
101
    m_text.append( "===== " );
 
102
    break;
 
103
  case 6:
 
104
    m_text.append( "====== " );
 
105
    break;
 
106
  default:
 
107
    break;
 
108
  }
 
109
}
 
110
 
 
111
void MediaWikiMarkupBuilder::endHeader( int level )
 
112
{
 
113
  switch ( level ) {
 
114
  case 1:
 
115
    m_text.append( " =\n" );
 
116
    break;
 
117
  case 2:
 
118
    m_text.append( " ==\n" );
 
119
    break;
 
120
  case 3:
 
121
    m_text.append( " ===\n" );
 
122
    break;
 
123
  case 4:
 
124
    m_text.append( " ====\n" );
 
125
    break;
 
126
  case 5:
 
127
    m_text.append( " =====\n" );
 
128
    break;
 
129
  case 6:
 
130
    m_text.append( " ======\n" );
 
131
    break;
 
132
  default:
 
133
    break;
 
134
  }
 
135
}
 
136
 
 
137
void MediaWikiMarkupBuilder::beginList( QTextListFormat::Style type )
 
138
{
 
139
  currentListItemStyles.append( type );
 
140
  switch ( 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" );
 
148
    break;
 
149
  default:
 
150
    break;
 
151
  }
 
152
}
 
153
 
 
154
void MediaWikiMarkupBuilder::endList()
 
155
{
 
156
  m_text.append( "\n" );
 
157
  currentListItemStyles.removeLast();
 
158
}
 
159
 
 
160
void MediaWikiMarkupBuilder::beginListItem()
 
161
{
 
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.
 
167
    break;
 
168
  case QTextListFormat::ListDecimal:
 
169
  case QTextListFormat::ListLowerAlpha:
 
170
  case QTextListFormat::ListUpperAlpha:
 
171
    m_text.append( "# " );
 
172
    break;
 
173
  default:
 
174
    break;
 
175
  }
 
176
}
 
177
 
 
178
void MediaWikiMarkupBuilder::endListItem()
 
179
{
 
180
  m_text.append( "\n" );
 
181
}
 
182
 
 
183
void MediaWikiMarkupBuilder::appendLiteralText( const QString &text )
 
184
{
 
185
  m_text.append( escape( text ) );
 
186
}
 
187
 
 
188
const QString MediaWikiMarkupBuilder::escape( const QString &s )
 
189
{
 
190
  if ( s.contains( "<" ) ) {    // TODO: This could contain more. "''" and "[" for example
 
191
    return QString( "<nowiki>" + s + "</nowiki>" );
 
192
  }
 
193
  return s;
 
194
}
 
195
 
 
196
QString MediaWikiMarkupBuilder::getResult()
 
197
{
 
198
  QString ret = m_text;
 
199
  m_text.clear();
 
200
  return ret;
 
201
}