]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-markdown.cpp
New upstream version 2.9.22
[sven/vym.git] / src / export-markdown.cpp
1 #include "export-markdown.h"
2
3 #include "mainwindow.h"
4 #include <QMessageBox>
5
6 extern QString vymName;
7 extern Main *mainWindow;
8
9 ExportMarkdown::ExportMarkdown()
10 {
11     exportName = "Markdown";
12     filter = "TXT (*.txt);;All (* *.*)";
13     caption = vymName + " -" + QObject::tr("Export as Markdown");
14 }
15
16 void ExportMarkdown::doExport()
17 {
18     QFile file(filePath);
19     if (!file.open(QIODevice::WriteOnly)) {
20         QMessageBox::critical(
21             0, QObject::tr("Critical Export Error"),
22             QObject::tr("Could not export as Markdown to %1").arg(filePath));
23         mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
24         return;
25     }
26
27     QString out;
28
29     // Main loop over all branches
30     QString s;
31     QString curIndent;
32     QString dashIndent;
33     int i;
34     BranchItem *cur = NULL;
35     BranchItem *prev = NULL;
36
37     QString curHeading;
38
39     int lastDepth = 0;
40
41     QStringList tasks;
42
43     model->nextBranch(cur, prev);
44     while (cur) {
45         if (cur->getType() == TreeItem::Branch ||
46             cur->getType() == TreeItem::MapCenter) {
47             // Insert newline after previous list
48             if (cur->depth() < lastDepth)
49                 out += "\n";
50
51             // Make indentstring
52             curIndent = "";
53             for (i = 1; i < cur->depth() - 1; i++)
54                 curIndent += indentPerDepth;
55
56             curHeading = cur->getHeadingText();
57
58             // If necessary, write heading as URL
59             if (!cur->getURL().isEmpty())
60                 curHeading = "[" + curHeading + "](" + cur->getURL() + ")";
61
62             if (!cur->hasHiddenExportParent()) {
63                 // qDebug() << "ExportMarkdown::
64                 // "<<curIndent.toStdString()<<cur->curHeading.toStdString();
65
66                 dashIndent = "";
67                 switch (cur->depth()) {
68                 case 0:
69                     out += underline(curHeading, QString("="));
70                     out += "\n";
71                     break;
72                 case 1:
73                     out += "\n";
74                     out += (underline(curHeading, QString("-")));
75                     out += "\n";
76                     break;
77                 case 2:
78                     out += "\n";
79                     out += (curIndent + "### " + curHeading);
80                     out += "\n";
81                     dashIndent = "  ";
82                     break;
83                 case 3:
84                     out += (curIndent + "- " + curHeading);
85                     out += "\n";
86                     dashIndent = "  ";
87                     break;
88                 default:
89                     out += (curIndent + "- " + curHeading);
90                     out += "\n";
91                     dashIndent = "  ";
92                     break;
93                 }
94
95                 // If there is a task, save it for potential later display
96                 if (listTasks && cur->getTask()) {
97                     tasks.append(QString("[%1]: %2")
98                                      .arg(cur->getTask()->getStatusString())
99                                      .arg(curHeading));
100                 }
101
102                 // If necessary, write vymlink
103                 if (!cur->getVymLink().isEmpty())
104                     out += (curIndent + dashIndent + cur->getVymLink()) +
105                            " (vym mindmap)\n";
106
107                 // If necessary, write note
108                 if (!cur->isNoteEmpty()) {
109                     // curIndent +="  | ";
110                     // Only indent for bullet points
111                     if (cur->depth() > 2)
112                         curIndent += "  ";
113                     out += '\n' + cur->getNoteASCII(curIndent, 80);
114                 }
115                 lastDepth = cur->depth();
116             }
117         }
118         model->nextBranch(cur, prev);
119     }
120
121     if (listTasks) {
122         out += "\n\nTasks\n-----\n\n";
123
124         foreach (QString t, tasks) {
125             out += " - " + t + "\n";
126         }
127     }
128     QTextStream ts(&file);
129     ts.setCodec("UTF-8");
130     ts << out;
131     file.close();
132
133     QClipboard *clipboard = QGuiApplication::clipboard();
134     clipboard->setText(out);
135
136     QString listTasksString = listTasks ? "true" : "false";
137
138     displayedDestination = filePath;
139
140     result = ExportBase::Success;
141
142     QStringList args;
143     args << filePath;
144     args << listTasksString;
145
146     completeExport(args);
147 }
148
149 QString ExportMarkdown::underline(const QString &text, const QString &line)
150 {
151     QString r = text + "\n";
152     for (int j = 0; j < text.length(); j++)
153         r += line;
154     return r;
155 }