001/*
002 * (C) Copyright 2016 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 *     Andre Justo
018 */
019
020package org.nuxeo.ecm.platform.preview.adapter;
021
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.Blobs;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.platform.preview.api.PreviewException;
026import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
027
028import java.io.IOException;
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * @since 8.2
034 */
035public class MarkdownPreviewer extends AbstractPreviewer implements MimeTypePreviewer {
036
037    @Override
038    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
039        List<Blob> blobResults = new ArrayList<>();
040        String basePath = VirtualHostHelper.getContextPathProperty();
041        StringBuffer html = new StringBuffer();
042        html.append("<html><head>");
043        html.append("<title>" + getPreviewTitle(dm) + "</title>");
044        html.append(String.format("<script src=\"%s/bower_components/webcomponentsjs/webcomponents-lite.js\"></script>", basePath));
045        html.append(String.format("<link rel=\"import\" href=\"%s/viewers/marked-element.vulcanized.html\">", basePath));
046        try {
047            html.append("<marked-element>");
048            html.append("<div class=\"markdown-html\"></div>");
049            html.append("<script type=\"text/markdown\">");
050            html.append(blob.getString());
051            html.append("</script>");
052            html.append("</marked-element>");
053            html.append("</body>");
054            Blob mainBlob = Blobs.createBlob(html.toString(), "text/html", null, "index.html");
055            blobResults.add(mainBlob);
056            return blobResults;
057        } catch (IOException e) {
058            throw new PreviewException(e);
059        }
060    }
061}