All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.nb.api.markdown.aggregator.ParsedMarkdown Maven / Gradle / Ivy

Go to download

The top level API module for NoSQLBench. This module should have no internal module dependencies other than the mvn-default module. All modules within NoSQLBench can safely depend on this module with circular dependencies. This module provides cross-cutting code infrastracture, such as path utilities and ways of describing services used between modules. It is also the transitive aggregation point for system-wide library dependencies for logging and testing or similar needs.

There is a newer version: 5.17.0
Show newest version
package io.nosqlbench.nb.api.markdown.aggregator;

import com.vladsch.flexmark.ext.yaml.front.matter.AbstractYamlFrontMatterVisitor;
import com.vladsch.flexmark.util.ast.Document;
import io.nosqlbench.nb.api.content.Content;
import io.nosqlbench.nb.api.markdown.FlexParser;
import io.nosqlbench.nb.api.markdown.types.FrontMatterInfo;
import io.nosqlbench.nb.api.markdown.types.HasDiagnostics;
import io.nosqlbench.nb.api.markdown.types.MarkdownInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.file.Path;
import java.util.*;

/**
 * TODO: Make this a value type
 */
public class ParsedMarkdown implements MarkdownInfo, HasDiagnostics {
    private final static Logger logger = LogManager.getLogger(MarkdownDocs.class);

    private final ParsedFrontMatter frontMatter;
    private final Content content;

    public ParsedMarkdown(Content content) {
        String rawMarkdown = content.asString();
        AbstractYamlFrontMatterVisitor v = new AbstractYamlFrontMatterVisitor();
        Document parsed = FlexParser.parser.parse(rawMarkdown);
        v.visit(parsed);
        Map> data = v.getData();
        frontMatter = new ParsedFrontMatter(data);
        this.content = content;
        logger.debug("created " + this.toString());
    }

    private ParsedMarkdown(ParsedFrontMatter frontMatter, Content content) {
        this.frontMatter = frontMatter;
        this.content = content;
    }

    @Override
    public Path getPath() {
        return content.asPath();
    }

    @Override
    public String getBody() {
        return null;
    }

    @Override
    public FrontMatterInfo getFrontmatter() {
        return frontMatter;
    }

    /**
     * Get a list of diagnostic warnings that might help users know of issues in their
     * markdown content before publication.
     * @param buffer A buffer object, for accumulating many lines of detail, if necessary.
     * @return The buffer, with possible additions
     */
    @Override
    public List getDiagnostics(List buffer) {
        List diagnostics = frontMatter.getDiagnostics();
        if (diagnostics.size()==0) {
            return List.of();
        }
        String[] diags = diagnostics.stream().map(s -> " " + s).toArray(String[]::new);
        buffer.add("found " + diagnostics.size() + " diagnostics for " + getPath().toString());
        buffer.addAll(Arrays.asList(diags));
        return buffer;
    }

    /**
     * The buffer-less version of {@link #getDiagnostics(List)}
     * @return a list of diagnostics lines, zero if there are none
     */
    @Override
    public List getDiagnostics() {
        return getDiagnostics(new ArrayList<>());
    }

    @Override
    public boolean hasAggregations() {
        return getFrontmatter().getAggregations().size()>0;
    }

    @Override
    public MarkdownInfo withTopics(List assigning) {
        return new ParsedMarkdown(frontMatter.withTopics(assigning), this.content);
    }

    public MarkdownInfo withIncluded(List included) {
        return new ParsedMarkdown(frontMatter.withIncluded(included), this.content);
    }

    @Override
    public String toString() {
        return "ParsedMarkdown/" +
            frontMatter.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ParsedMarkdown that = (ParsedMarkdown) o;
        return Objects.equals(frontMatter, that.frontMatter) &&
                Objects.equals(content, that.content);
    }

    @Override
    public int hashCode() {
        return Objects.hash(frontMatter, content);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy