Parent: [f6a999] (diff)

Child: [4f51c6] (diff)

Download this file

preview_plaintorich.cpp    187 lines (163 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Copyright (C) 2014 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "autoconfig.h"
#include <stdio.h>
#include <string>
#include <QString>
#include <QStringList>
#include "preview_plaintorich.h"
#include "plaintorich.h"
#include "log.h"
#include "guiutils.h"
#include "cancelcheck.h"
using namespace std;
PlainToRichQtPreview::PlainToRichQtPreview()
{
clear();
}
void PlainToRichQtPreview::clear()
{
m_curanchor = 1;
m_lastanchor = 0;
m_groupanchors.clear();
m_groupcuranchors.clear();
}
bool PlainToRichQtPreview::haveAnchors()
{
return m_lastanchor != 0;
}
string PlainToRichQtPreview::PlainToRichQtPreview::header()
{
if (!m_inputhtml) {
switch (prefs.previewPlainPre) {
case PrefsPack::PP_BR:
m_eolbr = true;
return "<qt><head><title></title></head><body>";
case PrefsPack::PP_PRE:
m_eolbr = false;
return "<qt><head><title></title></head><body><pre>";
case PrefsPack::PP_PREWRAP:
m_eolbr = false;
return "<qt><head><title></title></head><body>"
"<pre style=\"white-space: pre-wrap\">";
}
}
return cstr_null;
}
string PlainToRichQtPreview::startMatch(unsigned int grpidx)
{
LOGDEB2("startMatch, grpidx " << (grpidx) << "\n" );
grpidx = m_hdata->grpsugidx[grpidx];
LOGDEB2("startMatch, ugrpidx " << (grpidx) << "\n" );
m_groupanchors[grpidx].push_back(++m_lastanchor);
m_groupcuranchors[grpidx] = 0;
return string("<span style='color: ").
append((const char *)(prefs.qtermcolor.toUtf8())).
append(";font-weight: bold;").
append("'>").
append("<a name=\"").
append(termAnchorName(m_lastanchor)).
append("\">");
}
string PlainToRichQtPreview::endMatch()
{
return string("</a></span>");
}
string PlainToRichQtPreview::termAnchorName(int i) const
{
static const char *termAnchorNameBase = "TRM";
char acname[sizeof(termAnchorNameBase) + 20];
sprintf(acname, "%s%d", termAnchorNameBase, i);
return string(acname);
}
string PlainToRichQtPreview::startChunk()
{
return "<pre>";
}
int PlainToRichQtPreview::nextAnchorNum(int grpidx)
{
LOGDEB2("nextAnchorNum: group " << (grpidx) << "\n" );
map<unsigned int, unsigned int>::iterator curit =
m_groupcuranchors.find(grpidx);
map<unsigned int, vector<int> >::iterator vecit =
m_groupanchors.find(grpidx);
if (grpidx == -1 || curit == m_groupcuranchors.end() ||
vecit == m_groupanchors.end()) {
if (m_curanchor >= m_lastanchor)
m_curanchor = 1;
else
m_curanchor++;
} else {
if (curit->second >= vecit->second.size() -1)
m_groupcuranchors[grpidx] = 0;
else
m_groupcuranchors[grpidx]++;
m_curanchor = vecit->second[m_groupcuranchors[grpidx]];
LOGDEB2("nextAnchorNum: curanchor now " << (m_curanchor) << "\n" );
}
return m_curanchor;
}
int PlainToRichQtPreview::prevAnchorNum(int grpidx)
{
map<unsigned int, unsigned int>::iterator curit =
m_groupcuranchors.find(grpidx);
map<unsigned int, vector<int> >::iterator vecit =
m_groupanchors.find(grpidx);
if (grpidx == -1 || curit == m_groupcuranchors.end() ||
vecit == m_groupanchors.end()) {
if (m_curanchor <= 1)
m_curanchor = m_lastanchor;
else
m_curanchor--;
} else {
if (curit->second <= 0)
m_groupcuranchors[grpidx] = vecit->second.size() -1;
else
m_groupcuranchors[grpidx]--;
m_curanchor = vecit->second[m_groupcuranchors[grpidx]];
}
return m_curanchor;
}
QString PlainToRichQtPreview::curAnchorName() const
{
return QString::fromUtf8(termAnchorName(m_curanchor).c_str());
}
ToRichThread::ToRichThread(const string &i, const HighlightData& hd,
std::shared_ptr<PlainToRichQtPreview> ptr,
QStringList& qrichlist,
QObject *parent)
: QThread(parent), m_input(i), m_hdata(hd), m_ptr(ptr), m_output(qrichlist)
{
}
// Insert into editor by chunks so that the top becomes visible
// earlier for big texts. This provokes some artifacts (adds empty line),
// so we can't set it too low.
#define CHUNKL 500*1000
void ToRichThread::run()
{
list<string> out;
try {
m_ptr->plaintorich(m_input, out, m_hdata, CHUNKL);
} catch (CancelExcept) {
return;
}
// Convert C++ string list to QString list
for (list<string>::iterator it = out.begin();
it != out.end(); it++) {
m_output.push_back(QString::fromUtf8(it->c_str(), it->length()));
}
}