mediawikimarkupbuilder.cpp
00001 /* 00002 This file is part of KDE. 00003 00004 Copyright (c) 2008 Stephen Kelly <steveire@gmail.com> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "mediawikimarkupbuilder.h" 00023 #include <kdebug.h> 00024 00025 MediaWikiMarkupBuilder::MediaWikiMarkupBuilder() {} 00026 MediaWikiMarkupBuilder::~MediaWikiMarkupBuilder() {} 00027 00028 void MediaWikiMarkupBuilder::beginStrong() 00029 { 00030 m_text.append("'''"); 00031 } 00032 void MediaWikiMarkupBuilder::endStrong() 00033 { 00034 m_text.append("'''"); 00035 } 00036 void MediaWikiMarkupBuilder::beginEmph() 00037 { 00038 m_text.append("''"); 00039 } 00040 void MediaWikiMarkupBuilder::endEmph() 00041 { 00042 m_text.append("''"); 00043 } 00044 void MediaWikiMarkupBuilder::beginUnderline() 00045 { 00046 m_text.append("<u>"); 00047 } 00048 void MediaWikiMarkupBuilder::endUnderline() 00049 { 00050 m_text.append("</u>"); 00051 } 00052 void MediaWikiMarkupBuilder::beginStrikeout() 00053 { 00054 m_text.append("<s>"); 00055 } 00056 void MediaWikiMarkupBuilder::endStrikeout() 00057 { 00058 m_text.append("</s>"); 00059 } 00060 00061 void MediaWikiMarkupBuilder::endParagraph() 00062 { 00063 m_text.append("\n"); 00064 } 00065 void MediaWikiMarkupBuilder::addNewline() 00066 { 00067 m_text.append("\n"); 00068 } 00069 00070 void MediaWikiMarkupBuilder::beginAnchor(const QString &href, const QString &name) 00071 { 00072 Q_UNUSED(name); 00073 m_text.append(QString("[%1 ").arg(href)); 00074 } 00075 void MediaWikiMarkupBuilder::endAnchor() 00076 { 00077 m_text.append("]"); 00078 } 00079 00080 void MediaWikiMarkupBuilder::beginHeader(int level) 00081 { 00082 switch (level) { 00083 case 1: 00084 m_text.append("= "); 00085 break; 00086 case 2: 00087 m_text.append("== "); 00088 break; 00089 case 3: 00090 m_text.append("=== "); 00091 break; 00092 case 4: 00093 m_text.append("==== "); 00094 break; 00095 case 5: 00096 m_text.append("===== "); 00097 break; 00098 case 6: 00099 m_text.append("====== "); 00100 break; 00101 default: 00102 break; 00103 } 00104 } 00105 00106 void MediaWikiMarkupBuilder::endHeader(int level) 00107 { 00108 switch (level) { 00109 case 1: 00110 m_text.append(" =\n"); 00111 break; 00112 case 2: 00113 m_text.append(" ==\n"); 00114 break; 00115 case 3: 00116 m_text.append(" ===\n"); 00117 break; 00118 case 4: 00119 m_text.append(" ====\n"); 00120 break; 00121 case 5: 00122 m_text.append(" =====\n"); 00123 break; 00124 case 6: 00125 m_text.append(" ======\n"); 00126 break; 00127 default: 00128 break; 00129 } 00130 } 00131 00132 void MediaWikiMarkupBuilder::beginList(QTextListFormat::Style type) 00133 { 00134 currentListItemStyles.append(type); 00135 switch (type) { 00136 case QTextListFormat::ListDisc: 00137 case QTextListFormat::ListCircle: 00138 case QTextListFormat::ListSquare: 00139 case QTextListFormat::ListDecimal: 00140 case QTextListFormat::ListLowerAlpha: 00141 case QTextListFormat::ListUpperAlpha: 00142 m_text.append("\n"); 00143 break; 00144 default: 00145 break; 00146 } 00147 } 00148 00149 00150 00151 void MediaWikiMarkupBuilder::endList() 00152 { 00153 m_text.append("\n"); 00154 currentListItemStyles.removeLast(); 00155 } 00156 00157 00158 void MediaWikiMarkupBuilder::beginListItem() 00159 { 00160 00161 switch (currentListItemStyles.last()) { 00162 case QTextListFormat::ListDisc: 00163 case QTextListFormat::ListCircle: 00164 case QTextListFormat::ListSquare: 00165 m_text.append("* "); // Unordered lists are all disc type in MediaWikiMarkup. 00166 break; 00167 case QTextListFormat::ListDecimal: 00168 case QTextListFormat::ListLowerAlpha: 00169 case QTextListFormat::ListUpperAlpha: 00170 m_text.append("# "); 00171 break; 00172 default: 00173 break; 00174 } 00175 } 00176 void MediaWikiMarkupBuilder::endListItem() 00177 { 00178 m_text.append("\n"); 00179 } 00180 00181 void MediaWikiMarkupBuilder::appendLiteralText(const QString &text) 00182 { 00183 m_text.append(escape(text)); 00184 } 00185 00186 const QString MediaWikiMarkupBuilder::escape(const QString &s) 00187 { 00188 if (s.contains("<")) { // TODO: This could contain more. "''" and "[" for example 00189 return QString("<nowiki>" + s + "</nowiki>"); 00190 } 00191 return s; 00192 } 00193 00194 QString& MediaWikiMarkupBuilder::getResult() 00195 { 00196 return m_text; 00197 } 00198 00199