xref: /aosp_15_r20/external/stardoc/docs/advanced_stardoc_usage.md (revision b2fa42943c124aa9c7163734493fc7a7559681cf)
1<nav class="toc">
2  <h2>Contents</h2>
3  <ul>
4    <li><a href="#docstring-formatting">Docstring Formatting</a></li>
5    <li><a href="#custom-output">Custom Output</a></li>
6    <li><a href="#proto-output">Proto Output</a></li>
7  </ul>
8</nav>
9
10This document covers a number of advanced topics pertaining to using Stardoc.
11
12
13<a name="docstring-formatting"></a>
14## Docstring Formatting
15
16You may want to inline various kinds of formatting in the docstrings adjacent
17to your Starlark code. Use standard markdown formatting constructs instead of
18HTML tags.
19
20For example:
21```python
22def my_function(foo, bar):
23  """Does some cool stuff.
24
25  Oh, by the way, have you heard about [Stardoc](https://github.com/bazelbuild/stardoc)?
26
27  Args:
28    foo: You don't know what a **foo** is?
29    bar: Two variables, `x` and `y`, walk in to a bar...
30  """
31  ...
32```
33
34Markdown formatting constructs are handled appropriately by Stardoc's default
35output format ("markdown_tables"), even as part of a table.
36
37
38<a name="custom-output"></a>
39## Custom Output
40
41Stardoc's output format is customizable; while Stardoc's output is markdown
42by default, you may define a different output format for your documentation.
43
44Customization is done at the level of "output templates". To customize the
45doc output for a particular type of Starlark definition (such as a "rule" or a
46"function"), you will need to:
47
481. Create a new custom output template to describe how this type of object should
49   be rendered.
502. In your `stardoc()` target, set the matching `_template` attribute to point to
51   your new output template.
52
53For example, you might want to change the way rule documentation is generated.
54You might create a new output template file `package/rule.vm` and then define your
55`stardoc` target as follows:
56
57```python
58stardoc(
59    name = "my_docs",
60    input = "my_rule.bzl",
61    out = "my_rule_doc.md",
62    rule_template = "//package:rule.vm",
63)
64```
65
66The default values for the available templates may be found under
67[templates/markdown_tables](../stardoc/templates/markdown_tables). See the
68[Stardoc rule documentation](stardoc_rule.md) for a comprehensive list of which
69'_template' attributes are available.
70
71
72### Writing a custom output template
73
74Stardoc's output templates are defined using
75[Velocity Template Language (VTL)](https://velocity.apache.org/engine/1.7/user-guide.html)
76with utilities and model objects available in the evaluation context.
77
78The full comprehensive list of available utilities top-level objects is available in
79[the source for MarkdownRenderer](https://github.com/bazelbuild/bazel/blob/3fcfbe14ddec34889c5e3fe33415af2cf9124e7c/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownRenderer.java#L100).
80
81Information available for raw model objects (such rule information) is defined by
82Stardoc's underlying [proto schema](https://github.com/bazelbuild/bazel/blob/5eeccd8a647df10d154d3b86e9732e7f263c96db/src/main/java/com/google/devtools/build/skydoc/rendering/proto/stardoc_output.proto).
83
84This is a particularly advanced feature of Stardoc, so we would recommend using
85one of the existing canonical [templates](../stardoc/templates/markdown_tables) as a
86springboard to get started.
87
88
89<a name="proto-output"></a>
90## Proto Output
91
92Stardoc provides the option to output documentation information in raw proto
93format. You may find this useful if you need output customization beyond
94Stardoc's current custom-output-template capabilities: you might prefer to build
95your own custom output renderer binary using the data that Stardoc acquires by
96fully evaluating a Starlark file. If your changes could be incorporated into
97Stardoc, please first consider [contributing](contributing.md) instead.
98
99The proto schema may be found under
100[stardoc/proto/stardoc_output.proto](../stardoc/proto/stardoc_output.proto).
101Only the `.proto` file itself is provided (this prevents a transitive dependency on
102proto rules to support only a very-advanced usecase). We recommend using rules
103defined under
104[bazelbuild/rules_proto](https://github.com/bazelbuild/rules_proto), creating
105your own proto target using this source file, and adding it as a dependency of
106your renderer binary.
107
108To configure stardoc to output raw proto instead of markdown, use the `format`
109attribute of the [stardoc rule](stardoc_rule.md#stardoc-format). Specify `"proto"`.
110An example:
111
112```python
113stardoc(
114    name = "docs_proto_output",
115    out = "doc_output.raw",
116    input = ":my_rule.bzl",
117    deps = [":my_lib"],
118    format = "proto",
119)
120
121# Define a proto_library target to incorporate the stardoc_output_proto
122proto_library(
123    name = "stardoc_output_proto",
124    srcs = ["@io_bazel_stardoc//stardoc/proto:stardoc_output.proto"],
125)
126
127# You'll need to define your own rendering target. This might be a
128# `genrule` or your own custom rule.
129genrule(
130    name = "docs_markdown_output",
131    tools = ["my_renderer.sh"],
132    srcs = ["doc_output.raw"],
133    outs = ["doc_output.md"],
134    cmd = "$(location :my_renderer.sh) $@ $(SRCS)",
135)
136```
137