X-Git-Url: https://git.sven.stormbind.net/?a=blobdiff_plain;f=src%2Fexport-markdown.cpp;fp=src%2Fexport-markdown.cpp;h=b9c70dda1d9f7c094a23a26f52b0cdf3fd0496dc;hb=d483bd8e6523c23c6f1d8908a2e0611c2bc9ff4f;hp=0000000000000000000000000000000000000000;hpb=7dfa3fe589d1722d49681f42cdb0bf1e6efb5223;p=sven%2Fvym.git diff --git a/src/export-markdown.cpp b/src/export-markdown.cpp new file mode 100644 index 0000000..b9c70dd --- /dev/null +++ b/src/export-markdown.cpp @@ -0,0 +1,155 @@ +#include "export-markdown.h" + +#include "mainwindow.h" +#include + +extern QString vymName; +extern Main *mainWindow; + +ExportMarkdown::ExportMarkdown() +{ + exportName = "Markdown"; + filter = "TXT (*.txt);;All (* *.*)"; + caption = vymName + " -" + QObject::tr("Export as Markdown"); +} + +void ExportMarkdown::doExport() +{ + QFile file(filePath); + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::critical( + 0, QObject::tr("Critical Export Error"), + QObject::tr("Could not export as Markdown to %1").arg(filePath)); + mainWindow->statusMessage(QString(QObject::tr("Export failed."))); + return; + } + + QString out; + + // Main loop over all branches + QString s; + QString curIndent; + QString dashIndent; + int i; + BranchItem *cur = NULL; + BranchItem *prev = NULL; + + QString curHeading; + + int lastDepth = 0; + + QStringList tasks; + + model->nextBranch(cur, prev); + while (cur) { + if (cur->getType() == TreeItem::Branch || + cur->getType() == TreeItem::MapCenter) { + // Insert newline after previous list + if (cur->depth() < lastDepth) + out += "\n"; + + // Make indentstring + curIndent = ""; + for (i = 1; i < cur->depth() - 1; i++) + curIndent += indentPerDepth; + + curHeading = cur->getHeadingText(); + + // If necessary, write heading as URL + if (!cur->getURL().isEmpty()) + curHeading = "[" + curHeading + "](" + cur->getURL() + ")"; + + if (!cur->hasHiddenExportParent()) { + // qDebug() << "ExportMarkdown:: + // "<curHeading.toStdString(); + + dashIndent = ""; + switch (cur->depth()) { + case 0: + out += underline(curHeading, QString("=")); + out += "\n"; + break; + case 1: + out += "\n"; + out += (underline(curHeading, QString("-"))); + out += "\n"; + break; + case 2: + out += "\n"; + out += (curIndent + "### " + curHeading); + out += "\n"; + dashIndent = " "; + break; + case 3: + out += (curIndent + "- " + curHeading); + out += "\n"; + dashIndent = " "; + break; + default: + out += (curIndent + "- " + curHeading); + out += "\n"; + dashIndent = " "; + break; + } + + // If there is a task, save it for potential later display + if (listTasks && cur->getTask()) { + tasks.append(QString("[%1]: %2") + .arg(cur->getTask()->getStatusString()) + .arg(curHeading)); + } + + // If necessary, write vymlink + if (!cur->getVymLink().isEmpty()) + out += (curIndent + dashIndent + cur->getVymLink()) + + " (vym mindmap)\n"; + + // If necessary, write note + if (!cur->isNoteEmpty()) { + // curIndent +=" | "; + // Only indent for bullet points + if (cur->depth() > 2) + curIndent += " "; + out += '\n' + cur->getNoteASCII(curIndent, 80); + } + lastDepth = cur->depth(); + } + } + model->nextBranch(cur, prev); + } + + if (listTasks) { + out += "\n\nTasks\n-----\n\n"; + + foreach (QString t, tasks) { + out += " - " + t + "\n"; + } + } + QTextStream ts(&file); + ts.setCodec("UTF-8"); + ts << out; + file.close(); + + QClipboard *clipboard = QGuiApplication::clipboard(); + clipboard->setText(out); + + QString listTasksString = listTasks ? "true" : "false"; + + displayedDestination = filePath; + + result = ExportBase::Success; + + QStringList args; + args << filePath; + args << listTasksString; + + completeExport(args); +} + +QString ExportMarkdown::underline(const QString &text, const QString &line) +{ + QString r = text + "\n"; + for (int j = 0; j < text.length(); j++) + r += line; + return r; +}