Skip to content

Commit 77d7165

Browse files
committed
docs: add a runnable script-driven generator example
This adds a self-contained search-index generator that the docs page includes and CI runs. The extensions/script-driven-generators.adoc example section now includes the manifest and the generate.lua from this fixture, so the documented example is exactly the one the test runs.
1 parent 3edeb4f commit 77d7165

8 files changed

Lines changed: 78 additions & 21 deletions

File tree

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,19 @@ if (MRDOCS_BUILD_TESTS)
567567
endforeach ()
568568
endforeach ()
569569

570+
#-------------------------------------------------
571+
# Script-driven generator example
572+
#-------------------------------------------------
573+
add_test(
574+
NAME mrdocs-generator-script-driven-search-index
575+
COMMAND bash run.sh
576+
--addons=${CMAKE_CURRENT_SOURCE_DIR}/share/mrdocs/addons
577+
--output=${CMAKE_CURRENT_BINARY_DIR}/generator-examples/search-index/reference-output
578+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/examples/generators/script-driven/search-index
579+
)
580+
set_property(TEST mrdocs-generator-script-driven-search-index PROPERTY
581+
ENVIRONMENT "MRDOCS=$<TARGET_FILE:mrdocs>")
582+
570583
#-------------------------------------------------
571584
# Library-usage examples
572585
#

docs/antora-playbook.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ antora:
7171
strip_page_wrapper: true
7272
- source: examples/generators/data-driven
7373
target: data-driven-generators
74+
- source: examples/generators/script-driven
75+
target: script-driven-generators
7476
- source: examples
7577
target: examples
7678
- require: ./extensions/commands-registry-extension.js

docs/modules/ROOT/pages/extensions/script-driven-generators.adoc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,22 @@ Unlike a corpus-transform extension, whose hook is optional, a generator must de
4949

5050
== Example: a search index
5151

52-
This generator emits a single search-index.json aggregating every symbol, an artifact no per-page generator can produce:
52+
A complete, runnable example lives at `examples/generators/script-driven/search-index/`. It emits a single search-index.json aggregating every symbol, an artifact no per-page generator can produce.
5353

54-
[source,lua]
54+
The manifest names the script:
55+
56+
.`addons/generator/search-index/mrdocs-generator.yml`
57+
[source,yaml]
58+
----
59+
include::example$script-driven-generators/search-index/addons/generator/search-index/mrdocs-generator.yml[]
5560
----
56-
-- Quote a string as a JSON value.
57-
local function json_string(s)
58-
s = s:gsub('\\', '\\\\'):gsub('"', '\\"')
59-
return '"' .. s .. '"'
60-
end
6161

62-
function generate(corpus, output)
63-
local entries = {}
64-
for _, sym in ipairs(corpus.symbols) do
65-
local name = sym.name or ""
66-
if name ~= "" then
67-
entries[#entries + 1] =
68-
'{"name":' .. json_string(name) ..
69-
',"url":' .. json_string(sym._id .. ".html") .. "}"
70-
end
71-
end
72-
output.write(
73-
"search-index.json",
74-
"[" .. table.concat(entries, ",") .. "]")
75-
end
62+
The script itself:
63+
64+
.`addons/generator/search-index/generate.lua`
65+
[source,lua]
7666
----
67+
include::example$script-driven-generators/search-index/addons/generator/search-index/generate.lua[]
68+
----
69+
70+
Select it with `--generator=search-index`; it writes search-index.json into the output directory.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Quote a string as a JSON value.
2+
local function json_string(s)
3+
s = s:gsub('\\', '\\\\'):gsub('"', '\\"')
4+
return '"' .. s .. '"'
5+
end
6+
7+
function generate(corpus, output)
8+
local entries = {}
9+
for _, sym in ipairs(corpus.symbols) do
10+
local name = sym.name or ""
11+
if name ~= "" then
12+
entries[#entries + 1] =
13+
'{"name":' .. json_string(name) ..
14+
',"url":' .. json_string(sym._id .. ".html") .. "}"
15+
end
16+
end
17+
output.write(
18+
"search-index.json",
19+
"[" .. table.concat(entries, ",") .. "]")
20+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
script: generate.lua
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
addons-supplemental:
2+
- addons
3+
generator: search-index
4+
multipage: false
5+
show-namespaces: false
6+
warn-if-undocumented: false
7+
source-root: .
8+
input:
9+
- .
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
exec "${MRDOCS:-mrdocs}" --config=mrdocs.yml "$@"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// A point.
2+
struct Point
3+
{
4+
/** Distance from the origin.
5+
6+
@return The distance from the origin.
7+
*/
8+
double length() const;
9+
10+
/** Translate the point by an offset.
11+
12+
@param dx Horizontal offset.
13+
@param dy Vertical offset.
14+
*/
15+
void translate(int dx, int dy);
16+
};

0 commit comments

Comments
 (0)