delete local images when deleting markdown file

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-16 21:53:55 +08:00
parent a6e7a7e42d
commit 0306256863
4 changed files with 45 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <QDir> #include <QDir>
#include <QDebug> #include <QDebug>
#include <QRegularExpression> #include <QRegularExpression>
#include <QRegExp>
VUtils::VUtils() VUtils::VUtils()
{ {
@ -121,3 +122,25 @@ QString VUtils::basePathFromPath(const QString &path)
{ {
return QFileInfo(path).path(); return QFileInfo(path).path();
} }
// Collect image links like ![](images/xx.jpg)
QVector<QString> VUtils::imagesFromMarkdownFile(const QString &filePath)
{
Q_ASSERT(isMarkdown(filePath));
QVector<QString> 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;
}

View File

@ -23,6 +23,7 @@ public:
static inline QString directoryNameFromPath(const QString& path); static inline QString directoryNameFromPath(const QString& path);
static QString fileNameFromPath(const QString &path); static QString fileNameFromPath(const QString &path);
static QString basePathFromPath(const QString &path); static QString basePathFromPath(const QString &path);
static QVector<QString> imagesFromMarkdownFile(const QString &filePath);
private: private:
static inline void addQssVarToMap(QVector<QPair<QString, QString> > &map, static inline void addQssVarToMap(QVector<QPair<QString, QString> > &map,
const QString &key, const QString &value); const QString &key, const QString &value);

View File

@ -355,6 +355,9 @@ void VFileList::deleteFileAndUpdateList(const QString &p_notebook,
return; return;
} }
// Delete local images in ./images
deleteLocalImages(filePath);
// Delete the file // Delete the file
QFile file(filePath); QFile file(filePath);
if (!file.remove()) { if (!file.remove()) {
@ -539,3 +542,20 @@ void VFileList::convertFileType(const QString &notebook, const QString &fileRela
} }
VUtils::writeFileToDisk(filePath, fileText); VUtils::writeFileToDisk(filePath, fileText);
} }
void VFileList::deleteLocalImages(const QString &filePath)
{
if (!VUtils::isMarkdown(filePath)) {
return;
}
QVector<QString> 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;
}

View File

@ -63,6 +63,7 @@ private:
void convertFileType(const QString &notebook, const QString &fileRelativePath, void convertFileType(const QString &notebook, const QString &fileRelativePath,
DocType oldType, DocType newType); DocType oldType, DocType newType);
QListWidgetItem *findItem(const QString &p_notebook, const QString &p_relativePath); QListWidgetItem *findItem(const QString &p_notebook, const QString &p_relativePath);
void deleteLocalImages(const QString &filePath);
VNote *vnote; VNote *vnote;
QString notebook; QString notebook;