File indexing completed on 2024-04-21 15:55:46

0001 /*
0002  *
0003  * Copyright (C) 2004  Simon Martin <simartin@users.sourceforge.net>
0004  *
0005  * This program is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU General Public License as
0007  * published by the Free Software Foundation; either version 2 of
0008  * the License, or (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  *
0018  */
0019 
0020 #include "plaintolatexconverter.h"
0021 
0022 PlainToLaTeXConverter::PlainToLaTeXConverter()
0023 {
0024     // Fill the replacement map
0025     //TODO Do it only once!
0026     m_replaceMap.insert('$', "\\$");
0027     m_replaceMap.insert('%', "\\%");
0028     m_replaceMap.insert('^', "\\^");
0029     m_replaceMap.insert('&', "\\&");
0030     m_replaceMap.insert('_', "\\_");
0031     m_replaceMap.insert('#', "\\#");
0032     m_replaceMap.insert('{', "\\{");
0033     m_replaceMap.insert('}', "\\}");
0034     m_replaceMap.insert('~', "$\\sim$");
0035 }
0036 
0037 PlainToLaTeXConverter::~PlainToLaTeXConverter() {}
0038 
0039 /**
0040  * Converts plain text to LaTeX.
0041  * @param toConv The string to convert
0042  * @return The conversion's result
0043  */
0044 QString PlainToLaTeXConverter::ConvertToLaTeX(const QString& toConv) const
0045 {
0046     QString result(toConv);
0047 
0048     // Replacing what must be...
0049     uint sSize = result.length();
0050     QMap<QChar, QString>::const_iterator mapEnd = m_replaceMap.end();
0051     for(uint i = 0 ; i < sSize ; ++i)
0052     {
0053         QMap<QChar, QString>::const_iterator it = m_replaceMap.find(result.at(i));
0054 
0055         if(it != mapEnd) { // The character must be replaced
0056             result.replace(i, 1, *it);
0057             uint len = (*it).length();
0058             if(1 < len) {
0059                 i += len - 1;
0060                 sSize += len - 1;
0061             }
0062         }
0063     }
0064 
0065     return result;
0066 }