Search in sources :

Example 1 with DocModule

use of com.google.copybara.doc.DocBase.DocModule in project copybara by google.

the class MarkdownRenderer method render.

public CharSequence render(Iterable<DocModule> modules) {
    StringBuilder sb = new StringBuilder();
    sb.append(tableOfContents(modules));
    for (DocModule module : modules) {
        sb.append("\n");
        sb.append(module(module, MODULE_HEADING_LEVEL));
    }
    return sb;
}
Also used : DocModule(com.google.copybara.doc.DocBase.DocModule)

Example 2 with DocModule

use of com.google.copybara.doc.DocBase.DocModule in project copybara by google.

the class MarkdownRenderer method module.

private CharSequence module(DocModule module, int level) {
    StringBuilder sb = new StringBuilder();
    sb.append(title(level, module.name));
    sb.append(module.description).append("\n\n");
    if (!module.fields.isEmpty()) {
        sb.append(title(level + 2, "Fields:"));
        sb.append(tableHeader("Name", "Description"));
        for (DocField field : module.fields) {
            sb.append(tableRow(field.name, String.format("%s<br><p>%s</p>", typeName(field.type), field.description)));
        }
        sb.append("\n");
    }
    sb.append(flags(module.flags));
    for (DocFunction func : module.functions) {
        sb.append("<a id=\"").append(func.name).append("\" aria-hidden=\"true\"></a>");
        sb.append(title(level + 1, func.name));
        sb.append(func.description);
        sb.append("\n\n");
        if (func.returnType != null) {
            sb.append(typeName(func.returnType)).append(" ");
        }
        sb.append("`").append(func.name).append("(");
        Joiner.on(", ").appendTo(sb, Lists.transform(func.params, p -> p.name + (p.defaultValue == null ? "" : "=" + p.defaultValue)));
        sb.append(")`\n\n");
        if (!func.params.isEmpty()) {
            sb.append(title(level + 2, "Parameters:"));
            sb.append(tableHeader("Parameter", "Description"));
            for (DocParam param : func.params) {
                sb.append(tableRow(param.name, String.format("%s<br><p>%s</p>", param.allowedTypes.stream().map(this::typeName).collect(joining(" or ")), param.description)));
            }
            sb.append("\n");
        }
        if (!func.examples.isEmpty()) {
            sb.append(title(level + 2, func.examples.size() == 1 ? "Example:" : "Examples:"));
            for (DocExample example : func.examples) {
                sb.append(example(level + 3, example.example));
            }
            sb.append("\n");
        }
        sb.append(flags(func.flags));
    }
    return sb;
}
Also used : DocModule(com.google.copybara.doc.DocBase.DocModule) DocExample(com.google.copybara.doc.DocBase.DocExample) DocFlag(com.google.copybara.doc.DocBase.DocFlag) Ascii(com.google.common.base.Ascii) Collection(java.util.Collection) Set(java.util.Set) Collectors.joining(java.util.stream.Collectors.joining) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Example(com.google.copybara.doc.annotations.Example) DocParam(com.google.copybara.doc.DocBase.DocParam) Preconditions(com.google.common.base.Preconditions) DocFunction(com.google.copybara.doc.DocBase.DocFunction) DocField(com.google.copybara.doc.DocBase.DocField) Arrays.stream(java.util.Arrays.stream) Joiner(com.google.common.base.Joiner) DocExample(com.google.copybara.doc.DocBase.DocExample) DocFunction(com.google.copybara.doc.DocBase.DocFunction) DocField(com.google.copybara.doc.DocBase.DocField) DocParam(com.google.copybara.doc.DocBase.DocParam)

Example 3 with DocModule

use of com.google.copybara.doc.DocBase.DocModule in project copybara by google.

the class MarkdownRenderer method tableOfContents.

private CharSequence tableOfContents(Iterable<DocModule> modules) {
    StringBuilder sb = new StringBuilder();
    sb.append("## Table of Contents\n\n\n");
    for (DocModule module : modules) {
        headings.add(module.name);
        sb.append("  - ");
        sb.append(linkify(module.name));
        sb.append("\n");
        for (DocFunction f : module.functions) {
            headings.add(f.name);
            sb.append("    - ");
            sb.append(linkify(f.name));
            sb.append("\n");
        }
    }
    sb.append("\n");
    return sb;
}
Also used : DocFunction(com.google.copybara.doc.DocBase.DocFunction) DocModule(com.google.copybara.doc.DocBase.DocModule)

Example 4 with DocModule

use of com.google.copybara.doc.DocBase.DocModule in project copybara by google.

the class ModuleLoader method load.

public ImmutableList<DocModule> load(List<String> jarFiles) throws IOException {
    List<String> classes = loadClassList(jarFiles);
    List<DocModule> modules = new ArrayList<>();
    DocModule docModule = new DocModule("Globals", "Global functions available in Copybara");
    modules.add(docModule);
    for (String clsName : classes) {
        try {
            Class<?> cls = Generator.class.getClassLoader().loadClass(clsName);
            getAnnotation(cls, Library.class).ifPresent(library -> docModule.functions.addAll(processFunctions(cls, null)));
            getAnnotation(cls, StarlarkBuiltin.class).ifPresent(library -> {
                if (!library.documented()) {
                    return;
                }
                DocSignaturePrefix prefixAnn = cls.getAnnotation(DocSignaturePrefix.class);
                String prefix = prefixAnn != null ? prefixAnn.value() : library.name();
                DocModule mod = new DocModule(library.name(), library.doc());
                mod.functions.addAll(processFunctions(cls, prefix));
                mod.fields.addAll(processFields(cls));
                mod.flags.addAll(generateFlagsInfo(cls));
                modules.add(mod);
            });
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("Cannot generate documentation for " + clsName, e);
        }
    }
    return deduplicateAndSort(modules);
}
Also used : DocSignaturePrefix(com.google.copybara.doc.annotations.DocSignaturePrefix) StarlarkBuiltin(net.starlark.java.annot.StarlarkBuiltin) ArrayList(java.util.ArrayList) Library(com.google.copybara.doc.annotations.Library) DocModule(com.google.copybara.doc.DocBase.DocModule)

Aggregations

DocModule (com.google.copybara.doc.DocBase.DocModule)4 DocFunction (com.google.copybara.doc.DocBase.DocFunction)2 Ascii (com.google.common.base.Ascii)1 Joiner (com.google.common.base.Joiner)1 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 DocExample (com.google.copybara.doc.DocBase.DocExample)1 DocField (com.google.copybara.doc.DocBase.DocField)1 DocFlag (com.google.copybara.doc.DocBase.DocFlag)1 DocParam (com.google.copybara.doc.DocBase.DocParam)1 DocSignaturePrefix (com.google.copybara.doc.annotations.DocSignaturePrefix)1 Example (com.google.copybara.doc.annotations.Example)1 Library (com.google.copybara.doc.annotations.Library)1 ArrayList (java.util.ArrayList)1 Arrays.stream (java.util.Arrays.stream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors.joining (java.util.stream.Collectors.joining)1