asciidoc 转换为 html

将输出目录的 asciicdoc 转换为 html

pom.xml

<dependency>
	    <groupId>org.asciidoctor</groupId>
	    <artifactId>asciidoctorj</artifactId>
	    <version>2.0.0-RC.3</version>
</dependency>

AsciidocHtml.java

package com.scd.asciidoctorjhtml;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Asciidoctor.Factory;

import com.scd.util.FileUtil;

/**
 * @author chengdu
 *
 */
public class AsciidocHtml {
	
	public static List<String> getAdocListPath(File dirFile){
		List<String> adocPaths = new ArrayList<>(10);
		String[] filePaths = dirFile.list();
		for(String fName : filePaths){
			int dotIndex = fName.indexOf(".");
			if( dotIndex != -1){
				String ftype = fName.substring(dotIndex + 1);
				if("adoc".equals(ftype)){
					adocPaths.add(dirFile.getAbsolutePath() + File.separator + fName);
				}
			}
		}
		return adocPaths;
	}
	
	public static void convertToHtml() throws Exception {
		String basePath = "D:/SoftWare/Mars/workspace/swaggerdoc/target/asciidoc/generated";
		Asciidoctor asciidoctor = Factory.create();
		String filec = FileUtil.readFileAll(basePath + "paths.adoc", StandardCharsets.UTF_8.name());
		String html = asciidoctor.convert(filec, new HashMap<String, Object>());
		System.out.println(html);
	}

	public static void main(String[] args) throws Exception {
		Asciidoctor asciidoctor = Factory.create();
		String basePath = "D:/SoftWare/Mars/workspace/swaggerdoc/target/asciidoc/generated";
		File file = new File(basePath);
		if(file.isFile()){
			System.out.println("error file" + basePath + ", enter dir");
			return ;
		}
		Map<String, Object> options = new HashMap<String, Object>();
		List<String> paths = getAdocListPath(file);
		for(String path : paths){
			File f = new File(path);
			String adocName = f.getName();
			String htmlFName = basePath + File.separator + 
					adocName.substring(0, adocName.length() - 4) + "html";
			FileUtil.createNewDirFile(htmlFName);
			String asciidocstr = FileUtil.readFileAll(path, StandardCharsets.UTF_8.name());
			String html = asciidoctor.convert(asciidocstr, options);
			FileUtil.writeStrtoFile(htmlFName, html, false);
		}
	}

}