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.parser.markdown;
020
021import com.vladsch.flexmark.ext.attributes.AttributesExtension;
022import com.vladsch.flexmark.ext.definition.DefinitionExtension;
023import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
024import com.vladsch.flexmark.ext.tables.TablesExtension;
025import com.vladsch.flexmark.ext.toc.TocExtension;
026import com.vladsch.flexmark.html.HtmlRenderer;
027import com.vladsch.flexmark.parser.Parser;
028import com.vladsch.flexmark.parser.ParserEmulationProfile;
029import com.vladsch.flexmark.util.ast.Node;
030import com.vladsch.flexmark.util.data.MutableDataSet;
031import com.vladsch.flexmark.util.misc.Extension;
032import org.apache.oro.text.regex.Pattern;
033import org.apache.wiki.api.core.Context;
034import org.apache.wiki.api.core.Page;
035import org.apache.wiki.markdown.MarkdownForJSPWikiExtension;
036import org.apache.wiki.parser.MarkupParser;
037import org.apache.wiki.parser.WikiDocument;
038
039import java.util.Arrays;
040import java.util.List;
041
042
043/**
044 * Simple placeholder for Markdown Nodes
045 */
046public class MarkdownDocument extends WikiDocument {
047
048    private static final long serialVersionUID = 1L;
049
050    private final Node md;
051
052    public MarkdownDocument( final Page page, final Node md ) {
053        super( page );
054        this.md = md;
055    }
056
057    public Node getMarkdownNode() {
058        return md;
059    }
060
061    /**
062     * Configuration options for MarkdownRenderers.
063     *
064     * @param context current wiki context
065     * @return configuration options for MarkdownRenderers.
066     */
067    public static MutableDataSet options( final Context context, final boolean isImageInlining, final List< Pattern > inlineImagePatterns ) {
068        final MutableDataSet options = new MutableDataSet();
069        options.setFrom( ParserEmulationProfile.COMMONMARK );
070        options.set( AttributesExtension.ASSIGN_TEXT_ATTRIBUTES, true );
071        // align style of Markdown's footnotes extension with jspwiki footnotes refs
072        options.set( FootnoteExtension.FOOTNOTE_LINK_REF_CLASS, MarkupParser.CLASS_FOOTNOTE_REF );
073        options.set( HtmlRenderer.ESCAPE_HTML, !context.getBooleanWikiProperty( MarkupParser.PROP_ALLOWHTML, false ) );
074        options.set( Parser.EXTENSIONS, Arrays.asList( new Extension[] { new MarkdownForJSPWikiExtension( context, isImageInlining, inlineImagePatterns ),
075                                                                         AttributesExtension.create(),
076                                                                         DefinitionExtension.create(),
077                                                                         FootnoteExtension.create(),
078                                                                         TablesExtension.create(),
079                                                                         TocExtension.create() } ) );
080        return options;
081    }
082
083}