103 lines
3.2 KiB
C++
103 lines
3.2 KiB
C++
#include "denote_extractor.h"
|
|
|
|
#include "denote_frontmatter.h"
|
|
#include "denote_metadata.h"
|
|
|
|
#include <KFileMetaData/ExtractionResult>
|
|
#include <KFileMetaData/Properties>
|
|
#include <KFileMetaData/Types>
|
|
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
|
|
DenoteExtractor::DenoteExtractor(QObject *parent)
|
|
: KFileMetaData::ExtractorPlugin(parent)
|
|
{
|
|
}
|
|
|
|
QStringList DenoteExtractor::mimetypes() const
|
|
{
|
|
return {
|
|
QStringLiteral("text/markdown"),
|
|
QStringLiteral("text/x-org"),
|
|
QStringLiteral("text/plain"),
|
|
};
|
|
}
|
|
|
|
void DenoteExtractor::extract(KFileMetaData::ExtractionResult *result)
|
|
{
|
|
const QString path = result->inputUrl();
|
|
const QFileInfo fi(path);
|
|
|
|
// Only process files that look like denote files (have an identifier)
|
|
Denote::Metadata meta = Denote::parseFilename(path);
|
|
if (!meta.isValid())
|
|
return;
|
|
|
|
// Start with filename-derived metadata
|
|
QString title = meta.title;
|
|
QStringList tags = meta.tags;
|
|
QString signature = meta.signature;
|
|
QString identifier = meta.identifier;
|
|
|
|
// Try to get richer metadata from frontmatter
|
|
const QString ext = QStringLiteral(".") + fi.suffix();
|
|
Denote::FileType fileType = Denote::fileTypeFromExtension(ext);
|
|
|
|
QByteArray content;
|
|
if (fileType != Denote::FileType::Unknown) {
|
|
QFile file(path);
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
content = file.readAll();
|
|
file.close();
|
|
|
|
Denote::FrontMatter fm = Denote::parseFrontMatter(content, fileType);
|
|
// Prefer frontmatter values over filename-derived ones
|
|
if (!fm.title.isEmpty())
|
|
title = fm.title;
|
|
if (!fm.tags.isEmpty())
|
|
tags = fm.tags;
|
|
if (!fm.signature.isEmpty())
|
|
signature = fm.signature;
|
|
if (!fm.identifier.isEmpty())
|
|
identifier = fm.identifier;
|
|
}
|
|
}
|
|
|
|
// Emit metadata properties
|
|
if (result->inputFlags() & KFileMetaData::ExtractionResult::ExtractMetaData) {
|
|
result->addType(KFileMetaData::Type::Document);
|
|
|
|
if (!title.isEmpty())
|
|
result->add(KFileMetaData::Property::Title, title);
|
|
|
|
if (!tags.isEmpty())
|
|
result->add(KFileMetaData::Property::Keywords, tags.join(QStringLiteral(", ")));
|
|
|
|
if (!identifier.isEmpty()) {
|
|
QDateTime dt = QDateTime::fromString(identifier, QStringLiteral("yyyyMMdd'T'HHmmss"));
|
|
if (dt.isValid())
|
|
result->add(KFileMetaData::Property::CreationDate, dt);
|
|
}
|
|
|
|
if (!signature.isEmpty())
|
|
result->add(KFileMetaData::Property::Subject, signature);
|
|
}
|
|
|
|
// Emit body text for full-text indexing
|
|
if (result->inputFlags() & KFileMetaData::ExtractionResult::ExtractPlainText) {
|
|
if (content.isEmpty() && fileType != Denote::FileType::Unknown) {
|
|
QFile file(path);
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
content = file.readAll();
|
|
file.close();
|
|
}
|
|
}
|
|
if (!content.isEmpty()) {
|
|
QString body = Denote::extractBody(content, fileType);
|
|
if (!body.isEmpty())
|
|
result->append(body);
|
|
}
|
|
}
|
|
}
|