001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Quentin Lamerand <[email protected]> 018 */ 019 020package org.nuxeo.ecm.platform.convert.plugins; 021 022import java.io.IOException; 023import java.io.Serializable; 024import java.util.Map; 025 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.Blobs; 028import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 029import org.nuxeo.ecm.core.convert.api.ConversionException; 030import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 031import org.nuxeo.ecm.core.convert.extension.Converter; 032import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 033 034import com.cforcoding.jmd.MarkDownParserAndSanitizer; 035 036/** 037 * Markdown to HTML converter 038 * 039 * @author <a href="mailto:[email protected]">Quentin Lamerand</a> 040 * @since 5.5 041 */ 042public class Md2HtmlConverter implements Converter { 043 044 private static final String BODY_CONTENT_ONLY = "bodyContentOnly"; 045 046 private ConverterDescriptor descriptor; 047 048 @Override 049 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 050 051 Boolean bodyContentOnly = Boolean.FALSE; 052 if (parameters != null) { 053 bodyContentOnly = (Boolean) parameters.get(BODY_CONTENT_ONLY); 054 } 055 if (bodyContentOnly == null) { 056 bodyContentOnly = Boolean.FALSE; 057 } 058 try { 059 Blob inputBlob = blobHolder.getBlob(); 060 String mdString = inputBlob.getString(); 061 MarkDownParserAndSanitizer md = new MarkDownParserAndSanitizer(); 062 StringBuilder html = new StringBuilder(); 063 if (!bodyContentOnly) { 064 html.append("<html><head></head><body>"); 065 } 066 html.append(md.transform(mdString)); 067 if (!bodyContentOnly) { 068 html.append("</body></html>"); 069 } 070 Blob outputBlob = Blobs.createBlob(html.toString(), descriptor.getDestinationMimeType()); 071 String filename = inputBlob.getFilename(); 072 if (filename != null) { 073 int dotPosition = filename.lastIndexOf('.'); 074 if (dotPosition > -1) { 075 filename = filename.substring(0, dotPosition) + ".html"; 076 } 077 outputBlob.setFilename(filename); 078 } 079 return new SimpleCachableBlobHolder(outputBlob); 080 } catch (IOException e) { 081 throw new ConversionException("Could not get Markdown string from BlobHolder", e); 082 } 083 } 084 085 @Override 086 public void init(ConverterDescriptor descriptor) { 087 this.descriptor = descriptor; 088 } 089 090}