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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
latexhelpers.cpp - description
-------------------
copyright : Scribus Team
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "latexhelpers.h"
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include "prefsmanager.h"
#include "scpaths.h"
LatexHighlighter::LatexHighlighter(QTextDocument *document)
: QSyntaxHighlighter(document)
{
rules = 0;
}
void LatexHighlighter::highlightBlock(const QString &text)
{
//This is required to fix a QT incompatibility. See error message below for details.
static bool disable_highlighting = false;
if (disable_highlighting) return;
if (!rules) return;
foreach (LatexHighlighterRule *rule, *rules) {
int index = text.indexOf(rule->regex);
while (index >= 0) {
int length;
if (rule->regex.numCaptures() == 0) {
length = rule->regex.matchedLength();
} else {
length = rule->regex.cap(1).length();
index = rule->regex.pos(1);
}
if (length == 0) {
qWarning() << "Highlighter pattern" << rule->regex.pattern() << "matched a zero length string. This would lead to an infinite loop. Aborting. Please fix this pattern!";
break;
}
setFormat(index, length, rule->format);
int oldindex = index;
int offset = index + length;
index = text.indexOf(rule->regex, offset);
if (index >= 0 && (index == oldindex || index <= offset)) {
qWarning() << QObject::tr("Highlighter error: Invalid index returned by QT's QString.indexOf(). This is a incompatibility between different QT versions and it can only be fixed by recompiling Scribus with the same QT version that is running on this system. Syntax highlighting is disabled now, but render frames should continue to work without problems.") << "Additional debugging info: old index:" << oldindex << "new index:"<< index << "offset:" << offset;
disable_highlighting = true;
return;
}
}
}
}
QString LatexConfigParser::configBase()
{
return ScPaths::instance().shareDir() + "/editorconfig/";
}
QString LatexConfigParser::absoluteFilename(QString fn)
{
QFileInfo fi(fn);
if (!fi.exists()) {
return configBase() + fn;
} else {
return fn;
}
}
//TODO: Pass this information to LatexEditor, so the second parser can be removed
bool LatexConfigParser::parseConfigFile(QString fn)
{
fn = absoluteFilename(fn);
m_error = "";
m_filename = fn;
QFile f(fn);
if (!f.open(QIODevice::ReadOnly)) {
QMessageBox::critical(0, QObject::tr("Error"), "<qt>" +
QObject::tr("Opening the configfile %1 failed! %2").arg(
fn, f.errorString())
+ "</qt>", 1, 0, 0);
}
xml.setDevice(&f);
while (!xml.atEnd()) {
xml.readNext();
if (xml.isWhitespace() || xml.isComment() || xml.isStartDocument() || xml.isEndDocument()) continue;
if (xml.isStartElement() && xml.name() == "editorsettings") {
m_description = xml.attributes().value("description").toString();
m_icon = xml.attributes().value("icon").toString();
if (m_description.isEmpty()) {
m_description = fn;
}
parseElements();
} else {
formatError("Unexpected element at root level"+xml.name().toString()+", Token String: "+
xml.tokenString());
}
}
if (xml.hasError()) {
formatError(xml.errorString());
}
f.close();
if (!m_error.isEmpty()) {
return false;
}
return true;
}
void LatexConfigParser::parseElements()
{
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement() && xml.name() == "editorsettings") break;
if (xml.isWhitespace() || xml.isComment() || xml.isEndElement()) continue;
if (!xml.isStartElement()) {
formatError("Unexpected element in <editorsettings>"+xml.name().toString()+", Token String: "+
xml.tokenString());
continue;
}
if (xml.name() == "executable") {
m_executable = xml.attributes().value("command").toString();
} else if (xml.name() == "imagefile") {
m_imageExtension = xml.attributes().value("extension").toString();
} else if (xml.name() == "highlighter") {
parseHighlighter();
} else if (xml.name() == "empty-frame-text") {
m_emptyFrameText = xml.readI18nText(true);
} else if (xml.name() == "preamble") {
m_preamble = xml.readElementText();
} else if (xml.name() == "postamble") {
m_postamble = xml.readElementText();
} else if (xml.name() == "tab") {
parseTab();
} else {
formatError("Unknown tag in <editorsettings>: "+xml.name().toString());
}
}
}
void LatexConfigParser::formatError(QString message)
{
QString new_error = QString::number(xml.lineNumber()) + ":" +
QString::number(xml.columnNumber()) + ":" + message;
qWarning() << m_filename << new_error;
m_error += new_error + "\n";
}
bool LatexConfigParser::StrRefToBool(const QStringRef &str) const
{
if (str == "1" || str == "true") return true;
if (str == "0" || str == "false" || str.isEmpty()) return false;
qWarning() << "Invalid bool string:" << str.toString();
return false;
}
void LatexConfigParser::parseHighlighter()
{
foreach (LatexHighlighterRule *rule, highlighterRules) {
delete rule;
}
highlighterRules.clear();
while (!xml.atEnd()) {
xml.readNext();
if (xml.isWhitespace() || xml.isComment()) continue;
if (xml.isEndElement() && xml.name() == "highlighter") break;
if (xml.isEndElement() && xml.name() == "rule") continue;
if (!xml.isStartElement() || xml.name() != "rule") {
formatError("Unexpected element in <highlighter>: "+
xml.name().toString()+", Token String: "+
xml.tokenString());
continue;
}
QString regex = xml.attributes().value("regex").toString();
bool bold = StrRefToBool(xml.attributes().value("bold"));
bool italic = StrRefToBool(xml.attributes().value("italic"));
bool underline = StrRefToBool(xml.attributes().value("underline"));
bool minimal = StrRefToBool(xml.attributes().value("minimal"));
QString colorStr = xml.attributes().value("color").toString();
QColor color(colorStr);
if (!color.isValid()) {
color.fromRgb(0, 0, 0); //Black
if (!colorStr.isEmpty())
qWarning() << "Invalid color:" << colorStr;
}
LatexHighlighterRule *newRule = new LatexHighlighterRule();
newRule->format.setForeground(color);
newRule->format.setFontItalic(italic);
if (bold) {
newRule->format.setFontWeight(QFont::Bold);
}
newRule->format.setFontUnderline(underline);
newRule->regex.setPattern(regex);
newRule->regex.setMinimal(minimal);
highlighterRules.append(newRule);
}
}
void LatexConfigParser::parseTab()
{
QString type = xml.attributes().value("type").toString();
bool itemstab = (type == "items");
QString title = "";
QString name, text, default_value;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isWhitespace() || xml.isComment()) continue;
if (xml.isEndElement() && xml.name() == "tab") break;
if (!xml.isStartElement()) {
formatError("Unexpected element in <tab>: "+xml.name().toString()+", Token String: "+
xml.tokenString());
continue;
}
if (xml.name() == "title") {
if (!title.isEmpty()) {
formatError("Second <title> tag in <tab>");
}
title = xml.readI18nText();
} else if (xml.name() == "item") {
if (!itemstab) {
formatError("Found <item> in a 'settings'-tab!");
}
QString value = xml.attributes().value("value").toString();
QString img = xml.attributes().value("image").toString();
text = xml.readI18nText();
} else if (xml.name() == "comment" || xml.name() == "font"
|| xml.name() == "spinbox" || xml.name() == "color"
|| xml.name() == "text" || xml.name() == "list") {
//TODO: Store this + attributes in a list
QString tagname = xml.name().toString();
name = xml.attributes().value("name").toString();
default_value = xml.attributes().value("default").toString();
if (xml.name() != "list") {
text = xml.readI18nText();
} else {
ignoreList();
}
if (!name.isEmpty()) {
if (properties.contains(name)) {
formatError("Redeclared setting with name: " + name);
} else {
properties.insert(name, default_value);
}
}
//TODO: qDebug() << "For future use:" << tagname << name << text << default_value;
} else {
formatError("Unexpected element in <tab>: " + xml.name().toString());
}
}
if (title.isEmpty()) {
formatError("Tab ended here, but no title was found!");
}
}
void LatexConfigParser::ignoreList()
{
//TODO: Quick hack to avoid real parsing
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement() && xml.name() == "list") break;
}
}
QString LatexConfigParser::executable() const
{
QFileInfo f(m_filename);
QString fileName=f.fileName();
QString command = PrefsManager::instance()->latexCommands()[fileName];
if (command.isEmpty()) {
return m_executable;
} else {
return command;
}
}
QString I18nXmlStreamReader::readI18nText(bool unindent)
{
QString language = PrefsManager::instance()->guiLanguage();
QString result;
int matchquality = 0;
bool i18n = false;
if (!isStartElement()) raiseError("readI18nText called without startelement!");
QString startTag = name().toString();
while (!atEnd()) {
readNext();
if (isWhitespace() || isComment()) continue;
if (isStartElement() && name() == startTag) {
raiseError("Invalid nested elements.");
return "Error";
}
if (isEndElement() && name() == startTag) {
if (!unindent) {
return result.trimmed();
} else {
QStringList splitted = result.split("\n");
int i;
int minspaces = 0xffff;
/* NOTE: First line contains no leading whitespace so we start at 1 */
for (i = 1; i < splitted.size(); i++) {
if (splitted[i].trimmed().isEmpty()) continue;
int spaces;
QString tmp = splitted[i];
for (spaces = 0; spaces < tmp.length(); spaces++) {
if (!tmp[spaces].isSpace()) break;
}
if (spaces < minspaces) minspaces = spaces;
}
for (i = 1; i < splitted.size(); i++) {
splitted[i] = splitted[i].mid(minspaces);
}
return splitted.join("\n").trimmed();
}
}
if (i18n) {
if (isEndElement()) {
if (name() == "i18n") {
i18n = false;
} else {
raiseError("Invalid end element "+ name().toString());
}
continue;
}
if (!isStartElement()) {
raiseError("Unexpected data!");
}
if (name() == language) {
matchquality = 2; //Perfect match
result = readElementText();
} else if (language.startsWith(name().toString()) && matchquality <= 1) {
matchquality = 1; //Only beginning part matches
result = readElementText();
} else if (result.isEmpty()) {
matchquality = 0;
result = readElementText();
} else {
readElementText(); //Ignore the text
}
} else {
if (isStartElement()) {
if (name() == "i18n") {
i18n = true;
continue;
} else {
raiseError("Tag " + name().toString() +
"found, but \"i18n\" or string data expected.");
continue;
}
} else if (isCharacters()) {
result = result + text().toString();
}
}
}
raiseError("Unexpected end of XML file");
return result;
}
LatexConfigCache* LatexConfigCache::_instance = 0;
LatexConfigCache* LatexConfigCache::instance()
{
if (!_instance) {
_instance = new LatexConfigCache();
}
return _instance;
}
LatexConfigParser* LatexConfigCache::parser(QString filename, bool warnOnError)
{
if (parsers.contains(filename)) {
if (warnOnError && error[filename]) {
//Recreate element as error might have been fixed.
delete parsers[filename];
createParser(filename, warnOnError);
}
} else {
createParser(filename, warnOnError);
}
return parsers[filename];
}
void LatexConfigCache::createParser(QString filename, bool warnOnError)
{
LatexConfigParser *parser = new LatexConfigParser();
bool hasError = !parser->parseConfigFile(filename);
parsers[filename] = parser;
error[filename] = hasError;
if (hasError) {
QMessageBox::critical(0, QObject::tr("Error"), "<qt>" +
QObject::tr("Parsing the configfile %1 failed! Depending on the type of the error "
"render frames might not work correctly!\n%2").arg(
filename, parser->error())
+ "</qt>", 1, 0, 0);
}
}
bool LatexConfigCache::hasError(QString filename)
{
if (!error.contains(filename)) {
return true;
}
return error[filename];
}
QStringList LatexConfigCache::defaultConfigs()
{
QDir dir(LatexConfigParser::configBase());
QStringList files;
files = dir.entryList(QStringList("*.xml"));
files.sort();
int i;
for (i = 0; i < files.size(); i++) {
if (files[i].compare("sample.xml",Qt::CaseInsensitive)==0) {
files.removeAt(i);
i--;
}
}
return files;
}
|