Skip to content

Commit

Permalink
refactor(HandlebarsGenerator): simplify build function
Browse files Browse the repository at this point in the history
Unify code duplicated between build and buildOne.
  • Loading branch information
alandefreitas committed Nov 14, 2024
1 parent 7d7f949 commit 5209438
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 55 deletions.
8 changes: 8 additions & 0 deletions include/mrdocs/Support/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,14 @@ namespace detail
# define MRDOCS_CHECK_GET_MACRO(_1, _2, NAME, ...) NAME
# define MRDOCS_CHECK(...) \
MRDOCS_CHECK_GET_MACRO(__VA_ARGS__, MRDOCS_CHECK_MSG, MRDOCS_CHECK_VOID)(__VA_ARGS__)

# define MRDOCS_CHECK_OR(var, expr) \
if (detail::failed(var)) { \
return expr; \
} \
void(0)


#endif


Expand Down
13 changes: 11 additions & 2 deletions src/lib/Gen/hbs/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,22 @@ class Builder
dom::Value createContext(Info const& I);
dom::Value createContext(OverloadSet const& OS);

/** Render a Handlebars template from the templates directory.
*/
Expected<std::string>
callTemplate(
std::string_view name,
dom::Value const& context);

Expected<std::string> renderSinglePageHeader();
Expected<std::string> renderSinglePageFooter();
/** Render the header for a single page.
*/
Expected<std::string>
renderSinglePageHeader();

/** Render the footer for a single page.
*/
Expected<std::string>
renderSinglePageFooter();

/** Render the contents for a symbol.
*/
Expand Down
99 changes: 46 additions & 53 deletions src/lib/Gen/hbs/HandlebarsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ createExecutors(
return group;
}

//------------------------------------------------
//
// HandlebarsGenerator
//
//------------------------------------------------

/** Return loaded Options from a configuration.
*/
Options
Expand Down Expand Up @@ -79,6 +73,27 @@ loadOptions(
return opt;
}

HandlebarsCorpus
createDomCorpus(
HandlebarsGenerator const& gen,
Corpus const& corpus)
{
auto options = loadOptions(gen.fileExtension(), corpus.config);
return {
corpus,
std::move(options),
gen.fileExtension(),
[&gen](HandlebarsCorpus const& c, doc::Node const& n) {
return gen.toString(c, n);
}};
}

//------------------------------------------------
//
// HandlebarsGenerator
//
//------------------------------------------------

Error
HandlebarsGenerator::
build(
Expand All @@ -90,28 +105,18 @@ build(
return Generator::build(outputPath, corpus);
}

Options options = loadOptions(fileExtension(), corpus.config);
HandlebarsCorpus domCorpus(
corpus,
std::move(options),
fileExtension(),
[this](HandlebarsCorpus const& c, doc::Node const& n) {
return this->toString(c, n);
});
// Create corpus and executors
HandlebarsCorpus domCorpus = createDomCorpus(*this, corpus);
auto ex = createExecutors(domCorpus);
if (!ex)
{
return ex.error();
}
MRDOCS_CHECK_OR(ex, ex.error());

// Visit the corpus
MultiPageVisitor visitor(*ex, outputPath, corpus);
visitor(corpus.globalNamespace());

// Wait for all executors to finish and check errors
auto errors = ex->wait();
if (!errors.empty())
{
return Error(errors);
}
MRDOCS_CHECK_OR(errors.empty(), errors);
return Error::success();
}

Expand All @@ -121,47 +126,35 @@ buildOne(
std::ostream& os,
Corpus const& corpus) const
{
auto options = loadOptions(fileExtension(), corpus.config);

HandlebarsCorpus domCorpus(
corpus,
std::move(options),
fileExtension(),
[this](HandlebarsCorpus const& c, doc::Node const& n) {
return this->toString(c, n);
});
// Create corpus and executors
HandlebarsCorpus domCorpus = createDomCorpus(*this, corpus);
auto ex = createExecutors(domCorpus);
if (!ex)
{
return ex.error();
}
MRDOCS_CHECK_OR(ex, ex.error());

// Render the header
std::vector<Error> errors;
ex->async(
[&os](Builder& builder)
{
auto pageText = builder.renderSinglePageHeader().value();
os.write(pageText.data(), pageText.size());
});
ex->async([&os](Builder& builder) {
auto pageText = builder.renderSinglePageHeader().value();
os.write(pageText.data(), pageText.size());
});
errors = ex->wait();
if(! errors.empty())
return {errors};
MRDOCS_CHECK_OR(errors.empty(), {errors});

// Visit the corpus
SinglePageVisitor visitor(*ex, corpus, os);
visitor(corpus.globalNamespace());

// Wait for all executors to finish and check errors
errors = ex->wait();
if(! errors.empty())
return {errors};
MRDOCS_CHECK_OR(errors.empty(), errors);

ex->async(
[&os](Builder& builder)
{
auto pageText = builder.renderSinglePageFooter().value();
os.write(pageText.data(), pageText.size());
});
// Render the footer
ex->async([&os](Builder& builder) {
auto pageText = builder.renderSinglePageFooter().value();
os.write(pageText.data(), pageText.size());
});
errors = ex->wait();
if(! errors.empty())
return {errors};
MRDOCS_CHECK_OR(errors.empty(), {errors});

return Error::success();
}
Expand Down

0 comments on commit 5209438

Please sign in to comment.