xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/page.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan"""A helper module for generating documentation for rules_rust"""
2*d4726bddSHONG Yifan
3*d4726bddSHONG Yifandef page(name, symbols, header_template = None):
4*d4726bddSHONG Yifan    """Define a collection of attributes used to generate a page of documentation
5*d4726bddSHONG Yifan
6*d4726bddSHONG Yifan    Note, all templates are Velocity files: https://velocity.apache.org/engine/devel/user-guide.html
7*d4726bddSHONG Yifan
8*d4726bddSHONG Yifan    Args:
9*d4726bddSHONG Yifan        name (str): The name of the page
10*d4726bddSHONG Yifan        symbols (list): A list of symbol names
11*d4726bddSHONG Yifan        header_template (Label, optional): The label of a `header_template` stardoc attribute
12*d4726bddSHONG Yifan
13*d4726bddSHONG Yifan    Returns:
14*d4726bddSHONG Yifan        tuple: The name of the page with the page content
15*d4726bddSHONG Yifan    """
16*d4726bddSHONG Yifan    return (name, struct(
17*d4726bddSHONG Yifan        name = name,
18*d4726bddSHONG Yifan        header_template = header_template,
19*d4726bddSHONG Yifan        symbols = symbols,
20*d4726bddSHONG Yifan    ))
21*d4726bddSHONG Yifan
22*d4726bddSHONG Yifan# buildifier: disable=unnamed-macro
23*d4726bddSHONG Yifandef gen_header(page):
24*d4726bddSHONG Yifan    """Generate a header with a table of contents
25*d4726bddSHONG Yifan
26*d4726bddSHONG Yifan    Args:
27*d4726bddSHONG Yifan        page (struct): A `page` struct
28*d4726bddSHONG Yifan    """
29*d4726bddSHONG Yifan    name = "%s_gen_header_vm" % page.name
30*d4726bddSHONG Yifan    outs = ["%s_gen_header.vm" % page.name]
31*d4726bddSHONG Yifan
32*d4726bddSHONG Yifan    # Set the top level header
33*d4726bddSHONG Yifan    page_names = [w.capitalize() for w in page.name.split("_")]
34*d4726bddSHONG Yifan    cmd = [
35*d4726bddSHONG Yifan        "echo '<!-- Generated with Stardoc: http://skydoc.bazel.build -->'",
36*d4726bddSHONG Yifan        "echo '# {}'".format(" ".join(page_names)),
37*d4726bddSHONG Yifan        "echo ''",
38*d4726bddSHONG Yifan    ]
39*d4726bddSHONG Yifan
40*d4726bddSHONG Yifan    # Add table of contents
41*d4726bddSHONG Yifan    cmd.extend(["echo '* [{rule}](#{rule})'".format(rule = s) for s in page.symbols])
42*d4726bddSHONG Yifan
43*d4726bddSHONG Yifan    # Render an optional header
44*d4726bddSHONG Yifan    if page.header_template:
45*d4726bddSHONG Yifan        cmd.extend([
46*d4726bddSHONG Yifan            "echo ''",
47*d4726bddSHONG Yifan            "cat $(execpath {})".format(page.header_template),
48*d4726bddSHONG Yifan        ])
49*d4726bddSHONG Yifan        srcs = [page.header_template]
50*d4726bddSHONG Yifan    else:
51*d4726bddSHONG Yifan        srcs = []
52*d4726bddSHONG Yifan
53*d4726bddSHONG Yifan    native.genrule(
54*d4726bddSHONG Yifan        name = name,
55*d4726bddSHONG Yifan        outs = outs,
56*d4726bddSHONG Yifan        cmd = "{\n" + "\n".join(cmd) + "\n} > $@",
57*d4726bddSHONG Yifan        srcs = srcs,
58*d4726bddSHONG Yifan        output_to_bindir = True,
59*d4726bddSHONG Yifan    )
60