Commit 81d5a72afed3b9466185ce55d625447ee030a4a5
- Diff rendering mode:
- inline
- side by side
grantlee_core/CMakeLists.txt
(0 / 2)
|   | |||
| 12 | 12 | set (grantlee_core_SRCS | |
| 13 | 13 | context.cpp | |
| 14 | 14 | engine.cpp | |
| 15 | enginestate.cpp | ||
| 16 | 15 | filterexpression.cpp | |
| 17 | 16 | lexer.cpp | |
| 18 | 17 | mutabletemplate.cpp | |
| … | … | ||
| 52 | 52 | install(FILES | |
| 53 | 53 | context.h | |
| 54 | 54 | engine.h | |
| 55 | enginestate.h | ||
| 56 | 55 | exception.h | |
| 57 | 56 | filter.h | |
| 58 | 57 | filterexpression.h |
grantlee_core/engine.cpp
(46 / 84)
|   | |||
| 26 | 26 | #include <QPluginLoader> | |
| 27 | 27 | ||
| 28 | 28 | #include "taglibraryinterface.h" | |
| 29 | #include "enginestate_p.h" | ||
| 30 | 29 | #include "template_p.h" | |
| 31 | 30 | #include "templateloader.h" | |
| 32 | 31 | #include "grantlee_version.h" | |
| … | … | ||
| 59 | 59 | ||
| 60 | 60 | }; | |
| 61 | 61 | ||
| 62 | Engine* Engine::m_instance = 0; | ||
| 63 | Engine* Engine::instance() | ||
| 62 | Engine::Engine( QObject *parent ) | ||
| 63 | : QObject( parent ), d_ptr( new EnginePrivate( this ) ) | ||
| 64 | 64 | { | |
| 65 | if ( !m_instance ) { | ||
| 66 | m_instance = new Engine(); | ||
| 67 | } | ||
| 68 | return m_instance; | ||
| 65 | d_ptr->m_defaultLibraries << "grantlee_defaulttags" | ||
| 66 | << "grantlee_loadertags" | ||
| 67 | << "grantlee_defaultfilters" | ||
| 68 | << "grantlee_scriptabletags"; | ||
| 69 | 69 | } | |
| 70 | 70 | ||
| 71 | Engine::Engine() | ||
| 72 | : d_ptr( new EnginePrivate( this ) ) | ||
| 73 | { | ||
| 74 | } | ||
| 75 | |||
| 76 | 71 | Engine::~Engine() | |
| 77 | 72 | { | |
| 78 | 73 | qDeleteAll( d_ptr->m_scriptableLibraries ); | |
| … | … | ||
| 76 | 76 | pluginLoader->unload(); | |
| 77 | 77 | qDeleteAll(d_ptr->m_pluginLoaders); | |
| 78 | 78 | delete d_ptr; | |
| 79 | m_instance = 0; | ||
| 80 | 79 | } | |
| 81 | 80 | ||
| 82 | 81 | QList<AbstractTemplateLoader::Ptr> Engine::templateLoaders() | |
| 83 | 82 | { | |
| 84 | 83 | Q_D( Engine ); | |
| 85 | return d->m_currentState->d_ptr->m_loaders; | ||
| 84 | return d->m_loaders; | ||
| 86 | 85 | } | |
| 87 | 86 | ||
| 88 | 87 | void Engine::addTemplateLoader( AbstractTemplateLoader::Ptr loader ) | |
| 89 | 88 | { | |
| 90 | 89 | Q_D( Engine ); | |
| 91 | d->m_currentState->d_ptr->m_loaders << loader; | ||
| 90 | d->m_loaders << loader; | ||
| 92 | 91 | } | |
| 93 | 92 | ||
| 94 | QString Engine::mediaUri( const QString &fileName, const EngineState &_state ) const | ||
| 93 | QString Engine::mediaUri( const QString &fileName ) const | ||
| 95 | 94 | { | |
| 96 | 95 | Q_D( const Engine ); | |
| 97 | EngineState state = _state ? _state : d->m_currentState; | ||
| 98 | QListIterator<AbstractTemplateLoader::Ptr> it( state->d_ptr->m_loaders ); | ||
| 96 | QListIterator<AbstractTemplateLoader::Ptr> it( d->m_loaders ); | ||
| 99 | 97 | ||
| 100 | 98 | QString uri; | |
| 101 | 99 | while ( it.hasNext() ) { | |
| … | … | ||
| 108 | 108 | void Engine::setPluginDirs( const QStringList &dirs ) | |
| 109 | 109 | { | |
| 110 | 110 | Q_D( Engine ); | |
| 111 | d->m_currentState->d_ptr->m_pluginDirs = dirs; | ||
| 111 | d->m_pluginDirs = dirs; | ||
| 112 | 112 | } | |
| 113 | 113 | ||
| 114 | 114 | QStringList Engine::defaultLibraries() const | |
| 115 | 115 | { | |
| 116 | 116 | Q_D( const Engine ); | |
| 117 | return d->m_currentState->d_ptr->m_defaultLibraries; | ||
| 117 | return d->m_defaultLibraries; | ||
| 118 | 118 | } | |
| 119 | 119 | ||
| 120 | 120 | void Engine::addDefaultLibrary( const QString &libName ) | |
| 121 | 121 | { | |
| 122 | 122 | Q_D( Engine ); | |
| 123 | d->m_currentState->d_ptr->m_defaultLibraries << libName; | ||
| 123 | d->m_defaultLibraries << libName; | ||
| 124 | 124 | } | |
| 125 | 125 | ||
| 126 | 126 | void Engine::removeDefaultLibrary( const QString &libName ) | |
| 127 | 127 | { | |
| 128 | 128 | Q_D( Engine ); | |
| 129 | d->m_currentState->d_ptr->m_defaultLibraries.removeAll( libName ); | ||
| 129 | d->m_defaultLibraries.removeAll( libName ); | ||
| 130 | 130 | } | |
| 131 | 131 | ||
| 132 | void Engine::loadDefaultLibraries( const EngineState &_state ) | ||
| 132 | void Engine::loadDefaultLibraries() | ||
| 133 | 133 | { | |
| 134 | 134 | Q_D( Engine ); | |
| 135 | EngineState state = _state ? _state : d->m_currentState; | ||
| 136 | 135 | // Make sure we can load default scriptable libraries if we're supposed to. | |
| 137 | if ( state->d_ptr->m_defaultLibraries.contains( __scriptableLibName ) ) { | ||
| 138 | d->loadCppLibrary( __scriptableLibName, GRANTLEE_VERSION_MINOR, state ); | ||
| 136 | if ( d->m_defaultLibraries.contains( __scriptableLibName ) ) { | ||
| 137 | TagLibraryInterface *library = d->loadCppLibrary( __scriptableLibName, GRANTLEE_VERSION_MINOR ); | ||
| 138 | if ( library ) | ||
| 139 | { | ||
| 140 | library->setEngine( this ); | ||
| 141 | } | ||
| 139 | 142 | } | |
| 140 | 143 | ||
| 141 | foreach( const QString &libName, state->d_ptr->m_defaultLibraries ) { | ||
| 144 | foreach( const QString &libName, d->m_defaultLibraries ) { | ||
| 142 | 145 | if ( libName == __scriptableLibName ) | |
| 143 | 146 | continue; | |
| 144 | 147 | ||
| 145 | TagLibraryInterface *library = loadLibrary( libName, state ); | ||
| 148 | TagLibraryInterface *library = loadLibrary( libName ); | ||
| 146 | 149 | if ( !library ) | |
| 147 | 150 | continue; | |
| 148 | 151 | } | |
| 149 | 152 | } | |
| 150 | 153 | ||
| 151 | TagLibraryInterface* Engine::loadLibrary( const QString &name, const EngineState &_state ) | ||
| 154 | TagLibraryInterface* Engine::loadLibrary( const QString &name ) | ||
| 152 | 155 | { | |
| 153 | 156 | Q_D( Engine ); | |
| 154 | EngineState state = _state ? _state : d->m_currentState; | ||
| 155 | 157 | ||
| 156 | 158 | if ( name == __scriptableLibName ) | |
| 157 | 159 | return 0; | |
| … | … | ||
| 165 | 165 | uint minorVersion = GRANTLEE_VERSION_MINOR; | |
| 166 | 166 | while ( minorVersion >= GRANTLEE_MIN_PLUGIN_VERSION ) | |
| 167 | 167 | { | |
| 168 | TagLibraryInterface* library = d->loadLibrary( name, state, minorVersion-- ); | ||
| 168 | TagLibraryInterface* library = d->loadLibrary( name, minorVersion-- ); | ||
| 169 | 169 | if ( library ) | |
| 170 | 170 | return library; | |
| 171 | 171 | } | |
| 172 | 172 | return 0; | |
| 173 | 173 | } | |
| 174 | 174 | ||
| 175 | TagLibraryInterface* EnginePrivate::loadLibrary( const QString &name, const EngineState &state, uint minorVersion ) | ||
| 175 | TagLibraryInterface* EnginePrivate::loadLibrary( const QString &name, uint minorVersion ) | ||
| 176 | 176 | { | |
| 177 | TagLibraryInterface* scriptableLibrary = loadScriptableLibrary( name, minorVersion, state ); | ||
| 177 | TagLibraryInterface* scriptableLibrary = loadScriptableLibrary( name, minorVersion ); | ||
| 178 | 178 | if ( scriptableLibrary ) | |
| 179 | 179 | return scriptableLibrary; | |
| 180 | 180 | ||
| 181 | 181 | // else this is not a scriptable library. | |
| 182 | 182 | ||
| 183 | return loadCppLibrary( name, minorVersion, state ); | ||
| 183 | return loadCppLibrary( name, minorVersion ); | ||
| 184 | 184 | } | |
| 185 | 185 | ||
| 186 | 186 | EnginePrivate::EnginePrivate( Engine *engine ) | |
| 187 | 187 | : q_ptr( engine ) | |
| 188 | 188 | { | |
| 189 | m_currentState = staticEmptyState(); | ||
| 190 | 189 | } | |
| 191 | 190 | ||
| 192 | TagLibraryInterface* EnginePrivate::loadScriptableLibrary( const QString &name, uint minorVersion, const EngineState &_state ) | ||
| 191 | TagLibraryInterface* EnginePrivate::loadScriptableLibrary( const QString &name, uint minorVersion ) | ||
| 193 | 192 | { | |
| 194 | EngineState state = _state ? _state : m_currentState; | ||
| 195 | |||
| 196 | 193 | int pluginIndex = 0; | |
| 197 | 194 | QString libFileName; | |
| 198 | 195 | if ( !m_libraries.contains( __scriptableLibName ) ) | |
| 199 | 196 | return 0; | |
| 200 | 197 | ||
| 201 | while ( state->d_ptr->m_pluginDirs.size() > pluginIndex ) { | ||
| 202 | QString nextDir = state->d_ptr->m_pluginDirs.at( pluginIndex++ ); | ||
| 198 | while ( m_pluginDirs.size() > pluginIndex ) { | ||
| 199 | QString nextDir = m_pluginDirs.at( pluginIndex++ ); | ||
| 203 | 200 | libFileName = nextDir + QString( "/%1.%2" ).arg( GRANTLEE_VERSION_MAJOR ).arg( minorVersion ) + '/' + name + ".qs"; | |
| 204 | 201 | QFile file( libFileName ); | |
| 205 | 202 | if ( !file.exists() ) | |
| … | … | ||
| 211 | 211 | m_scriptableLibraries << library; | |
| 212 | 212 | return library; | |
| 213 | 213 | } | |
| 214 | |||
| 215 | 214 | return 0; | |
| 216 | 215 | } | |
| 217 | 216 | ||
| 218 | TagLibraryInterface* EnginePrivate::loadCppLibrary( const QString &name, uint minorVersion, const EngineState &_state ) | ||
| 217 | TagLibraryInterface* EnginePrivate::loadCppLibrary( const QString &name, uint minorVersion ) | ||
| 219 | 218 | { | |
| 220 | 219 | Q_Q( Engine ); | |
| 221 | EngineState state = _state ? _state : m_currentState; | ||
| 222 | |||
| 223 | 220 | int pluginIndex = 0; | |
| 224 | 221 | QString libFileName; | |
| 225 | 222 | ||
| 226 | 223 | QObject *plugin = 0; | |
| 227 | while ( state->d_ptr->m_pluginDirs.size() > pluginIndex ) { | ||
| 228 | QString nextDir = state->d_ptr->m_pluginDirs.at( pluginIndex++ ); | ||
| 224 | while ( m_pluginDirs.size() > pluginIndex ) { | ||
| 225 | QString nextDir = m_pluginDirs.at( pluginIndex++ ); | ||
| 229 | 226 | QDir pluginDir( nextDir + QString( "/%1.%2" ).arg( GRANTLEE_VERSION_MAJOR ).arg( minorVersion ) + '/' ); | |
| 230 | 227 | ||
| 231 | 228 | if ( !pluginDir.exists() ) | |
| … | … | ||
| 249 | 249 | return library; | |
| 250 | 250 | } | |
| 251 | 251 | ||
| 252 | Template Engine::loadByName( const QString &name, const EngineState &_state ) const | ||
| 252 | Template Engine::loadByName( const QString &name ) const | ||
| 253 | 253 | { | |
| 254 | 254 | Q_D( const Engine ); | |
| 255 | 255 | ||
| 256 | EngineState state = _state ? _state : d->m_currentState; | ||
| 257 | |||
| 258 | QListIterator<AbstractTemplateLoader::Ptr> it( state->d_ptr->m_loaders ); | ||
| 256 | QListIterator<AbstractTemplateLoader::Ptr> it( d->m_loaders ); | ||
| 259 | 257 | while ( it.hasNext() ) { | |
| 260 | 258 | AbstractTemplateLoader::Ptr loader = it.next(); | |
| 261 | 259 | ||
| 262 | 260 | if ( !loader->canLoadTemplate( name ) ) | |
| 263 | 261 | continue; | |
| 264 | 262 | ||
| 265 | Template t = loader->loadByName( name ); | ||
| 263 | Template t = loader->loadByName( name, this ); | ||
| 266 | 264 | ||
| 267 | 265 | if ( t ) { | |
| 268 | t->d_ptr->m_state = state; | ||
| 269 | 266 | return t; | |
| 270 | 267 | } | |
| 271 | 268 | } | |
| 272 | throw Grantlee::Exception( TagSyntaxError, QString( "Most recent state is invalid." ) ); | ||
| 269 | return Template(); | ||
| 273 | 270 | } | |
| 274 | 271 | ||
| 275 | MutableTemplate Engine::loadMutableByName( const QString &name, const EngineState &_state ) const | ||
| 272 | MutableTemplate Engine::loadMutableByName( const QString &name ) const | ||
| 276 | 273 | { | |
| 277 | 274 | Q_D( const Engine ); | |
| 278 | 275 | ||
| 279 | EngineState state = _state ? _state : d->m_currentState; | ||
| 276 | QListIterator<AbstractTemplateLoader::Ptr> it( d->m_loaders ); | ||
| 280 | 277 | ||
| 281 | QListIterator<AbstractTemplateLoader::Ptr> it( state->d_ptr->m_loaders ); | ||
| 282 | |||
| 283 | 278 | while ( it.hasNext() ) { | |
| 284 | 279 | AbstractTemplateLoader::Ptr loader = it.next(); | |
| 285 | MutableTemplate t = loader->loadMutableByName( name ); | ||
| 280 | MutableTemplate t = loader->loadMutableByName( name, this ); | ||
| 286 | 281 | if ( t ) { | |
| 287 | t->d_ptr->m_state = state; | ||
| 288 | 282 | return t; | |
| 289 | 283 | } | |
| 290 | 284 | } | |
| 291 | 285 | throw Grantlee::Exception( TagSyntaxError, QString( "Most recent state is invalid." ) ); | |
| 292 | 286 | } | |
| 293 | 287 | ||
| 294 | MutableTemplate Engine::newMutableTemplate( const QString &content, const QString &name, const EngineState &_state ) const | ||
| 288 | MutableTemplate Engine::newMutableTemplate( const QString &content, const QString &name ) const | ||
| 295 | 289 | { | |
| 296 | Q_D( const Engine ); | ||
| 297 | EngineState state = _state ? _state : d->m_currentState; | ||
| 298 | |||
| 299 | MutableTemplate t = MutableTemplate( new MutableTemplateImpl() ); | ||
| 290 | MutableTemplate t = MutableTemplate( new MutableTemplateImpl( this ) ); | ||
| 300 | 291 | t->setObjectName( name ); | |
| 301 | t->d_ptr->m_state = state; | ||
| 302 | 292 | t->setContent( content ); | |
| 303 | 293 | return t; | |
| 304 | 294 | } | |
| 305 | 295 | ||
| 306 | Template Engine::newTemplate( const QString &content, const QString &name, const EngineState &_state ) const | ||
| 296 | Template Engine::newTemplate( const QString &content, const QString &name ) const | ||
| 307 | 297 | { | |
| 308 | Q_D( const Engine ); | ||
| 309 | EngineState state = _state ? _state : d->m_currentState; | ||
| 310 | |||
| 311 | Template t = Template( new TemplateImpl() ); | ||
| 312 | |||
| 298 | Template t = Template( new TemplateImpl( this ) ); | ||
| 313 | 299 | t->setObjectName( name ); | |
| 314 | t->d_ptr->m_state = state; | ||
| 315 | 300 | t->setContent( content ); | |
| 316 | 301 | return t; | |
| 317 | 302 | } | |
| 318 | 303 | ||
| 319 | void Engine::resetState() | ||
| 320 | { | ||
| 321 | Q_D( Engine ); | ||
| 322 | // set the default empty state. | ||
| 323 | d->m_currentState = d->staticEmptyState(); | ||
| 324 | } | ||
| 325 | |||
| 326 | EngineState EnginePrivate::staticEmptyState() | ||
| 327 | { | ||
| 328 | static EngineState state = EngineState( new EngineStateImpl() ); | ||
| 329 | return state; | ||
| 330 | } | ||
| 304 | #include "engine.moc" |
grantlee_core/engine.h
(10 / 20)
|   | |||
| 62 | 62 | @author Stephen Kelly <steveire@gmail.com> | |
| 63 | 63 | @since 0.1 | |
| 64 | 64 | */ | |
| 65 | class GRANTLEE_CORE_EXPORT Engine | ||
| 65 | class GRANTLEE_CORE_EXPORT Engine : public QObject | ||
| 66 | 66 | { | |
| 67 | Q_OBJECT | ||
| 67 | 68 | public: | |
| 68 | 69 | /** | |
| 69 | 70 | Retrieve an instance of an Engine. | |
| 70 | 71 | */ | |
| 71 | static Engine* instance(); | ||
| 72 | Engine( QObject *parent = 0 ); | ||
| 72 | 73 | ||
| 73 | 74 | /** | |
| 74 | 75 | Destructor. | |
| … | … | ||
| 101 | 101 | This method will not usually be called by application code. | |
| 102 | 102 | To load media in a template, use the {% media_finder %} template tag. | |
| 103 | 103 | */ | |
| 104 | QString mediaUri( const QString &fileName, const EngineState &state = EngineState() ) const; | ||
| 104 | QString mediaUri( const QString &fileName ) const; | ||
| 105 | 105 | ||
| 106 | 106 | /** | |
| 107 | 107 | Resets the state of the Engine to the default. | |
| … | … | ||
| 116 | 116 | ||
| 117 | 117 | The Templates and plugins loaded and will be determined by the Engine or the EngineState @p state. | |
| 118 | 118 | */ | |
| 119 | Template loadByName( const QString &name, const EngineState &state = EngineState() ) const; | ||
| 119 | Template loadByName( const QString &name ) const; | ||
| 120 | 120 | ||
| 121 | 121 | /** | |
| 122 | 122 | Create a new Template with the content @p content identified by @p name with the optionally supplied EngineState. | |
| 123 | 123 | ||
| 124 | 124 | The secondary Templates and plugins loaded will be determined by the Engine or the EngineState @p state. | |
| 125 | 125 | */ | |
| 126 | Template newTemplate( const QString &content, const QString &name, const EngineState &state = EngineState() ) const; | ||
| 126 | Template newTemplate( const QString &content, const QString &name ) const; | ||
| 127 | 127 | ||
| 128 | 128 | /** | |
| 129 | 129 | Load the MutableTemplate identified by @p name with the optionally supplied EngineState. | |
| 130 | 130 | ||
| 131 | 131 | The Templates and plugins loaded and will be determined by the Engine or the EngineState @p state. | |
| 132 | 132 | */ | |
| 133 | MutableTemplate loadMutableByName( const QString &name, const EngineState &state = EngineState() ) const; | ||
| 133 | MutableTemplate loadMutableByName( const QString &name ) const; | ||
| 134 | 134 | ||
| 135 | 135 | /** | |
| 136 | 136 | Create a new MutableTemplate with the content @p content identified by @p name with the optionally supplied EngineState. | |
| 137 | 137 | ||
| 138 | 138 | The secondary Templates and plugins loaded will be determined by the Engine or the EngineState @p state. | |
| 139 | 139 | */ | |
| 140 | MutableTemplate newMutableTemplate( const QString &content, const QString &name, const EngineState &state = EngineState() ) const; | ||
| 140 | MutableTemplate newMutableTemplate( const QString &content, const QString &name ) const; | ||
| 141 | 141 | ||
| 142 | 142 | /** | |
| 143 | 143 | Returns the libraries available by default to new Templates. | |
| … | … | ||
| 159 | 159 | ||
| 160 | 160 | Loads and returns the libraries specified in defaultLibraries or @p state. | |
| 161 | 161 | */ | |
| 162 | void loadDefaultLibraries( const EngineState &state = EngineState() ); | ||
| 162 | void loadDefaultLibraries(); | ||
| 163 | 163 | ||
| 164 | 164 | /** | |
| 165 | 165 | @internal | |
| … | … | ||
| 168 | 168 | ||
| 169 | 169 | Templates wishing to load a library should use the "{% load %}" tag. | |
| 170 | 170 | */ | |
| 171 | TagLibraryInterface* loadLibrary( const QString &name, const EngineState &state = EngineState() ); | ||
| 171 | TagLibraryInterface* loadLibrary( const QString &name ); | ||
| 172 | 172 | ||
| 173 | /** | ||
| 174 | Returns the current state of the Engine. | ||
| 175 | @see template_factories | ||
| 176 | */ | ||
| 177 | EngineState state(); | ||
| 178 | |||
| 179 | private: | ||
| 180 | Engine(); | ||
| 181 | |||
| 182 | 173 | Q_DECLARE_PRIVATE( Engine ) | |
| 183 | 174 | EnginePrivate * const d_ptr; | |
| 184 | |||
| 185 | static Engine* m_instance; | ||
| 186 | 175 | }; | |
| 187 | 176 | ||
| 188 | 177 | } |
grantlee_core/engine_p.h
(9 / 9)
|   | |||
| 22 | 22 | #define GRANTLEE_ENGINE_P_H | |
| 23 | 23 | ||
| 24 | 24 | #include "engine.h" | |
| 25 | #include "enginestate_p.h" | ||
| 26 | 25 | ||
| 27 | 26 | class QPluginLoader; | |
| 28 | 27 | ||
| 28 | class QPluginLoader; | ||
| 29 | |||
| 29 | 30 | namespace Grantlee | |
| 30 | 31 | { | |
| 31 | 32 | ||
| … | … | ||
| 34 | 34 | { | |
| 35 | 35 | EnginePrivate( Engine *engine ); | |
| 36 | 36 | ||
| 37 | static EngineState staticEmptyState(); | ||
| 37 | TagLibraryInterface* loadLibrary( const QString &name, uint minorVersion ); | ||
| 38 | TagLibraryInterface* loadScriptableLibrary( const QString &name, uint minorVersion ); | ||
| 39 | TagLibraryInterface* loadCppLibrary( const QString& name, uint minorVersion ); | ||
| 38 | 40 | ||
| 39 | TagLibraryInterface* loadLibrary( const QString &name, const EngineState &state, uint minorVersion ); | ||
| 40 | TagLibraryInterface* loadScriptableLibrary( const QString &name, uint minorVersion, const EngineState &state = EngineState() ); | ||
| 41 | TagLibraryInterface* loadCppLibrary( const QString& name, uint minorVersion, const EngineState &state = EngineState() ); | ||
| 42 | |||
| 43 | 41 | Q_DECLARE_PUBLIC( Engine ) | |
| 44 | 42 | Engine *q_ptr; | |
| 45 | 43 | ||
| 46 | EngineState m_currentState; | ||
| 47 | |||
| 48 | 44 | QList<QPluginLoader*> m_pluginLoaders; | |
| 45 | |||
| 49 | 46 | QHash<QString, TagLibraryInterface*> m_libraries; | |
| 50 | 47 | QList<TagLibraryInterface*> m_scriptableLibraries; | |
| 51 | 48 | ||
| 52 | friend class EngineStateImpl; | ||
| 49 | QList<AbstractTemplateLoader::Ptr> m_loaders; | ||
| 50 | QStringList m_pluginDirs; | ||
| 51 | QStringList m_defaultLibraries; | ||
| 53 | 52 | }; | |
| 54 | 53 | ||
| 55 | 54 | } |
grantlee_core/enginestate.cpp
(0 / 41)
|   | |||
| 1 | /* | ||
| 2 | This file is part of the Grantlee template system. | ||
| 3 | |||
| 4 | Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> | ||
| 5 | |||
| 6 | This library is free software; you can redistribute it and/or | ||
| 7 | modify it under the terms of the GNU Lesser General Public | ||
| 8 | License version 3 only, as published by the Free Software Foundation. | ||
| 9 | |||
| 10 | This library is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | Lesser General Public License version 3 for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU Lesser General Public | ||
| 16 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | */ | ||
| 19 | |||
| 20 | #include "enginestate.h" | ||
| 21 | #include "enginestate_p.h" | ||
| 22 | |||
| 23 | #include "engine.h" | ||
| 24 | #include "engine_p.h" | ||
| 25 | |||
| 26 | using namespace Grantlee; | ||
| 27 | |||
| 28 | EngineStateImpl::EngineStateImpl() | ||
| 29 | : d_ptr( new EngineStateImplPrivate() ) | ||
| 30 | { | ||
| 31 | Q_D( EngineStateImpl ); | ||
| 32 | d->m_defaultLibraries << "grantlee_defaulttags" | ||
| 33 | << "grantlee_loadertags" | ||
| 34 | << "grantlee_defaultfilters" | ||
| 35 | << "grantlee_scriptabletags"; | ||
| 36 | } | ||
| 37 | |||
| 38 | Grantlee::EngineStateImpl::~EngineStateImpl() | ||
| 39 | { | ||
| 40 | delete d_ptr; | ||
| 41 | } |
grantlee_core/enginestate.h
(0 / 59)
|   | |||
| 1 | /* | ||
| 2 | This file is part of the Grantlee template system. | ||
| 3 | |||
| 4 | Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> | ||
| 5 | |||
| 6 | This library is free software; you can redistribute it and/or | ||
| 7 | modify it under the terms of the GNU Lesser General Public | ||
| 8 | License version 3 only, as published by the Free Software Foundation. | ||
| 9 | |||
| 10 | This library is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | Lesser General Public License version 3 for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU Lesser General Public | ||
| 16 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef GRANTLEE_ENGINE_STATE_H | ||
| 21 | #define GRANTLEE_ENGINE_STATE_H | ||
| 22 | |||
| 23 | #include <QtCore/QSharedPointer> | ||
| 24 | |||
| 25 | #include "grantlee_core_export.h" | ||
| 26 | |||
| 27 | namespace Grantlee | ||
| 28 | { | ||
| 29 | |||
| 30 | class EngineStateImplPrivate; | ||
| 31 | |||
| 32 | /** | ||
| 33 | @brief The EngineStateImpl class stores a particular configuration for an Engine. | ||
| 34 | |||
| 35 | This class deliberately does not have any public API. All EngineStates should be configured and retrieved using API | ||
| 36 | on the Engine class. | ||
| 37 | |||
| 38 | @see template_factories | ||
| 39 | */ | ||
| 40 | class GRANTLEE_CORE_EXPORT EngineStateImpl | ||
| 41 | { | ||
| 42 | public: | ||
| 43 | ~EngineStateImpl(); | ||
| 44 | private: | ||
| 45 | friend class Engine; | ||
| 46 | friend class EnginePrivate; | ||
| 47 | |||
| 48 | Q_DECLARE_PRIVATE( EngineStateImpl ) | ||
| 49 | EngineStateImplPrivate * const d_ptr; | ||
| 50 | |||
| 51 | EngineStateImpl(); | ||
| 52 | }; | ||
| 53 | |||
| 54 | typedef QWeakPointer<EngineStateImpl> EngineStateWeakPtr; | ||
| 55 | typedef QSharedPointer<EngineStateImpl> EngineState; | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | #endif |
grantlee_core/enginestate_p.h
(0 / 45)
|   | |||
| 1 | /* | ||
| 2 | This file is part of the Grantlee template system. | ||
| 3 | |||
| 4 | Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> | ||
| 5 | |||
| 6 | This library is free software; you can redistribute it and/or | ||
| 7 | modify it under the terms of the GNU Lesser General Public | ||
| 8 | License version 3 only, as published by the Free Software Foundation. | ||
| 9 | |||
| 10 | This library is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | Lesser General Public License version 3 for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU Lesser General Public | ||
| 16 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef GRANTLEE_ENGINE_STATE_P_H | ||
| 21 | #define GRANTLEE_ENGINE_STATE_P_H | ||
| 22 | |||
| 23 | #include "enginestate.h" | ||
| 24 | #include "templateloader.h" | ||
| 25 | |||
| 26 | namespace Grantlee | ||
| 27 | { | ||
| 28 | |||
| 29 | class EngineStateImplPrivate | ||
| 30 | { | ||
| 31 | Q_DECLARE_PUBLIC( EngineStateImpl ) | ||
| 32 | EngineStateImpl *q_ptr; | ||
| 33 | |||
| 34 | QList<AbstractTemplateLoader::Ptr> m_loaders; | ||
| 35 | QStringList m_pluginDirs; | ||
| 36 | QStringList m_defaultLibraries; | ||
| 37 | |||
| 38 | friend class Engine; | ||
| 39 | friend class EnginePrivate; | ||
| 40 | |||
| 41 | }; | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | #endif |
|   | |||
| 21 | 21 | ||
| 22 | 22 | using namespace Grantlee; | |
| 23 | 23 | ||
| 24 | MutableTemplateImpl::MutableTemplateImpl( QObject *parent ) | ||
| 25 | : TemplateImpl( parent ) | ||
| 24 | MutableTemplateImpl::MutableTemplateImpl( Engine const *engine, QObject *parent ) | ||
| 25 | : TemplateImpl( engine, parent ) | ||
| 26 | 26 | { | |
| 27 | 27 | ||
| 28 | 28 | } |
|   | |||
| 39 | 39 | /** | |
| 40 | 40 | Constructor. | |
| 41 | 41 | */ | |
| 42 | explicit MutableTemplateImpl( QObject *parent = 0 ); | ||
| 42 | explicit MutableTemplateImpl( Engine const *engine, QObject *parent = 0 ); | ||
| 43 | 43 | ||
| 44 | 44 | /** | |
| 45 | 45 | Renders the Template, possibly mutating it. |
grantlee_core/parser.cpp
(13 / 6)
|   | |||
| 23 | 23 | ||
| 24 | 24 | #include "taglibraryinterface.h" | |
| 25 | 25 | #include "template.h" | |
| 26 | #include "template_p.h" | ||
| 26 | 27 | #include "engine.h" | |
| 27 | 28 | #include "filter.h" | |
| 28 | 29 | #include "exception.h" | |
| … | … | ||
| 86 | 86 | { | |
| 87 | 87 | Q_D( Parser ); | |
| 88 | 88 | ||
| 89 | Engine *engine = Engine::instance(); | ||
| 90 | |||
| 91 | 89 | TemplateImpl *ti = qobject_cast<TemplateImpl *>( parent ); | |
| 92 | 90 | ||
| 93 | engine->loadDefaultLibraries( ti->state() ); | ||
| 94 | foreach( const QString &libraryName, engine->defaultLibraries()) { | ||
| 95 | TagLibraryInterface *library = engine->loadLibrary( libraryName, ti->state() ); | ||
| 91 | Engine const *cengine = ti->engine(); | ||
| 92 | if (!cengine) | ||
| 93 | return; | ||
| 94 | Engine *engine = const_cast<Engine *>( cengine ); | ||
| 95 | engine->loadDefaultLibraries(); | ||
| 96 | foreach( const QString &libraryName, engine->defaultLibraries() ) { | ||
| 97 | TagLibraryInterface *library = engine->loadLibrary( libraryName ); | ||
| 96 | 98 | if(!library) | |
| 97 | 99 | continue; | |
| 98 | 100 | d->openLibrary( library ); | |
| … | … | ||
| 121 | 121 | { | |
| 122 | 122 | Q_D( Parser ); | |
| 123 | 123 | TemplateImpl *ti = qobject_cast<TemplateImpl *>( parent() ); | |
| 124 | TagLibraryInterface *library = Engine::instance()->loadLibrary( name, ti->state() ); | ||
| 124 | Engine const *cengine = ti->engine(); | ||
| 125 | if (!cengine) | ||
| 126 | return; | ||
| 127 | Engine *engine = const_cast<Engine *>( cengine ); | ||
| 128 | TagLibraryInterface *library = engine->loadLibrary( name ); | ||
| 125 | 129 | if ( !library ) | |
| 126 | 130 | return; | |
| 127 | 131 | d->openLibrary( library ); |
grantlee_core/template.cpp
(4 / 4)
|   | |||
| 37 | 37 | return p.parse( q ); | |
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | TemplateImpl::TemplateImpl( QObject *parent ) | ||
| 41 | : QObject( parent ), d_ptr( new TemplatePrivate( this ) ) | ||
| 40 | TemplateImpl::TemplateImpl( Engine const *engine, QObject *parent ) | ||
| 41 | : QObject( parent ), d_ptr( new TemplatePrivate( engine, this ) ) | ||
| 42 | 42 | { | |
| 43 | 43 | Q_D( Template ); | |
| 44 | 44 | d->m_settingsToken = reinterpret_cast<qint64>( this ); | |
| … | … | ||
| 107 | 107 | return d->m_errorString; | |
| 108 | 108 | } | |
| 109 | 109 | ||
| 110 | EngineState TemplateImpl::state() const | ||
| 110 | Engine const * TemplateImpl::engine() const | ||
| 111 | 111 | { | |
| 112 | 112 | Q_D( const Template ); | |
| 113 | return d->m_state; | ||
| 113 | return d->m_engine.data(); | ||
| 114 | 114 | } | |
| 115 | 115 | ||
| 116 | 116 | #include "template.moc" |
grantlee_core/template.h
(4 / 6)
|   | |||
| 23 | 23 | #include <QtCore/QStringList> | |
| 24 | 24 | #include <QtCore/QSharedPointer> | |
| 25 | 25 | ||
| 26 | #include "enginestate.h" | ||
| 27 | 26 | #include "node.h" | |
| 28 | 27 | ||
| 29 | 28 | #include "global.h" | |
| … | … | ||
| 31 | 31 | namespace Grantlee | |
| 32 | 32 | { | |
| 33 | 33 | class Context; | |
| 34 | class Engine; | ||
| 34 | 35 | class TemplateImpl; | |
| 35 | 36 | ||
| 36 | 37 | typedef QWeakPointer<TemplateImpl> TemplateWeakPtr; | |
| … | … | ||
| 81 | 81 | */ | |
| 82 | 82 | QString errorString(); | |
| 83 | 83 | ||
| 84 | /** | ||
| 85 | The state used and accessed while parsing and rendering the Template. | ||
| 86 | */ | ||
| 87 | EngineState state() const; | ||
| 84 | Engine const * engine() const; | ||
| 88 | 85 | ||
| 89 | 86 | protected: | |
| 90 | TemplateImpl( QObject *parent = 0 ); | ||
| 87 | TemplateImpl( Engine const *engine, QObject *parent = 0 ); | ||
| 91 | 88 | ||
| 92 | 89 | void setContent( const QString &templateString ); | |
| 93 | 90 | ||
| … | … | ||
| 92 | 92 | Q_DECLARE_PRIVATE( Template ) | |
| 93 | 93 | TemplatePrivate * const d_ptr; | |
| 94 | 94 | friend class Engine; | |
| 95 | friend class Parser; | ||
| 95 | 96 | }; | |
| 96 | 97 | ||
| 97 | 98 | } |
grantlee_core/template_p.h
(6 / 4)
|   | |||
| 22 | 22 | ||
| 23 | 23 | #include "template.h" | |
| 24 | 24 | ||
| 25 | #include "enginestate_p.h" | ||
| 25 | #include "engine.h" | ||
| 26 | 26 | ||
| 27 | 27 | namespace Grantlee | |
| 28 | 28 | { | |
| … | … | ||
| 31 | 31 | ||
| 32 | 32 | class TemplatePrivate | |
| 33 | 33 | { | |
| 34 | TemplatePrivate( TemplateImpl *t ) | ||
| 35 | : q_ptr( t ), m_error( NoError ) { | ||
| 34 | TemplatePrivate( Engine const *engine, TemplateImpl *t ) | ||
| 35 | : q_ptr( t ), m_error( NoError ), m_engine( engine ) | ||
| 36 | { | ||
| 36 | 37 | ||
| 37 | 38 | } | |
| 38 | 39 | ||
| … | … | ||
| 48 | 48 | Error m_error; | |
| 49 | 49 | QString m_errorString; | |
| 50 | 50 | NodeList m_nodeList; | |
| 51 | EngineState m_state; | ||
| 51 | QWeakPointer<Engine const> m_engine; | ||
| 52 | 52 | ||
| 53 | 53 | friend class Grantlee::Engine; | |
| 54 | friend class Parser; | ||
| 54 | 55 | ||
| 55 | 56 | }; | |
| 56 | 57 |
|   | |||
| 90 | 90 | } | |
| 91 | 91 | ||
| 92 | 92 | // TODO Refactor these two. | |
| 93 | MutableTemplate FileSystemTemplateLoader::loadMutableByName( const QString &fileName ) const | ||
| 93 | MutableTemplate FileSystemTemplateLoader::loadMutableByName( const QString &fileName, Engine const *engine ) const | ||
| 94 | 94 | { | |
| 95 | 95 | int i = 0; | |
| 96 | 96 | QFile file; | |
| … | … | ||
| 114 | 114 | QString content; | |
| 115 | 115 | content = file.readAll(); | |
| 116 | 116 | ||
| 117 | MutableTemplate t = Engine::instance()->newMutableTemplate( content, fileName ); | ||
| 117 | MutableTemplate t = engine->newMutableTemplate( content, fileName ); | ||
| 118 | 118 | return t; | |
| 119 | 119 | } | |
| 120 | 120 | ||
| 121 | Template FileSystemTemplateLoader::loadByName( const QString &fileName ) const | ||
| 121 | Template FileSystemTemplateLoader::loadByName( const QString &fileName, Engine const *engine ) const | ||
| 122 | 122 | { | |
| 123 | 123 | int i = 0; | |
| 124 | 124 | QFile file; | |
| … | … | ||
| 140 | 140 | ||
| 141 | 141 | QString content; | |
| 142 | 142 | content = file.readAll(); | |
| 143 | Template t = Engine::instance()->newTemplate( content, fileName ); | ||
| 143 | Template t = engine->newTemplate( content, fileName ); | ||
| 144 | 144 | return t; | |
| 145 | 145 | } | |
| 146 | 146 | ||
| … | … | ||
| 178 | 178 | return m_namedTemplates.contains( name ); | |
| 179 | 179 | } | |
| 180 | 180 | ||
| 181 | Template InMemoryTemplateLoader::loadByName( const QString& name ) const | ||
| 181 | Template InMemoryTemplateLoader::loadByName( const QString& name, Engine const *engine ) const | ||
| 182 | 182 | { | |
| 183 | 183 | if ( m_namedTemplates.contains( name ) ) { | |
| 184 | Template t = Engine::instance()->newTemplate( m_namedTemplates.value( name ), name ); | ||
| 184 | Template t = engine->newTemplate( m_namedTemplates.value( name ), name ); | ||
| 185 | 185 | return t; | |
| 186 | 186 | } | |
| 187 | 187 | throw Grantlee::Exception( TagSyntaxError, QString( "Couldn't load template %1. Template does not exist." ).arg( name ) ); | |
| 188 | 188 | } | |
| 189 | 189 | ||
| 190 | MutableTemplate InMemoryTemplateLoader::loadMutableByName( const QString& name ) const | ||
| 190 | MutableTemplate InMemoryTemplateLoader::loadMutableByName( const QString& name, Engine const *engine ) const | ||
| 191 | 191 | { | |
| 192 | 192 | if ( m_namedTemplates.contains( name ) ) { | |
| 193 | MutableTemplate t = Engine::instance()->newMutableTemplate( m_namedTemplates.value( name ), name ); | ||
| 193 | MutableTemplate t = engine->newMutableTemplate( m_namedTemplates.value( name ), name ); | ||
| 194 | 194 | return t; | |
| 195 | 195 | } | |
| 196 | 196 | throw Grantlee::Exception( TagSyntaxError, QString( "Couldn't load template %1. Template does not exist." ).arg( name ) ); |
|   | |||
| 51 | 51 | /** | |
| 52 | 52 | Load a MutableTemplate called @p name. Return an invalid Template if no content by that name exists. | |
| 53 | 53 | */ | |
| 54 | virtual MutableTemplate loadMutableByName( const QString &name ) const = 0; | ||
| 54 | virtual MutableTemplate loadMutableByName( const QString &name, Engine const *engine ) const = 0; | ||
| 55 | 55 | ||
| 56 | 56 | /** | |
| 57 | 57 | Load a Template called @p name. Return an invalid Template if no content by that name exists. | |
| 58 | 58 | */ | |
| 59 | virtual Template loadByName( const QString &name ) const = 0; | ||
| 59 | virtual Template loadByName( const QString &name, Engine const *engine ) const = 0; | ||
| 60 | 60 | ||
| 61 | 61 | /** | |
| 62 | 62 | Return a complete URI for media identified by fileName. | |
| … | … | ||
| 126 | 126 | */ | |
| 127 | 127 | virtual ~FileSystemTemplateLoader(); | |
| 128 | 128 | ||
| 129 | /* reimp */ MutableTemplate loadMutableByName( const QString &name ) const; | ||
| 129 | /* reimp */ MutableTemplate loadMutableByName( const QString &name, Engine const *engine ) const; | ||
| 130 | 130 | ||
| 131 | /* reimp */ Template loadByName( const QString &name ) const; | ||
| 131 | /* reimp */ Template loadByName( const QString &name, Engine const *engine ) const; | ||
| 132 | 132 | ||
| 133 | 133 | /* reimp */ bool canLoadTemplate( const QString &name ) const; | |
| 134 | 134 | ||
| … | … | ||
| 158 | 158 | InMemoryTemplateLoader(); | |
| 159 | 159 | virtual ~InMemoryTemplateLoader(); | |
| 160 | 160 | ||
| 161 | /* reimp */ MutableTemplate loadMutableByName( const QString &name ) const; | ||
| 161 | /* reimp */ MutableTemplate loadMutableByName( const QString &name, Engine const *engine ) const; | ||
| 162 | 162 | ||
| 163 | /* reimp */ Template loadByName( const QString &name ) const; | ||
| 163 | /* reimp */ Template loadByName( const QString &name, Engine const *engine ) const; | ||
| 164 | 164 | ||
| 165 | 165 | /* reimp */ bool canLoadTemplate( const QString &name ) const; | |
| 166 | 166 |
|   | |||
| 52 | 52 | ||
| 53 | 53 | QString MediaFinderNode::render( Context* c ) | |
| 54 | 54 | { | |
| 55 | Grantlee::Engine *engine = Grantlee::Engine::instance(); | ||
| 56 | 55 | foreach( const FilterExpression &fe, m_mediaExpressionList ) { | |
| 57 | 56 | if ( fe.isTrue( c ) ) { | |
| 58 | 57 | TemplateImpl *ti = containerTemplate(); | |
| 59 | 58 | ||
| 60 | return engine->mediaUri( Util::getSafeString( fe.resolve( c ) ), ti->state() ); | ||
| 59 | return ti->engine()->mediaUri( Util::getSafeString( fe.resolve( c ) ) ); | ||
| 61 | 60 | } | |
| 62 | 61 | } | |
| 63 | 62 | return QString(); |
grantlee_defaulttags/ssi.cpp
(1 / 1)
|   | |||
| 78 | 78 | ||
| 79 | 79 | if ( m_parse ) { | |
| 80 | 80 | TemplateImpl *ti = containerTemplate(); | |
| 81 | Template t = Engine::instance()->newTemplate( content, m_filename, ti->state() ); | ||
| 81 | Template t = ti->engine()->newTemplate( content, m_filename ); | ||
| 82 | 82 | return t->render( c ); | |
| 83 | 83 | } | |
| 84 | 84 | return content; |
|   | |||
| 110 | 110 | } else { | |
| 111 | 111 | parentName = m_name; | |
| 112 | 112 | } | |
| 113 | Engine *engine = Engine::instance(); | ||
| 114 | 113 | ||
| 115 | 114 | TemplateImpl *ti = containerTemplate(); | |
| 116 | 115 | ||
| 117 | Template t = engine->loadByName( parentName, ti->state() ); | ||
| 116 | Template t = ti->engine()->loadByName( parentName ); | ||
| 118 | 117 | ||
| 119 | 118 | return t; | |
| 120 | 119 | } |
|   | |||
| 61 | 61 | { | |
| 62 | 62 | QString filename = Util::getSafeString( m_filterExpression.resolve( c ) ); | |
| 63 | 63 | ||
| 64 | Engine *engine = Engine::instance(); | ||
| 65 | |||
| 66 | 64 | TemplateImpl *ti = containerTemplate(); | |
| 65 | |||
| 67 | 66 | try { | |
| 68 | Template t = engine->loadByName( filename, ti->state() ); | ||
| 67 | Template t = ti->engine()->loadByName( filename ); | ||
| 69 | 68 | ||
| 70 | 69 | if ( !t ) | |
| 71 | 70 | return QString(); | |
| … | … | ||
| 84 | 84 | ||
| 85 | 85 | QString ConstantIncludeNode::render( Context *c ) | |
| 86 | 86 | { | |
| 87 | Engine *engine = Engine::instance(); | ||
| 88 | |||
| 89 | 87 | TemplateImpl *ti = containerTemplate(); | |
| 90 | 88 | ||
| 91 | 89 | try { | |
| 92 | Template t = engine->loadByName( m_name, ti->state() ); | ||
| 90 | Template t = ti->engine()->loadByName( m_name ); | ||
| 93 | 91 | if ( !t ) | |
| 94 | 92 | return QString(); | |
| 95 | 93 |
|   | |||
| 35 | 35 | #include "token.h" | |
| 36 | 36 | ||
| 37 | 37 | Q_DECLARE_METATYPE( Token ) | |
| 38 | Q_DECLARE_METATYPE( Engine* ) | ||
| 38 | 39 | ||
| 39 | 40 | QScriptValue tokenToScriptValue( QScriptEngine *engine, const Token &t ) | |
| 40 | 41 | { | |
| … | … | ||
| 94 | 94 | QScriptValue markSafeFunctionObject = m_scriptEngine->newFunction( markSafeFunction ); | |
| 95 | 95 | m_scriptEngine->globalObject().setProperty( "mark_safe", markSafeFunctionObject ); | |
| 96 | 96 | ||
| 97 | } | ||
| 98 | |||
| 99 | void ScriptableTagLibrary::setEngine( Engine *engine ) | ||
| 100 | { | ||
| 101 | m_scriptEngine->setProperty( "templateEngine", QVariant::fromValue( engine ) ); | ||
| 97 | 102 | } | |
| 98 | 103 | ||
| 99 | 104 | bool ScriptableTagLibrary::evaluateScript( const QString &name ) |
|   | |||
| 32 | 32 | ||
| 33 | 33 | namespace Grantlee | |
| 34 | 34 | { | |
| 35 | class Engine; | ||
| 35 | 36 | class Parser; | |
| 36 | 37 | } | |
| 37 | 38 | ||
| … | … | ||
| 48 | 48 | virtual QHash<QString, AbstractNodeFactory*> nodeFactories( const QString &name = QString() ); | |
| 49 | 49 | ||
| 50 | 50 | virtual QHash<QString, Filter*> filters( const QString &name = QString() ); | |
| 51 | |||
| 52 | /* reimp */ void setEngine( Grantlee::Engine *engine ); | ||
| 51 | 53 | ||
| 52 | 54 | public slots: | |
| 53 | 55 | void addFactory( const QString &factoryName, const QString &tagname ); |
|   | |||
| 27 | 27 | #include "node.h" | |
| 28 | 28 | #include "scriptablecontext.h" | |
| 29 | 29 | ||
| 30 | Q_DECLARE_METATYPE( Engine* ) | ||
| 30 | 31 | ||
| 32 | |||
| 31 | 33 | QScriptValue ScriptableTemplateConstructor( QScriptContext *context, | |
| 32 | 34 | QScriptEngine *engine ) | |
| 33 | 35 | { | |
| 34 | 36 | QString content = context->argument( 0 ).toString(); | |
| 35 | 37 | QString name = context->argument( 1 ).toString(); | |
| 36 | 38 | QObject *parent = context->argument( 2 ).toQObject(); | |
| 39 | Engine *templateEngine = engine->property("templateEngine").value<Engine *>(); | ||
| 37 | 40 | ||
| 38 | Template t = Engine::instance()->newTemplate( content, name ); | ||
| 41 | if ( !templateEngine ) | ||
| 42 | return QScriptValue(); | ||
| 43 | |||
| 44 | Template t = templateEngine->newTemplate( content, name ); | ||
| 39 | 45 | ||
| 40 | 46 | ScriptableTemplate *object = new ScriptableTemplate( t, parent ); | |
| 41 | 47 | return engine->newQObject( object ); |
tests/testbuiltins.cpp
(25 / 16)
|   | |||
| 134 | 134 | private: | |
| 135 | 135 | Engine *m_engine; | |
| 136 | 136 | ||
| 137 | Engine* getEngine(); | ||
| 138 | |||
| 137 | 139 | void doTest(); | |
| 138 | 140 | ||
| 139 | 141 | }; | |
| 140 | 142 | ||
| 141 | 143 | void TestBuiltinSyntax::initTestCase() | |
| 142 | 144 | { | |
| 143 | m_engine = Engine::instance(); | ||
| 144 | m_engine->setPluginDirs( QStringList() << GRANTLEE_PLUGIN_PATH ); | ||
| 145 | m_engine = getEngine(); | ||
| 145 | 146 | } | |
| 146 | 147 | ||
| 148 | Engine* TestBuiltinSyntax::getEngine() | ||
| 149 | { | ||
| 150 | Engine *engine = new Engine( this ); | ||
| 151 | engine->setPluginDirs( QStringList() << GRANTLEE_PLUGIN_PATH ); | ||
| 152 | return engine; | ||
| 153 | } | ||
| 154 | |||
| 147 | 155 | void TestBuiltinSyntax::cleanupTestCase() | |
| 148 | 156 | { | |
| 149 | 157 | delete m_engine; | |
| … | … | ||
| 164 | 164 | QFETCH( QString, output ); | |
| 165 | 165 | QFETCH( Grantlee::Error, error ); | |
| 166 | 166 | ||
| 167 | Template t = Engine::instance()->newTemplate( input, QTest::currentDataTag() ); | ||
| 167 | Template t = m_engine->newTemplate( input, QTest::currentDataTag() ); | ||
| 168 | 168 | ||
| 169 | 169 | Context context( dict ); | |
| 170 | 170 | ||
| … | … | ||
| 575 | 575 | ||
| 576 | 576 | void TestBuiltinSyntax::testMultipleStates() | |
| 577 | 577 | { | |
| 578 | Engine *engine = Engine::instance(); | ||
| 578 | Engine *engine1 = getEngine(); | ||
| 579 | 579 | ||
| 580 | 580 | InMemoryTemplateLoader::Ptr loader1 = InMemoryTemplateLoader::Ptr( new InMemoryTemplateLoader() ); | |
| 581 | 581 | ||
| 582 | 582 | loader1->setTemplate( "template1", "Template 1" ); | |
| 583 | engine->addTemplateLoader( loader1 ); | ||
| 583 | engine1->addTemplateLoader( loader1 ); | ||
| 584 | 584 | ||
| 585 | Template t1 = engine->newTemplate( "{% include \"template1\" %}", "\"template1\"" ); | ||
| 585 | Template t1 = engine1->newTemplate( "{% include \"template1\" %}", "\"template1\"" ); | ||
| 586 | 586 | ||
| 587 | Engine *engine2 = getEngine(); | ||
| 588 | |||
| 587 | 589 | InMemoryTemplateLoader::Ptr loader2 = InMemoryTemplateLoader::Ptr( new InMemoryTemplateLoader() ); | |
| 588 | 590 | ||
| 589 | 591 | loader2->setTemplate( "template2", "Template 2" ); | |
| 590 | 592 | ||
| 591 | engine->addTemplateLoader( loader2 ); | ||
| 593 | engine2->addTemplateLoader( loader2 ); | ||
| 592 | 594 | ||
| 593 | Template t2 = engine->newTemplate( "{% include \"template2\" %}", "\"template2\"" ); | ||
| 595 | Template t2 = engine2->newTemplate( "{% include \"template2\" %}", "\"template2\"" ); | ||
| 594 | 596 | ||
| 597 | Engine *engine3 = getEngine(); | ||
| 598 | |||
| 595 | 599 | InMemoryTemplateLoader::Ptr loader3 = InMemoryTemplateLoader::Ptr( new InMemoryTemplateLoader() ); | |
| 596 | 600 | ||
| 597 | 601 | loader3->setTemplate( "template3", "Template 3" ); | |
| 598 | 602 | ||
| 599 | engine->addTemplateLoader( loader3 ); | ||
| 603 | engine3->addTemplateLoader( loader3 ); | ||
| 600 | 604 | ||
| 601 | Template t3 = engine->newTemplate( "{% include var %}", "var" ); | ||
| 605 | Template t3 = engine3->newTemplate( "{% include var %}", "var" ); | ||
| 602 | 606 | ||
| 603 | // Cause Engine to change to a new state. | ||
| 604 | Template t4 = engine->newTemplate( "", "Dummy template" ); | ||
| 605 | |||
| 606 | 607 | QVariantHash h; | |
| 607 | 608 | h.insert( "var", "template3" ); | |
| 608 | 609 | Context c( h ); | |
| 610 | t1->render( &c ); | ||
| 609 | 611 | ||
| 610 | 612 | QString expected1 = "Template 1"; | |
| 611 | 613 | QString expected2 = "Template 2"; | |
| … | … | ||
| 615 | 615 | QCOMPARE( t1->render( &c ), expected1 ); | |
| 616 | 616 | QCOMPARE( t2->render( &c ), expected2 ); | |
| 617 | 617 | QCOMPARE( t3->render( &c ), expected3 ); | |
| 618 | |||
| 619 | 618 | } | |
| 620 | 619 | ||
| 621 | 620 | void TestBuiltinSyntax::testTemplatePathSafety_data() | |
| … | … | ||
| 641 | 641 | f.write( inputPath.toUtf8() ); | |
| 642 | 642 | f.close(); | |
| 643 | 643 | ||
| 644 | Template t = loader->loadByName( inputPath ); | ||
| 644 | Template t = loader->loadByName( inputPath, m_engine ); | ||
| 645 | 645 | Context c; | |
| 646 | 646 | if ( output.isEmpty() ) | |
| 647 | 647 | QVERIFY( !t ); | |
| 648 | 648 | else | |
| 649 | 649 | QCOMPARE( t->render( &c ), inputPath ); | |
| 650 | 650 | ||
| 651 | MutableTemplate mt = loader->loadMutableByName( inputPath ); | ||
| 651 | MutableTemplate mt = loader->loadMutableByName( inputPath, m_engine ); | ||
| 652 | 652 | if ( output.isEmpty() ) | |
| 653 | 653 | QVERIFY( !mt ); | |
| 654 | 654 | else |
tests/testdefaulttags.cpp
(2 / 2)
|   | |||
| 134 | 134 | ||
| 135 | 135 | void TestDefaultTags::initTestCase() | |
| 136 | 136 | { | |
| 137 | m_engine = Engine::instance(); | ||
| 137 | m_engine = new Engine( this ); | ||
| 138 | 138 | m_engine->setPluginDirs( QStringList() << GRANTLEE_PLUGIN_PATH ); | |
| 139 | 139 | } | |
| 140 | 140 | ||
| … | … | ||
| 150 | 150 | QFETCH( QString, output ); | |
| 151 | 151 | QFETCH( Grantlee::Error, error ); | |
| 152 | 152 | ||
| 153 | Template t = Engine::instance()->newTemplate( input, QTest::currentDataTag() ); | ||
| 153 | Template t = m_engine->newTemplate( input, QTest::currentDataTag() ); | ||
| 154 | 154 | ||
| 155 | 155 | Context context( dict ); | |
| 156 | 156 |
tests/testfilters.cpp
(2 / 2)
|   | |||
| 87 | 87 | ||
| 88 | 88 | void TestFilters::initTestCase() | |
| 89 | 89 | { | |
| 90 | m_engine = Engine::instance(); | ||
| 90 | m_engine = new Engine( this ); | ||
| 91 | 91 | ||
| 92 | 92 | loader = InMemoryTemplateLoader::Ptr( new InMemoryTemplateLoader() ); | |
| 93 | 93 | m_engine->addTemplateLoader( loader ); | |
| … | … | ||
| 110 | 110 | QFETCH( QString, output ); | |
| 111 | 111 | QFETCH( Grantlee::Error, errorNumber ); | |
| 112 | 112 | ||
| 113 | Template t = Engine::instance()->newTemplate( input, QTest::currentDataTag() ); | ||
| 113 | Template t = m_engine->newTemplate( input, QTest::currentDataTag() ); | ||
| 114 | 114 | ||
| 115 | 115 | Context context( dict ); | |
| 116 | 116 |
tests/testloadertags.cpp
(5 / 5)
|   | |||
| 65 | 65 | ||
| 66 | 66 | void TestLoaderTags::initTestCase() | |
| 67 | 67 | { | |
| 68 | m_engine = Engine::instance(); | ||
| 68 | m_engine = new Engine( this ); | ||
| 69 | 69 | ||
| 70 | 70 | loader = InMemoryTemplateLoader::Ptr( new InMemoryTemplateLoader() ); | |
| 71 | 71 | m_engine->addTemplateLoader( loader ); | |
| … | … | ||
| 88 | 88 | QFETCH( QString, output ); | |
| 89 | 89 | QFETCH( Grantlee::Error, errorNumber ); | |
| 90 | 90 | ||
| 91 | Template t = Engine::instance()->newTemplate( input, QTest::currentDataTag() ); | ||
| 91 | Template t = m_engine->newTemplate( input, QTest::currentDataTag() ); | ||
| 92 | 92 | ||
| 93 | 93 | Context context( dict ); | |
| 94 | 94 | ||
| … | … | ||
| 256 | 256 | ||
| 257 | 257 | // Inheritance from local context without use of template loader | |
| 258 | 258 | ||
| 259 | Template t = Engine::instance()->newTemplate( "1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", "context_template" ); | ||
| 259 | Template t = m_engine->newTemplate( "1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", "context_template" ); | ||
| 260 | 260 | dict.insert( "context_template", QVariant::fromValue( t ) ); | |
| 261 | 261 | ||
| 262 | 262 | QTest::newRow( "inheritance24" ) << "{% extends context_template %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}" << dict << "1234" << NoError; | |
| … | … | ||
| 264 | 264 | dict.clear(); | |
| 265 | 265 | QVariantList list; | |
| 266 | 266 | ||
| 267 | Template t1 = Engine::instance()->newTemplate( "Wrong", "context_template_1" ); | ||
| 268 | Template t2 = Engine::instance()->newTemplate( "1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", "context_template_2" ); | ||
| 267 | Template t1 = m_engine->newTemplate( "Wrong", "context_template_1" ); | ||
| 268 | Template t2 = m_engine->newTemplate( "1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", "context_template_2" ); | ||
| 269 | 269 | list << QVariant::fromValue( t1 ); | |
| 270 | 270 | list << QVariant::fromValue( t2 ); | |
| 271 | 271 |
tests/testmutabletags.cpp
(6 / 6)
|   | |||
| 59 | 59 | ||
| 60 | 60 | void TestMutableTagsSyntax::initTestCase() | |
| 61 | 61 | { | |
| 62 | m_engine = Engine::instance(); | ||
| 62 | m_engine = new Engine( this ); | ||
| 63 | 63 | m_engine->setPluginDirs( QStringList() << GRANTLEE_PLUGIN_PATH ); | |
| 64 | 64 | ||
| 65 | 65 | m_engine->addDefaultLibrary( "grantlee_mutabletags" ); | |
| … | … | ||
| 81 | 81 | QString content = "Begin" | |
| 82 | 82 | "{% raw %} Stuff {% endraw %}" | |
| 83 | 83 | "Afters."; | |
| 84 | Template t = Engine::instance()->newMutableTemplate( content, "t1" ); | ||
| 84 | Template t = m_engine->newMutableTemplate( content, "t1" ); | ||
| 85 | 85 | ||
| 86 | 86 | Context c1( dict ); | |
| 87 | 87 | QString result = t->render( &c1 ); | |
| … | … | ||
| 110 | 110 | QString content = "Begin {% for name in nameList %}{{ name }},{% endfor %}" | |
| 111 | 111 | "{% raw %}{% for name in nameList %}{{ name }},{% endfor %} var: {{ var }}. {% endraw %}" | |
| 112 | 112 | "Afters."; | |
| 113 | Template t = Engine::instance()->newMutableTemplate( content, "t" ); | ||
| 113 | Template t = m_engine->newMutableTemplate( content, "t" ); | ||
| 114 | 114 | ||
| 115 | 115 | Context c1( dict ); | |
| 116 | 116 | QString result = t->render( &c1 ); | |
| … | … | ||
| 148 | 148 | "{% repeater %}{% for name in nameList %}{{ name }},{% endfor %} var: {{ var }}. {% endrepeater %}" | |
| 149 | 149 | "Afters"; | |
| 150 | 150 | ||
| 151 | Template t = Engine::instance()->newMutableTemplate( content, "t" ); | ||
| 151 | Template t = m_engine->newMutableTemplate( content, "t" ); | ||
| 152 | 152 | ||
| 153 | 153 | Context c1( dict ); | |
| 154 | 154 | QString result = t->render( &c1 ); | |
| … | … | ||
| 196 | 196 | "{% endrepeater %}" | |
| 197 | 197 | "After."; | |
| 198 | 198 | ||
| 199 | Template t = Engine::instance()->newMutableTemplate( content, "t" ); | ||
| 199 | Template t = m_engine->newMutableTemplate( content, "t" ); | ||
| 200 | 200 | ||
| 201 | 201 | QVariantHash h; | |
| 202 | 202 | Context c( h ); | |
| … | … | ||
| 213 | 213 | "Bar." | |
| 214 | 214 | "{% endrepeater %}" | |
| 215 | 215 | "After."; | |
| 216 | Template t2 = Engine::instance()->newMutableTemplate( content, "t2" ); | ||
| 216 | Template t2 = m_engine->newMutableTemplate( content, "t2" ); | ||
| 217 | 217 | ||
| 218 | 218 | h.insert( "var", "String" ); | |
| 219 | 219 | Context c2( h ); |
tests/testscriptabletags.cpp
(2 / 2)
|   | |||
| 60 | 60 | ||
| 61 | 61 | void TestScriptableTagsSyntax::initTestCase() | |
| 62 | 62 | { | |
| 63 | m_engine = Engine::instance(); | ||
| 63 | m_engine = new Engine( this ); | ||
| 64 | 64 | QString appDirPath = QFileInfo( QCoreApplication::applicationDirPath() ).absoluteDir().path(); | |
| 65 | 65 | m_engine->setPluginDirs( QStringList() << GRANTLEE_PLUGIN_PATH | |
| 66 | 66 | << ":/grantlee/" // For scripteddefaults.qs | |
| … | … | ||
| 79 | 79 | QFETCH( QString, output ); | |
| 80 | 80 | QFETCH( Grantlee::Error, error ); | |
| 81 | 81 | ||
| 82 | Template t = Engine::instance()->newTemplate( input, QTest::currentDataTag() ); | ||
| 82 | Template t = m_engine->newTemplate( input, QTest::currentDataTag() ); | ||
| 83 | 83 | ||
| 84 | 84 | Context context( dict ); | |
| 85 | 85 |

