File indexing completed on 2025-02-09 04:28:37
0001 /* 0002 This file is part of the KTextTemplate library 0003 0004 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com> 0005 0006 SPDX-License-Identifier: LGPL-2.1-or-later 0007 0008 */ 0009 0010 #ifndef KTEXTTEMPLATE_ENGINE_H 0011 #define KTEXTTEMPLATE_ENGINE_H 0012 0013 #include "template.h" 0014 #include "templateloader.h" 0015 0016 namespace KTextTemplate 0017 { 0018 class TagLibraryInterface; 0019 0020 class EnginePrivate; 0021 0022 /// @headerfile engine.h <KTextTemplate/Engine> 0023 0024 /** 0025 @brief **%KTextTemplate::Engine** is the main entry point for creating %KTextTemplate 0026 Templates. 0027 0028 The **%KTextTemplate::Engine** is responsible for configuring and creating Template 0029 objects. In typical use, one or more AbstractTemplateLoader objects will be 0030 added to the Engine to load template objects, and plugin directories will be 0031 set to enable finding template tags and filters. 0032 0033 @code 0034 auto engine = new Engine(); 0035 0036 auto loader = QSharedPointer<FileSystemTemplateLoader>::create(); 0037 loader->setTemplateDirs( {"/usr/share/myapp/templates"} ); 0038 engine->addTemplateLoader( loader ); 0039 0040 engine->addPluginPath( "/usr/lib/myapp" ); 0041 0042 auto template1 = engine->newTemplate( "Template content", "template name" ); 0043 0044 auto template2 = engine->loadByName( "templatefile.html" ); 0045 @endcode 0046 0047 Once it is configured, the **%Engine** can be used to create new templates by 0048 name by loading the templates with the @ref loadByName method, or by defining 0049 the content in the @ref newTemplate method. 0050 0051 By default the builtin tags and filters distributed with %KTextTemplate are 0052 available in all templates without using the @gr_tag{load} tag in the 0053 template. These pre-loaded libraries may be configured if appropriate to the 0054 application. For example, an application which defines its own tags and 0055 filters may want them to be always available, or it may be desirable to 0056 restrict the features available to template authors by removing built in 0057 libraries. 0058 0059 Different **%Engine** objects can be used to create templates with differing 0060 configurations. 0061 0062 @section smart_trim Insignificant whitespace 0063 0064 The output of rendering a template depends on the content of the template. In 0065 some cases when generating content in which whitespace is significant, this 0066 can have undesired effects. For example, given a template to generate C++ code 0067 like: 0068 0069 @verbatim 0070 class MyClass { 0071 {# This loop creates the #} 0072 {# methods in the class #} 0073 {% for method in methods %} 0074 {% if method.hasDox %} 0075 {{ method.dox }} 0076 {% endif %} 0077 {{ method.signature }} 0078 {% endfor %} 0079 }; 0080 @endverbatim 0081 0082 The output would have a lot of whitespace which is not necessarily wanted. 0083 0084 @code 0085 class MyClass { 0086 0087 0088 0089 0090 void foo() const; 0091 0092 }; 0093 @endcode 0094 0095 It is possible to strip insignificant whitespace by enabling the smartTrim 0096 feature with @ref setSmartTrimEnabled. When enabled the output will not 0097 contain a newline for any line in the template which has only one token of 0098 template syntax, such as a comment, tag or variable. 0099 0100 @code 0101 class MyClass { 0102 0103 void foo() const; 0104 }; 0105 @endcode 0106 0107 @author Stephen Kelly <steveire@gmail.com> 0108 */ 0109 class KTEXTTEMPLATE_EXPORT Engine : public QObject 0110 { 0111 Q_OBJECT 0112 public: 0113 /** 0114 Constructor 0115 */ 0116 Engine(QObject *parent = {}); 0117 0118 /** 0119 Destructor. 0120 */ 0121 ~Engine() override; 0122 0123 /** 0124 Returns the TemplateLoaders currently configured on the **%Engine**. 0125 */ 0126 QList<QSharedPointer<AbstractTemplateLoader>> templateLoaders(); 0127 0128 /** 0129 Adds @p loader to the TemplateLoaders currently configured on 0130 the **%Engine**. 0131 */ 0132 void addTemplateLoader(QSharedPointer<AbstractTemplateLoader> loader); 0133 0134 /** 0135 Sets the plugin dirs currently configured on the **%Engine** to @p dirs. 0136 0137 @warning This overwrites the default paths. You normally want 0138 @ref addPluginPath. 0139 0140 @see @ref finding_plugins 0141 */ 0142 void setPluginPaths(const QStringList &dirs); 0143 0144 /** 0145 Prepend @p dir to the list of plugin dirs. 0146 */ 0147 void addPluginPath(const QString &dir); 0148 0149 /** 0150 Removes all instances of @p dir from the list of plugin dirs. 0151 */ 0152 void removePluginPath(const QString &dir); 0153 0154 /** 0155 Returns the currently configured plugin dirs 0156 */ 0157 QStringList pluginPaths() const; 0158 0159 /** 0160 Returns a URI for a media item with the name @p name. 0161 0162 Typically this will be used for images. For example the media URI for the 0163 image <tt>"header_logo.png"</tt> may be 0164 <tt>"/home/user/common/header_logo.png"</tt> or 0165 <tt>"/home/user/some_theme/header_logo.png"</tt> 0166 depending on the @ref templateLoaders configured. 0167 0168 This method will not usually be called by application code. To load media 0169 in a template, use the @gr_tag{media_finder} template tag. 0170 */ 0171 std::pair<QString, QString> mediaUri(const QString &fileName) const; 0172 0173 /** 0174 Load the Template identified by @p name. 0175 0176 The Templates and plugins loaded will be determined by 0177 the **%Engine** configuration. 0178 */ 0179 Template loadByName(const QString &name) const; 0180 0181 /** 0182 Create a new Template with the content @p content identified by @p name. 0183 0184 The secondary Templates and plugins loaded will be determined by 0185 the **%Engine** configuration. 0186 */ 0187 Template newTemplate(const QString &content, const QString &name) const; 0188 0189 /** 0190 Returns the libraries available by default to new Templates. 0191 */ 0192 QStringList defaultLibraries() const; 0193 0194 /** 0195 Adds the library named @p libName to the libraries available by default to 0196 new Templates. 0197 */ 0198 void addDefaultLibrary(const QString &libName); 0199 0200 /** 0201 Removes the library named @p libName from the libraries available by 0202 default to new Templates. 0203 */ 0204 void removeDefaultLibrary(const QString &libName); 0205 0206 /** 0207 Returns whether the smart trim feature is enabled for newly loaded 0208 templates. 0209 0210 @see smart_trim 0211 0212 This is false by default. 0213 */ 0214 bool smartTrimEnabled() const; 0215 0216 /** 0217 Sets whether the smart trim feature is enabled for newly loaded templates. 0218 0219 @see smart_trim 0220 */ 0221 void setSmartTrimEnabled(bool enabled); 0222 0223 #ifndef K_DOXYGEN 0224 /** 0225 @internal 0226 0227 Loads and returns the libraries specified in defaultLibraries or @p state. 0228 */ 0229 void loadDefaultLibraries(); 0230 0231 /** 0232 @internal 0233 0234 Loads and returns the library specified by @p name in the 0235 current **%Engine** configuration or @p state. 0236 0237 Templates wishing to load a library should use the @gr_tag{load} tag. 0238 */ 0239 TagLibraryInterface *loadLibrary(const QString &name); 0240 #endif 0241 0242 private: 0243 Q_DECLARE_PRIVATE(Engine) 0244 EnginePrivate *const d_ptr; 0245 }; 0246 } 0247 0248 #endif