package org.asciidoctor;


import static org.asciidoctor.OptionsBuilder.options;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Map;
import java.util.logging.Logger;


import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

@Named
public class AsciidoctorProcessor {

	private Asciidoctor asciidoctor;

	@Inject
	private Logger logger;

	@PostConstruct
	public void init() {
		asciidoctor = Asciidoctor.Factory.create();
	}

	public Path convertToHTML(final String inputFilename) throws URISyntaxException, IOException,
			InterruptedException {

		logger.info("converting adoc to html...");
		Map<String, Object> parameters;
		parameters = options()
				.backend("html5")
				.safe(SafeMode.UNSAFE)
				.headerFooter(true)
				.eruby("erubis")
				.attributes(
						AttributesBuilder.attributes().attribute("icons!", "")
								.attribute("allow-uri-read")
								.attribute("copycss!", "").asMap()).asMap();

        asciidoctor.convert(getResourceAsString(inputFilename),
                parameters(getOutputDir(), "sample.html", "html5"));
        return FileSystems.getDefault().getPath(getOutputDir() + "sample.html");

		//return asciidoctor.convert(getResourceAsString(inputFilename), parameters);

	}

	public Path convertToPDF(final String inputFilename) throws URISyntaxException, IOException,
			InterruptedException {
		logger.info("Converting adoc to PDF to" + getOutputDir());

		// sample adoc to pdf
		String outputFilename = "sample.pdf";
		asciidoctor.convert(getResourceAsString(inputFilename),
				parameters(getOutputDir(), outputFilename, "pdf"));
		Path out = FileSystems.getDefault().getPath(getOutputDir() + outputFilename);
		return out;
	}

	private Map<String, Object> parameters(String outputDir,
			String outputFilename, String backend) {
		return options().backend(backend).safe(SafeMode.UNSAFE)
				.headerFooter(true).inPlace(true)
				.toFile(new File(outputDir + outputFilename)).asMap();
	}


    public static final String getOutputDir() {
        return "/opt/jboss/documents/";
    }


	private String getResourceAsString(String path) {
		try {
			URL resource = Thread.currentThread().getContextClassLoader()
					.getResource(path);
			if (resource != null) {
				return readFromStream(Thread.currentThread()
						.getContextClassLoader().getResourceAsStream(path));
			} else {
				throw new RuntimeException(new FileNotFoundException(path));
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	private String readFromStream(final InputStream is) {
		if (is == null) {
			return "";
		}
		final char[] buffer = new char[1024];
		final StringBuilder out = new StringBuilder();
		try {
			final Reader in = new InputStreamReader(is, "UTF-8");
			try {
				for (;;) {
					int rsz = in.read(buffer, 0, buffer.length);
					if (rsz < 0)
						break;
					out.append(buffer, 0, rsz);
				}
			} finally {
				in.close();
			}
		} catch (UnsupportedEncodingException ex) {
			/* ... */
		} catch (IOException ex) {
			/* ... */
		}
		return out.toString();
	}


}

@@ -933,329 +933,8 @@
 .%22);
-%0A%09%09Map%3CString, Object%3E parameters;%0A%09%09parameters = options()%0A%09%09%09%09.backend(%22html5%22)%0A%09%09%09%09.safe(SafeMode.UNSAFE)%0A%09%09%09%09.headerFooter(true)%0A%09%09%09%09.eruby(%22erubis%22)%0A%09%09%09%09.attributes(%0A%09%09%09%09%09%09AttributesBuilder.attributes().attribute(%22icons!%22, %22%22)%0A%09%09%09%09%09%09%09%09.attribute(%22allow-uri-read%22)%0A%09%09%09%09%09%09%09%09.attribute(%22copycss!%22, %22%22).asMap()).asMap();
 %0A%0A  
@@ -1149,90 +1149,8 @@
 %22);%0A
-%0A%09%09//return asciidoctor.convert(getResourceAsString(inputFilename), parameters);%0A%0A
 %09%7D%0A%0A

clean code