Skip to content

Commit

Permalink
Add render function which does not escape html characters
Browse files Browse the repository at this point in the history
  • Loading branch information
poelzi committed Jan 20, 2021
1 parent 8d90ae9 commit 74f571d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/library/export/trackexportworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ QString TrackExportWorker::applyPattern(
m_context->insert(QStringLiteral("index"), QVariant(index));
m_context->insert(QStringLiteral("dup"), QVariant(duplicateCounter));

QString newName = m_template->render(m_context);
QString newName = Formatter::renderNoEscape(m_template, *m_context);

// remove the context stack so it is clean again
m_context->pop();
Expand Down
32 changes: 32 additions & 0 deletions src/util/formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
#include "util/formatter.h"

#include <grantlee/context.h>
#include <grantlee/engine.h>
#include <grantlee/outputstream.h>
#include <grantlee/template.h>

#include <QSharedPointer>
#include <QTextStream>

class NoEscapeStream : public Grantlee::OutputStream {
public:
NoEscapeStream()
: Grantlee::OutputStream() {
}
NoEscapeStream(QTextStream* stream)
: Grantlee::OutputStream(stream) {
}
~NoEscapeStream() override{};

QString escape(const QString& input) const override {
return input;
}
QSharedPointer<Grantlee::OutputStream> clone(QTextStream* stream) const override {
return QSharedPointer<NoEscapeStream>::create(stream);
}
};

Grantlee::Engine* Formatter::getEngine(QObject* parent) {
auto engine = new Grantlee::Engine(parent);
// register custom
return engine;
}

QString Formatter::renderNoEscape(Grantlee::Template& tmpl, Grantlee::Context& context) {
auto rendered = QString();
auto stream = QTextStream(&rendered);
NoEscapeStream os(&stream);
tmpl->render(&os, &context);
return rendered;
}
6 changes: 6 additions & 0 deletions src/util/formatter.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#pragma once

#include <grantlee/engine.h>
#include <grantlee/template.h>

#include <QObject>
#include <QString>

class Formatter {
public:
static Grantlee::Engine* getEngine(QObject* parent);
// render template without escaping html characters
static QString renderNoEscape(
Grantlee::Template& templ,
Grantlee::Context& context);
};

0 comments on commit 74f571d

Please sign in to comment.