001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Quentin Lamerand <qlamerand@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.platform.convert.plugins;
019
020import java.io.IOException;
021import java.io.Serializable;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.Blobs;
026import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
027import org.nuxeo.ecm.core.convert.api.ConversionException;
028import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
029import org.nuxeo.ecm.core.convert.extension.Converter;
030import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
031
032import com.cforcoding.jmd.MarkDownParserAndSanitizer;
033
034/**
035 * Markdown to HTML converter
036 *
037 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a>
038 * @since 5.5
039 */
040public class Md2HtmlConverter implements Converter {
041
042    private static final String BODY_CONTENT_ONLY = "bodyContentOnly";
043
044    private ConverterDescriptor descriptor;
045
046    @Override
047    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
048
049        Boolean bodyContentOnly = Boolean.FALSE;
050        if (parameters != null) {
051            bodyContentOnly = (Boolean) parameters.get(BODY_CONTENT_ONLY);
052        }
053        if (bodyContentOnly == null) {
054            bodyContentOnly = Boolean.FALSE;
055        }
056        try {
057            Blob inputBlob = blobHolder.getBlob();
058            String mdString = inputBlob.getString();
059            MarkDownParserAndSanitizer md = new MarkDownParserAndSanitizer();
060            StringBuilder html = new StringBuilder();
061            if (!bodyContentOnly) {
062                html.append("<html><head></head><body>");
063            }
064            html.append(md.transform(mdString));
065            if (!bodyContentOnly) {
066                html.append("</body></html>");
067            }
068            Blob outputBlob = Blobs.createBlob(html.toString(), descriptor.getDestinationMimeType());
069            String filename = inputBlob.getFilename();
070            if (filename != null) {
071                int dotPosition = filename.lastIndexOf('.');
072                if (dotPosition > -1) {
073                    filename = filename.substring(0, dotPosition) + ".html";
074                }
075                outputBlob.setFilename(filename);
076            }
077            return new SimpleCachableBlobHolder(outputBlob);
078        } catch (IOException e) {
079            throw new ConversionException("Could not get Markdown string from BlobHolder", e);
080        }
081    }
082
083    @Override
084    public void init(ConverterDescriptor descriptor) {
085        this.descriptor = descriptor;
086    }
087
088}