21 #include "scriptabletags.h" 23 #include <QtCore/QFile> 26 #include <QtQml/QJSEngine> 27 #include <QtQml/QJSValueIterator> 29 #include "nodebuiltins_p.h" 32 #include "exception.h" 34 #include "scriptablefilter.h" 35 #include "scriptablefilterexpression.h" 36 #include "scriptablenode.h" 37 #include "scriptablesafestring.h" 38 #include "scriptabletemplate.h" 39 #include "scriptablevariable.h" 44 Q_DECLARE_METATYPE(
Token)
56 ssObj->setSafety(
true);
68 QJSValue ScriptableHelperFunctions::ScriptableFilterExpressionConstructor(
75 object->init(name, p);
81 ScriptableHelperFunctions::ScriptableNodeConstructor(
QJSValue callContext)
85 auto scriptableNodeName = it.value().toString();
95 auto renderMethod = concreteNode.
property(QStringLiteral(
"render"));
98 object->setObjectName(scriptableNodeName);
99 object->setScriptEngine(m_scriptEngine);
100 object->init(concreteNode, renderMethod);
104 QJSValue ScriptableHelperFunctions::ScriptableTemplateConstructor(
113 auto t = templateEngine->newTemplate(content, name);
119 QJSValue ScriptableHelperFunctions::ScriptableVariableConstructor(
QString name)
127 object->setContent(name);
132 ScriptableTagLibrary::ScriptableTagLibrary(
QObject *parent)
134 m_functions(m_scriptEngine->newQObject(
138 QStringLiteral(
"internalCuteleeFunctions"), m_functions);
141 m_scriptEngine->globalObject().setProperty(
142 QStringLiteral(
"Node"),
143 m_scriptEngine->evaluate(QStringLiteral(R
"javascript( 145 return internalCuteleeFunctions.ScriptableNodeConstructor( 146 Array.prototype.slice.call(arguments)); 151 m_scriptEngine->globalObject().setProperty(
152 QStringLiteral(
"Variable"),
153 m_functions.property(QStringLiteral(
"ScriptableVariableConstructor")));
156 m_scriptEngine->globalObject().setProperty(
157 QStringLiteral(
"FilterExpression"),
158 m_functions.property(
159 QStringLiteral(
"ScriptableFilterExpressionConstructor")));
162 m_scriptEngine->globalObject().setProperty(
163 QStringLiteral(
"Template"),
164 m_functions.property(QStringLiteral(
"ScriptableTemplateConstructor")));
167 auto libraryObject = m_scriptEngine->newQObject(
this);
168 m_scriptEngine->globalObject().setProperty(QStringLiteral(
"Library"),
173 auto nodeFactoryObject = m_scriptEngine->newQObject(nodeFactory);
174 m_scriptEngine->globalObject().setProperty(
175 QStringLiteral(
"AbstractNodeFactory"), nodeFactoryObject);
178 m_scriptEngine->globalObject().setProperty(
179 QStringLiteral(
"mark_safe"),
180 m_functions.property(QStringLiteral(
"markSafeFunction")));
183 bool ScriptableTagLibrary::evaluateScript(
const QString &name)
185 QFile scriptFile(name);
187 if (!scriptFile.exists()
193 fstream.setCodec(
"UTF-8");
194 const auto fileContent = fstream.readAll();
208 m_factoryNames.
clear();
209 m_nodeFactories.clear();
212 if (!evaluateScript(name)) {
216 return getFactories();
221 m_filterNames.
clear();
225 if (!evaluateScript(name)) {
235 for (
auto it = m_factoryNames.
begin(), end = m_factoryNames.
end(); it != end;
237 auto factoryName = it.
value();
238 auto tagName = it.key();
243 snf->setScriptEngine(m_scriptEngine);
244 snf->setFactory(factoryObject);
246 factories.
insert(tagName, snf);
256 for (
auto &filterNameString : m_filterNames) {
262 filters.insert(filterName, filter);
268 void ScriptableTagLibrary::addFactory(
const QString &factoryName,
271 m_factoryNames.
insert(tagName, factoryName);
274 void ScriptableTagLibrary::addFilter(
const QString &filterName)
276 m_filterNames << filterName;
QHash::iterator insert(const Key &key, const T &value)
QObject * toQObject() const const
Cutelee::SafeString markSafe(const Cutelee::SafeString &input)
The Cutelee namespace holds all public Cutelee API.
QJSValue newQObject(QObject *object)
QString toString() const const
bool isError() const const
QJSValue property(const QString &name) const const
bool isString() const const
QVariant property(const char *name) const const
Cutelee::Engine is the main entry point for creating Cutelee Templates.
QJSValue evaluate(const QString &program, const QString &fileName, int lineNumber)
Utility functions used throughout Cutelee.
QHash< QString, Filter * > filters(const QString &name={}) override
const T value(const Key &key) const const
QJSValue callAsConstructor(const QJSValueList &args)
The Parser class processes a string template into a tree of nodes.
QJSValue globalObject() const const
bool isQObject() const const
bool setProperty(const char *name, const QVariant &value)
QHash< QString, AbstractNodeFactory * > nodeFactories(const QString &name={}) override
T qobject_cast(QObject *object)
QObject * parent() const const
An exception for use when implementing template tags.