Skip to content

Commit

Permalink
fix(HandlebarsGenerator): escape reserved chars
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 22, 2024
1 parent ece7e7d commit a89e82f
Show file tree
Hide file tree
Showing 200 changed files with 1,828 additions and 1,746 deletions.
2 changes: 1 addition & 1 deletion share/mrdocs/addons/generator/adoc/layouts/index.adoc.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! The section with a symbol in single page output or the symbol page in multi page output }}
{{#if relfileprefix}}:relfileprefix: {{relfileprefix}}{{/if}}

[#{{sectionref}}]
[#{{{sectionref}}}]
{{> symbol symbol=symbol}}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
:mrdocs:
{{/unless}}
{{! Content generated with index.hbs }}
{{contents}}
{{{contents}}}

[.small]#Created with https://www.mrdocs.com[MrDocs]#
Original file line number Diff line number Diff line change
@@ -1 +1 @@
xref:{{href}}[{{> @partial-block }}]
xref:{{{href}}}[{{> @partial-block }}]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- alias --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- concept --}}
= {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

== Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- enum --}}
={{#unless @root.config.multipage}}={{/unless}} {{#if symbol.name}}{{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}{{else}}Unnamed enum{{/if}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand All @@ -18,7 +18,7 @@
|===
|Name |Description
{{#each symbol.members}}
|xref:{{ref}}[`pass:v[{{>types/declarator-id . nolink=true}}]`] | {{~doc.brief}}
|xref:{{{ref}}}[`pass:v[{{>types/declarator-id . nolink=true}}]`] | {{~doc.brief}}
{{/each}}
|===
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- enumerator --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- field --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- friend --}}
={{#unless @root.config.multipage}}={{/unless}} {{#if symbol.symbol}}{{>types/nested-name-specifier symbol=symbol.symbol.parent includeNamespace=true}}{{symbol.symbol.name}}{{else}}{{symbol.type.name}}{{/if}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- function --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- guides --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- record --}}
={{#unless @root.config.multipage}}={{/unless}} {{#if symbol.name}}{{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}{{else}}Unnamed class{{/if}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- typedef --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- symbols/using.adoc.hbs --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{!-- variable --}}
={{#unless @root.config.multipage}}={{/unless}} {{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}
{{{symbol.doc.brief}}}

=={{#unless @root.config.multipage}}={{/unless}} Synopsis

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
{{~/markup/td}}
{{#>markup/td~}}
{{#if (ne kind "overload")~}}
{{~doc.brief}}
{{{~doc.brief}}}
{{else~}}
{{#each (unique (pluck (pluck members "doc") "brief"))~}}
{{.}}
{{{.}}}
{{/each~}}
{{/if}}
{{~/markup/td}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<div>
{{! Content generated with index.hbs }}
{{contents}}
{{{contents}}}
</div>
<div>
<h4>Created with <a href="https://www.mrdocs.com">MrDocs</a></h4>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a href="{{href}}">{{> @partial-block }}</a>
<a href="{{{href}}}">{{> @partial-block }}</a>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div {{#if sectionref}}id="{{sectionref}}"{{/if}}>
<div>
<h2>{{>types/nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}</h2>
{{symbol.doc.brief}}
{{{symbol.doc.brief}}}
</div>
<div>
<h3>Synopsis</h3>
Expand Down
19 changes: 19 additions & 0 deletions src/lib/Gen/adoc/AdocGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "AdocGenerator.hpp"
#include "DocVisitor.hpp"
#include <mrdocs/Support/Handlebars.hpp>

namespace clang {
namespace mrdocs {
Expand All @@ -31,6 +32,24 @@ toString(hbs::HandlebarsCorpus const& c, doc::Node const& I) const
return s;
}

void
AdocGenerator::
escape(OutputRef& os, std::string_view str) const
{
static constexpr std::string_view formattingChars = "\\`*_{}[]()#+-.!|";
bool const needsEscape = str.find_first_of(formattingChars) != std::string_view::npos;
if (needsEscape)
{
os << "pass:[";
os << str;
os << "]";
}
else
{
os << str;
}
}

} // adoc

std::unique_ptr<Generator>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Gen/adoc/AdocGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class AdocGenerator
toString(
hbs::HandlebarsCorpus const&,
doc::Node const&) const override;

void
escape(OutputRef& os, std::string_view str) const override;
};

} // adoc
Expand Down
8 changes: 5 additions & 3 deletions src/lib/Gen/hbs/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ loadPartials(

Builder::

Check warning on line 74 in src/lib/Gen/hbs/Builder.cpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20

Build Warning - g++-14 - [-Wreorder]

g++-14 - when initialized here ([-Wreorder])

Check warning on line 74 in src/lib/Gen/hbs/Builder.cpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20 (Coverage)

Build Warning - g++-14 - [-Wreorder]

g++-14 - when initialized here ([-Wreorder])
Builder(
HandlebarsCorpus const& corpus)
HandlebarsCorpus const& corpus,
std::function<void(OutputRef&, std::string_view)> escapeFn)
: domCorpus(corpus)

Check warning on line 78 in src/lib/Gen/hbs/Builder.cpp

View workflow job for this annotation

GitHub Actions / Apple-Clang

Build Warning - clang++ - [-Wreorder-ctor]

clang++ - field 'domCorpus' will be initialized after field 'escapeFn_' ([-Wreorder-ctor])

Check warning on line 78 in src/lib/Gen/hbs/Builder.cpp

View workflow job for this annotation

GitHub Actions / Clang 18: C++20

Build Warning - clang++-18 - [-Wreorder-ctor]

clang++-18 - field 'domCorpus' will be initialized after field 'escapeFn_' ([-Wreorder-ctor])
, escapeFn_(std::move(escapeFn))
{
namespace fs = std::filesystem;

Expand Down Expand Up @@ -156,7 +158,7 @@ callTemplate(
auto pathName = files::appendPath(layoutDir(), name);
MRDOCS_TRY(auto fileText, files::getFileText(pathName));
HandlebarsOptions options;
options.noEscape = true;
options.escapeFunction = escapeFn_;
Expected<std::string, HandlebarsError> exp =
hbs_.try_render(fileText, context, options);
if (!exp)
Expand Down Expand Up @@ -285,7 +287,7 @@ renderWrapped(
auto pathName = files::appendPath(layoutDir(), wrapperFile);
MRDOCS_TRY(auto fileText, files::getFileText(pathName));
HandlebarsOptions options;
options.noEscape = true;
options.escapeFunction = escapeFn_;
OutputRef outRef(os);
Expected<void, HandlebarsError> exp =
hbs_.try_render_to(
Expand Down
5 changes: 4 additions & 1 deletion src/lib/Gen/hbs/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Builder
{
js::Context ctx_;
Handlebars hbs_;
std::function<void(OutputRef&, std::string_view)> escapeFn_;

Check warning on line 36 in src/lib/Gen/hbs/Builder.hpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20

Build Warning - g++-14 - [-Wreorder]

g++-14 - 'std::function<void(clang::mrdocs::OutputRef&, std::basic_string_view<char>)> clang::mrdocs::hbs::Builder::escapeFn_' ([-Wreorder])

Check warning on line 36 in src/lib/Gen/hbs/Builder.hpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20 (Coverage)

Build Warning - g++-14 - [-Wreorder]

g++-14 - 'std::function<void(clang::mrdocs::OutputRef&, std::basic_string_view<char>)> clang::mrdocs::hbs::Builder::escapeFn_' ([-Wreorder])

std::string
getRelPrefix(std::size_t depth);
Expand All @@ -41,7 +42,9 @@ class Builder
HandlebarsCorpus const& domCorpus;

Check warning on line 42 in src/lib/Gen/hbs/Builder.hpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20

Build Warning - g++-14 - [-Wreorder]

g++-14 - 'clang::mrdocs::hbs::Builder::domCorpus' will be initialized after ([-Wreorder])

Check warning on line 42 in src/lib/Gen/hbs/Builder.hpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20 (Coverage)

Build Warning - g++-14 - [-Wreorder]

g++-14 - 'clang::mrdocs::hbs::Builder::domCorpus' will be initialized after ([-Wreorder])

explicit
Builder(HandlebarsCorpus const& corpus);
Builder(
HandlebarsCorpus const& corpus,
std::function<void(OutputRef&, std::string_view)> escapeFn);

/** Render the contents for a symbol.
Expand Down
24 changes: 20 additions & 4 deletions src/lib/Gen/hbs/HandlebarsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ namespace clang {
namespace mrdocs {
namespace hbs {

std::function<void(OutputRef&, std::string_view)>
createEscapeFn(HandlebarsGenerator const& gen)
{
return [&gen](OutputRef out, std::string_view str) {
return gen.escape(out, str);
};
}

Expected<ExecutorGroup<Builder>>
createExecutors(
HandlebarsGenerator const& gen,
HandlebarsCorpus const& hbsCorpus)
{
auto const& config = hbsCorpus->config;
Expand All @@ -33,7 +42,7 @@ createExecutors(
{
try
{
group.emplace(hbsCorpus);
group.emplace(hbsCorpus, createEscapeFn(gen));
}
catch(Exception const& ex)
{
Expand Down Expand Up @@ -75,7 +84,7 @@ build(

// Create corpus and executors
HandlebarsCorpus domCorpus = createDomCorpus(*this, corpus);
MRDOCS_TRY(ExecutorGroup<Builder> ex, createExecutors(domCorpus));
MRDOCS_TRY(ExecutorGroup<Builder> ex, createExecutors(*this, domCorpus));

// Visit the corpus
MultiPageVisitor visitor(ex, outputPath, corpus);
Expand All @@ -95,7 +104,7 @@ buildOne(
{
// Create corpus and executors
HandlebarsCorpus domCorpus = createDomCorpus(*this, corpus);
MRDOCS_TRY(ExecutorGroup<Builder> ex, createExecutors(domCorpus));
MRDOCS_TRY(ExecutorGroup<Builder> ex, createExecutors(*this, domCorpus));

// Embedded mode
if (corpus.config->embedded)
Expand All @@ -112,7 +121,7 @@ buildOne(
}

// Wrapped mode
Builder inlineBuilder(domCorpus);
Builder inlineBuilder(domCorpus, createEscapeFn(*this));
return inlineBuilder.renderWrapped(os, [&]() -> Expected<void> {
// This helper will write contents directly to ostream
SinglePageVisitor visitor(ex, corpus, os);
Expand All @@ -126,6 +135,13 @@ buildOne(
});
}

void
HandlebarsGenerator::
escape(OutputRef& out, std::string_view str) const
{
out << str;
}

} // hbs
} // mrdocs
} // clang
9 changes: 9 additions & 0 deletions src/lib/Gen/hbs/HandlebarsGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

namespace clang {
namespace mrdocs {

class OutputRef;

namespace hbs {

class HandlebarsGenerator
Expand Down Expand Up @@ -72,6 +75,12 @@ class HandlebarsGenerator
{
return {};
}

/** Output a escaped string to the output stream.
*/
virtual
void
escape(OutputRef& os, std::string_view str) const;
};

} // hbs
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Gen/html/HTMLGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "HTMLGenerator.hpp"
#include "DocVisitor.hpp"
#include <mrdocs/Support/Handlebars.hpp>

namespace clang {
namespace mrdocs {
Expand All @@ -31,6 +32,13 @@ toString(hbs::HandlebarsCorpus const& c, doc::Node const& I) const
return s;
}

void
HTMLGenerator::
escape(OutputRef& os, std::string_view str) const
{
HTMLEscape(os, str);
}

} // html

//------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Gen/html/HTMLGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class HTMLGenerator
toString(
hbs::HandlebarsCorpus const&,
doc::Node const&) const override;

void
escape(OutputRef& os, std::string_view str) const override;
};

} // html
Expand Down
Loading

0 comments on commit a89e82f

Please sign in to comment.