    001/*
    002    Licensed to the Apache Software Foundation (ASF) under one
    003    or more contributor license agreements.  See the NOTICE file
    004    distributed with this work for additional information
    005    regarding copyright ownership.  The ASF licenses this file
    006    to you under the Apache License, Version 2.0 (the
    007    "License"); you may not use this file except in compliance
    008    with the License.  You may obtain a copy of the License at
    009
    010       http://www.apache.org/licenses/LICENSE-2.0
    011
    012    Unless required by applicable law or agreed to in writing,
    013    software distributed under the License is distributed on an
    014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015    KIND, either express or implied.  See the License for the
    016    specific language governing permissions and limitations
    017    under the License.
    018 */
    019package org.apache.wiki.render.markdown;
    020
    021import com.vladsch.flexmark.html.HtmlRenderer;
    022import org.apache.commons.lang3.StringUtils;
    023import org.apache.oro.text.regex.Pattern;
    024import org.apache.wiki.api.core.Context;
    025import org.apache.wiki.parser.MarkupParser;
    026import org.apache.wiki.parser.WikiDocument;
    027import org.apache.wiki.parser.markdown.MarkdownDocument;
    028import org.apache.wiki.render.RenderingManager;
    029import org.apache.wiki.render.WikiRenderer;
    030
    031import java.io.IOException;
    032import java.util.List;
    033
    034
    035/**
    036 * Class handling the markdown rendering.
    037 */
    038public class MarkdownRenderer extends WikiRenderer {
    039
    040    private final HtmlRenderer renderer;
    041
    042    public MarkdownRenderer( final Context context, final WikiDocument doc ) {
    043        super( context, doc );
    044        final MarkupParser mp = context.getEngine()
    045                                       .getManager( RenderingManager.class )
    046                                       .getParser( context, StringUtils.defaultString( doc.getPageData() ) );
    047        final boolean isImageInlining = mp.isImageInlining();
    048        final List< Pattern > inlineImagePatterns = mp.getInlineImagePatterns();
    049        renderer = HtmlRenderer.builder( MarkdownDocument.options( context, isImageInlining, inlineImagePatterns ) ).build();
    050    }
    051
    052    /**
    053     * {@inheritDoc}
    054     */
    055    @Override
    056    public String getString() throws IOException {
    057        m_document.setContext( m_context );
    058        if( m_document instanceof MarkdownDocument ) {
    059            return renderer.render( ( ( MarkdownDocument )m_document ).getMarkdownNode() );
    060        } else {
    061            throw new IOException( "MarkdownRenderer requires to be used with MarkdownParser" );
    062        }
    063    }
    064
    065}