@@ -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*/
5448Options
@@ -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+
8297Error
8398HandlebarsGenerator::
8499build (
@@ -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