Skip to content

Commit

Permalink
refactor(HandlebarsGenerator): toString customization is virtual func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
alandefreitas committed Nov 14, 2024
1 parent eb464a3 commit 7d7f949
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 25 deletions.
20 changes: 11 additions & 9 deletions src/lib/Gen/adoc/AdocGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ namespace adoc {

AdocGenerator::
AdocGenerator()
: hbs::HandlebarsGenerator("Asciidoc", "adoc", [](
hbs::HandlebarsCorpus const& c,
doc::Node const& I) -> std::string
{
std::string s;
DocVisitor visitor(c, s);
doc::visit(I, visitor);
return s;
})
: hbs::HandlebarsGenerator("Asciidoc", "adoc")
{}

std::string
AdocGenerator::
toString(hbs::HandlebarsCorpus const& c, doc::Node const& I) const
{
std::string s;
DocVisitor visitor(c, s);
doc::visit(I, visitor);
return s;
}

} // adoc

std::unique_ptr<Generator>
Expand Down
5 changes: 5 additions & 0 deletions src/lib/Gen/adoc/AdocGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class AdocGenerator
{
public:
AdocGenerator();

std::string
toString(
hbs::HandlebarsCorpus const&,
doc::Node const&) const override;
};

} // adoc
Expand Down
4 changes: 4 additions & 0 deletions src/lib/Gen/hbs/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ class Builder
Expected<std::string> renderSinglePageHeader();
Expected<std::string> renderSinglePageFooter();

/** Render the contents for a symbol.
*/
template<class T>
Expected<std::string>
operator()(T const&);

/** Render the contents for an overload set.
*/
Expected<std::string>
operator()(OverloadSet const&);
};
Expand Down
14 changes: 12 additions & 2 deletions src/lib/Gen/hbs/HandlebarsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ build(

Options options = loadOptions(fileExtension(), corpus.config);
HandlebarsCorpus domCorpus(
corpus, std::move(options), fileExtension(), toStringFn);
corpus,
std::move(options),
fileExtension(),
[this](HandlebarsCorpus const& c, doc::Node const& n) {
return this->toString(c, n);
});
auto ex = createExecutors(domCorpus);
if (!ex)
{
Expand All @@ -119,7 +124,12 @@ buildOne(
auto options = loadOptions(fileExtension(), corpus.config);

HandlebarsCorpus domCorpus(
corpus, std::move(options), fileExtension(), toStringFn);
corpus,
std::move(options),
fileExtension(),
[this](HandlebarsCorpus const& c, doc::Node const& n) {
return this->toString(c, n);
});
auto ex = createExecutors(domCorpus);
if (!ex)
{
Expand Down
15 changes: 10 additions & 5 deletions src/lib/Gen/hbs/HandlebarsGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ class HandlebarsGenerator
{
std::string displayName_;
std::string fileExtension_;
using JavadocToStringFn = std::function<std::string(HandlebarsCorpus const&, doc::Node const&)>;
JavadocToStringFn toStringFn;

public:
HandlebarsGenerator(
std::string_view displayName,
std::string_view fileExtension,
JavadocToStringFn toStringFn)
std::string_view fileExtension)
: displayName_(displayName)
, fileExtension_(fileExtension)
, toStringFn(std::move(toStringFn))
{}

std::string_view
Expand Down Expand Up @@ -68,6 +64,15 @@ class HandlebarsGenerator
buildOne(
std::ostream& os,
Corpus const& corpus) const override;

/** Convert a Javadoc node to a string.
*/
virtual
std::string
toString(HandlebarsCorpus const&, doc::Node const&) const
{
return {};
}
};

} // hbs
Expand Down
20 changes: 11 additions & 9 deletions src/lib/Gen/html/HTMLGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ namespace html {

HTMLGenerator::
HTMLGenerator()
: hbs::HandlebarsGenerator("HTML", "html", [](
hbs::HandlebarsCorpus const& c,
doc::Node const& I) -> std::string
{
std::string s;
DocVisitor visitor(c, s);
doc::visit(I, visitor);
return s;
})
: hbs::HandlebarsGenerator("HTML", "html")
{}

std::string
HTMLGenerator::
toString(hbs::HandlebarsCorpus const& c, doc::Node const& I) const
{
std::string s;
DocVisitor visitor(c, s);
doc::visit(I, visitor);
return s;
}

} // html

//------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions src/lib/Gen/html/HTMLGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class HTMLGenerator
{
public:
HTMLGenerator();

std::string
toString(
hbs::HandlebarsCorpus const&,
doc::Node const&) const override;
};

} // html
Expand Down

0 comments on commit 7d7f949

Please sign in to comment.