]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-latex.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / export-latex.cpp
1 #include "export-latex.h"
2
3 #include "mainwindow.h"
4 #include <QMessageBox>
5
6 extern Main *mainWindow;
7 extern Settings settings;
8
9 ExportLaTeX::ExportLaTeX()
10 {
11     exportName = "LaTeX";
12     filter = "LaTeX files (*.tex);;All (* *.*)";
13
14     // Note: key in hash on left side is the regular expression, which
15     // will be replaced by string on right side
16     // E.g. a literal $ will be replaced by \$
17     esc["\\$"] = "\\$";
18     esc["\\^"] = "\\^";
19     esc["%"] = "\\%";
20     esc["&"] = "\\&";
21     esc["~"] = "\\~";
22     esc["_"] = "\\_";
23     esc["\\\\"] = "\\";
24     esc["\\{"] = "\\{";
25     esc["\\}"] = "\\}";
26 }
27
28 QString ExportLaTeX::escapeLaTeX(const QString &s)
29 {
30     QString r = s;
31
32     QRegExp rx;
33     rx.setMinimal(true);
34
35     foreach (QString p, esc.keys()) {
36         rx.setPattern(p);
37         r.replace(rx, esc[p]);
38     }
39     return r;
40 }
41
42 void ExportLaTeX::doExport()
43 {
44     // Exports a map to a LaTex file.
45     // This file needs to be included
46     // or inported into a LaTex document
47     // it will not add a preamble, or anything
48     // that makes a full LaTex document.
49     QFile file(filePath);
50     if (!file.open(QIODevice::WriteOnly)) {
51         QMessageBox::critical(
52             0, QObject::tr("Critical Export Error"),
53             QObject::tr("Could not export as LaTeX to %1").arg(filePath));
54         mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
55         return;
56     }
57
58     // Read default section names
59     QStringList sectionNames;
60     sectionNames << ""
61                  << "chapter"
62                  << "section"
63                  << "subsection"
64                  << "subsubsection"
65                  << "paragraph";
66
67     for (int i = 0; i < 6; i++)
68         sectionNames.replace(
69             i, settings
70                    .value(QString("/export/latex/sectionName-%1").arg(i),
71                           sectionNames.at(i))
72                    .toString());
73
74     QString out;
75
76     // Main loop over all branches
77     QString s;
78     BranchItem *cur = NULL;
79     BranchItem *prev = NULL;
80     model->nextBranch(cur, prev);
81     while (cur) {
82         if (!cur->hasHiddenExportParent()) {
83             int d = cur->depth();
84             s = escapeLaTeX(cur->getHeadingPlain());
85             if (sectionNames.at(d).isEmpty() || d >= sectionNames.count())
86                 out += s + "\n";
87             else {
88                 out += "\n";
89                 out += "\\" + sectionNames.at(d) + "{" + s + "}";
90                 out += "\n";
91             }
92             // If necessary, write note
93             if (!cur->isNoteEmpty()) {
94                 out += (cur->getNoteASCII());
95                 out += "\n";
96             }
97         }
98         model->nextBranch(cur, prev);
99     }
100
101     QTextStream ts(&file);
102     ts.setCodec("UTF-8");
103     ts << out;
104     file.close();
105
106     QClipboard *clipboard = QGuiApplication::clipboard();
107     clipboard->setText(out);
108
109     result = ExportBase::Success;
110
111     displayedDestination = filePath;
112     completeExport();
113 }