Skip to content

Commit 5209438

Browse files
committed
refactor(HandlebarsGenerator): simplify build function
Unify code duplicated between build and buildOne.
1 parent 7d7f949 commit 5209438

File tree

3 files changed

+65
-55
lines changed

3 files changed

+65
-55
lines changed

include/mrdocs/Support/Error.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,14 @@ namespace detail
606606
# define MRDOCS_CHECK_GET_MACRO(_1, _2, NAME, ...) NAME
607607
# define MRDOCS_CHECK(...) \
608608
MRDOCS_CHECK_GET_MACRO(__VA_ARGS__, MRDOCS_CHECK_MSG, MRDOCS_CHECK_VOID)(__VA_ARGS__)
609+
610+
# define MRDOCS_CHECK_OR(var, expr) \
611+
if (detail::failed(var)) { \
612+
return expr; \
613+
} \
614+
void(0)
615+
616+
609617
#endif
610618

611619

src/lib/Gen/hbs/Builder.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,22 @@ class Builder
4747
dom::Value createContext(Info const& I);
4848
dom::Value createContext(OverloadSet const& OS);
4949

50+
/** Render a Handlebars template from the templates directory.
51+
*/
5052
Expected<std::string>
5153
callTemplate(
5254
std::string_view name,
5355
dom::Value const& context);
5456

55-
Expected<std::string> renderSinglePageHeader();
56-
Expected<std::string> renderSinglePageFooter();
57+
/** Render the header for a single page.
58+
*/
59+
Expected<std::string>
60+
renderSinglePageHeader();
61+
62+
/** Render the footer for a single page.
63+
*/
64+
Expected<std::string>
65+
renderSinglePageFooter();
5766

5867
/** Render the contents for a symbol.
5968
*/

src/lib/Gen/hbs/HandlebarsGenerator.cpp

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ createExecutors(
4343
return group;
4444
}
4545

46-
//------------------------------------------------
47-
//
48-
// HandlebarsGenerator
49-
//
50-
//------------------------------------------------
51-
5246
/** Return loaded Options from a configuration.
5347
*/
5448
Options
@@ -79,6 +73,27 @@ loadOptions(
7973
return opt;
8074
}
8175

76+
HandlebarsCorpus
77+
createDomCorpus(
78+
HandlebarsGenerator const& gen,
79+
Corpus const& corpus)
80+
{
81+
auto options = loadOptions(gen.fileExtension(), corpus.config);
82+
return {
83+
corpus,
84+
std::move(options),
85+
gen.fileExtension(),
86+
[&gen](HandlebarsCorpus const& c, doc::Node const& n) {
87+
return gen.toString(c, n);
88+
}};
89+
}
90+
91+
//------------------------------------------------
92+
//
93+
// HandlebarsGenerator
94+
//
95+
//------------------------------------------------
96+
8297
Error
8398
HandlebarsGenerator::
8499
build(
@@ -90,28 +105,18 @@ build(
90105
return Generator::build(outputPath, corpus);
91106
}
92107

93-
Options options = loadOptions(fileExtension(), corpus.config);
94-
HandlebarsCorpus domCorpus(
95-
corpus,
96-
std::move(options),
97-
fileExtension(),
98-
[this](HandlebarsCorpus const& c, doc::Node const& n) {
99-
return this->toString(c, n);
100-
});
108+
// Create corpus and executors
109+
HandlebarsCorpus domCorpus = createDomCorpus(*this, corpus);
101110
auto ex = createExecutors(domCorpus);
102-
if (!ex)
103-
{
104-
return ex.error();
105-
}
111+
MRDOCS_CHECK_OR(ex, ex.error());
106112

113+
// Visit the corpus
107114
MultiPageVisitor visitor(*ex, outputPath, corpus);
108115
visitor(corpus.globalNamespace());
109116

117+
// Wait for all executors to finish and check errors
110118
auto errors = ex->wait();
111-
if (!errors.empty())
112-
{
113-
return Error(errors);
114-
}
119+
MRDOCS_CHECK_OR(errors.empty(), errors);
115120
return Error::success();
116121
}
117122

@@ -121,47 +126,35 @@ buildOne(
121126
std::ostream& os,
122127
Corpus const& corpus) const
123128
{
124-
auto options = loadOptions(fileExtension(), corpus.config);
125-
126-
HandlebarsCorpus domCorpus(
127-
corpus,
128-
std::move(options),
129-
fileExtension(),
130-
[this](HandlebarsCorpus const& c, doc::Node const& n) {
131-
return this->toString(c, n);
132-
});
129+
// Create corpus and executors
130+
HandlebarsCorpus domCorpus = createDomCorpus(*this, corpus);
133131
auto ex = createExecutors(domCorpus);
134-
if (!ex)
135-
{
136-
return ex.error();
137-
}
132+
MRDOCS_CHECK_OR(ex, ex.error());
138133

134+
// Render the header
139135
std::vector<Error> errors;
140-
ex->async(
141-
[&os](Builder& builder)
142-
{
143-
auto pageText = builder.renderSinglePageHeader().value();
144-
os.write(pageText.data(), pageText.size());
145-
});
136+
ex->async([&os](Builder& builder) {
137+
auto pageText = builder.renderSinglePageHeader().value();
138+
os.write(pageText.data(), pageText.size());
139+
});
146140
errors = ex->wait();
147-
if(! errors.empty())
148-
return {errors};
141+
MRDOCS_CHECK_OR(errors.empty(), {errors});
149142

143+
// Visit the corpus
150144
SinglePageVisitor visitor(*ex, corpus, os);
151145
visitor(corpus.globalNamespace());
146+
147+
// Wait for all executors to finish and check errors
152148
errors = ex->wait();
153-
if(! errors.empty())
154-
return {errors};
149+
MRDOCS_CHECK_OR(errors.empty(), errors);
155150

156-
ex->async(
157-
[&os](Builder& builder)
158-
{
159-
auto pageText = builder.renderSinglePageFooter().value();
160-
os.write(pageText.data(), pageText.size());
161-
});
151+
// Render the footer
152+
ex->async([&os](Builder& builder) {
153+
auto pageText = builder.renderSinglePageFooter().value();
154+
os.write(pageText.data(), pageText.size());
155+
});
162156
errors = ex->wait();
163-
if(! errors.empty())
164-
return {errors};
157+
MRDOCS_CHECK_OR(errors.empty(), {errors});
165158

166159
return Error::success();
167160
}

0 commit comments

Comments
 (0)