@@ -28,63 +28,17 @@ namespace hbs {
2828
2929namespace {
3030
31- std::vector<std::string>
32- makePartialDirs (std::vector<std::string> const & roots, std::string_view ext)
33- {
34- std::vector<std::string> dirs;
35- dirs.reserve (roots.size () * 2 );
36-
37- // Preserve root precedence: for each root load common first, then format.
38- // Later roots (supplemental addons) still override earlier ones.
39- for (auto const & root : roots)
40- {
41- auto const commonDir = files::appendPath (root, " generator" , " common" , " partials" );
42- if (files::exists (commonDir))
43- dirs.push_back (commonDir);
44-
45- auto const formatDir = files::appendPath (root, " generator" , ext, " partials" );
46- if (files::exists (formatDir))
47- dirs.push_back (formatDir);
48- }
49-
50- return dirs;
51- }
31+ /* * Loads Handlebars partial templates from a directory.
5232
53- std::vector<std::string>
54- makeHelperDirs (std::vector<std::string> const & roots, std::string_view ext)
55- {
56- std::vector<std::string> dirs;
57- dirs.reserve (roots.size () * 2 );
58-
59- // Preserve root precedence: for each root load common first, then format.
60- // Later roots (supplemental addons) still override earlier ones.
61- for (auto const & root : roots)
62- {
63- auto const commonDir = files::appendPath (root, " generator" , " common" , " helpers" );
64- if (files::exists (commonDir))
65- dirs.push_back (commonDir);
66-
67- auto const formatDir = files::appendPath (root, " generator" , ext, " helpers" );
68- if (files::exists (formatDir))
69- dirs.push_back (formatDir);
70- }
71- return dirs;
72- }
73-
74- std::vector<std::string>
75- makeLayoutDirs (std::vector<std::string> const & roots, std::string_view ext)
76- {
77- std::vector<std::string> dirs;
78- dirs.reserve (roots.size ());
79- for (auto const & root : roots)
80- {
81- auto const dir = files::appendPath (root, " generator" , ext, " layouts" );
82- if (files::exists (dir))
83- dirs.push_back (dir);
84- }
85- return dirs;
86- }
33+ Recursively scans the specified directory for `.hbs` files and
34+ registers each as a Handlebars partial. The partial name is derived
35+ from the file's relative path (without extension), allowing
36+ subdirectory organization (e.g., `components/button.hbs` becomes
37+ partial `components/button`).
8738
39+ @param hbs The Handlebars instance to register partials with.
40+ @param partialsPath The directory path to scan for partial files.
41+ */
8842void
8943loadPartials (
9044 Handlebars& hbs,
@@ -126,19 +80,22 @@ loadPartials(
12680 }
12781}
12882
129- /* Make a URL relative to another URL.
83+ /* * Makes a URL relative to another URL.
13084
131- This function is a version of the Antora `relativize` helper,
132- used to create relative URLs between two paths in Antora projects.
85+ This function implements Antora-style URL relativization, creating
86+ relative URLs between two paths. It takes a target path (`to`) and
87+ a source path (`from`), returning a relative path from `from` to `to`.
13388
134- The function takes two paths, `to` and `from`, and returns a
135- relative path from `from` to `to` .
89+ When called with a single argument, the current symbol's URL (from
90+ `data.root.symbol.url`) is used as the source path .
13691
137- If `from` is not provided, then the URL of the symbol being
138- rendered is used as the `from` path.
92+ @param to0 The target URL to make relative.
93+ @param from0 The source URL to relativize from (optional).
94+ @param options Handlebars options containing context data.
95+ @return The relative URL path, or the original URL if not absolute.
13996
140- @see https://gitlab.com/antora/antora-ui-default/-/blob/master/src/helpers/relativize.js
141- */
97+ @see https://gitlab.com/antora/antora-ui-default/-/blob/master/src/helpers/relativize.js
98+ */
14299dom::Value
143100relativize_fn (dom::Value to0, dom::Value from0, dom::Value options)
144101{
@@ -214,15 +171,35 @@ relativize_fn(dom::Value to0, dom::Value from0, dom::Value options)
214171 return relativePath;
215172}
216173
217- // Registration helpers
174+ /* * Registers partial templates from multiple directories.
218175
176+ Iterates through each directory and loads all `.hbs` files as
177+ Handlebars partials. Later directories in the list can override
178+ partials from earlier ones, enabling supplemental addons to
179+ customize templates.
180+
181+ @param hbs The Handlebars instance to register partials with.
182+ @param dirs The list of directories to load partials from.
183+ */
219184void
220185registerPartials (Handlebars& hbs, std::vector<std::string> const & dirs)
221186{
222187 for (auto const & dir : dirs)
223188 loadPartials (hbs, dir);
224189}
225190
191+ /* * Registers default Handlebars Generator helpers.
192+
193+ Registers the default set of helpers available in all templates:
194+ - `primary_location`: Returns the primary source location for a symbol
195+ - `relativize`: Creates relative URLs between paths
196+ - Constructor, string, Antora, logical, math, container, and type helpers
197+
198+ These helpers are registered before user-defined helpers, allowing
199+ users to override built-in behavior if needed.
200+
201+ @param hbs The Handlebars instance to register helpers with.
202+ */
226203void
227204registerDefaultHelpers (Handlebars& hbs)
228205{
@@ -263,6 +240,24 @@ registerDefaultHelpers(Handlebars& hbs)
263240 hbs.registerHelper (" relativize" , dom::makeInvocable (relativize_fn));
264241}
265242
243+ /* * Registers user-defined JavaScript helpers from addon directories.
244+
245+ Scans the specified directories for JavaScript files and registers
246+ them as Handlebars helpers. Files are categorized into two types:
247+
248+ - **Utility files** (prefixed with `_`): Executed as scripts to define
249+ shared globals. Loaded alphabetically before helper files.
250+ - **Helper files**: Registered as Handlebars helpers with the filename
251+ (minus `.js`) as the helper name.
252+
253+ This separation allows helpers to share common code through utilities
254+ without duplicating implementations.
255+
256+ @param hbs The Handlebars instance to register helpers with.
257+ @param ctx The JavaScript context for script execution.
258+ @param helperDirs The directories to scan for helper files.
259+ @return Success, or an error if loading/registration fails.
260+ */
266261Expected<void >
267262registerUserHelpers (
268263 Handlebars& hbs,
@@ -340,6 +335,17 @@ registerUserHelpers(
340335 return {};
341336}
342337
338+ /* * Loads a layout template from addon directories.
339+
340+ Searches through the layout directories for the specified template
341+ file. If found in multiple directories, later directories override
342+ earlier ones (enabling supplemental addons to customize layouts).
343+
344+ @param templates The map to store loaded templates (filename -> content).
345+ @param layoutDirs The directories to search for the template.
346+ @param filename The template filename to load (e.g., "index.html.hbs").
347+ @return Success, or throws an error if the template is not found.
348+ */
343349Expected<void , Error>
344350loadLayoutTemplate (
345351 std::map<std::string, std::string, std::less<>>& templates,
@@ -374,9 +380,9 @@ Builder(
374380
375381 auto const & config = domCorpus->config ;
376382 auto const roots = addon_paths::addonRoots (config);
377- auto const partialDirs = makePartialDirs (roots, domCorpus.fileExtension );
378- auto const helperDirs = makeHelperDirs (roots, domCorpus.fileExtension );
379- auto const layoutDirs = makeLayoutDirs (roots, domCorpus.fileExtension );
383+ auto const partialDirs = addon_paths::partialDirs (roots, domCorpus.fileExtension );
384+ auto const helperDirs = addon_paths::helperDirs (roots, domCorpus.fileExtension );
385+ auto const layoutDirs = addon_paths::layoutDirs (roots, domCorpus.fileExtension );
380386
381387 // Load partials (later dirs overwrite earlier ones because we walk in order)
382388 registerPartials (hbs_, partialDirs);
0 commit comments