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