Search in sources :

Example 1 with StaticCaption

use of org.ballerinalang.docgen.model.StaticCaption in project ballerina by ballerina-lang.

the class WriterTest method testWriterWithInvalidPath.

@Test(description = "Test writer with invalid output path.")
public void testWriterWithInvalidPath() {
    URL location = Writer.class.getProtectionDomain().getCodeSource().getLocation();
    String outputPath = location.getPath();
    Writer.writeHtmlDocument(new Page(new StaticCaption(""), new ArrayList<>(), new ArrayList<>()), "page", outputPath);
    Assert.assertTrue(outContent.toString().contains("docerina: could not write HTML file"), "An error was expected");
}
Also used : ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) URL(java.net.URL) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) Test(org.testng.annotations.Test)

Example 2 with StaticCaption

use of org.ballerinalang.docgen.model.StaticCaption in project ballerina by ballerina-lang.

the class WriterTest method testWriter.

@Test(description = "Test if writer class is working.")
public void testWriter() {
    URL location = Writer.class.getProtectionDomain().getCodeSource().getLocation();
    String outputPath = location.getPath() + File.separator + "sample.html";
    Writer.writeHtmlDocument(new Page(new StaticCaption(""), new ArrayList<>(), new ArrayList<>()), "page", outputPath);
}
Also used : ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) URL(java.net.URL) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) Test(org.testng.annotations.Test)

Example 3 with StaticCaption

use of org.ballerinalang.docgen.model.StaticCaption in project ballerina by ballerina-lang.

the class Generator method generatePageForPrimitives.

/**
 * Generate the page for primitive types.
 * @param balPackage The ballerina.builtin package.
 * @param packages List of available packages.
 * @return A page model for the primitive types.
 */
public static Page generatePageForPrimitives(BLangPackage balPackage, List<Link> packages) {
    ArrayList<Documentable> primitiveTypes = new ArrayList<>();
    // Check for functions in the package
    if (balPackage.getFunctions().size() > 0) {
        for (BLangFunction function : balPackage.getFunctions()) {
            if (function.getFlags().contains(Flag.PUBLIC) && function.getReceiver() != null) {
                TypeNode langType = function.getReceiver().getTypeNode();
                if (!(langType instanceof BLangUserDefinedType)) {
                    // Check for primitives in ballerina.builtin
                    Optional<PrimitiveTypeDoc> existingPrimitiveType = primitiveTypes.stream().filter((doc) -> doc instanceof PrimitiveTypeDoc && (((PrimitiveTypeDoc) doc)).name.equals(langType.toString())).map(doc -> (PrimitiveTypeDoc) doc).findFirst();
                    PrimitiveTypeDoc primitiveTypeDoc;
                    if (existingPrimitiveType.isPresent()) {
                        primitiveTypeDoc = existingPrimitiveType.get();
                    } else {
                        primitiveTypeDoc = new PrimitiveTypeDoc(langType.toString(), new ArrayList<>());
                        primitiveTypes.add(primitiveTypeDoc);
                    }
                    primitiveTypeDoc.children.add(createDocForNode(function));
                }
            }
        }
    }
    // Create the links to select which page or package is active
    List<Link> links = new ArrayList<>();
    for (Link pkgLink : packages) {
        if (BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME.equals(pkgLink.caption.value)) {
            links.add(new Link(pkgLink.caption, pkgLink.href, true));
        } else {
            links.add(new Link(pkgLink.caption, pkgLink.href, false));
        }
    }
    StaticCaption primitivesPageHeading = new StaticCaption(BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME);
    return new Page(primitivesPageHeading, primitiveTypes, links);
}
Also used : AnnotationDoc(org.ballerinalang.docgen.model.AnnotationDoc) AnnotatableNode(org.ballerinalang.model.tree.AnnotatableNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) Link(org.ballerinalang.docgen.model.Link) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) Field(org.ballerinalang.docgen.model.Field) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Flag(org.ballerinalang.model.elements.Flag) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) EnumNode(org.ballerinalang.model.tree.EnumNode) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) ConnectorDoc(org.ballerinalang.docgen.model.ConnectorDoc) EnumDoc(org.ballerinalang.docgen.model.EnumDoc) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) ActionDoc(org.ballerinalang.docgen.model.ActionDoc) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) Variable(org.ballerinalang.docgen.model.Variable) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) PackageName(org.ballerinalang.docgen.model.PackageName) Optional(java.util.Optional) Documentable(org.ballerinalang.docgen.model.Documentable) GlobalVariableDoc(org.ballerinalang.docgen.model.GlobalVariableDoc) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BallerinaDocConstants(org.ballerinalang.docgen.docs.BallerinaDocConstants) PrimitiveTypeDoc(org.ballerinalang.docgen.model.PrimitiveTypeDoc) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangType(org.wso2.ballerinalang.compiler.tree.types.BLangType) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) Documentable(org.ballerinalang.docgen.model.Documentable) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) PrimitiveTypeDoc(org.ballerinalang.docgen.model.PrimitiveTypeDoc) Link(org.ballerinalang.docgen.model.Link)

Example 4 with StaticCaption

use of org.ballerinalang.docgen.model.StaticCaption in project ballerina by ballerina-lang.

the class BallerinaDocGenerator method generateApiDocs.

/**
 * API to generate Ballerina API documentation.
 *
 * @param output        path to the output directory where the API documentation will be written to.
 * @param packageFilter comma separated list of package names to be filtered from the documentation.
 * @param isNative      whether the given packages are native or not.
 * @param sources       either the path to the directories where Ballerina source files reside or a
 *                      path to a Ballerina file which does not belong to a package.
 */
public static void generateApiDocs(String output, String packageFilter, boolean isNative, String... sources) {
    out.println("docerina: API documentation generation for sources - " + Arrays.toString(sources));
    for (String source : sources) {
        source = source.trim();
        try {
            Map<String, BLangPackage> docsMap;
            if (source.endsWith(".bal")) {
                Path sourceFilePath = Paths.get(source);
                Path parentDir = sourceFilePath.getParent();
                Path fileName = sourceFilePath.getFileName();
                if (fileName == null) {
                    log.warn("Skipping the source generation for invalid path: " + sourceFilePath);
                    continue;
                }
                if (parentDir == null) {
                    parentDir = Paths.get(".");
                }
                docsMap = generatePackageDocsFromBallerina(parentDir.toString(), fileName, packageFilter, isNative);
            } else {
                Path dirPath = Paths.get(source);
                // TODO fix this properly
                // TODO Temporary fix that creates .ballerina to create project structure
                Path projectFolder = dirPath.resolve(ProjectDirConstants.DOT_BALLERINA_DIR_NAME);
                Files.createDirectory(projectFolder);
                Path sourceRootPath = LauncherUtils.getSourceRootPath(dirPath.toString());
                docsMap = generatePackageDocsFromBallerina(sourceRootPath.toString(), dirPath, packageFilter, isNative);
            }
            if (docsMap.size() == 0) {
                out.println("docerina: no package definitions found!");
                return;
            }
            if (BallerinaDocUtils.isDebugEnabled()) {
                out.println("Generating HTML API documentation...");
            }
            String userDir = System.getProperty("user.dir");
            // If output directory is empty
            if (output == null) {
                output = System.getProperty(BallerinaDocConstants.HTML_OUTPUT_PATH_KEY, userDir + File.separator + "api-docs" + File.separator + "html");
            }
            // Create output directories
            Files.createDirectories(Paths.get(output));
            // Sort packages by package path
            List<BLangPackage> packageList = new ArrayList<>(docsMap.values());
            packageList.sort(Comparator.comparing(pkg -> pkg.packageID.toString()));
            // Iterate over the packages to generate the pages
            List<String> packageNames = new ArrayList<>(docsMap.keySet());
            // Sort the package names
            Collections.sort(packageNames);
            List<Link> packageNameList = PackageName.convertList(packageNames);
            if (packageNames.contains("ballerina.builtin")) {
                StaticCaption primitivesLinkName = new StaticCaption(BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME);
                packageNameList.add(0, new Link(primitivesLinkName, BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_HREF, false));
            }
            // Generate pages for the packages
            String packageTemplateName = System.getProperty(BallerinaDocConstants.PACKAGE_TEMPLATE_NAME_KEY, "page");
            for (BLangPackage bLangPackage : packageList) {
                // Sort functions, connectors, structs, type mappers and annotationDefs
                bLangPackage.getFunctions().sort(Comparator.comparing(f -> (f.getReceiver() == null ? "" : f.getReceiver().getName()) + f.getName().getValue()));
                bLangPackage.getConnectors().sort(Comparator.comparing(c -> c.getName().getValue()));
                bLangPackage.getStructs().sort(Comparator.comparing(s -> s.getName().getValue()));
                bLangPackage.getAnnotations().sort(Comparator.comparing(a -> a.getName().getValue()));
                bLangPackage.getEnums().sort(Comparator.comparing(a -> a.getName().getValue()));
                // Sort connector actions
                if ((bLangPackage.getConnectors() != null) && (bLangPackage.getConnectors().size() > 0)) {
                    bLangPackage.getConnectors().forEach(connector -> connector.getActions().sort(Comparator.comparing(a -> a.getName().getValue())));
                }
                String packagePath = refinePackagePath(bLangPackage);
                Page page = Generator.generatePage(bLangPackage, packageNameList);
                String filePath = output + File.separator + packagePath + HTML;
                Writer.writeHtmlDocument(page, packageTemplateName, filePath);
                if ("ballerina.builtin".equals(packagePath)) {
                    Page primitivesPage = Generator.generatePageForPrimitives(bLangPackage, packageNameList);
                    String primitivesFilePath = output + File.separator + "primitive-types" + HTML;
                    Writer.writeHtmlDocument(primitivesPage, packageTemplateName, primitivesFilePath);
                }
            }
            // Generate the index file with the list of all packages
            String indexTemplateName = System.getProperty(BallerinaDocConstants.PACKAGE_TEMPLATE_NAME_KEY, "index");
            String indexFilePath = output + File.separator + "index" + HTML;
            Writer.writeHtmlDocument(packageNameList, indexTemplateName, indexFilePath);
            if (BallerinaDocUtils.isDebugEnabled()) {
                out.println("Copying HTML theme...");
            }
            BallerinaDocUtils.copyResources("docerina-theme", output);
        } catch (IOException e) {
            out.println(String.format("docerina: API documentation generation failed for %s: %s", source, e.getMessage()));
            log.error(String.format("API documentation generation failed for %s", source), e);
        }
    }
    try {
        String zipPath = System.getProperty(BallerinaDocConstants.OUTPUT_ZIP_PATH);
        if (zipPath != null) {
            BallerinaDocUtils.packageToZipFile(output, zipPath);
        }
    } catch (IOException e) {
        out.println(String.format("docerina: API documentation zip packaging failed for %s: %s", output, e.getMessage()));
        log.error(String.format("API documentation zip packaging failed for %s", output), e);
    }
}
Also used : Path(java.nio.file.Path) ProjectDirConstants(org.wso2.ballerinalang.compiler.util.ProjectDirConstants) Arrays(java.util.Arrays) Link(org.ballerinalang.docgen.model.Link) Generator(org.ballerinalang.docgen.Generator) LoggerFactory(org.slf4j.LoggerFactory) Compiler(org.wso2.ballerinalang.compiler.Compiler) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Writer(org.ballerinalang.docgen.Writer) Map(java.util.Map) Names(org.wso2.ballerinalang.compiler.util.Names) Path(java.nio.file.Path) LauncherUtils(org.ballerinalang.launcher.LauncherUtils) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Files(java.nio.file.Files) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CodeAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer) File(java.io.File) SemanticAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer) FileVisitResult(java.nio.file.FileVisitResult) BallerinaDocUtils(org.ballerinalang.docgen.docs.utils.BallerinaDocUtils) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) List(java.util.List) Paths(java.nio.file.Paths) StringJoiner(java.util.StringJoiner) PackageName(org.ballerinalang.docgen.model.PackageName) CompilerOptionName(org.ballerinalang.compiler.CompilerOptionName) Comparator(java.util.Comparator) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) IOException(java.io.IOException) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Link(org.ballerinalang.docgen.model.Link)

Example 5 with StaticCaption

use of org.ballerinalang.docgen.model.StaticCaption in project ballerina by ballerina-lang.

the class HtmlDocTest method testPrimitiveConstructsWithFunctions.

@Test(description = "Testing primitive constructs.")
public void testPrimitiveConstructsWithFunctions() {
    BLangPackage bLangPackage = createPackage("package ballerina.builtin;" + "public native function <blob b> data (string encoding) returns" + "(string);" + "public native function <blob b> sample () returns (string);");
    List<Link> packages = new ArrayList<>();
    packages.add(new Link(new PackageName((bLangPackage.symbol).pkgID.name.value, ""), "", false));
    packages.add(new Link(new StaticCaption(BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME), BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_HREF, false));
    Page primitivesPage = Generator.generatePageForPrimitives(bLangPackage, packages);
    Assert.assertEquals(primitivesPage.constructs.size(), 1);
    Assert.assertEquals(primitivesPage.constructs.get(0).children.size(), 2);
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) PackageName(org.ballerinalang.docgen.model.PackageName) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Link(org.ballerinalang.docgen.model.Link) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)5 Page (org.ballerinalang.docgen.model.Page)5 StaticCaption (org.ballerinalang.docgen.model.StaticCaption)5 Link (org.ballerinalang.docgen.model.Link)3 PackageName (org.ballerinalang.docgen.model.PackageName)3 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 URL (java.net.URL)2 List (java.util.List)2 Test (org.testng.annotations.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 FileVisitResult (java.nio.file.FileVisitResult)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1