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

hr.fer.tel.markdown.MdToHtmlConverter Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
package hr.fer.tel.markdown;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;

public class MdToHtmlConverter {

  private Reader markdownReader;

  public MdToHtmlConverter(Path markdownPath) throws IOException {
    this(Files.newBufferedReader(markdownPath, StandardCharsets.UTF_8));
  }

  public MdToHtmlConverter(String markdownContent) {
    this(new StringReader(markdownContent));
  }

  public MdToHtmlConverter(Reader markdownReader) {
    this.markdownReader = markdownReader;
  }

  public String convert() throws IOException {
    MutableDataSet options = new MutableDataSet();
    options.set(Parser.EXTENSIONS, List.of(PlantUmlRendererOptions.PlantUmlExtension.create()));

    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();

    Node document = parser.parseReader(markdownReader);
    String html = renderer.render(document);

    StringBuilder sb = new StringBuilder();
    sb.append(createPrefix());
    sb.append(html.replaceAll("
", "
"));
    sb.append(createSuffix());

    return sb.toString();
  }

  private String createSuffix() {
    return createJsLinks() + """
            
            
            """;
  }

  private String createJsLinks() {
    return """
             
           """;
  }

  private String createPrefix() {
    String styles = """
        
      """;
    return """
            
            
            
              
           """ +
           createCssLinks() +
           styles +
            """
            
            
            """;
  }

  private String createCssLinks() {
    return """
             
           """;
  }

  public void convertAndSave(Path htmlPath) throws IOException {
    Path resourcesPath = htmlPath.getParent().resolve("res");
    Files.createDirectories(resourcesPath);
    copyResources(resourcesPath);

    String htmlString = convert();
    Files.writeString(htmlPath, htmlString);
  }

  private void copyResources(Path resourcesPath) throws IOException {
    Class class1 = getClass();
    InputStream prismJs = class1.getResourceAsStream("/prism/prism.js");
    OutputStream destStream = Files.newOutputStream(resourcesPath.resolve("prism.js"));
    prismJs.transferTo(destStream);

    InputStream prismCss = class1.getResourceAsStream("/prism/prism.css");
    destStream = Files.newOutputStream(resourcesPath.resolve("prism.css"));
    prismCss.transferTo(destStream);
  }

  public static void main(String[] args) throws Exception {
    Path markdownPath = Paths.get("src", "test", "resources", "withPlantUml", "main.md");
    MdToHtmlConverter converter = new MdToHtmlConverter(markdownPath);
    converter.convertAndSave(Paths.get("build", "main.html"));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy