Restore the full include/grantlee stub tree
include/grantlee/context.h (the only grantlee header referenced by kept code) includes its sibling stubs (abstractlocalizer.h, ...); the file-level include/ prune left those siblings behind, breaking codegen_grantlee compilation (fatal error: abstractlocalizer.h). The grantlee engine stubs are a self-contained header set — keep the whole dir, same treatment as include/GeographicLib.
This commit is contained in:
1 parent
5183c9419c
commit
7c1c22637e
29 files changed
+4694
No files matched your search
@@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_ABSTRACTLOCALIZER_H
|
||||||
|
#define GRANTLEE_ABSTRACTLOCALIZER_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QLocale>
|
||||||
|
#include <QtCore/QSharedPointer>
|
||||||
|
#include <QtCore/QVariantList>
|
||||||
|
|
||||||
|
class QDateTime;
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Interface for implementing an internationalization system.
|
||||||
|
|
||||||
|
This class defines an interface for accessing an internationalization, such as
|
||||||
|
QLocale/QTranslator or gettext/KLocale.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT AbstractLocalizer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor
|
||||||
|
*/
|
||||||
|
AbstractLocalizer();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
virtual ~AbstractLocalizer();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Processes the data in @p variant into a localized string.
|
||||||
|
|
||||||
|
The base implementation can localize numbers, dates and times, and
|
||||||
|
strings.
|
||||||
|
*/
|
||||||
|
virtual QString localize(const QVariant &variant) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the current locale as a string.
|
||||||
|
*/
|
||||||
|
virtual QString currentLocale() const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Makes this localizer use the locale specified by @p localeName for output.
|
||||||
|
*/
|
||||||
|
virtual void pushLocale(const QString &localeName) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Makes this localizer use the previous locale for output.
|
||||||
|
*/
|
||||||
|
virtual void popLocale() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads the @p catalog from @p path.
|
||||||
|
*/
|
||||||
|
virtual void loadCatalog(const QString &path, const QString &catalog) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Unloads the @p catalog
|
||||||
|
*/
|
||||||
|
virtual void unloadCatalog(const QString &catalog) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p number
|
||||||
|
*/
|
||||||
|
virtual QString localizeNumber(int number) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p number
|
||||||
|
*/
|
||||||
|
virtual QString localizeNumber(qreal number) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p value as a monetary value in the currency specified by @p
|
||||||
|
currencyCode.
|
||||||
|
*/
|
||||||
|
virtual QString localizeMonetaryValue(qreal value, const QString ¤cyCode
|
||||||
|
= {}) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p date with the specified @p formatType
|
||||||
|
*/
|
||||||
|
virtual QString localizeDate(const QDate &date,
|
||||||
|
QLocale::FormatType formatType
|
||||||
|
= QLocale::ShortFormat) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p time with the specified @p formatType
|
||||||
|
*/
|
||||||
|
virtual QString localizeTime(const QTime &time,
|
||||||
|
QLocale::FormatType formatType
|
||||||
|
= QLocale::ShortFormat) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p dateTime with the specified @p formatType
|
||||||
|
*/
|
||||||
|
virtual QString localizeDateTime(const QDateTime &dateTime,
|
||||||
|
QLocale::FormatType formatType
|
||||||
|
= QLocale::ShortFormat) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p string with the specified @p arguments for substitution
|
||||||
|
*/
|
||||||
|
virtual QString localizeString(const QString &string,
|
||||||
|
const QVariantList &arguments = {}) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p string, disambiguated by @p context with the specified @p
|
||||||
|
arguments for substitution
|
||||||
|
*/
|
||||||
|
virtual QString
|
||||||
|
localizeContextString(const QString &string, const QString &context,
|
||||||
|
const QVariantList &arguments = {}) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p string or its @p pluralForm with the specified @p arguments
|
||||||
|
for substitution
|
||||||
|
*/
|
||||||
|
virtual QString
|
||||||
|
localizePluralString(const QString &string, const QString &pluralForm,
|
||||||
|
const QVariantList &arguments = {}) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Localizes @p string or its @p pluralForm, disambiguated by @p context with
|
||||||
|
the specified @p arguments for substitution
|
||||||
|
*/
|
||||||
|
virtual QString
|
||||||
|
localizePluralContextString(const QString &string, const QString &pluralForm,
|
||||||
|
const QString &context,
|
||||||
|
const QVariantList &arguments = {}) const = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DISABLE_COPY(AbstractLocalizer)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,273 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2008,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_ABSTRACTMARKUPBUILDER_H
|
||||||
|
#define GRANTLEE_ABSTRACTMARKUPBUILDER_H
|
||||||
|
|
||||||
|
#include "grantlee_textdocument_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QString>
|
||||||
|
#include <QtGui/QTextListFormat>
|
||||||
|
|
||||||
|
class QBrush;
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class AbstractMarkupBuilderPrivate;
|
||||||
|
|
||||||
|
/// @headerfile abstractmarkupbuilder.h grantlee/abstractmarkupbuilder.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Interface for creating marked-up text output.
|
||||||
|
|
||||||
|
The **%AbstractMarkupBuilder** is used by a MarkupDirector to create marked-up
|
||||||
|
output such as html or markdown.
|
||||||
|
|
||||||
|
See PlainTextMarkupBuilder and TextHTMLBuilder for example implementations.
|
||||||
|
|
||||||
|
This interface can be extended to handle custom format types in a
|
||||||
|
QTextDocument. @see @ref custom_qtextobject
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEXTDOCUMENT_EXPORT AbstractMarkupBuilder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/** Destructor */
|
||||||
|
virtual ~AbstractMarkupBuilder() {}
|
||||||
|
|
||||||
|
/** Begin a bold element in the markup */
|
||||||
|
virtual void beginStrong() = 0;
|
||||||
|
|
||||||
|
/** Close the bold element in the markup */
|
||||||
|
virtual void endStrong() = 0;
|
||||||
|
|
||||||
|
/** Begin an emphasised element in the markup */
|
||||||
|
virtual void beginEmph() = 0;
|
||||||
|
|
||||||
|
/** Close the emphasised element in the markup */
|
||||||
|
virtual void endEmph() = 0;
|
||||||
|
|
||||||
|
/** Begin an underlined element in the markup */
|
||||||
|
virtual void beginUnderline() = 0;
|
||||||
|
|
||||||
|
/** Close the underlined element in the markup */
|
||||||
|
virtual void endUnderline() = 0;
|
||||||
|
|
||||||
|
/** Begin a struck out element in the markup */
|
||||||
|
virtual void beginStrikeout() = 0;
|
||||||
|
|
||||||
|
/** Close the struck out element in the markup */
|
||||||
|
virtual void endStrikeout() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a decorarated foreground element in the markup (A text color)
|
||||||
|
using @p brush
|
||||||
|
*/
|
||||||
|
virtual void beginForeground(const QBrush &brush) = 0;
|
||||||
|
|
||||||
|
/** Close the decorarated foreground element in the markup */
|
||||||
|
virtual void endForeground() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a decorarated background element in the markup (A text background
|
||||||
|
color) using @p brush
|
||||||
|
*/
|
||||||
|
virtual void beginBackground(const QBrush &brush) = 0;
|
||||||
|
|
||||||
|
/** Close the decorarated background element in the markup */
|
||||||
|
virtual void endBackground() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a url anchor element in the markup
|
||||||
|
@param href The href of the anchor.
|
||||||
|
@param name The name of the anchor.
|
||||||
|
*/
|
||||||
|
virtual void beginAnchor(const QString &href = {}, const QString &name = {})
|
||||||
|
= 0;
|
||||||
|
|
||||||
|
/** Close the anchor element */
|
||||||
|
virtual void endAnchor() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new font familiy element in the markup
|
||||||
|
@param family The name of the font family to begin.
|
||||||
|
*/
|
||||||
|
virtual void beginFontFamily(const QString &family) = 0;
|
||||||
|
|
||||||
|
/** End font family element */
|
||||||
|
virtual void endFontFamily() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new font point size element in the markup
|
||||||
|
@param size The point size to begin.
|
||||||
|
*/
|
||||||
|
virtual void beginFontPointSize(int size) = 0;
|
||||||
|
|
||||||
|
/** End font point size element */
|
||||||
|
virtual void endFontPointSize() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new paragraph in the markup
|
||||||
|
@param a The alignment of the new paragraph.
|
||||||
|
@param top The top margin of the new paragraph.
|
||||||
|
@param bottom The bottom margin of the new paragraph.
|
||||||
|
@param left The left margin of the new paragraph.
|
||||||
|
@param right The right margin of the new paragraph.
|
||||||
|
*/
|
||||||
|
virtual void beginParagraph(Qt::Alignment a = Qt::AlignLeft, qreal top = 0.0,
|
||||||
|
qreal bottom = 0.0, qreal left = 0.0,
|
||||||
|
qreal right = 0.0)
|
||||||
|
= 0;
|
||||||
|
|
||||||
|
/** Close the paragraph in the markup. */
|
||||||
|
virtual void endParagraph() = 0;
|
||||||
|
/** Add a newline to the markup. */
|
||||||
|
virtual void addNewline() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Insert a horizontal rule into the markup.
|
||||||
|
@param width The width of the rule. Default is full width.
|
||||||
|
*/
|
||||||
|
virtual void insertHorizontalRule(int width = -1) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Insert a new image element into the markup.
|
||||||
|
@param url The url of the image
|
||||||
|
@param width The width of the image
|
||||||
|
@param height The height of the image.
|
||||||
|
*/
|
||||||
|
virtual void insertImage(const QString &url, qreal width, qreal height) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new list element in the markup.
|
||||||
|
A list element contains list items, and may contain other lists.
|
||||||
|
@param style The style of list to create.
|
||||||
|
*/
|
||||||
|
virtual void beginList(QTextListFormat::Style style) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Close the list.
|
||||||
|
*/
|
||||||
|
virtual void endList() = 0;
|
||||||
|
|
||||||
|
/** Begin a new list item in the markup */
|
||||||
|
virtual void beginListItem() = 0;
|
||||||
|
|
||||||
|
/** End the list item */
|
||||||
|
virtual void endListItem() = 0;
|
||||||
|
|
||||||
|
/** Begin a superscript element */
|
||||||
|
virtual void beginSuperscript() = 0;
|
||||||
|
|
||||||
|
/** End superscript element */
|
||||||
|
virtual void endSuperscript() = 0;
|
||||||
|
|
||||||
|
/** Begin a subscript element */
|
||||||
|
virtual void beginSubscript() = 0;
|
||||||
|
|
||||||
|
/** End subscript element */
|
||||||
|
virtual void endSubscript() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a table element.
|
||||||
|
|
||||||
|
@param cellpadding The padding attribute for the table.
|
||||||
|
@param cellspacing The spacing attribute for the table.
|
||||||
|
@param width The width of the table. May be either an integer, or a
|
||||||
|
percentage value.
|
||||||
|
*/
|
||||||
|
virtual void beginTable(qreal cellpadding, qreal cellspacing,
|
||||||
|
const QString &width)
|
||||||
|
= 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new table row
|
||||||
|
*/
|
||||||
|
virtual void beginTableRow() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new table header cell.
|
||||||
|
@param width The width of the cell.
|
||||||
|
@param colSpan The column span of the cell.
|
||||||
|
@param rowSpan The row span of the cell.
|
||||||
|
*/
|
||||||
|
virtual void beginTableHeaderCell(const QString &width, int colSpan,
|
||||||
|
int rowSpan)
|
||||||
|
= 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new table cell.
|
||||||
|
@param width The width of the cell.
|
||||||
|
@param colSpan The column span of the cell.
|
||||||
|
@param rowSpan The row span of the cell.
|
||||||
|
*/
|
||||||
|
virtual void beginTableCell(const QString &width, int colSpan, int rowSpan)
|
||||||
|
= 0;
|
||||||
|
|
||||||
|
/** End a table element */
|
||||||
|
virtual void endTable() = 0;
|
||||||
|
|
||||||
|
/** End a table row */
|
||||||
|
virtual void endTableRow() = 0;
|
||||||
|
|
||||||
|
/** End a table header cell */
|
||||||
|
virtual void endTableHeaderCell() = 0;
|
||||||
|
|
||||||
|
/** End a table cell */
|
||||||
|
virtual void endTableCell() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a level @p level header
|
||||||
|
@param level An integer between 1 and 6
|
||||||
|
*/
|
||||||
|
virtual void beginHeader(int level) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
End a level @p level header
|
||||||
|
@param level An integer between 1 and 6
|
||||||
|
*/
|
||||||
|
virtual void endHeader(int level) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Append the plain text @p text to the markup
|
||||||
|
|
||||||
|
@param text The text to append.
|
||||||
|
*/
|
||||||
|
virtual void appendLiteralText(const QString &text) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Append the raw text @p text to the markup. @p text is added unescaped
|
||||||
|
*/
|
||||||
|
virtual void appendRawText(const QString &text) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Return the fully marked up result of the building process.
|
||||||
|
|
||||||
|
This may contain metadata etc, such as a head element in html.
|
||||||
|
|
||||||
|
@return The fully marked up text.
|
||||||
|
*/
|
||||||
|
virtual QString getResult() = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_BBCODEBUILDER_H
|
||||||
|
#define GRANTLEE_BBCODEBUILDER_H
|
||||||
|
|
||||||
|
#include "abstractmarkupbuilder.h"
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Builder to create BBCode from a QTextDocument
|
||||||
|
*/
|
||||||
|
class BBCodeBuilder : public AbstractMarkupBuilder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Creates a new BBCodeBuilder.
|
||||||
|
*/
|
||||||
|
BBCodeBuilder();
|
||||||
|
|
||||||
|
~BBCodeBuilder() override;
|
||||||
|
|
||||||
|
void beginStrong() override;
|
||||||
|
void endStrong() override;
|
||||||
|
void beginEmph() override;
|
||||||
|
void endEmph() override;
|
||||||
|
void beginUnderline() override;
|
||||||
|
void endUnderline() override;
|
||||||
|
void beginStrikeout() override;
|
||||||
|
void endStrikeout() override;
|
||||||
|
void beginForeground(const QBrush &brush) override;
|
||||||
|
void endForeground() override;
|
||||||
|
|
||||||
|
// Background colour not supported by BBCode.
|
||||||
|
|
||||||
|
void beginAnchor(const QString &href = {}, const QString &name = {}) override;
|
||||||
|
void endAnchor() override;
|
||||||
|
|
||||||
|
// Font family not supported by BBCode.
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin an element of font size @p size. Note that this size is in pixels,
|
||||||
|
and must be converted before it is suitable for use in BBCode.
|
||||||
|
@param size The size of font to begin.
|
||||||
|
*/
|
||||||
|
void beginFontPointSize(int size) override;
|
||||||
|
void endFontPointSize() override;
|
||||||
|
|
||||||
|
void beginParagraph(Qt::Alignment a = Qt::AlignLeft, qreal top = 0.0,
|
||||||
|
qreal bottom = 0.0, qreal left = 0.0,
|
||||||
|
qreal right = 0.0) override;
|
||||||
|
|
||||||
|
void endParagraph() override;
|
||||||
|
void addNewline() override;
|
||||||
|
|
||||||
|
void insertImage(const QString &src, qreal width, qreal height) override;
|
||||||
|
|
||||||
|
void beginList(QTextListFormat::Style type) override;
|
||||||
|
|
||||||
|
void endList() override;
|
||||||
|
|
||||||
|
void beginListItem() override;
|
||||||
|
|
||||||
|
void beginSuperscript() override;
|
||||||
|
|
||||||
|
void endSuperscript() override;
|
||||||
|
|
||||||
|
void beginSubscript() override;
|
||||||
|
|
||||||
|
void endSubscript() override;
|
||||||
|
|
||||||
|
void beginTable(qreal, qreal, const QString &) override;
|
||||||
|
|
||||||
|
void beginTableRow() override;
|
||||||
|
|
||||||
|
void appendLiteralText(const QString &text) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Escapes @p text appropriately for BBCode.
|
||||||
|
*/
|
||||||
|
const QString escape(const QString &text) const;
|
||||||
|
|
||||||
|
QString getResult() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<QTextListFormat::Style> m_currentListItemStyles;
|
||||||
|
|
||||||
|
QString m_text;
|
||||||
|
|
||||||
|
Qt::Alignment m_currentAlignment;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_CACHINGLOADERDECORATOR_H
|
||||||
|
#define GRANTLEE_CACHINGLOADERDECORATOR_H
|
||||||
|
|
||||||
|
#include "templateloader.h"
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class CachingLoaderDecoratorPrivate;
|
||||||
|
|
||||||
|
/// @headerfile cachingloaderdecorator.h grantlee/cachingloaderdecorator.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Implements a loader decorator which caches compiled Template objects.
|
||||||
|
|
||||||
|
The **%CachingLoaderDecorator** can be used with any implementation of
|
||||||
|
Grantlee::AbstractTemplateLoader.
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto loader = QSharedPointer<Grantlee::FileSystemTemplateLoader>::create();
|
||||||
|
loader->setTemplateDirs({"/path/one", "/path/two"});
|
||||||
|
|
||||||
|
auto cache =
|
||||||
|
QSharedPointer<Grantlee::CachingLoaderDecorator>::create( loader );
|
||||||
|
m_engine->addTemplateLoader( cache );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The effect is that templates do not need to be loaded from the filesystem and
|
||||||
|
compiled into Template objects on each access, but may be returned from the
|
||||||
|
cache instead.
|
||||||
|
|
||||||
|
This can be significant if loading Templates in a loop, or loading the same
|
||||||
|
Template very often in an application.
|
||||||
|
|
||||||
|
@code
|
||||||
|
<ul>
|
||||||
|
{% for item in list %}
|
||||||
|
<li>{% include "itemtemplate.html" %}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
If the loading of Templates is a bottleneck in an application, it may make
|
||||||
|
sense to use the caching decorator.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT CachingLoaderDecorator
|
||||||
|
: public AbstractTemplateLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor
|
||||||
|
*/
|
||||||
|
CachingLoaderDecorator(QSharedPointer<AbstractTemplateLoader> loader);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
~CachingLoaderDecorator() override;
|
||||||
|
|
||||||
|
bool canLoadTemplate(const QString &name) const override;
|
||||||
|
|
||||||
|
QPair<QString, QString> getMediaUri(const QString &fileName) const override;
|
||||||
|
|
||||||
|
Template loadByName(const QString &name,
|
||||||
|
const Grantlee::Engine *engine) const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Clears the Templates objects cached in the decorator.
|
||||||
|
*/
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the number of Template objects cached in the decorator.
|
||||||
|
*/
|
||||||
|
int size() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether the cache is empty.
|
||||||
|
*/
|
||||||
|
bool isEmpty() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(CachingLoaderDecorator)
|
||||||
|
CachingLoaderDecoratorPrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,259 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_ENGINE_H
|
||||||
|
#define GRANTLEE_ENGINE_H
|
||||||
|
|
||||||
|
#include "template.h"
|
||||||
|
#include "templateloader.h"
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
class TagLibraryInterface;
|
||||||
|
|
||||||
|
class EnginePrivate;
|
||||||
|
|
||||||
|
/// @headerfile engine.h grantlee/engine.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief **%Grantlee::Engine** is the main entry point for creating %Grantlee
|
||||||
|
Templates.
|
||||||
|
|
||||||
|
The **%Grantlee::Engine** is responsible for configuring and creating Template
|
||||||
|
objects. In typical use, one or more AbstractTemplateLoader objects will be
|
||||||
|
added to the Engine to load template objects, and plugin directories will be
|
||||||
|
set to enable finding template tags and filters.
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto engine = new Engine();
|
||||||
|
|
||||||
|
auto loader = QSharedPointer<FileSystemTemplateLoader>::create();
|
||||||
|
loader->setTemplateDirs( {"/usr/share/myapp/templates"} );
|
||||||
|
engine->addTemplateLoader( loader );
|
||||||
|
|
||||||
|
engine->addPluginPath( "/usr/lib/myapp" );
|
||||||
|
|
||||||
|
auto template1 = engine->newTemplate( "Template content", "template name" );
|
||||||
|
|
||||||
|
auto template2 = engine->loadByName( "templatefile.html" );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Once it is configured, the **%Engine** can be used to create new templates by
|
||||||
|
name by loading the templates with the @ref loadByName method, or by defining
|
||||||
|
the content in the @ref newTemplate method.
|
||||||
|
|
||||||
|
By default the builtin tags and filters distributed with %Grantlee are
|
||||||
|
available in all templates without using the @gr_tag{load} tag in the
|
||||||
|
template. These pre-loaded libraries may be configured if appropriate to the
|
||||||
|
application. For example, an application which defines its own tags and
|
||||||
|
filters may want them to be always available, or it may be desirable to
|
||||||
|
restrict the features available to template authors by removing built in
|
||||||
|
libraries.
|
||||||
|
|
||||||
|
Different **%Engine** objects can be used to create templates with differing
|
||||||
|
configurations.
|
||||||
|
|
||||||
|
@section smart_trim Insignificant whitespace
|
||||||
|
|
||||||
|
The output of rendering a template depends on the content of the template. In
|
||||||
|
some cases when generating content in which whitespace is significant, this
|
||||||
|
can have undesired effects. For example, given a template to generate C++ code
|
||||||
|
like:
|
||||||
|
|
||||||
|
@verbatim
|
||||||
|
class MyClass {
|
||||||
|
{# This loop creates the #}
|
||||||
|
{# methods in the class #}
|
||||||
|
{% for method in methods %}
|
||||||
|
{% if method.hasDox %}
|
||||||
|
{{ method.dox }}
|
||||||
|
{% endif %}
|
||||||
|
{{ method.signature }}
|
||||||
|
{% endfor %}
|
||||||
|
};
|
||||||
|
@endverbatim
|
||||||
|
|
||||||
|
The output would have a lot of whitespace which is not necessarily wanted.
|
||||||
|
|
||||||
|
@code
|
||||||
|
class MyClass {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void foo() const;
|
||||||
|
|
||||||
|
};
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
It is possible to strip insignificant whitespace by enabling the smartTrim
|
||||||
|
feature with @ref setSmartTrimEnabled. When enabled the output will not
|
||||||
|
contain a newline for any line in the template which has only one token of
|
||||||
|
template syntax, such as a comment, tag or variable.
|
||||||
|
|
||||||
|
@code
|
||||||
|
class MyClass {
|
||||||
|
|
||||||
|
void foo() const;
|
||||||
|
};
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT Engine : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor
|
||||||
|
*/
|
||||||
|
Engine(QObject *parent = {});
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
~Engine() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the TemplateLoaders currently configured on the **%Engine**.
|
||||||
|
*/
|
||||||
|
QList<QSharedPointer<AbstractTemplateLoader>> templateLoaders();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Adds @p loader to the TemplateLoaders currently configured on
|
||||||
|
the **%Engine**.
|
||||||
|
*/
|
||||||
|
void addTemplateLoader(QSharedPointer<AbstractTemplateLoader> loader);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets the plugin dirs currently configured on the **%Engine** to @p dirs.
|
||||||
|
|
||||||
|
@warning This overwrites the default paths. You normally want
|
||||||
|
@ref addPluginPath.
|
||||||
|
|
||||||
|
@see @ref finding_plugins
|
||||||
|
*/
|
||||||
|
void setPluginPaths(const QStringList &dirs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Prepend @p dir to the list of plugin dirs.
|
||||||
|
*/
|
||||||
|
void addPluginPath(const QString &dir);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Removes all instances of @p dir from the list of plugin dirs.
|
||||||
|
*/
|
||||||
|
void removePluginPath(const QString &dir);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the currently configured plugin dirs
|
||||||
|
*/
|
||||||
|
QStringList pluginPaths() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a URI for a media item with the name @p name.
|
||||||
|
|
||||||
|
Typically this will be used for images. For example the media URI for the
|
||||||
|
image <tt>"header_logo.png"</tt> may be
|
||||||
|
<tt>"/home/user/common/header_logo.png"</tt> or
|
||||||
|
<tt>"/home/user/some_theme/header_logo.png"</tt>
|
||||||
|
depending on the @ref templateLoaders configured.
|
||||||
|
|
||||||
|
This method will not usually be called by application code. To load media
|
||||||
|
in a template, use the @gr_tag{media_finder} template tag.
|
||||||
|
*/
|
||||||
|
QPair<QString, QString> mediaUri(const QString &fileName) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Load the Template identified by @p name.
|
||||||
|
|
||||||
|
The Templates and plugins loaded will be determined by
|
||||||
|
the **%Engine** configuration.
|
||||||
|
*/
|
||||||
|
Template loadByName(const QString &name) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Create a new Template with the content @p content identified by @p name.
|
||||||
|
|
||||||
|
The secondary Templates and plugins loaded will be determined by
|
||||||
|
the **%Engine** configuration.
|
||||||
|
*/
|
||||||
|
Template newTemplate(const QString &content, const QString &name) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the libraries available by default to new Templates.
|
||||||
|
*/
|
||||||
|
QStringList defaultLibraries() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Adds the library named @p libName to the libraries available by default to
|
||||||
|
new Templates.
|
||||||
|
*/
|
||||||
|
void addDefaultLibrary(const QString &libName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Removes the library named @p libName from the libraries available by
|
||||||
|
default to new Templates.
|
||||||
|
*/
|
||||||
|
void removeDefaultLibrary(const QString &libName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether the smart trim feature is enabled for newly loaded
|
||||||
|
templates.
|
||||||
|
|
||||||
|
@see smart_trim
|
||||||
|
|
||||||
|
This is false by default.
|
||||||
|
*/
|
||||||
|
bool smartTrimEnabled() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets whether the smart trim feature is enabled for newly loaded templates.
|
||||||
|
|
||||||
|
@see smart_trim
|
||||||
|
*/
|
||||||
|
void setSmartTrimEnabled(bool enabled);
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
Loads and returns the libraries specified in defaultLibraries or @p state.
|
||||||
|
*/
|
||||||
|
void loadDefaultLibraries();
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
Loads and returns the library specified by @p name in the
|
||||||
|
current **%Engine** configuration or @p state.
|
||||||
|
|
||||||
|
Templates wishing to load a library should use the @gr_tag{load} tag.
|
||||||
|
*/
|
||||||
|
TagLibraryInterface *loadLibrary(const QString &name);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(Engine)
|
||||||
|
EnginePrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_EXCEPTION_H
|
||||||
|
#define GRANTLEE_EXCEPTION_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QString>
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
Types of errors that can occur while using %Grantlee
|
||||||
|
*/
|
||||||
|
enum Error {
|
||||||
|
NoError,
|
||||||
|
EmptyVariableError,
|
||||||
|
EmptyBlockTagError,
|
||||||
|
InvalidBlockTagError,
|
||||||
|
UnclosedBlockTagError,
|
||||||
|
UnknownFilterError,
|
||||||
|
TagSyntaxError,
|
||||||
|
// VariableSyntaxError,
|
||||||
|
|
||||||
|
VariableNotInContext,
|
||||||
|
ObjectReturnTypeInvalid,
|
||||||
|
CompileFunctionError
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @headerfile exception.h grantlee/exception.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief An exception for use when implementing template tags.
|
||||||
|
|
||||||
|
The **%Exception** class can be used when implementing
|
||||||
|
AbstractNodeFactory::getNode. An exception can be thrown to indicate that
|
||||||
|
the syntax of a particular tag is invalid.
|
||||||
|
|
||||||
|
For example, the following template markup should throw an error because the
|
||||||
|
include tag should have exactly one argument:
|
||||||
|
|
||||||
|
@code
|
||||||
|
<div>
|
||||||
|
{% include %}
|
||||||
|
</div>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The corresponding implementation of IncludeNodeFactory::getNode is
|
||||||
|
|
||||||
|
@code
|
||||||
|
QStringList tagContents = smartSplit( tagContent );
|
||||||
|
|
||||||
|
if ( tagContents.size() != 2 )
|
||||||
|
throw Grantlee::Exception( TagSyntaxError,
|
||||||
|
"Error: Include tag takes exactly one argument" );
|
||||||
|
|
||||||
|
// The item at index 0 in the list is the tag name, "include"
|
||||||
|
QString includeName = tagContents.at( 1 );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT Exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Creates an exception for the error @p errorCode and the verbose
|
||||||
|
message @p what
|
||||||
|
*/
|
||||||
|
Exception(Error errorCode, const QString &what)
|
||||||
|
: m_errorCode(errorCode), m_what(what)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Exception() throw() {}
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
Returns the verbose message for the exception.
|
||||||
|
*/
|
||||||
|
const QString what() const throw() { return m_what; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
Returns the error code for the exception.
|
||||||
|
*/
|
||||||
|
Error errorCode() const { return m_errorCode; }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
Error m_errorCode;
|
||||||
|
QString m_what;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// krazy:excludeall=dpointer
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_FILTER_H
|
||||||
|
#define GRANTLEE_FILTER_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
#include "outputstream.h"
|
||||||
|
#include "safestring.h"
|
||||||
|
|
||||||
|
#include <QtCore/QSharedPointer>
|
||||||
|
#include <QtCore/QStringList>
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/// @headerfile filter.h grantlee/filter.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Base class for all filters.
|
||||||
|
|
||||||
|
The **%Filter** class can be implemented in plugin libraries to make
|
||||||
|
additional functionality available to templates.
|
||||||
|
|
||||||
|
Developers are required only to implement the @ref doFilter method and
|
||||||
|
integrate the filter as part of a custom plugin, but will never create or
|
||||||
|
access filters directly in application code.
|
||||||
|
|
||||||
|
The FilterExpression class is the access interface to a chain of **%Filter**
|
||||||
|
objects.
|
||||||
|
|
||||||
|
The @ref escape and @ref conditionalEscape methods are available for escaping
|
||||||
|
data where needed.
|
||||||
|
|
||||||
|
@see @ref filters
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT Filter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
virtual ~Filter();
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
FilterExpression makes it possible to access stream methods like escape
|
||||||
|
while resolving.
|
||||||
|
*/
|
||||||
|
void setStream(OutputStream *stream);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
Escapes and returns @p input. The OutputStream::escape method is used to
|
||||||
|
escape @p input.
|
||||||
|
*/
|
||||||
|
SafeString escape(const QString &input) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Escapes and returns @p input. The OutputStream::escape method is used to
|
||||||
|
escape @p input.
|
||||||
|
*/
|
||||||
|
SafeString escape(const SafeString &input) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Escapes @p input if not already safe from further escaping and returns it.
|
||||||
|
The OutputStream::escape method is used to escape @p input.
|
||||||
|
*/
|
||||||
|
SafeString conditionalEscape(const SafeString &input) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reimplement to filter @p input given @p argument.
|
||||||
|
|
||||||
|
@p autoescape determines whether the autoescape feature is currently on or
|
||||||
|
off. Most filters will not use this.
|
||||||
|
|
||||||
|
@see @ref autoescaping
|
||||||
|
*/
|
||||||
|
virtual QVariant doFilter(const QVariant &input,
|
||||||
|
const QVariant &argument = {},
|
||||||
|
bool autoescape = {}) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reimplement to return whether this filter is safe.
|
||||||
|
*/
|
||||||
|
virtual bool isSafe() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
OutputStream *m_stream;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,200 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_FILTEREXPRESSION_H
|
||||||
|
#define GRANTLEE_FILTEREXPRESSION_H
|
||||||
|
|
||||||
|
#include "variable.h"
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
class Filter;
|
||||||
|
class OutputStream;
|
||||||
|
class Parser;
|
||||||
|
struct Token;
|
||||||
|
|
||||||
|
class FilterExpressionPrivate;
|
||||||
|
|
||||||
|
/// @headerfile filterexpression.h grantlee/filterexpression.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief A **%FilterExpression** object represents a filter expression in a
|
||||||
|
template.
|
||||||
|
|
||||||
|
This class is only relevant if implementing custom tags or filters. Most of
|
||||||
|
the API here is internal.
|
||||||
|
Usually when implementing tags or filters, filter expressions will just be
|
||||||
|
created and resolved.
|
||||||
|
|
||||||
|
In template markup, a filter expression is a variable followed by one or more
|
||||||
|
filters separated by pipes:
|
||||||
|
|
||||||
|
%Filter expressions may appear in variable nodes:
|
||||||
|
@code
|
||||||
|
{{ some_var|upper_filter|lower_filter }}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Or as arguments to tags
|
||||||
|
@code
|
||||||
|
{% some_tag some_arg1|filter1|filter2 some_arg2|filter3 %}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The **%FilterExpression** class would be used in the getNode implementation of
|
||||||
|
the AbstractNodeFactory implementation for the <tt>some_tag</tt> tag.
|
||||||
|
|
||||||
|
@code
|
||||||
|
Node* SomeTagFactory::getNode(const QString &tagContent, Parser *p) const {
|
||||||
|
auto parts = smartSplit( tagContent );
|
||||||
|
|
||||||
|
parts.removeFirst(); // Remove the "some_tag" part.
|
||||||
|
|
||||||
|
FilterExpression arg1( parts.first(), p );
|
||||||
|
FilterExpression arg2( parts.at( 1 ), p );
|
||||||
|
|
||||||
|
return new SomeTagNode( arg1, arg2, p );
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@see AbstractNodeFactory::getFilterExpressionList
|
||||||
|
|
||||||
|
When implementing the Node::render method, the @ref resolve method may be used
|
||||||
|
to process the filter expression.
|
||||||
|
|
||||||
|
For example, if our <tt>SomeTagNode</tt> was to concatenate the resolved
|
||||||
|
values given as arguments:
|
||||||
|
|
||||||
|
@code
|
||||||
|
void SomeTagNode::render( QTextStream *stream, Context *c ) {
|
||||||
|
m_arg1.resolve( stream, c );
|
||||||
|
m_arg2.resolve( stream, c );
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Because Filters are highly generic, they do not all write data to the stream.
|
||||||
|
For example, a Filter might take as input a string, and return a list by
|
||||||
|
splitting the string on commas, or a Filter might compare an input to its
|
||||||
|
argument and return whether they are the same, but not write anything to the
|
||||||
|
stream. For that reason, the @ref resolve method writes data to the given
|
||||||
|
stream, and returns the same data in its returned QVariant.
|
||||||
|
|
||||||
|
The suitability of either of the @ref resolve methods will depend on the
|
||||||
|
implementation and requirements of your custom tag. For example if the
|
||||||
|
<tt>SomeTagNode</tt> ran a comparison of the arguments:
|
||||||
|
|
||||||
|
@code
|
||||||
|
void SomeTagNode::render( QTextStream *stream, Context *c ) {
|
||||||
|
QString first = m_arg1.resolve( c ).toString();
|
||||||
|
QString second = m_arg2.resolve( c ).toString();
|
||||||
|
|
||||||
|
if ( first == second )
|
||||||
|
m_trueList.render( stream, c );
|
||||||
|
else
|
||||||
|
m_falseList.render( stream, c );
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@see @ref tags_with_end_tags
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT FilterExpression
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructs an invalid **%FilterExpression**.
|
||||||
|
*/
|
||||||
|
FilterExpression();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Constructs a filter expression from the string @p varString. The Parser @p
|
||||||
|
parser is used to retrieve filters.
|
||||||
|
*/
|
||||||
|
FilterExpression(const QString &varString, Grantlee::Parser *parser);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Copy constructor.
|
||||||
|
*/
|
||||||
|
FilterExpression(const FilterExpression &other);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
~FilterExpression();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Assignment operator.
|
||||||
|
*/
|
||||||
|
FilterExpression &operator=(const FilterExpression &other);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the initial variable in the **%FilterExpression**.
|
||||||
|
*/
|
||||||
|
Variable variable() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Resolves the **%FilterExpression** in the Context @p c and writes it to the
|
||||||
|
stream @p stream.
|
||||||
|
*/
|
||||||
|
QVariant resolve(OutputStream *stream, Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Resolves the **%FilterExpression** in the Context @p c.
|
||||||
|
*/
|
||||||
|
QVariant resolve(Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether the Filter resolves to true in the Context @p c.
|
||||||
|
@see @ref truthiness
|
||||||
|
*/
|
||||||
|
bool isTrue(Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a list for the **%FilterExpression**.
|
||||||
|
|
||||||
|
If the **%FilterExpression** can not be resolved to a list, an empty list
|
||||||
|
will be returned.
|
||||||
|
*/
|
||||||
|
QVariantList toList(Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether a filter expression is valid.
|
||||||
|
|
||||||
|
A **%FilterExpression** is valid if all filters in the expression exist and
|
||||||
|
the initial variable being filtered is valid.
|
||||||
|
*/
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
Returns the list of filters in the **%FilterExpression**.
|
||||||
|
*/
|
||||||
|
QStringList filters() const;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(FilterExpression)
|
||||||
|
FilterExpressionPrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
|
||||||
|
#ifndef GRANTLEE_TEMPLATES_EXPORT_H
|
||||||
|
#define GRANTLEE_TEMPLATES_EXPORT_H
|
||||||
|
|
||||||
|
#ifdef GRANTLEE_TEMPLATES_STATIC_DEFINE
|
||||||
|
# define GRANTLEE_TEMPLATES_EXPORT
|
||||||
|
# define GRANTLEE_TEMPLATES_NO_EXPORT
|
||||||
|
#else
|
||||||
|
# ifndef GRANTLEE_TEMPLATES_EXPORT
|
||||||
|
# ifdef Grantlee_Templates_EXPORTS
|
||||||
|
/* We are building this library */
|
||||||
|
# define GRANTLEE_TEMPLATES_EXPORT __attribute__((visibility("default")))
|
||||||
|
# else
|
||||||
|
/* We are using this library */
|
||||||
|
# define GRANTLEE_TEMPLATES_EXPORT __attribute__((visibility("default")))
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# ifndef GRANTLEE_TEMPLATES_NO_EXPORT
|
||||||
|
# define GRANTLEE_TEMPLATES_NO_EXPORT __attribute__((visibility("hidden")))
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEMPLATES_DEPRECATED
|
||||||
|
# define GRANTLEE_TEMPLATES_DEPRECATED __attribute__ ((__deprecated__))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEMPLATES_DEPRECATED_EXPORT
|
||||||
|
# define GRANTLEE_TEMPLATES_DEPRECATED_EXPORT GRANTLEE_TEMPLATES_EXPORT GRANTLEE_TEMPLATES_DEPRECATED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEMPLATES_DEPRECATED_NO_EXPORT
|
||||||
|
# define GRANTLEE_TEMPLATES_DEPRECATED_NO_EXPORT GRANTLEE_TEMPLATES_NO_EXPORT GRANTLEE_TEMPLATES_DEPRECATED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 0 /* DEFINE_NO_DEPRECATED */
|
||||||
|
# ifndef GRANTLEE_TEMPLATES_NO_DEPRECATED
|
||||||
|
# define GRANTLEE_TEMPLATES_NO_DEPRECATED
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* GRANTLEE_TEMPLATES_EXPORT_H */
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
|
||||||
|
#ifndef GRANTLEE_TEXTDOCUMENT_EXPORT_H
|
||||||
|
#define GRANTLEE_TEXTDOCUMENT_EXPORT_H
|
||||||
|
|
||||||
|
#ifdef GRANTLEE_TEXTDOCUMENT_STATIC_DEFINE
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_EXPORT
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_NO_EXPORT
|
||||||
|
#else
|
||||||
|
# ifndef GRANTLEE_TEXTDOCUMENT_EXPORT
|
||||||
|
# ifdef Grantlee_TextDocument_EXPORTS
|
||||||
|
/* We are building this library */
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_EXPORT __attribute__((visibility("default")))
|
||||||
|
# else
|
||||||
|
/* We are using this library */
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_EXPORT __attribute__((visibility("default")))
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# ifndef GRANTLEE_TEXTDOCUMENT_NO_EXPORT
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_NO_EXPORT __attribute__((visibility("hidden")))
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEXTDOCUMENT_DEPRECATED
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_DEPRECATED __attribute__ ((__deprecated__))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEXTDOCUMENT_DEPRECATED_EXPORT
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_DEPRECATED_EXPORT GRANTLEE_TEXTDOCUMENT_EXPORT GRANTLEE_TEXTDOCUMENT_DEPRECATED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEXTDOCUMENT_DEPRECATED_NO_EXPORT
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_DEPRECATED_NO_EXPORT GRANTLEE_TEXTDOCUMENT_NO_EXPORT GRANTLEE_TEXTDOCUMENT_DEPRECATED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 0 /* DEFINE_NO_DEPRECATED */
|
||||||
|
# ifndef GRANTLEE_TEXTDOCUMENT_NO_DEPRECATED
|
||||||
|
# define GRANTLEE_TEXTDOCUMENT_NO_DEPRECATED
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* GRANTLEE_TEXTDOCUMENT_EXPORT_H */
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#define GRANTLEE_VERSION_MAJOR 5
|
||||||
|
#define GRANTLEE_VERSION_MINOR 3
|
||||||
|
#define GRANTLEE_VERSION_PATCH 0
|
||||||
|
#define GRANTLEE_VERSION_STRING "5.3.0"
|
||||||
|
#define GRANTLEE_VERSION ((5<<16)|(3<<8)|(0))
|
||||||
|
#define GRANTLEE_MAJOR_MINOR_VERSION_STRING "5.3"
|
||||||
|
#define GRANTLEE_MIN_PLUGIN_VERSION 0
|
||||||
@@ -0,0 +1,306 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_MARKUPDIRECTOR_H
|
||||||
|
#define GRANTLEE_MARKUPDIRECTOR_H
|
||||||
|
|
||||||
|
#include "abstractmarkupbuilder.h"
|
||||||
|
#include "grantlee_textdocument_export.h"
|
||||||
|
|
||||||
|
#include <QtGui/QTextDocument>
|
||||||
|
#include <QtGui/QTextFrame>
|
||||||
|
|
||||||
|
class QTextTable;
|
||||||
|
class QTextTableCell;
|
||||||
|
class QTextList;
|
||||||
|
class QTextCharFormat;
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class MarkupDirectorPrivate;
|
||||||
|
|
||||||
|
/// @headerfile markupdirector.h grantlee/markupdirector.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Instructs a builder object to create markup output
|
||||||
|
|
||||||
|
The **%MarkupDirector** is used with an implementation of
|
||||||
|
AbstractMarkupBuilder to create a marked up document output.
|
||||||
|
|
||||||
|
Usage can be quite simple.
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto doc = editor->document(); // editor is a QTextEdit
|
||||||
|
|
||||||
|
auto builder = new HTMLBuilder();
|
||||||
|
auto md = new MarkupDirector(builder);
|
||||||
|
md->processDocument(doc);
|
||||||
|
browser->setHtml(builder->getResult()); // browser is a QTextBrowser.
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Or with a different builder:
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto builder = new PlainTextMarkupBuilder();
|
||||||
|
auto md = new MarkupDirector(builder);
|
||||||
|
md->processDocument(doc);
|
||||||
|
browser->setPlainText(builder->getResult());
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The **%MarkupDirector** also provides API for processing just part of a
|
||||||
|
QTextDocument, such as a QTextFrame or a QTextBlock. The appropriate method
|
||||||
|
may then be called with an invalid iterator as appropriate.
|
||||||
|
|
||||||
|
@code
|
||||||
|
// ... Do some processing to get a QTextFrame.
|
||||||
|
auto frame = getFrame();
|
||||||
|
|
||||||
|
auto builder = new PlainTextMarkupBuilder();
|
||||||
|
auto md = new MarkupDirector(builder);
|
||||||
|
|
||||||
|
// Create output from only the frame.
|
||||||
|
md->processFrame(QTextFrame::iterator(), frame);
|
||||||
|
browser->setPlainText(builder->getResult());
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The behaviour of the **%MarkupDirector** can be customized by subclassing.
|
||||||
|
Support for custom types can also be added by implementing the @ref
|
||||||
|
processCustomFragment method.
|
||||||
|
|
||||||
|
@see @ref custom_qtextobject
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEXTDOCUMENT_EXPORT MarkupDirector
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor
|
||||||
|
*/
|
||||||
|
MarkupDirector(AbstractMarkupBuilder *builder);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
virtual ~MarkupDirector();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Constructs the output by directing the builder to create the markup.
|
||||||
|
*/
|
||||||
|
virtual void processDocument(QTextDocument *doc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the single @p frame. If calling
|
||||||
|
this method directly, an invalid QTextFrame::iterator may be used.
|
||||||
|
*/
|
||||||
|
virtual QTextFrame::iterator processFrame(QTextFrame::iterator it,
|
||||||
|
QTextFrame *frame);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the single @p block. If calling
|
||||||
|
this method directly, an invalid QTextFrame::iterator may be used.
|
||||||
|
|
||||||
|
This method does not process the contents of the @p block, but uses the
|
||||||
|
@ref processBlockContents method to do so.
|
||||||
|
*/
|
||||||
|
virtual QTextFrame::iterator processBlock(QTextFrame::iterator it,
|
||||||
|
const QTextBlock &block);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the single @p textObject. If
|
||||||
|
calling this method directly, an invalid QTextFrame::iterator may be used.
|
||||||
|
|
||||||
|
The block @p block is the container of the @p textObject.
|
||||||
|
*/
|
||||||
|
virtual QTextFrame::iterator processObject(QTextFrame::iterator it,
|
||||||
|
const QTextBlock &block,
|
||||||
|
QTextObject *textObject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the single @p textBlockGroup. If
|
||||||
|
calling this method directly, an invalid QTextFrame::iterator may be used.
|
||||||
|
|
||||||
|
The block @p block is the first block in the @p textBlockGroup.
|
||||||
|
*/
|
||||||
|
virtual QPair<QTextFrame::iterator, QTextBlock>
|
||||||
|
processBlockGroup(QTextFrame::iterator it, const QTextBlock &block,
|
||||||
|
QTextBlockGroup *textBlockGroup);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the single @p textList. If
|
||||||
|
calling this method directly, an invalid QTextFrame::iterator may be used.
|
||||||
|
|
||||||
|
The block @p block is the first block in the @p textList.
|
||||||
|
*/
|
||||||
|
virtual QPair<QTextFrame::iterator, QTextBlock>
|
||||||
|
processList(QTextFrame::iterator it, const QTextBlock &block,
|
||||||
|
QTextList *textList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the contents of the single @p
|
||||||
|
block. If calling this method directly, an invalid QTextFrame::iterator
|
||||||
|
may be used.
|
||||||
|
*/
|
||||||
|
virtual QTextFrame::iterator processBlockContents(QTextFrame::iterator it,
|
||||||
|
const QTextBlock &block);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Hook for instructing the builder to create output for the @p fragemnt with
|
||||||
|
a custom type. @p doc is the document the fragment is in.
|
||||||
|
*/
|
||||||
|
virtual void processCustomFragment(const QTextFragment &fragment,
|
||||||
|
QTextDocument const *doc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the contents of the single @p
|
||||||
|
fragment. If calling this method directly, an invalid QTextBlock::iterator
|
||||||
|
may be used. @p doc is the document the fragment is in.
|
||||||
|
*/
|
||||||
|
virtual QTextBlock::iterator processFragment(QTextBlock::iterator it,
|
||||||
|
const QTextFragment &fragment,
|
||||||
|
QTextDocument const *doc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the contents of the single @p
|
||||||
|
textObject. The @p textObject is represented in the QTextDocument with the
|
||||||
|
QTextFragment @p fragment.
|
||||||
|
|
||||||
|
If calling this method directly, an invalid QTextBlock::iterator may be
|
||||||
|
used.
|
||||||
|
*/
|
||||||
|
virtual QTextBlock::iterator
|
||||||
|
processCharTextObject(QTextBlock::iterator it, const QTextFragment &fragment,
|
||||||
|
QTextObject *textObject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the image represented by the @p
|
||||||
|
imageFormat.
|
||||||
|
|
||||||
|
If calling this method directly, an invalid QTextBlock::iterator may be
|
||||||
|
used. @p doc is the document the fragment is in.
|
||||||
|
*/
|
||||||
|
virtual QTextBlock::iterator processImage(QTextBlock::iterator it,
|
||||||
|
const QTextImageFormat &imageFormat,
|
||||||
|
QTextDocument *doc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the contents of the single @p
|
||||||
|
table.
|
||||||
|
|
||||||
|
If calling this method directly, an invalid QTextFrame::iterator may be
|
||||||
|
used.
|
||||||
|
*/
|
||||||
|
virtual QTextFrame::iterator processTable(QTextFrame::iterator it,
|
||||||
|
QTextTable *table);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to create output for the contents of the single @p
|
||||||
|
tableCell. The tableCell is in the @p table.
|
||||||
|
*/
|
||||||
|
virtual void processTableCell(const QTextTableCell &tableCell,
|
||||||
|
QTextTable *table);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
Processes the document between @p begin and @p end
|
||||||
|
*/
|
||||||
|
void processDocumentContents(QTextFrame::iterator begin,
|
||||||
|
QTextFrame::iterator end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Iterates the iterator @p it to the first block after @p blockGroup. @p
|
||||||
|
_block is any block in the @p blockGroup.
|
||||||
|
|
||||||
|
The return pair is the iterator pointing after the end of @p blockGroup
|
||||||
|
and the first block after @p blockGroup.
|
||||||
|
*/
|
||||||
|
QPair<QTextFrame::iterator, QTextBlock>
|
||||||
|
skipBlockGroup(QTextFrame::iterator it, const QTextBlock &_block,
|
||||||
|
QTextBlockGroup *blockGroup);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a list of tags contained in @p openingTags sorted so they can be
|
||||||
|
opened in order and will be closed in the correct order.
|
||||||
|
|
||||||
|
@p openingTags should be a set of tags opened at the fragment pointed to
|
||||||
|
by @p it.
|
||||||
|
*/
|
||||||
|
QList<int> sortOpeningOrder(QSet<int> openingTags,
|
||||||
|
QTextBlock::iterator it) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to close the appropriate tags at the position of @p
|
||||||
|
it.
|
||||||
|
*/
|
||||||
|
virtual void processClosingElements(QTextBlock::iterator it);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Directs the builder to open the appropriate tags at the position of @p it.
|
||||||
|
*/
|
||||||
|
virtual void processOpeningElements(QTextBlock::iterator it);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the tags that should be closed at the position of @p it.
|
||||||
|
*/
|
||||||
|
virtual QSet<int> getElementsToClose(QTextBlock::iterator it) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the tags that should be opened at the position of @p it.
|
||||||
|
*/
|
||||||
|
virtual QList<int> getElementsToOpen(QTextBlock::iterator it);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Flags for the tags that may be open.
|
||||||
|
*/
|
||||||
|
enum OpenElementValues {
|
||||||
|
None = 0x0, /// No tags are open
|
||||||
|
SuperScript = 0x01, /// A superscript tag is open
|
||||||
|
SubScript = 0x02, /// A subscript tag is open
|
||||||
|
Anchor = 0x04, /// An anchor tag is open
|
||||||
|
SpanForeground = 0x08, /// A foreground altering span tag is open.
|
||||||
|
SpanBackground = 0x10, /// A background altering span tag is open.
|
||||||
|
SpanFontFamily = 0x20, /// A font family altering span tag is open.
|
||||||
|
SpanFontPointSize = 0x40, /// A font size altering span tag is open.
|
||||||
|
Strong = 0x80, /// A strong tag is open.
|
||||||
|
Emph = 0x100, /// A emphasis tag is open.
|
||||||
|
Underline = 0x200, /// An underline tag is open.
|
||||||
|
StrikeOut = 0x400 /// A strikeout tag is open.
|
||||||
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
MarkupDirectorPrivate *const d_ptr;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
The builder this MarkupDirector is operating on. This is available when
|
||||||
|
subclassing to customize behaviour.
|
||||||
|
*/
|
||||||
|
AbstractMarkupBuilder *m_builder;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(MarkupDirector)
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_MEDIAWIKIMARKUPBUILDER_H
|
||||||
|
#define GRANTLEE_MEDIAWIKIMARKUPBUILDER_H
|
||||||
|
|
||||||
|
#include "abstractmarkupbuilder.h"
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Creates MediaWiki markup from a QTextDocument
|
||||||
|
*/
|
||||||
|
class MediaWikiMarkupBuilder : public AbstractMarkupBuilder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MediaWikiMarkupBuilder();
|
||||||
|
~MediaWikiMarkupBuilder() override;
|
||||||
|
|
||||||
|
void beginStrong() override;
|
||||||
|
void endStrong() override;
|
||||||
|
void beginEmph() override;
|
||||||
|
void endEmph() override;
|
||||||
|
void beginUnderline() override;
|
||||||
|
void endUnderline() override;
|
||||||
|
void beginStrikeout() override;
|
||||||
|
void endStrikeout() override;
|
||||||
|
|
||||||
|
void endParagraph() override;
|
||||||
|
void addNewline() override;
|
||||||
|
|
||||||
|
void beginAnchor(const QString &href = {}, const QString &name = {}) override;
|
||||||
|
void endAnchor() override;
|
||||||
|
|
||||||
|
void beginHeader(int level) override;
|
||||||
|
void endHeader(int level) override;
|
||||||
|
|
||||||
|
void beginList(QTextListFormat::Style type) override;
|
||||||
|
|
||||||
|
void endList() override;
|
||||||
|
|
||||||
|
void beginListItem() override;
|
||||||
|
void endListItem() override;
|
||||||
|
|
||||||
|
void appendLiteralText(const QString &text) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Escapes @p text appropriately for MediaWiki.
|
||||||
|
*/
|
||||||
|
const QString escape(const QString &s);
|
||||||
|
|
||||||
|
QString getResult() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<QTextListFormat::Style> currentListItemStyles;
|
||||||
|
|
||||||
|
QString m_text;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Michael Jansen <kde@michael-jansen.biz>
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_METATYPE_H
|
||||||
|
#define GRANTLEE_METATYPE_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include "typeaccessor.h"
|
||||||
|
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
|
/// @file
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/// @headerfile metatype.h grantlee/metatype.h
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@brief The **%MetaType** is the interface to the Grantlee introspection
|
||||||
|
system.
|
||||||
|
|
||||||
|
The **%MetaType** class is used as part of the type registration system of
|
||||||
|
%Grantlee.
|
||||||
|
|
||||||
|
@see @ref generic_types_and_templates
|
||||||
|
@author Michael Jansen <kde@michael-jansen.biz>
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT MetaType
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
@internal The signature for a property lookup method
|
||||||
|
*/
|
||||||
|
typedef QVariant (*LookupFunction)(const QVariant &, const QString &);
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal Registers a property lookup method
|
||||||
|
*/
|
||||||
|
static void registerLookUpOperator(int id, LookupFunction f);
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
static void internalLock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
static void internalUnlock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
static QVariant lookup(const QVariant &object, const QString &property);
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
static bool lookupAlreadyRegistered(int id);
|
||||||
|
|
||||||
|
private:
|
||||||
|
MetaType();
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
This is a helper to select an appropriate overload of indexAccess
|
||||||
|
*/
|
||||||
|
template <typename RealType, typename HandleAs> struct LookupTrait {
|
||||||
|
static QVariant doLookUp(const QVariant &object, const QString &property)
|
||||||
|
{
|
||||||
|
typedef typename Grantlee::TypeAccessor<RealType> Accessor;
|
||||||
|
return Accessor::lookUp(object.value<RealType>(), property);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename RealType, typename HandleAs>
|
||||||
|
struct LookupTrait<RealType &, HandleAs &> {
|
||||||
|
static QVariant doLookUp(const QVariant &object, const QString &property)
|
||||||
|
{
|
||||||
|
typedef typename Grantlee::TypeAccessor<HandleAs &> Accessor;
|
||||||
|
return Accessor::lookUp(object.value<HandleAs>(), property);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename RealType, typename HandleAs> static int doRegister(int id)
|
||||||
|
{
|
||||||
|
if (MetaType::lookupAlreadyRegistered(id))
|
||||||
|
return id;
|
||||||
|
|
||||||
|
QVariant (*lf)(const QVariant &, const QString &)
|
||||||
|
= LookupTrait<RealType, HandleAs>::doLookUp;
|
||||||
|
|
||||||
|
MetaType::registerLookUpOperator(
|
||||||
|
id, reinterpret_cast<MetaType::LookupFunction>(lf));
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Register a type so grantlee knows how to handle it.
|
||||||
|
*/
|
||||||
|
template <typename RealType, typename HandleAs> struct InternalRegisterType {
|
||||||
|
static int doReg()
|
||||||
|
{
|
||||||
|
const int id = qMetaTypeId<RealType>();
|
||||||
|
return doRegister<RealType &, HandleAs &>(id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename RealType, typename HandleAs>
|
||||||
|
struct InternalRegisterType<RealType *, HandleAs *> {
|
||||||
|
static int doReg()
|
||||||
|
{
|
||||||
|
const int id = qMetaTypeId<RealType *>();
|
||||||
|
return doRegister<RealType *, HandleAs *>(id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Registers the type RealType with the metatype system.
|
||||||
|
|
||||||
|
This method can take a second template parameter to specify a cast
|
||||||
|
that should be invoked during registration. This is useful if a base type is
|
||||||
|
already supported.
|
||||||
|
|
||||||
|
@code
|
||||||
|
class SomeType
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QString someProp() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
// define some introspectable API for SomeType
|
||||||
|
|
||||||
|
GRANTLEE_BEGIN_LOOKUP(SomeType)
|
||||||
|
if (property == "someProp")
|
||||||
|
return object.someProp();
|
||||||
|
GRANTLEE_END_LOOKUP
|
||||||
|
|
||||||
|
|
||||||
|
class OtherType : public SomeType
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
registerMetaType<SomeType>();
|
||||||
|
|
||||||
|
// Only the introspectable API from SomeType is needed, so we can reuse that
|
||||||
|
registration.
|
||||||
|
registerMetaType<OtherType, SomeType>();
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@see @ref generic_types_and_templates
|
||||||
|
*/
|
||||||
|
template <typename RealType, typename HandleAs> int registerMetaType()
|
||||||
|
{
|
||||||
|
MetaType::internalLock();
|
||||||
|
|
||||||
|
const int id = InternalRegisterType<RealType, HandleAs>::doReg();
|
||||||
|
|
||||||
|
MetaType::internalUnlock();
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
Register a type so %Grantlee knows how to handle it.
|
||||||
|
|
||||||
|
This is a convenience method.
|
||||||
|
*/
|
||||||
|
template <typename Type> int registerMetaType()
|
||||||
|
{
|
||||||
|
return registerMetaType<Type, Type>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
} // namespace Grantlee
|
||||||
|
|
||||||
|
/**
|
||||||
|
Top boundary of a lookup function for Type.
|
||||||
|
|
||||||
|
@see @ref generic_types
|
||||||
|
*/
|
||||||
|
#define GRANTLEE_BEGIN_LOOKUP(Type) \
|
||||||
|
namespace Grantlee \
|
||||||
|
{ \
|
||||||
|
template <> \
|
||||||
|
inline QVariant TypeAccessor<Type &>::lookUp(const Type &object, \
|
||||||
|
const QString &property) \
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
Top boundary of a lookup function for Type*.
|
||||||
|
|
||||||
|
@see @ref generic_types
|
||||||
|
*/
|
||||||
|
#define GRANTLEE_BEGIN_LOOKUP_PTR(Type) \
|
||||||
|
namespace Grantlee \
|
||||||
|
{ \
|
||||||
|
template <> \
|
||||||
|
inline QVariant TypeAccessor<Type *>::lookUp(const Type *const object, \
|
||||||
|
const QString &property) \
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
Bottom boundary of a lookup function for Type.
|
||||||
|
|
||||||
|
@see @ref generic_types
|
||||||
|
*/
|
||||||
|
#define GRANTLEE_END_LOOKUP \
|
||||||
|
return QVariant(); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // #define GRANTLEE_METATYPE_H
|
||||||
@@ -0,0 +1,380 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_NODE_H
|
||||||
|
#define GRANTLEE_NODE_H
|
||||||
|
|
||||||
|
// krazy:excludeall=dpointer
|
||||||
|
|
||||||
|
#include "context.h"
|
||||||
|
#include "filterexpression.h"
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
#include "outputstream.h"
|
||||||
|
#include "safestring.h"
|
||||||
|
|
||||||
|
#include <QtCore/QStringList>
|
||||||
|
|
||||||
|
// Need these for inheriting from QList<T> to work
|
||||||
|
// http://lists.trolltech.com/qt-interest/2008-01/thread00578-0.html
|
||||||
|
#include <QtCore/QSet>
|
||||||
|
#include <QtCore/QVector>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class Engine;
|
||||||
|
class NodeList;
|
||||||
|
class TemplateImpl;
|
||||||
|
|
||||||
|
class NodePrivate;
|
||||||
|
|
||||||
|
/// @headerfile node.h grantlee/node.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Base class for all nodes.
|
||||||
|
|
||||||
|
The **%Node** class can be implemented to make additional functionality
|
||||||
|
available to Templates.
|
||||||
|
|
||||||
|
A node is represented in template markup as content surrounded by percent
|
||||||
|
signed tokens.
|
||||||
|
|
||||||
|
@code
|
||||||
|
text content
|
||||||
|
{% some_tag arg1 arg2 %}
|
||||||
|
text content
|
||||||
|
{% some_other_tag arg1 arg2 %}
|
||||||
|
text content
|
||||||
|
{% end_some_other_tag %}
|
||||||
|
text content
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
This is parsed into a tree of **%Node** objects by an implementation of
|
||||||
|
AbstractNodeFactory. The **%Node** objects can then later be rendered by their
|
||||||
|
@ref render method.
|
||||||
|
|
||||||
|
Rendering a **%Node** will usually mean writing some output to the stream. The
|
||||||
|
content written to the stream could be determined by the arguments to the tag,
|
||||||
|
or by the content of child nodes between a start and end tag, or both.
|
||||||
|
|
||||||
|
@see FilterExpression
|
||||||
|
@see @ref tags
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT Node : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor.
|
||||||
|
|
||||||
|
@param parent The parent QObject
|
||||||
|
*/
|
||||||
|
explicit Node(QObject *parent = {});
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
~Node() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reimplement this to render the template in the Context @p c.
|
||||||
|
|
||||||
|
This will also involve calling render on and child nodes.
|
||||||
|
*/
|
||||||
|
virtual void render(OutputStream *stream, Context *c) const = 0;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
virtual bool mustBeFirst()
|
||||||
|
{ // krazy:exclude:inline
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
Renders the value @p input in the Context @p c. This will involve escaping
|
||||||
|
@p input if necessary.
|
||||||
|
|
||||||
|
This is only relevant to developing template tags.
|
||||||
|
*/
|
||||||
|
void streamValueInContext(OutputStream *stream, const QVariant &input,
|
||||||
|
Grantlee::Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a raw pointer to the Template this **%Node** is in.
|
||||||
|
*/
|
||||||
|
TemplateImpl *containerTemplate() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(Node)
|
||||||
|
NodePrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @headerfile node.h grantlee/node.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief A list of Nodes with some convenience API for rendering them.
|
||||||
|
|
||||||
|
Typically, tags which have an end tag will create and later render a list of
|
||||||
|
child nodes.
|
||||||
|
|
||||||
|
This class contains API such as @ref append and @ref render to make creating
|
||||||
|
such list easily.
|
||||||
|
|
||||||
|
The @ref findChildren method behaves similarly to the QObject::findChildren
|
||||||
|
method, returning a list of nodes of a particular type from the Node objects
|
||||||
|
contained in the list (and their children).
|
||||||
|
|
||||||
|
@see @ref tags_with_end_tags
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT NodeList : public QList<Grantlee::Node *>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Creates an empty **%NodeList**.
|
||||||
|
*/
|
||||||
|
NodeList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Copy constructor.
|
||||||
|
*/
|
||||||
|
NodeList(const NodeList &list);
|
||||||
|
|
||||||
|
NodeList &operator=(const NodeList &list);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convenience constructor
|
||||||
|
*/
|
||||||
|
/* implicit */ NodeList(const QList<Grantlee::Node *> &list);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
~NodeList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Appends @p node to the end of this **%NodeList**.
|
||||||
|
*/
|
||||||
|
void append(Grantlee::Node *node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Appends @p nodeList to the end of this **%NodeList**.
|
||||||
|
*/
|
||||||
|
void append(QList<Grantlee::Node *> nodeList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns true if this **%NodeList** contains non-text nodes.
|
||||||
|
*/
|
||||||
|
bool containsNonText() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
A recursive listing of nodes in this tree of type @p T.
|
||||||
|
*/
|
||||||
|
template <typename T> QList<T> findChildren()
|
||||||
|
{
|
||||||
|
QList<T> children;
|
||||||
|
QList<Grantlee::Node *>::const_iterator it;
|
||||||
|
const QList<Grantlee::Node *>::const_iterator first = constBegin();
|
||||||
|
const QList<Grantlee::Node *>::const_iterator last = constEnd();
|
||||||
|
for (it = first; it != last; ++it) {
|
||||||
|
T object = qobject_cast<T>(*it);
|
||||||
|
if (object) {
|
||||||
|
children << object;
|
||||||
|
}
|
||||||
|
children << (*it)->findChildren<T>();
|
||||||
|
}
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Renders the list of Nodes in the Context @p c.
|
||||||
|
*/
|
||||||
|
void render(OutputStream *stream, Context *c) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_containsNonText;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AbstractNodeFactoryPrivate;
|
||||||
|
|
||||||
|
/// @headerfile node.h grantlee/node.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Base class for all NodeFactories
|
||||||
|
|
||||||
|
This class can be used to make custom tags available to templates.
|
||||||
|
The getNode method should be implemented to return a Node to be rendered.
|
||||||
|
|
||||||
|
A node is represented in template markup as content surrounded by percent
|
||||||
|
signed tokens.
|
||||||
|
|
||||||
|
@code
|
||||||
|
text content
|
||||||
|
{% some_tag arg1 arg2 %}
|
||||||
|
text content
|
||||||
|
{% some_other_tag arg1 arg2 %}
|
||||||
|
text content
|
||||||
|
{% end_some_other_tag %}
|
||||||
|
text content
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
It is the responsibility of an **%AbstractNodeFactory** implementation to
|
||||||
|
process the contents of a tag and return a Node implementation from its
|
||||||
|
getNode method.
|
||||||
|
|
||||||
|
The @ref getNode method would for example be called with the tagContent
|
||||||
|
\"<tt>some_tag arg1 arg2</tt>\". That content could then be split up, the
|
||||||
|
arguments processed and a Node created
|
||||||
|
|
||||||
|
@code
|
||||||
|
Node* SomeTagFactory::getNode(const QString &tagContent, Parser *p) {
|
||||||
|
QStringList parts = smartSplit( tagContent );
|
||||||
|
|
||||||
|
parts.removeFirst(); // Remove the "some_tag" part.
|
||||||
|
|
||||||
|
FilterExpression arg1( parts.first(), p );
|
||||||
|
FilterExpression arg2( parts.at( 1 ), p );
|
||||||
|
|
||||||
|
return new SomeTagNode( arg1, arg2, p );
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The @ref getNode implementation might also advance the parser. For example if
|
||||||
|
we had a @gr_tag{times} tag which rendered content the amount of times it was
|
||||||
|
given in its argument, it could be used like this:
|
||||||
|
|
||||||
|
@code
|
||||||
|
Some text content.
|
||||||
|
{% times 5 %}
|
||||||
|
the bit to be repeated
|
||||||
|
{% end_times %}
|
||||||
|
End text content
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The argument to @gr_tag{times} might not be a simple number, but could be a
|
||||||
|
FilterExpression such as \"<tt>someobject.some_property|getDigit:1</tt>\".
|
||||||
|
|
||||||
|
The implementation could look like
|
||||||
|
|
||||||
|
@code
|
||||||
|
Node* SomeOtherTagFactory::getNode(const QString &tagContent, Parser *p) {
|
||||||
|
QStringList parts = smartSplit( tagContent );
|
||||||
|
|
||||||
|
parts.removeFirst(); // Remove the "times" part.
|
||||||
|
|
||||||
|
FilterExpression arg( parts.first(), p );
|
||||||
|
|
||||||
|
auto node = new SomeTagNode( arg, p );
|
||||||
|
auto childNodes = p->parse( node, "end_times" );
|
||||||
|
node->setChildNodes( childNodes );
|
||||||
|
p->removeNextToken();
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Note that it is necessary to invoke the parser to create the child nodes only
|
||||||
|
after creating the Node to return. That node must be passed to the Parser to
|
||||||
|
perform as the parent QObject to the child nodes.
|
||||||
|
|
||||||
|
@see Parser::parse
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT AbstractNodeFactory : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor.
|
||||||
|
|
||||||
|
@param parent The parent QObject
|
||||||
|
*/
|
||||||
|
explicit AbstractNodeFactory(QObject *parent = {});
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
~AbstractNodeFactory() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
This method should be reimplemented to return a Node which can be
|
||||||
|
rendered.
|
||||||
|
|
||||||
|
The @p tagContent is the content of the tag including the tag name and
|
||||||
|
arguments. For example, if the template content is @gr_tag{my_tag arg1
|
||||||
|
arg2}, the tagContent will be "my_tag arg1 arg2".
|
||||||
|
|
||||||
|
The Parser @p p is available and can be advanced if appropriate. For
|
||||||
|
example, if the tag has an end tag, the parser can be advanced to the end
|
||||||
|
tag.
|
||||||
|
|
||||||
|
@see tags
|
||||||
|
*/
|
||||||
|
virtual Node *getNode(const QString &tagContent, Parser *p) const = 0;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
Sets the Engine which created this NodeFactory. Used by the
|
||||||
|
ScriptableNodeFactory.
|
||||||
|
*/
|
||||||
|
virtual void setEngine(Engine *) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
Splits @p str into a list, taking quote marks into account.
|
||||||
|
|
||||||
|
This is typically used in the implementation of getNode with the
|
||||||
|
tagContent.
|
||||||
|
|
||||||
|
If @p str is 'one "two three" four 'five " six' seven', the
|
||||||
|
returned list will contain the following strings:
|
||||||
|
|
||||||
|
- one
|
||||||
|
- "two three"
|
||||||
|
- four
|
||||||
|
- five " six
|
||||||
|
- seven
|
||||||
|
*/
|
||||||
|
Q_INVOKABLE QStringList smartSplit(const QString &str) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
Returns a list of FilterExpression objects created with Parser @p p as
|
||||||
|
described by the content of @p list.
|
||||||
|
|
||||||
|
This is used for convenience when handling the arguments to a tag.
|
||||||
|
*/
|
||||||
|
QList<FilterExpression> getFilterExpressionList(const QStringList &list,
|
||||||
|
Parser *p) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(AbstractNodeFactory)
|
||||||
|
AbstractNodeFactoryPrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_OUTPUTSTREAM_H
|
||||||
|
#define GRANTLEE_OUTPUTSTREAM_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QSharedPointer>
|
||||||
|
#include <QtCore/QTextStream>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class SafeString;
|
||||||
|
|
||||||
|
/// @headerfile outputstream.h grantlee/outputstream.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The **%OutputStream** class is used to render templates to a
|
||||||
|
QTextStream
|
||||||
|
|
||||||
|
A **%OutputStream** instance may be passed to the render method of a Template
|
||||||
|
to render the template to a stream.
|
||||||
|
|
||||||
|
@code
|
||||||
|
QFile outputFile("./output");
|
||||||
|
outputFile.open(QFile::WriteOnly);
|
||||||
|
QTextStream tstream( &outputFile );
|
||||||
|
|
||||||
|
OutputStream os(&tstream);
|
||||||
|
t->render( &os, &context );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The **%OutputStream** is used to escape the content streamed to it. By
|
||||||
|
default, the escaping is html escaping, converting "&" to "&" for example.
|
||||||
|
If generating non-html output, the @ref escape method may be overriden to
|
||||||
|
perform a different escaping, or non at all.
|
||||||
|
|
||||||
|
If overriding the @ref escape method, the @ref clone method must also be
|
||||||
|
overriden to return an **%OutputStream** with the same escaping behaviour.
|
||||||
|
|
||||||
|
@code
|
||||||
|
class NoEscapeStream : public Grantlee::OutputStream
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// ...
|
||||||
|
|
||||||
|
QString escape( const QString &input ) const
|
||||||
|
{
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
QSharedPointer<OutputStream> clone( QTextStream *stream ) const
|
||||||
|
{
|
||||||
|
return QSharedPointer<NoEscapeStream>::create( stream );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT OutputStream
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Creates a null **%OutputStream**. Content streamed to
|
||||||
|
this **%OutputStream** is sent to <tt>/dev/null</tt>
|
||||||
|
*/
|
||||||
|
OutputStream();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates an **%OutputStream** which will stream content to @p stream
|
||||||
|
with appropriate escaping.
|
||||||
|
*/
|
||||||
|
explicit OutputStream(QTextStream *stream);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
virtual ~OutputStream();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns an escaped version of @p input. Does not write anything to the
|
||||||
|
stream.
|
||||||
|
*/
|
||||||
|
virtual QString escape(const QString &input) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns an escaped version of @p input. Does not write anything to the
|
||||||
|
stream.
|
||||||
|
*/
|
||||||
|
QString escape(const SafeString &input) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a cloned **%OutputStream** with the same filtering behaviour.
|
||||||
|
*/
|
||||||
|
virtual QSharedPointer<OutputStream> clone(QTextStream *stream) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns @p after escaping it, unless @p input is "safe", in which case,
|
||||||
|
@p input is returned unmodified.
|
||||||
|
*/
|
||||||
|
QString conditionalEscape(const Grantlee::SafeString &input) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Writes @p input to the stream after escaping it.
|
||||||
|
*/
|
||||||
|
OutputStream &operator<<(const QString &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Writes @p input to the stream after escaping it if necessary.
|
||||||
|
*/
|
||||||
|
OutputStream &operator<<(const SafeString &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reads the content of @p stream and writes it unmodified to the result
|
||||||
|
stream.
|
||||||
|
*/
|
||||||
|
OutputStream &operator<<(QTextStream *stream);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QTextStream *m_stream;
|
||||||
|
Q_DISABLE_COPY(OutputStream)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_PARSER_H
|
||||||
|
#define GRANTLEE_PARSER_H
|
||||||
|
|
||||||
|
#include "filter.h"
|
||||||
|
#include "node.h"
|
||||||
|
#include "token.h"
|
||||||
|
|
||||||
|
#include <QtCore/QStringList>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
class TemplateImpl;
|
||||||
|
|
||||||
|
class ParserPrivate;
|
||||||
|
|
||||||
|
/// @headerfile parser.h grantlee/parser.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The **%Parser** class processes a string template into a tree of nodes.
|
||||||
|
|
||||||
|
For application developers, this class is internal.
|
||||||
|
|
||||||
|
For template tag authors it may be necessary to advance the parser and process
|
||||||
|
contained tags if the tag works in a tag -- endtag fashion.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT Parser : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor.
|
||||||
|
|
||||||
|
Initialises the **%Parser** with the @p tokenList.
|
||||||
|
*/
|
||||||
|
Parser(const QList<Token> &tokenList, QObject *parent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor.
|
||||||
|
*/
|
||||||
|
~Parser() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Advance the parser, using @p parent as the parent of new Nodes. The parser
|
||||||
|
will stop if it encounters a tag which is contained in the list @p stopAt.
|
||||||
|
|
||||||
|
For example, the @gr_tag{if} tag would stopAt both @gr_tag{endif} and
|
||||||
|
@gr_tag{else} tags.
|
||||||
|
|
||||||
|
@see AbstractNodeFactory::getNode
|
||||||
|
*/
|
||||||
|
NodeList parse(Node *parent, const QStringList &stopAt = {});
|
||||||
|
|
||||||
|
/**
|
||||||
|
This is an overloaded method.
|
||||||
|
@see parse.
|
||||||
|
*/
|
||||||
|
NodeList parse(TemplateImpl *parent, const QStringList &stopAt = {});
|
||||||
|
|
||||||
|
/**
|
||||||
|
This is an overloaded method.
|
||||||
|
@see parse.
|
||||||
|
*/
|
||||||
|
NodeList parse(Node *parent, const QString &stopAt);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the filter object called @p name or an invalid object if no filter
|
||||||
|
with that name is loaded.
|
||||||
|
*/
|
||||||
|
QSharedPointer<Filter> getFilter(const QString &name) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Advances the parser to the tag @p tag. This method is similar to @ref parse,
|
||||||
|
but it does not create nodes for tags encountered.
|
||||||
|
*/
|
||||||
|
void skipPast(const QString &tag);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the next token to be processed by the parser. This can be examined
|
||||||
|
in template tag implementations to determine why parsing stopped.
|
||||||
|
|
||||||
|
For example, if the @gr_tag{if} tag, parsing may stop at an @gr_tag{else}
|
||||||
|
tag, in which case parsing should be restarted, or it could stop at an
|
||||||
|
@gr_tag{endif} tag, in which case parsing is finished for that node.
|
||||||
|
*/
|
||||||
|
Token takeNextToken();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether the parser has another token to process.
|
||||||
|
*/
|
||||||
|
bool hasNextToken() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Deletes the next token available to the parser.
|
||||||
|
*/
|
||||||
|
void removeNextToken();
|
||||||
|
|
||||||
|
void invalidBlockTag(const Token &token, const QString &command,
|
||||||
|
const QStringList &stopAt = {});
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
Used by the @gr_tag{load} tag to load libraries.
|
||||||
|
*/
|
||||||
|
void loadLib(const QString &name);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
Puts the token @p token to the front of the list to be processed by the
|
||||||
|
parser.
|
||||||
|
*/
|
||||||
|
void prependToken(const Token &token);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(Parser)
|
||||||
|
ParserPrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_PLAINTEXTMARKUPBUILDER_H
|
||||||
|
#define GRANTLEE_PLAINTEXTMARKUPBUILDER_H
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#pragma warning(disable : 4250)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define LETTERSINALPHABET 26
|
||||||
|
#define DIGITSOFFSET 10
|
||||||
|
|
||||||
|
#include "abstractmarkupbuilder.h"
|
||||||
|
#include "grantlee_textdocument_export.h"
|
||||||
|
#include "markupdirector.h"
|
||||||
|
|
||||||
|
class QBrush;
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class PlainTextMarkupBuilderPrivate;
|
||||||
|
|
||||||
|
/// @headerfile plaintextmarkupbuilder.h grantlee/plaintextmarkupbuilder.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Creates a simple marked up plain text document
|
||||||
|
|
||||||
|
This class creates a simple plain text markup.
|
||||||
|
|
||||||
|
Text that may be represented as
|
||||||
|
|
||||||
|
@code
|
||||||
|
A paragraph with <b>bold</b> text, <i>italic</i> text,
|
||||||
|
and <u>underlined</u> text.
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
would be output as
|
||||||
|
|
||||||
|
@code
|
||||||
|
A paragraph with *bold* text /italic/ text,
|
||||||
|
and _underlined_ text.
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The markup is intended to be simple, plain and easily human readable. No
|
||||||
|
markup is created for different font-familiy, font-size, foreground or
|
||||||
|
background colors.
|
||||||
|
|
||||||
|
Lists are marked up by preceding the list element with '*' for disc, 'o' for
|
||||||
|
circle, 'X' for square, or a letter or number. Lists are also indented if
|
||||||
|
nested. eg:
|
||||||
|
|
||||||
|
@code
|
||||||
|
A. One
|
||||||
|
B. Two
|
||||||
|
o Three
|
||||||
|
o Four
|
||||||
|
\* Five
|
||||||
|
\* Six
|
||||||
|
C. Seven
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
External references such as external urls and images are represented in the
|
||||||
|
body text as a reference, and references are maintained at the bottom of the
|
||||||
|
output.
|
||||||
|
|
||||||
|
Eg,
|
||||||
|
@code
|
||||||
|
Here is a link to <a href="http://www.kde.org">KDE</a> and
|
||||||
|
the <a href="http://pim.kde.org">KDEPIM project</a>.
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
becomes:
|
||||||
|
|
||||||
|
@code
|
||||||
|
Here is a link to KDE[1], and
|
||||||
|
the KDEPIM project[2].
|
||||||
|
|
||||||
|
---- References ----
|
||||||
|
[1] http://www.kde.org
|
||||||
|
[2] http://pim.kde.org
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEXTDOCUMENT_EXPORT PlainTextMarkupBuilder
|
||||||
|
: virtual public AbstractMarkupBuilder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PlainTextMarkupBuilder();
|
||||||
|
|
||||||
|
~PlainTextMarkupBuilder() override;
|
||||||
|
|
||||||
|
void beginStrong() override;
|
||||||
|
void endStrong() override;
|
||||||
|
void beginEmph() override;
|
||||||
|
void endEmph() override;
|
||||||
|
void beginUnderline() override;
|
||||||
|
void endUnderline() override;
|
||||||
|
void beginStrikeout() override;
|
||||||
|
void endStrikeout() override;
|
||||||
|
|
||||||
|
void beginAnchor(const QString &href = {}, const QString &name = {}) override;
|
||||||
|
|
||||||
|
void endAnchor() override;
|
||||||
|
|
||||||
|
void beginForeground(const QBrush &brush) override;
|
||||||
|
|
||||||
|
void endForeground() override;
|
||||||
|
|
||||||
|
void beginBackground(const QBrush &brush) override;
|
||||||
|
|
||||||
|
void endBackground() override;
|
||||||
|
|
||||||
|
void beginFontFamily(const QString &family) override;
|
||||||
|
|
||||||
|
void endFontFamily() override;
|
||||||
|
|
||||||
|
void beginFontPointSize(int size) override;
|
||||||
|
|
||||||
|
void endFontPointSize() override;
|
||||||
|
|
||||||
|
void beginParagraph(Qt::Alignment a = Qt::AlignLeft, qreal top = 0.0,
|
||||||
|
qreal bottom = 0.0, qreal left = 0.0,
|
||||||
|
qreal right = 0.0) override;
|
||||||
|
|
||||||
|
void endParagraph() override;
|
||||||
|
void addNewline() override;
|
||||||
|
|
||||||
|
void insertHorizontalRule(int width = -1) override;
|
||||||
|
|
||||||
|
void insertImage(const QString &src, qreal width, qreal height) override;
|
||||||
|
|
||||||
|
void beginList(QTextListFormat::Style style) override;
|
||||||
|
|
||||||
|
void endList() override;
|
||||||
|
|
||||||
|
void beginListItem() override;
|
||||||
|
|
||||||
|
void endListItem() override;
|
||||||
|
|
||||||
|
void beginSuperscript() override;
|
||||||
|
|
||||||
|
void endSuperscript() override;
|
||||||
|
|
||||||
|
void beginSubscript() override;
|
||||||
|
|
||||||
|
void endSubscript() override;
|
||||||
|
|
||||||
|
void beginTable(qreal cellpadding, qreal cellspacing,
|
||||||
|
const QString &width) override;
|
||||||
|
|
||||||
|
void beginTableRow() override;
|
||||||
|
|
||||||
|
void beginTableHeaderCell(const QString &width, int colSpan,
|
||||||
|
int rowSpan) override;
|
||||||
|
|
||||||
|
void beginTableCell(const QString &width, int colSpan, int rowSpan) override;
|
||||||
|
|
||||||
|
void endTable() override;
|
||||||
|
|
||||||
|
void endTableRow() override;
|
||||||
|
|
||||||
|
void endTableHeaderCell() override;
|
||||||
|
|
||||||
|
void endTableCell() override;
|
||||||
|
|
||||||
|
void beginHeader(int level) override;
|
||||||
|
|
||||||
|
void endHeader(int level) override;
|
||||||
|
|
||||||
|
void appendLiteralText(const QString &text) override;
|
||||||
|
|
||||||
|
void appendRawText(const QString &text) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Adds a reference to @p reference to the internal list of references in the
|
||||||
|
document.
|
||||||
|
*/
|
||||||
|
int addReference(const QString &reference);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the finalised plain text markup, including references at the end.
|
||||||
|
*/
|
||||||
|
QString getResult() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
PlainTextMarkupBuilderPrivate *const d_ptr;
|
||||||
|
Q_DECLARE_PRIVATE(PlainTextMarkupBuilder)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_QTLOCALIZER_P_H
|
||||||
|
#define GRANTLEE_QTLOCALIZER_P_H
|
||||||
|
|
||||||
|
#include "abstractlocalizer.h"
|
||||||
|
|
||||||
|
class QTranslator;
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class QtLocalizerPrivate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Provides internationalization based on QLocale and QTranslator.
|
||||||
|
|
||||||
|
This class implements access to the %Qt Localization system. Localizable
|
||||||
|
types such as strings, numbers, dates and times can be processed.
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto t = m_engine->loadTemplate(someTemplate);
|
||||||
|
auto c = getContext();
|
||||||
|
|
||||||
|
// Render with the German locale
|
||||||
|
c.setLocalizer(m_de_Localizer);
|
||||||
|
auto deText = t->render(&c);
|
||||||
|
de_display->setText(deText);
|
||||||
|
|
||||||
|
// Render with the French locale
|
||||||
|
c.setLocalizer(m_fr_Localizer);
|
||||||
|
auto frText = t->render(&c);
|
||||||
|
fr_display->setText(frText);
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT QtLocalizer : public AbstractLocalizer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructs a new **%QtLocalizer** using the @p locale
|
||||||
|
*/
|
||||||
|
QtLocalizer(const QLocale &locale = QLocale::system());
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
~QtLocalizer() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Set the path to look for translations of the application strings.
|
||||||
|
*/
|
||||||
|
void setAppTranslatorPath(const QString &path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Set the prefix of translation files. For example, if the German translation
|
||||||
|
file is called <tt>myapp_de_DE.qm</tt>, the prefix should be set to
|
||||||
|
<tt>myapp_</tt>.
|
||||||
|
*/
|
||||||
|
void setAppTranslatorPrefix(const QString &prefix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Install a @p translator to use for a particular @p localeName.
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto deTranslator = new QTranslator(this);
|
||||||
|
auto loaded = deTranslator->load("myapp_de_DE");
|
||||||
|
if (!loaded)
|
||||||
|
return;
|
||||||
|
|
||||||
|
de_localizer->installTranslator(deTranslator, "de_DE");
|
||||||
|
@endcode
|
||||||
|
*/
|
||||||
|
void installTranslator(QTranslator *translator,
|
||||||
|
const QString &localeName = QLocale::system().name());
|
||||||
|
|
||||||
|
QString currentLocale() const override;
|
||||||
|
void pushLocale(const QString &localeName) override;
|
||||||
|
void popLocale() override;
|
||||||
|
void loadCatalog(const QString &path, const QString &catalog) override;
|
||||||
|
void unloadCatalog(const QString &catalog) override;
|
||||||
|
|
||||||
|
QString localizeNumber(int number) const override;
|
||||||
|
QString localizeNumber(qreal number) const override;
|
||||||
|
QString localizeMonetaryValue(qreal value, const QString ¤cyCode
|
||||||
|
= {}) const override;
|
||||||
|
QString localizeDate(const QDate &date,
|
||||||
|
QLocale::FormatType formatType
|
||||||
|
= QLocale::ShortFormat) const override;
|
||||||
|
QString localizeTime(const QTime &time,
|
||||||
|
QLocale::FormatType formatType
|
||||||
|
= QLocale::ShortFormat) const override;
|
||||||
|
QString localizeDateTime(const QDateTime &dateTime,
|
||||||
|
QLocale::FormatType formatType
|
||||||
|
= QLocale::ShortFormat) const override;
|
||||||
|
QString localizeString(const QString &string,
|
||||||
|
const QVariantList &arguments = {}) const override;
|
||||||
|
QString localizeContextString(const QString &string, const QString &context,
|
||||||
|
const QVariantList &arguments
|
||||||
|
= {}) const override;
|
||||||
|
QString localizePluralContextString(const QString &string,
|
||||||
|
const QString &pluralForm,
|
||||||
|
const QString &context,
|
||||||
|
const QVariantList &arguments
|
||||||
|
= {}) const override;
|
||||||
|
QString localizePluralString(const QString &string, const QString &pluralForm,
|
||||||
|
const QVariantList &arguments
|
||||||
|
= {}) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(QtLocalizer)
|
||||||
|
Q_DISABLE_COPY(QtLocalizer)
|
||||||
|
QtLocalizerPrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_RENDERCONTEXT_H
|
||||||
|
#define GRANTLEE_RENDERCONTEXT_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QVariantHash>
|
||||||
|
|
||||||
|
/// @headerfile rendercontext.h grantlee/rendercontext.h
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class ContextPrivate;
|
||||||
|
class TemplateImpl;
|
||||||
|
class Node;
|
||||||
|
|
||||||
|
class RenderContextPrivate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Provides storage facility for state while rendering a template.
|
||||||
|
|
||||||
|
This class can not be instatiated by users of it. An instance
|
||||||
|
of **%RenderContext** is available through Context::renderContext() for use
|
||||||
|
with implementations of re-entrant template tags.
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT RenderContext
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Returns the data for the node @p scopeNode.
|
||||||
|
*/
|
||||||
|
QVariant &data(const Node *const scopeNode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether the **%RenderContext** contains data for @p scopeNode.
|
||||||
|
*/
|
||||||
|
bool contains(Node *const scopeNode) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
~RenderContext();
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
private:
|
||||||
|
RenderContext();
|
||||||
|
|
||||||
|
void push();
|
||||||
|
|
||||||
|
void pop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class ContextPrivate;
|
||||||
|
friend class TemplateImpl;
|
||||||
|
|
||||||
|
Q_DISABLE_COPY(RenderContext)
|
||||||
|
Q_DECLARE_PRIVATE(RenderContext)
|
||||||
|
RenderContextPrivate *const d_ptr;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,436 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_SAFESTRING_H
|
||||||
|
#define GRANTLEE_SAFESTRING_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QString>
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/// @headerfile safestring.h grantlee/safestring.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief A QString wrapper class for containing whether a string is safe or
|
||||||
|
needs to be escaped.
|
||||||
|
|
||||||
|
This allows lazy escaping of strings. Otherwise a string may be escaped
|
||||||
|
multiple times where it should only be escaped once.
|
||||||
|
|
||||||
|
The **%SafeString** class itself provides information about whether a string
|
||||||
|
is safe from further escaping through the @ref isSafe method. The actual
|
||||||
|
string content held by the **%SafeString** instance is available through the
|
||||||
|
@ref get method. The @ref get method returns a QString subclass which should
|
||||||
|
be used like any other QString. The difference is that all methods on
|
||||||
|
NestedString return a **%SafeString** instead of a QString.
|
||||||
|
|
||||||
|
@code
|
||||||
|
SafeString s("this & that", SafeString::IsSafe);
|
||||||
|
s.get().replace( "this", "these" ).toUpper();
|
||||||
|
|
||||||
|
qDebug() << s.get() << s.isSafe(); // outputs "these & that" false
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Note that most operations on strings make the string unsafe. For example,
|
||||||
|
while <tt>"K & R"</tt> is safe, using replace("m", "n") will result in
|
||||||
|
<tt>"K &anp; R"</tt>, which is unsafe. Likewise using upper() will return
|
||||||
|
<tt>"K & R"</tt>, which is unsafe. Because the **%SafeString** can not
|
||||||
|
determine whether a method call with particular arguments will change
|
||||||
|
a **%SafeString** from being safe to being unsafe, any operation which can
|
||||||
|
possibly make the string unsafe does cause the string to become unsafe. It is
|
||||||
|
then up to the caller to restore safe-ness if needed.
|
||||||
|
|
||||||
|
NestedString has overloads for SafeStrings whereever appropriate so that
|
||||||
|
strings remain marked as safe where possible.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
@code
|
||||||
|
SafeString s1("this & that", SafeString::IsSafe);
|
||||||
|
s2 = s1;
|
||||||
|
s1.append( QString( " & the other" ) );
|
||||||
|
// s1 is now "this & that & the other" and is unsafe.
|
||||||
|
|
||||||
|
SafeString s3(" Wobl & Bob", SafeString::IsSafe);
|
||||||
|
s2.append(s3);
|
||||||
|
// Both s2 and s3 are safe, and append is a safe operation, so s2
|
||||||
|
// is still safe
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@see @ref autoescaping
|
||||||
|
@see OutputStream::escape
|
||||||
|
|
||||||
|
The **%SafeString** class has appropriate operator overloads to make it
|
||||||
|
convenient to use in methods returning a QVariant, such as Filter::doFilter,
|
||||||
|
or as a QString. Note that a raw QString is essentially the same
|
||||||
|
as a **%SafeString** which is marked as unsafe.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT SafeString
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Possible safety states of a **%SafeString**
|
||||||
|
*/
|
||||||
|
enum Safety {
|
||||||
|
IsSafe, ///< The string is safe and requires no further escaping
|
||||||
|
IsNotSafe ///< The string is not safe. It will be escaped before being
|
||||||
|
/// added to the output of rendering.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Constructs an empty **%SafeString**.
|
||||||
|
*/
|
||||||
|
SafeString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Copy constructor
|
||||||
|
*/
|
||||||
|
SafeString(const SafeString &safeString);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Constructs a **%SafeString** with the content @p str whose safety is given
|
||||||
|
by @p safe.
|
||||||
|
*/
|
||||||
|
SafeString(const QString &str, bool safe);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Constructs a **%SafeString** with the content @p str whose safety is given
|
||||||
|
by @p safety.
|
||||||
|
*/
|
||||||
|
/* implicit */ SafeString(const QString &str,
|
||||||
|
Safety safety
|
||||||
|
= IsNotSafe); // krazy:exclude=explicit
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
~SafeString();
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
Set whether the string should be escaped.
|
||||||
|
*/
|
||||||
|
void setNeedsEscape(bool needsEscape);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
Whether the string needs to be escaped.
|
||||||
|
*/
|
||||||
|
bool needsEscape() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Whether the string is safe.
|
||||||
|
*/
|
||||||
|
bool isSafe() const;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
Set whether the string is safe.
|
||||||
|
*/
|
||||||
|
void setSafety(Safety safety);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The NestedString is a QString whose methods always return a
|
||||||
|
SafeString
|
||||||
|
|
||||||
|
This class is largely an implementation detail. See the SafeString
|
||||||
|
documentation for details.
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT NestedString : public QString
|
||||||
|
{
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
friend class SafeString;
|
||||||
|
SafeString *m_safeString;
|
||||||
|
|
||||||
|
public:
|
||||||
|
NestedString(SafeString *safeString);
|
||||||
|
NestedString(const QString &content, SafeString *safeString);
|
||||||
|
|
||||||
|
SafeString &append(const SafeString &str);
|
||||||
|
SafeString &append(const QString &str);
|
||||||
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
SafeString &append(const QStringRef &ref);
|
||||||
|
#endif
|
||||||
|
SafeString &append(const QLatin1String &str);
|
||||||
|
#ifndef QT_NO_CAST_FROM_ASCII
|
||||||
|
SafeString &append(const QByteArray &ba)
|
||||||
|
{
|
||||||
|
QString::append(ba);
|
||||||
|
m_safeString->m_safety = IsNotSafe;
|
||||||
|
return *m_safeString;
|
||||||
|
}
|
||||||
|
|
||||||
|
SafeString &append(const char *str)
|
||||||
|
{
|
||||||
|
QString::append(str);
|
||||||
|
m_safeString->m_safety = IsNotSafe;
|
||||||
|
return *m_safeString;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
SafeString &append(const QChar ch);
|
||||||
|
|
||||||
|
SafeString &fill(QChar ch, int size = -1);
|
||||||
|
|
||||||
|
SafeString &insert(int position, const SafeString &str);
|
||||||
|
SafeString &insert(int position, const QString &str);
|
||||||
|
SafeString &insert(int position, const QLatin1String &str);
|
||||||
|
SafeString &insert(int position, const QChar *unicode, int size);
|
||||||
|
SafeString &insert(int position, QChar ch);
|
||||||
|
|
||||||
|
SafeString left(int n) const;
|
||||||
|
SafeString leftJustified(int width, QChar fill = QLatin1Char(' '),
|
||||||
|
bool truncate = {}) const;
|
||||||
|
SafeString mid(int position, int n = -1) const;
|
||||||
|
|
||||||
|
SafeString normalized(NormalizationForm mode) const;
|
||||||
|
SafeString normalized(NormalizationForm mode,
|
||||||
|
QChar::UnicodeVersion version) const;
|
||||||
|
|
||||||
|
SafeString &prepend(const SafeString &str);
|
||||||
|
SafeString &prepend(const QString &str);
|
||||||
|
SafeString &prepend(const QLatin1String &str);
|
||||||
|
#ifndef QT_NO_CAST_FROM_ASCII
|
||||||
|
SafeString &prepend(const QByteArray &ba)
|
||||||
|
{
|
||||||
|
QString::prepend(ba);
|
||||||
|
m_safeString->m_safety = IsNotSafe;
|
||||||
|
return *m_safeString;
|
||||||
|
}
|
||||||
|
|
||||||
|
SafeString &prepend(const char *str)
|
||||||
|
{
|
||||||
|
QString::prepend(str);
|
||||||
|
m_safeString->m_safety = IsNotSafe;
|
||||||
|
return *m_safeString;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
SafeString &prepend(QChar ch);
|
||||||
|
|
||||||
|
void push_back(const SafeString &other);
|
||||||
|
void push_front(const SafeString &other);
|
||||||
|
|
||||||
|
SafeString &remove(int position, int n);
|
||||||
|
SafeString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &remove(const SafeString &str,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &remove(const QString &str,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &remove(const QRegularExpression &rx);
|
||||||
|
SafeString repeated(int times) const;
|
||||||
|
SafeString &replace(int position, int n, const SafeString &after);
|
||||||
|
SafeString &replace(int position, int n, const QString &after);
|
||||||
|
SafeString &replace(int position, int n, const QChar *unicode, int size);
|
||||||
|
SafeString &replace(int position, int n, QChar after);
|
||||||
|
SafeString &replace(const SafeString &before, const SafeString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QString &before, const SafeString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const SafeString &before, const QString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QString &before, const QString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QChar *before, int blen, const QChar *after,
|
||||||
|
int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(QChar ch, const SafeString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(QChar ch, const QString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(QChar before, QChar after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QLatin1String &before, const QLatin1String &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QLatin1String &before, const SafeString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QLatin1String &before, const QString &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const SafeString &before, const QLatin1String &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QString &before, const QLatin1String &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(QChar c, const QLatin1String &after,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||||
|
SafeString &replace(const QRegularExpression &rx, const SafeString &after);
|
||||||
|
SafeString &replace(const QRegularExpression &rx, const QString &after);
|
||||||
|
|
||||||
|
SafeString right(int n) const;
|
||||||
|
SafeString rightJustified(int width, QChar fill = QLatin1Char(' '),
|
||||||
|
bool truncate = {}) const;
|
||||||
|
|
||||||
|
SafeString section(QChar sep, int start, int end = -1,
|
||||||
|
SectionFlags flags = SectionDefault) const;
|
||||||
|
SafeString section(const SafeString &sep, int start, int end = -1,
|
||||||
|
SectionFlags flags = SectionDefault) const;
|
||||||
|
SafeString section(const QString &sep, int start, int end = -1,
|
||||||
|
SectionFlags flags = SectionDefault) const;
|
||||||
|
SafeString section(const QRegularExpression ®, int start, int end = -1,
|
||||||
|
SectionFlags flags = SectionDefault) const;
|
||||||
|
SafeString &setNum(int n, int base = 10);
|
||||||
|
SafeString &setNum(uint n, int base = 10);
|
||||||
|
SafeString &setNum(long n, int base = 10);
|
||||||
|
SafeString &setNum(ulong n, int base = 10);
|
||||||
|
SafeString &setNum(qlonglong n, int base = 10);
|
||||||
|
SafeString &setNum(qulonglong n, int base = 10);
|
||||||
|
SafeString &setNum(short n, int base = 10);
|
||||||
|
SafeString &setNum(ushort n, int base = 10);
|
||||||
|
SafeString &setNum(double n, char format = 'g', int precision = 6);
|
||||||
|
SafeString &setNum(float n, char format = 'g', int precision = 6);
|
||||||
|
SafeString &setUnicode(const QChar *unicode, int size);
|
||||||
|
SafeString &setUtf16(const ushort *unicode, int size);
|
||||||
|
SafeString simplified() const;
|
||||||
|
|
||||||
|
// QT_DEPRECATED_SINCE(5, 15) to support consumers building with
|
||||||
|
// -DQT_DISABLE_DEPRECATED_BEFORE=0x050f00
|
||||||
|
// where QString enums will be invisible to the compiler
|
||||||
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_DEPRECATED_SINCE(5, 15)
|
||||||
|
// Calls with default argument for "behavior" should pick the Qt >= 5.14
|
||||||
|
// version, if available
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
||||||
|
QStringList split(const SafeString &sep, QString::SplitBehavior behavior,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QString &sep, QString::SplitBehavior behavior,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QChar &sep, QString::SplitBehavior behavior,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QRegularExpression &rx,
|
||||||
|
QString::SplitBehavior behavior) const;
|
||||||
|
#else
|
||||||
|
QStringList split(const SafeString &sep,
|
||||||
|
QString::SplitBehavior behavior = QString::KeepEmptyParts,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QString &sep,
|
||||||
|
QString::SplitBehavior behavior = QString::KeepEmptyParts,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QChar &sep,
|
||||||
|
QString::SplitBehavior behavior = QString::KeepEmptyParts,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QRegularExpression &rx,
|
||||||
|
QString::SplitBehavior behavior
|
||||||
|
= QString::KeepEmptyParts) const;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
||||||
|
QStringList split(const SafeString &sep,
|
||||||
|
Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QString &sep,
|
||||||
|
Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QChar &sep,
|
||||||
|
Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
|
||||||
|
Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||||
|
QStringList split(const QRegularExpression &rx,
|
||||||
|
Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SafeString toLower() const;
|
||||||
|
SafeString toUpper() const;
|
||||||
|
SafeString trimmed() const;
|
||||||
|
|
||||||
|
void chop(int n);
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the String held by this **%SafeString**
|
||||||
|
*/
|
||||||
|
const NestedString &get() const { return m_nestedString; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the String held by this **%SafeString**
|
||||||
|
*/
|
||||||
|
NestedString &get() { return m_nestedString; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convenience operator for treating a **%SafeString** like a QString.
|
||||||
|
*/
|
||||||
|
operator QString() const { return m_nestedString; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
Assignment operator.
|
||||||
|
*/
|
||||||
|
SafeString &operator=(const SafeString &str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a concatenation of this with @p str.
|
||||||
|
|
||||||
|
The result is not safe because str is not safe.
|
||||||
|
*/
|
||||||
|
SafeString operator+(const QString &str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a concatenation of this with @p str.
|
||||||
|
|
||||||
|
The result is safe if both this and str are safe.
|
||||||
|
*/
|
||||||
|
SafeString operator+(const SafeString &str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Appends the content of @p str to this.
|
||||||
|
|
||||||
|
The result is not safe because @p str is not safe.
|
||||||
|
*/
|
||||||
|
SafeString &operator+=(const QString &str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Appends the content of @p str to this.
|
||||||
|
|
||||||
|
The result is safe if both this and @p str are safe.
|
||||||
|
*/
|
||||||
|
SafeString &operator+=(const SafeString &str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns true if the content of @p other matches the content of this.
|
||||||
|
|
||||||
|
Safeness and needing escaping are not accounted for in the comparison.
|
||||||
|
*/
|
||||||
|
bool operator==(const SafeString &other) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns true if the content of @p other matches the content of this.
|
||||||
|
|
||||||
|
Safeness and needing escaping are not accounted for in the comparison.
|
||||||
|
*/
|
||||||
|
bool operator==(const QString &other) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convenience operator for storing a **%SafeString** in a QVariant.
|
||||||
|
*/
|
||||||
|
operator QVariant() const { return QVariant::fromValue(*this); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
NestedString m_nestedString;
|
||||||
|
#endif
|
||||||
|
Safety m_safety;
|
||||||
|
bool m_needsescape;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(Grantlee::SafeString)
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TAGLIBRARYINTERFACE_H
|
||||||
|
#define TAGLIBRARYINTERFACE_H
|
||||||
|
|
||||||
|
#include "outputstream.h"
|
||||||
|
|
||||||
|
#include <QtCore/QHash>
|
||||||
|
#include <QtCore/QObject>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
class AbstractNodeFactory;
|
||||||
|
class Engine;
|
||||||
|
class Filter;
|
||||||
|
|
||||||
|
/// @headerfile taglibraryinterface.h grantlee/taglibraryinterface.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The **%TagLibraryInterface** returns available tags and filters from
|
||||||
|
libraries.
|
||||||
|
|
||||||
|
This interface must be implemented in tag and filter libraries.
|
||||||
|
|
||||||
|
The implementation will usually be very simple.
|
||||||
|
|
||||||
|
@code
|
||||||
|
class MyTagLibrary : public QObject, public TagLibraryInterface
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_INTERFACES( Grantlee::TagLibraryInterface )
|
||||||
|
public:
|
||||||
|
MyTagLibrary( QObject *parent = {} )
|
||||||
|
: QObject( parent ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<QString, AbstractNodeFactory*>
|
||||||
|
nodeFactories(const QString &name = {}) {
|
||||||
|
Q_UNUSED( name );
|
||||||
|
QHash<QString, AbstractNodeFactory*> nodeFactories;
|
||||||
|
nodeFactories.insert( "mytag1", new MyTag1() );
|
||||||
|
nodeFactories.insert( "mytag2", new MyTag2() );
|
||||||
|
return nodeFactories;
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<QString, Filter*> filters( const QString &name = {} ) {
|
||||||
|
Q_UNUSED( name );
|
||||||
|
|
||||||
|
QHash<QString, Filter*> filters;
|
||||||
|
|
||||||
|
filters.insert( "myfilter1", new MyFilter1() );
|
||||||
|
filters.insert( "myfilter2", new MyFilter2() );
|
||||||
|
|
||||||
|
return filters;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class TagLibraryInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~TagLibraryInterface() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the AbstractNodeFactory implementations available in this library.
|
||||||
|
*/
|
||||||
|
virtual QHash<QString, AbstractNodeFactory *>
|
||||||
|
nodeFactories(const QString &name = {})
|
||||||
|
{
|
||||||
|
Q_UNUSED(name);
|
||||||
|
static const QHash<QString, AbstractNodeFactory *> h;
|
||||||
|
return h;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the Filter implementations available in this library.
|
||||||
|
*/
|
||||||
|
virtual QHash<QString, Filter *> filters(const QString &name = {})
|
||||||
|
{
|
||||||
|
Q_UNUSED(name);
|
||||||
|
static const QHash<QString, Filter *> h;
|
||||||
|
return h;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_DECLARE_INTERFACE(Grantlee::TagLibraryInterface,
|
||||||
|
"org.grantlee.TagLibraryInterface/1.0")
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEMPLATE_H
|
||||||
|
#define GRANTLEE_TEMPLATE_H
|
||||||
|
|
||||||
|
#include "exception.h"
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
#include "node.h"
|
||||||
|
|
||||||
|
#include <QtCore/QSharedPointer>
|
||||||
|
#include <QtCore/QStringList>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
class Context;
|
||||||
|
class Engine;
|
||||||
|
class TemplateImpl;
|
||||||
|
class OutputStream;
|
||||||
|
|
||||||
|
#ifdef Q_QDOC
|
||||||
|
#define TemplateImpl Template
|
||||||
|
#else
|
||||||
|
typedef QSharedPointer<TemplateImpl> Template;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class TemplatePrivate;
|
||||||
|
|
||||||
|
/// @headerfile template.h grantlee/template.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The **%Template** class is a tree of nodes which may be rendered.
|
||||||
|
|
||||||
|
All Templates are created through the Grantlee::Engine class.
|
||||||
|
A **%Template** is created by parsing some text markup passed into the Engine,
|
||||||
|
or by reading it from a file.
|
||||||
|
|
||||||
|
Note that **%Template** is actually a typedef for a
|
||||||
|
<tt>QSharedPointer<TemplateImpl></tt>, so all of its members should be
|
||||||
|
accessed with <tt>operator->()</tt>.
|
||||||
|
|
||||||
|
The result of parsing is a **%Template** object which can be rendered multiple
|
||||||
|
times with multiple different Contexts.
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto engine = getEngine();
|
||||||
|
auto t = engine->newTemplate(
|
||||||
|
"{{ name }} is aged {{ age }}",
|
||||||
|
"simple template" );
|
||||||
|
if ( t->error() )
|
||||||
|
{
|
||||||
|
// Tokenizing or parsing error, or couldn't find custom tags or filters.
|
||||||
|
qDebug() << t->errorString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QTextStream textStream( stdout );
|
||||||
|
OutputStream stream( textStream );
|
||||||
|
|
||||||
|
for ( ... )
|
||||||
|
{
|
||||||
|
Context c;
|
||||||
|
// ... c.insert
|
||||||
|
t->render( stream, c );
|
||||||
|
|
||||||
|
if (t->error())
|
||||||
|
{
|
||||||
|
// Rendering error.
|
||||||
|
qDebug() << t->errorString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
If there is an error in parsing or rendering, the @ref error and @ref
|
||||||
|
errorString methods can be used to check the source of the error.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT TemplateImpl : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
~TemplateImpl() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Renders the **%Template** to a string given the Context @p c.
|
||||||
|
*/
|
||||||
|
QString render(Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Renders the **%Template** to the OutputStream @p stream given the Context c.
|
||||||
|
*/
|
||||||
|
OutputStream *render(OutputStream *stream, Context *c) const;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
NodeList nodeList() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
*/
|
||||||
|
void setNodeList(const NodeList &list);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns an error code for the error encountered.
|
||||||
|
*/
|
||||||
|
Error error() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns more information to developers in the form of a string.
|
||||||
|
*/
|
||||||
|
QString errorString() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the Engine that created this **%Template**.
|
||||||
|
*/
|
||||||
|
Engine const *engine() const;
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
protected:
|
||||||
|
TemplateImpl(Engine const *engine, QObject *parent = {});
|
||||||
|
TemplateImpl(Engine const *engine, bool smartTrim, QObject *parent = {});
|
||||||
|
|
||||||
|
void setContent(const QString &templateString);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Don't allow setting the parent on a Template, which is memory managed as
|
||||||
|
// a QSharedPointer.
|
||||||
|
using QObject::setParent;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(Template)
|
||||||
|
TemplatePrivate *const d_ptr;
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
friend class Engine;
|
||||||
|
friend class Parser;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,229 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEMPLATELOADER_H
|
||||||
|
#define GRANTLEE_TEMPLATELOADER_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
#include "template.h"
|
||||||
|
|
||||||
|
#include <QtCore/QSharedPointer>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class AbstractLocalizer;
|
||||||
|
|
||||||
|
/// @headerfile templateloader.h grantlee/templateloader.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief An retrieval interface to a storage location for Template objects.
|
||||||
|
|
||||||
|
This interface can be implemented to define new ways of retrieving the content
|
||||||
|
of Templates.
|
||||||
|
|
||||||
|
The interface of this class should not be called directly from applications.
|
||||||
|
TemplateLoaders will typically be created, configured and added to the
|
||||||
|
Grantlee::Engine which will call the appropriate API.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT AbstractTemplateLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
virtual ~AbstractTemplateLoader();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Load a Template called @p name. Return an invalid Template if no content
|
||||||
|
by that name exists.
|
||||||
|
*/
|
||||||
|
virtual Template loadByName(const QString &name,
|
||||||
|
Engine const *engine) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Return a complete URI for media identified by fileName.
|
||||||
|
*/
|
||||||
|
virtual QPair<QString, QString>
|
||||||
|
getMediaUri(const QString &fileName) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Return true if a Template identified by @p name exists and can be loaded.
|
||||||
|
*/
|
||||||
|
virtual bool canLoadTemplate(const QString &name) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @headerfile templateloader.h grantlee/templateloader.h
|
||||||
|
|
||||||
|
class FileSystemTemplateLoaderPrivate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The **%FileSystemTemplateLoader** loads Templates from the file system.
|
||||||
|
|
||||||
|
This template loader works by traversing a list of directories to find
|
||||||
|
templates. Directories are checked in order, and the first match hit is parsed
|
||||||
|
and returned.
|
||||||
|
|
||||||
|
@code
|
||||||
|
loader->setTemplateDirs({
|
||||||
|
"/home/user/app/templates",
|
||||||
|
"/usr/local/share/app/templates"
|
||||||
|
});
|
||||||
|
engine->setTemplateLoader( loader );
|
||||||
|
|
||||||
|
// This will try /home/user/app/templates/mytemplate.html
|
||||||
|
// followed by /usr/local/share/app/templates/mytemplate.html
|
||||||
|
engine->loadByName( "mytemplate.html" );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Additionally, a themeName may be set on the template loader, which will be
|
||||||
|
appended to search paths before the template name.
|
||||||
|
|
||||||
|
@code
|
||||||
|
loader->setTemplateDirs({
|
||||||
|
"/home/user/app/templates" <<
|
||||||
|
"/usr/local/share/app/templates"
|
||||||
|
});
|
||||||
|
loader->setTheme( "simple_theme" );
|
||||||
|
engine->setTemplateLoader( loader );
|
||||||
|
|
||||||
|
// This will try /home/user/app/templates/simple_theme/mytemplate.html
|
||||||
|
// followed by /usr/local/share/app/templates/simple_theme/mytemplate.html
|
||||||
|
engine->loadByName( "mytemplate.html" );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Media URIs may be retrieved for media relative to the directories searched
|
||||||
|
queried for templates.
|
||||||
|
|
||||||
|
@code
|
||||||
|
loader->setTemplateDirs({
|
||||||
|
"/home/user/app/templates",
|
||||||
|
"/usr/local/share/app/templates"
|
||||||
|
});
|
||||||
|
loader->setTheme( "simple_theme" );
|
||||||
|
engine->setTemplateLoader( loader );
|
||||||
|
|
||||||
|
// This will try /home/user/app/templates/simple_theme/logo.png
|
||||||
|
// followed by /usr/local/share/app/templates/simple_theme/logo.png
|
||||||
|
// and return the first one that exists.
|
||||||
|
engine->mediaUri( "logo.png" );
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The template files loaded by a %**FileSystemTemplateLoader** must be UTF-8
|
||||||
|
encoded.
|
||||||
|
|
||||||
|
@see @ref deploying_templates
|
||||||
|
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT FileSystemTemplateLoader
|
||||||
|
: public AbstractTemplateLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructor
|
||||||
|
*/
|
||||||
|
FileSystemTemplateLoader(const QSharedPointer<AbstractLocalizer> localizer
|
||||||
|
= {});
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
~FileSystemTemplateLoader() override;
|
||||||
|
|
||||||
|
Template loadByName(const QString &name, Engine const *engine) const override;
|
||||||
|
|
||||||
|
bool canLoadTemplate(const QString &name) const override;
|
||||||
|
|
||||||
|
QPair<QString, QString> getMediaUri(const QString &fileName) const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets the theme of this loader to @p themeName
|
||||||
|
*/
|
||||||
|
void setTheme(const QString &themeName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
The themeName of this TemplateLoader
|
||||||
|
*/
|
||||||
|
QString themeName() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets the directories to look for template files to @p dirs.
|
||||||
|
*/
|
||||||
|
void setTemplateDirs(const QStringList &dirs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
The directories this TemplateLoader looks in for template files.
|
||||||
|
*/
|
||||||
|
QStringList templateDirs() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(FileSystemTemplateLoader)
|
||||||
|
FileSystemTemplateLoaderPrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @headerfile templateloader.h grantlee/templateloader.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The **%InMemoryTemplateLoader** loads Templates set dynamically in
|
||||||
|
memory
|
||||||
|
|
||||||
|
This class is mostly used for testing purposes, but can also be used for
|
||||||
|
simple uses of %Grantlee.
|
||||||
|
|
||||||
|
Templates can be made available using the @ref setTemplate method, and will
|
||||||
|
then be retrieved by the Grantlee::Engine as appropriate.
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT InMemoryTemplateLoader
|
||||||
|
: public AbstractTemplateLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
InMemoryTemplateLoader();
|
||||||
|
~InMemoryTemplateLoader() override;
|
||||||
|
|
||||||
|
Template loadByName(const QString &name, Engine const *engine) const override;
|
||||||
|
|
||||||
|
bool canLoadTemplate(const QString &name) const override;
|
||||||
|
|
||||||
|
QPair<QString, QString> getMediaUri(const QString &fileName) const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Add a template content to this Loader.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto loader = QSharedPointer<InMemoryTemplateLoader::create();
|
||||||
|
loader->setTemplate( "name_template", "My name is {{ name }}" );
|
||||||
|
loader->setTemplate( "age_template", "My age is {{ age }}" );
|
||||||
|
engine->addTemplateLoader( loader );
|
||||||
|
|
||||||
|
// Both templates may now be retrieved by calling Engine::loadByName.
|
||||||
|
@endcode
|
||||||
|
*/
|
||||||
|
void setTemplate(const QString &name, const QString &content);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QHash<QString, QString> m_namedTemplates;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TEXTHTMLBUILDER_H
|
||||||
|
#define GRANTLEE_TEXTHTMLBUILDER_H
|
||||||
|
|
||||||
|
#ifdef Q_CC_MSVC
|
||||||
|
#pragma warning(disable : 4250)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "abstractmarkupbuilder.h"
|
||||||
|
#include "grantlee_textdocument_export.h"
|
||||||
|
#include "markupdirector.h"
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
class TextHTMLBuilderPrivate;
|
||||||
|
|
||||||
|
/// @headerfile texthtmlbuilder.h grantlee/texthtmlbuilder.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The TextHTMLBuilder creates a clean html markup output.
|
||||||
|
|
||||||
|
This class creates html output which is as minimal as possible and restricted
|
||||||
|
to the rich text features supported in %Qt.
|
||||||
|
(https://doc.qt.io/qt-5/richtext-html-subset.html)
|
||||||
|
|
||||||
|
The output contains only the body content, not the head element or other
|
||||||
|
metadata.
|
||||||
|
|
||||||
|
eg:
|
||||||
|
|
||||||
|
@code
|
||||||
|
<p>
|
||||||
|
This is some <strong>formatted content</strong> in a paragraph.
|
||||||
|
</p>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
instead of the content produced by %Qt:
|
||||||
|
|
||||||
|
@code
|
||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
|
||||||
|
"http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||||
|
<html><head><meta name="qrichtext" content="1" /><meta
|
||||||
|
http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style
|
||||||
|
type="text/css">
|
||||||
|
p, li { white-space: pre-wrap; }
|
||||||
|
</style></head><body style=" font-family:'Sans Serif'; font-size:10pt;
|
||||||
|
font-weight:400; font-style:normal;">
|
||||||
|
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px;
|
||||||
|
margin-right:0px; -qt-block-indent:0; text-indent:0px;">This is some <span
|
||||||
|
style=" font-weight:600;">formatted content</span> in a paragraph.
|
||||||
|
</p></body></html>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Such tags should be created separately. For example:
|
||||||
|
|
||||||
|
@code
|
||||||
|
auto b = new TextHTMLBuilder();
|
||||||
|
auto md = new MarkupDirector(b);
|
||||||
|
md->constructContent();
|
||||||
|
QString cleanHtml(
|
||||||
|
"<head>\n<title>%1</title>\n</head>\n<body>%2</body>\n</html>")
|
||||||
|
.arg(document.metaInformation(QTextDocument::DocumentTitle))
|
||||||
|
.arg(b->getOutput());
|
||||||
|
file.write(cleanHtml);
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Font formatting information on elements is represented by individual span
|
||||||
|
elements.
|
||||||
|
|
||||||
|
eg:
|
||||||
|
@code
|
||||||
|
<span style"color:blue;">
|
||||||
|
<span style="background-color:red;">
|
||||||
|
Blue text on red background
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
instead of
|
||||||
|
|
||||||
|
@code
|
||||||
|
<span style="color:blue;background-color:red;">
|
||||||
|
Blue text on red background
|
||||||
|
</span>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
It my be possible to change this if necessary.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEXTDOCUMENT_EXPORT TextHTMLBuilder
|
||||||
|
: virtual public AbstractMarkupBuilder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TextHTMLBuilder();
|
||||||
|
~TextHTMLBuilder() override;
|
||||||
|
|
||||||
|
void beginStrong() override;
|
||||||
|
void endStrong() override;
|
||||||
|
void beginEmph() override;
|
||||||
|
void endEmph() override;
|
||||||
|
void beginUnderline() override;
|
||||||
|
void endUnderline() override;
|
||||||
|
void beginStrikeout() override;
|
||||||
|
void endStrikeout() override;
|
||||||
|
void beginForeground(const QBrush &brush) override;
|
||||||
|
void endForeground() override;
|
||||||
|
void beginBackground(const QBrush &brush) override;
|
||||||
|
void endBackground() override;
|
||||||
|
void beginAnchor(const QString &href = {}, const QString &name = {}) override;
|
||||||
|
void endAnchor() override;
|
||||||
|
|
||||||
|
// Maybe this stuff should just be added to a list, and then when I add
|
||||||
|
// literal text,
|
||||||
|
// add some kind of style attribute in one span instead of many.
|
||||||
|
void beginFontFamily(const QString &family) override;
|
||||||
|
void endFontFamily() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new font point size
|
||||||
|
@param size The new size to begin.
|
||||||
|
*/
|
||||||
|
void beginFontPointSize(int size) override;
|
||||||
|
void endFontPointSize() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new paragraph
|
||||||
|
@param al The new paragraph alignment
|
||||||
|
@param topMargin The new paragraph topMargin
|
||||||
|
@param bottomMargin The new paragraph bottomMargin
|
||||||
|
@param leftMargin The new paragraph leftMargin
|
||||||
|
@param rightMargin The new paragraph rightMargin
|
||||||
|
*/
|
||||||
|
void beginParagraph(Qt::Alignment al = Qt::AlignLeft, qreal topMargin = 0.0,
|
||||||
|
qreal bottomMargin = 0.0, qreal leftMargin = 0.0,
|
||||||
|
qreal rightMargin = 0.0) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Begin a new header element.
|
||||||
|
@param level The new level to begin.
|
||||||
|
*/
|
||||||
|
void beginHeader(int level) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
End a header element.
|
||||||
|
@param level The new level to end.
|
||||||
|
*/
|
||||||
|
void endHeader(int level) override;
|
||||||
|
|
||||||
|
void endParagraph() override;
|
||||||
|
void addNewline() override;
|
||||||
|
|
||||||
|
void insertHorizontalRule(int width = -1) override;
|
||||||
|
|
||||||
|
void insertImage(const QString &src, qreal width, qreal height) override;
|
||||||
|
|
||||||
|
void beginList(QTextListFormat::Style type) override;
|
||||||
|
|
||||||
|
void endList() override;
|
||||||
|
|
||||||
|
void beginListItem() override;
|
||||||
|
void endListItem() override;
|
||||||
|
|
||||||
|
void beginSuperscript() override;
|
||||||
|
|
||||||
|
void endSuperscript() override;
|
||||||
|
|
||||||
|
void beginSubscript() override;
|
||||||
|
|
||||||
|
void endSubscript() override;
|
||||||
|
|
||||||
|
void beginTable(qreal cellpadding, qreal cellspacing,
|
||||||
|
const QString &width) override;
|
||||||
|
|
||||||
|
void beginTableRow() override;
|
||||||
|
void beginTableHeaderCell(const QString &width, int colspan,
|
||||||
|
int rowspan) override;
|
||||||
|
|
||||||
|
void beginTableCell(const QString &width, int colspan, int rowspan) override;
|
||||||
|
|
||||||
|
void endTable() override;
|
||||||
|
void endTableRow() override;
|
||||||
|
void endTableHeaderCell() override;
|
||||||
|
void endTableCell() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reimplemented from AbstractMarkupBuilder.
|
||||||
|
|
||||||
|
This implementation escapes the text before appending so that
|
||||||
|
|
||||||
|
@verbatim
|
||||||
|
A sample <b>bold</b> word.
|
||||||
|
@endverbatim
|
||||||
|
|
||||||
|
becomes
|
||||||
|
|
||||||
|
@verbatim
|
||||||
|
A sample <b>bold</b> word.
|
||||||
|
@endverbatim
|
||||||
|
*/
|
||||||
|
void appendLiteralText(const QString &text) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Append @p text without escaping.
|
||||||
|
|
||||||
|
This is useful if extending MarkupDirector
|
||||||
|
*/
|
||||||
|
void appendRawText(const QString &text) override;
|
||||||
|
|
||||||
|
QString getResult() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
TextHTMLBuilderPrivate *d_ptr;
|
||||||
|
Q_DECLARE_PRIVATE(TextHTMLBuilder)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TOKEN_H
|
||||||
|
#define GRANTLEE_TOKEN_H
|
||||||
|
|
||||||
|
#include <QtCore/QString>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
|
||||||
|
The available token types.
|
||||||
|
*/
|
||||||
|
enum TokenType {
|
||||||
|
TextToken, ///< The Token is a text fragment
|
||||||
|
VariableToken, ///< The Token is a variable node
|
||||||
|
BlockToken, ///< The Token is a block, ie, part of a tag
|
||||||
|
CommentToken ///< The Token is a comment node.
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @headerfile token.h grantlee/token.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
A token in a parse stream for a template.
|
||||||
|
|
||||||
|
This class is only relevant for template tag implementations.
|
||||||
|
*/
|
||||||
|
struct Token {
|
||||||
|
int tokenType; ///< The Type of this Token
|
||||||
|
int linenumber; ///< The line number this Token starts at
|
||||||
|
QString content; ///< The content of this Token
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_DECLARE_TYPEINFO(Grantlee::Token, Q_MOVABLE_TYPE);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_TYPEACCESSOR_H
|
||||||
|
#define GRANTLEE_TYPEACCESSOR_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QObject>
|
||||||
|
|
||||||
|
/// @file
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
template <typename T> struct TypeAccessor {
|
||||||
|
static QVariant lookUp(const T object, const QString &property);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T> struct TypeAccessor<T *> {
|
||||||
|
static QVariant lookUp(const T *const object, const QString &property);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T> struct TypeAccessor<T &> {
|
||||||
|
static QVariant lookUp(const T &object, const QString &property);
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_UTIL_P_H
|
||||||
|
#define GRANTLEE_UTIL_P_H
|
||||||
|
|
||||||
|
#include "context.h"
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
#include "safestring.h"
|
||||||
|
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
|
/// @file util.h Utility functions used throughout %Grantlee.
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether the @p variant is evaluated to true.
|
||||||
|
|
||||||
|
@see @ref truthiness
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT bool variantIsTrue(const QVariant &variant);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts the @p input into its unescaped form.
|
||||||
|
|
||||||
|
Double slashes are converted to single slashes, \\" is converted to "
|
||||||
|
etc.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT QString unescapeStringLiteral(const QString &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Marks the @p input as safe.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT Grantlee::SafeString
|
||||||
|
markSafe(const Grantlee::SafeString &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Marks the @p input as requiring escaping.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT Grantlee::SafeString
|
||||||
|
markForEscaping(const Grantlee::SafeString &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Retrieves and returns a SafeString from the @p input.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT Grantlee::SafeString
|
||||||
|
getSafeString(const QVariant &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether @p input contains a SafeString.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT bool isSafeString(const QVariant &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns true if the type of @p input can be inserted into a rendered template
|
||||||
|
directly.
|
||||||
|
|
||||||
|
Note that lists, hashes and QObject*s can not be.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT bool supportedOutputType(const QVariant &input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Compares @p lhs and @p rhs for equality. SafeStrings are compared as raw
|
||||||
|
QStrings. Their safeness is not part of the comparison.
|
||||||
|
|
||||||
|
@see QVariant::operator==
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT bool equals(const QVariant &lhs, const QVariant &rhs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts @p size into the nearest file size unit like MB or MiB, based on the
|
||||||
|
@p unitSystem value. Use @c 2 for the @p unitSystem to get binary units, use
|
||||||
|
@c 10 to get decimal units - by default, decimal units will be returned. The
|
||||||
|
@p multiplier can be used if the input @p size is not in pure bytes. If @p
|
||||||
|
size is for example given in @a KiB, use a multiplier of @a 1024. The returned
|
||||||
|
pair will have the converted size as @a first and the unit as @a second.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT std::pair<qreal, QString>
|
||||||
|
calcFileSize(qreal size, int unitSystem = 10, qreal multiplier = 1.0);
|
||||||
|
|
||||||
|
#ifndef Q_QDOC
|
||||||
|
/**
|
||||||
|
@internal
|
||||||
|
Returns the @p list as a formatted string. This is for debugging purposes
|
||||||
|
only.
|
||||||
|
*/
|
||||||
|
GRANTLEE_TEMPLATES_EXPORT Grantlee::SafeString
|
||||||
|
toString(const QVariantList &list);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
This file is part of the Grantlee template system.
|
||||||
|
|
||||||
|
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either version
|
||||||
|
2.1 of the Licence, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRANTLEE_VARIABLE_H
|
||||||
|
#define GRANTLEE_VARIABLE_H
|
||||||
|
|
||||||
|
#include "grantlee_templates_export.h"
|
||||||
|
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
|
namespace Grantlee
|
||||||
|
{
|
||||||
|
class Context;
|
||||||
|
|
||||||
|
class VariablePrivate;
|
||||||
|
|
||||||
|
/// @headerfile variable.h grantlee/variable.h
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief A container for static variables defined in Templates.
|
||||||
|
|
||||||
|
This class is only relevant to Template tag authors.
|
||||||
|
|
||||||
|
When processing a template tag in a AbstractNodeFactory implementation, it
|
||||||
|
will sometimes make sense to process arguments to the tag as
|
||||||
|
Grantlee::Variables. Note that usually they should be processed as
|
||||||
|
FilterExpression objects instead.
|
||||||
|
|
||||||
|
Arguments to the tag can be used to construct Variables, which may then be
|
||||||
|
resolved into the objects they represent in the given Context in the render
|
||||||
|
stage.
|
||||||
|
|
||||||
|
@author Stephen Kelly <steveire@gmail.com>
|
||||||
|
*/
|
||||||
|
class GRANTLEE_TEMPLATES_EXPORT Variable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
Constructs an invalid **%Variable**
|
||||||
|
*/
|
||||||
|
Variable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates a **%Variable** represented by the given @p var
|
||||||
|
*/
|
||||||
|
explicit Variable(const QString &var);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Copy constructor
|
||||||
|
*/
|
||||||
|
Variable(const Variable &other);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Destructor
|
||||||
|
*/
|
||||||
|
~Variable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Assignment operator.
|
||||||
|
*/
|
||||||
|
Variable &operator=(const Variable &other);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether this **%Variable** is valid.
|
||||||
|
*/
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether this **%Variable** evaluates to true with the Context @p c.
|
||||||
|
*/
|
||||||
|
bool isTrue(Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Resolves this **%Variable** with the Context @p c.
|
||||||
|
*/
|
||||||
|
QVariant resolve(Context *c) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether this **%Variable** is a constant in the Template. A constant
|
||||||
|
is represented as a static string in the template
|
||||||
|
|
||||||
|
@code
|
||||||
|
Text content
|
||||||
|
{% some_tag "constant" variable %}
|
||||||
|
@endcode
|
||||||
|
*/
|
||||||
|
bool isConstant() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether this variable is localized, that is, if it is wrapped with
|
||||||
|
_(). @see @ref i18n_l10n
|
||||||
|
*/
|
||||||
|
bool isLocalized() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns whether this variable is a literal string or number. A
|
||||||
|
literal **%Variable** does not have any lookup components.
|
||||||
|
*/
|
||||||
|
QVariant literal() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the lookup components of this **%Variable**.
|
||||||
|
*/
|
||||||
|
QStringList lookups() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DECLARE_PRIVATE(Variable)
|
||||||
|
VariablePrivate *const d_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user