-
Notifications
You must be signed in to change notification settings - Fork 6
/
export-latex.cpp
116 lines (99 loc) · 2.87 KB
/
export-latex.cpp
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
#include "export-latex.h"
#include <QMessageBox>
#include "mainwindow.h"
extern Main *mainWindow;
extern Settings settings;
ExportLaTeX::ExportLaTeX()
{
exportName="LaTeX";
filter="LaTeX files (*.tex);;All (* *.*)";
// Note: key in hash on left side is the regular expression, which
// will be replaced by string on right side
// E.g. a literal $ will be replaced by \$
esc["\\$"]="\\$";
esc["\\^"]="\\^";
esc["%"]="\\%";
esc["&"]="\\&";
esc["~"]="\\~";
esc["_"]="\\_";
esc["\\\\"]="\\";
esc["\\{"]="\\{";
esc["\\}"]="\\}";
}
QString ExportLaTeX::escapeLaTeX(const QString &s)
{
QString r=s;
QRegExp rx;
rx.setMinimal(true);
foreach (QString p,esc.keys() )
{
rx.setPattern (p);
r.replace (rx, esc[p] );
}
return r;
}
void ExportLaTeX::doExport()
{
// Exports a map to a LaTex file.
// This file needs to be included
// or inported into a LaTex document
// it will not add a preamble, or anything
// that makes a full LaTex document.
QFile file (filePath);
if ( !file.open( QIODevice::WriteOnly ) ) {
QMessageBox::critical (
0,
QObject::tr("Critical Export Error"),
QObject::tr("Could not export as LaTeX to %1").arg(filePath));
mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
return;
}
// Read default section names
QStringList sectionNames;
sectionNames << ""
<< "chapter"
<< "section"
<< "subsection"
<< "subsubsection"
<< "paragraph";
for (int i=0; i<6; i++)
sectionNames.replace(i,settings.value(
QString("/export/latex/sectionName-%1").arg(i),sectionNames.at(i)).toString() );
QString out;
// Main loop over all branches
QString s;
BranchItem *cur=NULL;
BranchItem *prev=NULL;
model->nextBranch(cur,prev);
while (cur)
{
if (!cur->hasHiddenExportParent() )
{
int d=cur->depth();
s=escapeLaTeX (cur->getHeadingPlain() );
if ( sectionNames.at(d).isEmpty() || d>=sectionNames.count() )
out += s + "\n";
else
{
out += "\n";
out += "\\" + sectionNames.at(d) + "{" + s + "}";
out += "\n";
}
// If necessary, write note
if (!cur->isNoteEmpty()) {
out += (cur->getNoteASCII());
out += "\n";
}
}
model->nextBranch(cur,prev);
}
QTextStream ts( &file );
ts.setCodec("UTF-8");
ts << out;
file.close();
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(out);
success = true;
destination = filePath;
completeExport();
}