Java tutorial
/** * Copyright (c) 2011 Json Shen, http://www.shenjia.org/ * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package org.shenjia.maven.markdown; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.nio.charset.Charset; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.parboiled.common.FileUtils; import org.pegdown.Extensions; import org.pegdown.PegDownProcessor; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; /** * Markdown maven plugin mojo * * @author json.shen@gmail.com (Json Shen) * @goal pdf * @phase process-sources */ public class MarkdownToPdfMojo extends MarkdownMojo { public void execute() throws MojoExecutionException, MojoFailureException { if (!outputDirectory.exists()) { outputDirectory.mkdirs(); } // ?markdown processMarkdown(sourceDirectory, new PegDownProcessor(Extensions.ALL)); } private void processMarkdown(File file, PegDownProcessor processor) { // markdown int srcDirPathLen = sourceDirectory.getPath().length(); File[] files = file.listFiles(createFileFilter()); for (File markdown : files) { if (markdown.isDirectory()) { processMarkdown(markdown, processor); } else { BufferedReader in = null; BufferedWriter out = null; try { // File outDir = outputDirectory; String subPath = markdown.getParentFile().getPath().substring(srcDirPathLen); if (subPath.length() > 0) { outDir = new File(outputDirectory, subPath); } if (!outDir.exists()) { outDir.mkdirs(); } // ?? String pdfName = markdown.getName().replaceAll("\\.md", ".pdf"); // html File pdf = new File(outDir, pdfName); getLog().info("MARKDOWN -> " + markdown.getPath() + " to PDF -> " + pdf.getPath()); // markdown chars char[] chars = FileUtils.readAllChars(markdown); String content = processor.markdownToHtml(chars); String doc = new StringBuffer(getHtmlHeader()).append(content).append(getHtmlFooter()) .toString(); getLog().debug("HTML -> " + doc); html2Pdf(new ByteArrayInputStream(doc.getBytes()), pdf); } catch (Exception e) { getLog().error(e.getMessage(), e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } } /** * HTML?PDF * * @throws Exception */ private void html2Pdf(InputStream htmlIn, File pdfFile) throws Exception { Document doc = new Document(); PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(pdfFile)); doc.open(); XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, doc, htmlIn, Charset.forName(charset)); doc.close(); } }