added txt support
This commit is contained in:
1 parent
64a70bc4cf
commit
21656a3e9c
10 files changed
+441
-120
No files matched your search
@@ -8,6 +8,8 @@
|
||||
#include <uchardet/uchardet.h>
|
||||
#include <iconv.h>
|
||||
|
||||
static constexpr int kChunkSize = 4096;
|
||||
|
||||
static QString decodeWithIconv(const QByteArray &raw, const char *charset)
|
||||
{
|
||||
iconv_t cd = iconv_open("UTF-8", charset);
|
||||
@@ -31,6 +33,69 @@ static QString decodeWithIconv(const QByteArray &raw, const char *charset)
|
||||
return QString::fromUtf8(out);
|
||||
}
|
||||
|
||||
// Convert a raw text fragment into HTML paragraphs.
|
||||
// Splits on blank lines (\n\n+) to create <p> blocks;
|
||||
// single newlines within a block become <br>.
|
||||
static QString textToHtml(const QString &text)
|
||||
{
|
||||
QString html;
|
||||
const QStringList blocks = text.split(QRegularExpression("\\n\\n+"));
|
||||
for (const QString &block : blocks) {
|
||||
QString escaped = block.toHtmlEscaped().trimmed();
|
||||
if (escaped.isEmpty())
|
||||
continue;
|
||||
escaped.replace('\n', QStringLiteral("<br>\n"));
|
||||
html += QStringLiteral("<p>") + escaped + QStringLiteral("</p>\n");
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
// Split raw text into chunks of approximately kChunkSize characters,
|
||||
// preferring to split at \n boundaries, falling back to whitespace.
|
||||
static QStringList splitRawText(const QString &content)
|
||||
{
|
||||
QStringList rawChunks;
|
||||
int pos = 0;
|
||||
const int len = content.size();
|
||||
|
||||
while (pos < len) {
|
||||
if (len - pos <= kChunkSize) {
|
||||
rawChunks.append(content.mid(pos));
|
||||
break;
|
||||
}
|
||||
|
||||
int end = pos + kChunkSize;
|
||||
|
||||
// Look backward for a newline to split at
|
||||
int splitAt = -1;
|
||||
for (int i = end; i > pos; --i) {
|
||||
if (content.at(i) == '\n') {
|
||||
splitAt = i + 1; // include the newline in the current chunk
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// No newline found — fall back to whitespace
|
||||
if (splitAt < 0) {
|
||||
for (int i = end; i > pos; --i) {
|
||||
if (content.at(i).isSpace()) {
|
||||
splitAt = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No whitespace either — hard split
|
||||
if (splitAt < 0)
|
||||
splitAt = end;
|
||||
|
||||
rawChunks.append(content.mid(pos, splitAt - pos));
|
||||
pos = splitAt;
|
||||
}
|
||||
|
||||
return rawChunks;
|
||||
}
|
||||
|
||||
bool TxtReader::open(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
@@ -89,18 +154,17 @@ Chapter TxtReader::loadChapter(int index)
|
||||
if (index != 0 || m_content.isEmpty())
|
||||
return Chapter{index, {}, {}};
|
||||
|
||||
// Split on blank lines to create paragraphs; single newlines become <br>
|
||||
QString html;
|
||||
const QStringList blocks = m_content.split(QRegularExpression("\\n\\n+"));
|
||||
for (const QString &block : blocks) {
|
||||
QString escaped = block.toHtmlEscaped().trimmed();
|
||||
if (escaped.isEmpty())
|
||||
continue;
|
||||
escaped.replace('\n', QStringLiteral("<br>\n"));
|
||||
html += QStringLiteral("<p>") + escaped + QStringLiteral("</p>\n");
|
||||
QStringList rawChunks = splitRawText(m_content);
|
||||
QStringList htmlChunks;
|
||||
htmlChunks.reserve(rawChunks.size());
|
||||
|
||||
for (const QString &raw : rawChunks) {
|
||||
QString html = textToHtml(raw);
|
||||
if (!html.isEmpty())
|
||||
htmlChunks.append(html);
|
||||
}
|
||||
|
||||
return Chapter{0, m_title, html};
|
||||
return Chapter{0, m_title, htmlChunks};
|
||||
}
|
||||
|
||||
QList<TocEntry> TxtReader::tableOfContents()
|
||||
|
||||
Reference in new issue
Block a user