From 030625686359a091533639b0cb5ca43ce2b97cb3 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 16 Nov 2016 21:53:55 +0800 Subject: [PATCH] delete local images when deleting markdown file Signed-off-by: Le Tan --- src/utils/vutils.cpp | 23 +++++++++++++++++++++++ src/utils/vutils.h | 1 + src/vfilelist.cpp | 20 ++++++++++++++++++++ src/vfilelist.h | 1 + 4 files changed, 45 insertions(+) diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index adb79ecb..c70bef93 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -3,6 +3,7 @@ #include #include #include +#include VUtils::VUtils() { @@ -121,3 +122,25 @@ QString VUtils::basePathFromPath(const QString &path) { return QFileInfo(path).path(); } + +// Collect image links like ![](images/xx.jpg) +QVector VUtils::imagesFromMarkdownFile(const QString &filePath) +{ + Q_ASSERT(isMarkdown(filePath)); + QVector images; + if (filePath.isEmpty()) { + return images; + } + QString basePath = basePathFromPath(filePath); + QString text = readFileFromDisk(filePath); + QRegExp regExp("\\!\\[[^\\]]*\\]\\((images/[^/\\)]+)\\)"); + int pos = 0; + + while (pos < text.size() && (pos = regExp.indexIn(text, pos)) != -1) { + Q_ASSERT(regExp.captureCount() == 1); + qDebug() << regExp.capturedTexts()[0] << regExp.capturedTexts()[1]; + images.append(QDir(basePath).filePath(regExp.capturedTexts()[1])); + pos += regExp.matchedLength(); + } + return images; +} diff --git a/src/utils/vutils.h b/src/utils/vutils.h index b8295c3e..8ce1c048 100644 --- a/src/utils/vutils.h +++ b/src/utils/vutils.h @@ -23,6 +23,7 @@ public: static inline QString directoryNameFromPath(const QString& path); static QString fileNameFromPath(const QString &path); static QString basePathFromPath(const QString &path); + static QVector imagesFromMarkdownFile(const QString &filePath); private: static inline void addQssVarToMap(QVector > &map, const QString &key, const QString &value); diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index 6a63d227..da152638 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -355,6 +355,9 @@ void VFileList::deleteFileAndUpdateList(const QString &p_notebook, return; } + // Delete local images in ./images + deleteLocalImages(filePath); + // Delete the file QFile file(filePath); if (!file.remove()) { @@ -539,3 +542,20 @@ void VFileList::convertFileType(const QString ¬ebook, const QString &fileRela } VUtils::writeFileToDisk(filePath, fileText); } + +void VFileList::deleteLocalImages(const QString &filePath) +{ + if (!VUtils::isMarkdown(filePath)) { + return; + } + + QVector images = VUtils::imagesFromMarkdownFile(filePath); + int deleted = 0; + for (int i = 0; i < images.size(); ++i) { + QFile file(images[i]); + if (file.remove()) { + ++deleted; + } + } + qDebug() << "delete" << deleted << "images for" << filePath; +} diff --git a/src/vfilelist.h b/src/vfilelist.h index 8038aeab..8215e61e 100644 --- a/src/vfilelist.h +++ b/src/vfilelist.h @@ -63,6 +63,7 @@ private: void convertFileType(const QString ¬ebook, const QString &fileRelativePath, DocType oldType, DocType newType); QListWidgetItem *findItem(const QString &p_notebook, const QString &p_relativePath); + void deleteLocalImages(const QString &filePath); VNote *vnote; QString notebook;