bugfix: VImagePreviewer to preview images with optional title

Do not preview images in code block.
This commit is contained in:
Le Tan 2017-05-09 19:35:51 +08:00
parent bf55ba01ce
commit 7a4d86eca9
4 changed files with 28 additions and 4 deletions

View File

@ -24,6 +24,8 @@ extern VConfigManager vconfig;
const QVector<QPair<QString, QString>> VUtils::c_availableLanguages = {QPair<QString, QString>("en_US", "Englisth(US)"),
QPair<QString, QString>("zh_CN", "Chinese")};
const QString VUtils::c_imageLinkRegExp = QString("\\!\\[([^\\]]*)\\]\\(([^\\)\"]+)\\s*(\".*\")?\\s*\\)");
VUtils::VUtils()
{
}

View File

@ -58,6 +58,14 @@ public:
static QChar keyToChar(int p_key);
static QString getLocale();
// Regular expression for image link.
// ![image title]( http://github.com/tamlok/vnote.jpg "alt text" )
// Captured texts (need to be trimmed):
// 1. Image Alt Text (Title);
// 2. Image URL;
// 3. Image Optional Title with double quotes;
static const QString c_imageLinkRegExp;
private:
// <value, name>
static const QVector<QPair<QString, QString>> c_availableLanguages;

View File

@ -10,6 +10,7 @@
#include "utils/vutils.h"
#include "vfile.h"
#include "vdownloader.h"
#include "hgmarkdownhighlighter.h"
extern VConfigManager vconfig;
@ -72,6 +73,11 @@ void VImagePreviewer::handleContentChange(int /* p_position */,
m_timer->start();
}
bool VImagePreviewer::isNormalBlock(const QTextBlock &p_block)
{
return p_block.userState() == HighlightBlockState::Normal;
}
void VImagePreviewer::previewImages()
{
if (m_isPreviewing) {
@ -86,7 +92,7 @@ void VImagePreviewer::previewImages()
while (block.isValid() && m_enablePreview) {
if (isImagePreviewBlock(block)) {
// Image preview block. Check if it is parentless.
if (!isValidImagePreviewBlock(block)) {
if (!isValidImagePreviewBlock(block) || !isNormalBlock(block)) {
QTextBlock nblock = block.next();
removeBlock(block);
block = nblock;
@ -96,7 +102,11 @@ void VImagePreviewer::previewImages()
} else {
clearCorruptedImagePreviewBlock(block);
if (isNormalBlock(block)) {
block = previewImageOfOneBlock(block);
} else {
block = block.next();
}
}
}
@ -158,7 +168,8 @@ bool VImagePreviewer::isValidImagePreviewBlock(QTextBlock &p_block)
QString VImagePreviewer::fetchImageUrlToPreview(const QString &p_text)
{
QRegExp regExp("\\!\\[[^\\]]*\\]\\(([^\\)]+)\\)");
QRegExp regExp(VUtils::c_imageLinkRegExp);
int index = regExp.indexIn(p_text);
if (index == -1) {
return QString();
@ -169,7 +180,7 @@ QString VImagePreviewer::fetchImageUrlToPreview(const QString &p_text)
return QString();
}
return regExp.capturedTexts()[1];
return regExp.capturedTexts()[2].trimmed();
}
QString VImagePreviewer::fetchImagePathToPreview(const QString &p_text)

View File

@ -93,6 +93,9 @@ private:
// Return true if and only if there is update.
bool updateImageWidth(QTextImageFormat &p_format);
// Whether it is a normal block or not.
bool isNormalBlock(const QTextBlock &p_block);
VMdEdit *m_edit;
QTextDocument *m_document;
VFile *m_file;