Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling partials from standalone < tags. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/mustache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QRegExp>
#include <QtCore/QTextStream>

using namespace Mustache;
Expand Down Expand Up @@ -208,6 +209,8 @@ QString PartialFileLoader::getPartial(const QString& name)
return m_cache.value(name);
}

const QRegExp Renderer::LINE_SEP("(\n|^)(.)");

Renderer::Renderer()
: m_errorPos(-1)
, m_defaultTagStartMarker("{{")
Expand Down Expand Up @@ -323,6 +326,7 @@ QString Renderer::render(const QString& _template, int startPos, int endPos, Con
m_partialStack.push(tag.key);

QString partial = context->partialValue(tag.key);
partial.replace(LINE_SEP, "\\1" + m_indent + "\\2");
output += render(partial, 0, partial.length(), context);
lastTagEnd = tag.end;

Expand Down Expand Up @@ -418,7 +422,11 @@ Tag Renderer::findTag(const QString& content, int pos, int endPos)
tag.key = readTagName(content, pos, endPos);
}

if (tag.type != Tag::Value) {
if (tag.type == Tag::Partial) {
int oldStart = tag.start;
expandTag(tag, content);
m_indent = QString(oldStart - tag.start, QLatin1Char(' '));
} else if (tag.type != Tag::Value) {
expandTag(tag, content);
}

Expand Down
3 changes: 3 additions & 0 deletions src/mustache.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class Renderer
static void expandTag(Tag& tag, const QString& content);

QStack<QString> m_partialStack;
QString m_indent;
QString m_error;
int m_errorPos;
QString m_errorPartial;
Expand All @@ -266,6 +267,8 @@ class Renderer

QString m_defaultTagStartMarker;
QString m_defaultTagEndMarker;

static const QRegExp LINE_SEP;
};

/** A convenience function which renders a template using the given data. */
Expand Down
3 changes: 0 additions & 3 deletions tests/test_mustache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,6 @@ void TestMustache::testConformance()
QEXPECT_FAIL("interpolation.json - Dotted Names - Arbitrary Depth", "TODO", Abort);
QEXPECT_FAIL("interpolation.json - Dotted Names - Initial Resolution", "TODO", Abort);
QEXPECT_FAIL("inverted.json - Dotted Names - Truthy", "TODO", Abort);
QEXPECT_FAIL("partials.json - Standalone Without Previous Line", "TODO", Abort);
QEXPECT_FAIL("partials.json - Standalone Without Newline", "TODO", Abort);
QEXPECT_FAIL("partials.json - Standalone Indentation", "TODO", Abort);
QEXPECT_FAIL("sections.json - Implicit Iterator - String", "TODO", Abort);
QEXPECT_FAIL("sections.json - Implicit Iterator - Integer", "TODO", Abort);
QEXPECT_FAIL("sections.json - Implicit Iterator - Decimal", "TODO", Abort);
Expand Down