From 520943832acc11ed302c3ab15567ee40a77098d7 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Wed, 13 Nov 2024 14:06:21 -0300 Subject: [PATCH] refactor(HandlebarsGenerator): simplify build function Unify code duplicated between build and buildOne. --- include/mrdocs/Support/Error.hpp | 8 ++ src/lib/Gen/hbs/Builder.hpp | 13 +++- src/lib/Gen/hbs/HandlebarsGenerator.cpp | 99 ++++++++++++------------- 3 files changed, 65 insertions(+), 55 deletions(-) diff --git a/include/mrdocs/Support/Error.hpp b/include/mrdocs/Support/Error.hpp index a713bc442..fc92b4c36 100644 --- a/include/mrdocs/Support/Error.hpp +++ b/include/mrdocs/Support/Error.hpp @@ -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 diff --git a/src/lib/Gen/hbs/Builder.hpp b/src/lib/Gen/hbs/Builder.hpp index c80c640f9..7d1d1e921 100644 --- a/src/lib/Gen/hbs/Builder.hpp +++ b/src/lib/Gen/hbs/Builder.hpp @@ -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 callTemplate( std::string_view name, dom::Value const& context); - Expected renderSinglePageHeader(); - Expected renderSinglePageFooter(); + /** Render the header for a single page. + */ + Expected + renderSinglePageHeader(); + + /** Render the footer for a single page. + */ + Expected + renderSinglePageFooter(); /** Render the contents for a symbol. */ diff --git a/src/lib/Gen/hbs/HandlebarsGenerator.cpp b/src/lib/Gen/hbs/HandlebarsGenerator.cpp index 677f9b7e9..39bf4774c 100644 --- a/src/lib/Gen/hbs/HandlebarsGenerator.cpp +++ b/src/lib/Gen/hbs/HandlebarsGenerator.cpp @@ -43,12 +43,6 @@ createExecutors( return group; } -//------------------------------------------------ -// -// HandlebarsGenerator -// -//------------------------------------------------ - /** Return loaded Options from a configuration. */ Options @@ -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( @@ -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(); } @@ -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 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(); }