gui: PreviewGeneratorPlainText: Move snippet gen to own function for reuse

This commit is contained in:
Albert S. 2022-07-24 11:34:52 +02:00
parent ac4d7dd0a5
commit fdbf3a7358
2 changed files with 21 additions and 15 deletions

View File

@ -3,19 +3,10 @@
#include "previewgeneratorplaintext.h"
#include "previewresultplaintext.h"
QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath,
unsigned int page)
QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderConfig config, QString fileName)
{
PreviewResultPlainText *result = new PreviewResultPlainText(documentPath, page);
QFile file(documentPath);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
return QSharedPointer<PreviewResultPlainText>(result);
}
QTextStream in(&file);
QString resulText = "";
QString content = in.readAll();
QMap<int, QString> snippet;
int coveredRange = 0;
@ -71,9 +62,7 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
resulText.replace(word, "<span style=\"background-color: yellow;\">" + word + "</span>", Qt::CaseInsensitive);
}
QFileInfo info{documentPath};
QString header = "<b>" + info.fileName() + "</b> ";
QString header = "<b>" + fileName + "</b> ";
for(QString &word : config.wordsToHighlight)
{
header += word + ": " + QString::number(countmap[word]) + " ";
@ -85,6 +74,22 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
header += "<hr>";
result->setText(header + resulText.replace("\n", "<br>").mid(0, 1000));
return header + resulText.replace("\n", "<br>").mid(0, 1000);
}
QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath,
unsigned int page)
{
PreviewResultPlainText *result = new PreviewResultPlainText(documentPath, page);
QFile file(documentPath);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
return QSharedPointer<PreviewResultPlainText>(result);
}
QTextStream in(&file);
QString content = in.readAll();
QFileInfo info{documentPath};
result->setText(generatePreviewText(content, config, info.fileName()));
return QSharedPointer<PreviewResultPlainText>(result);
}

View File

@ -6,6 +6,7 @@ class PreviewGeneratorPlainText : public PreviewGenerator
{
public:
using PreviewGenerator::PreviewGenerator;
QString generatePreviewText(QString content, RenderConfig config, QString fileName);
QSharedPointer<PreviewResult> generate(RenderConfig config, QString documentPath, unsigned int page);
};