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.markdown.extensions.attributesanitizer.AttributeSanitizerExtension;
037import org.apache.wiki.parser.MarkupParser;
038import org.apache.wiki.parser.WikiDocument;
039
040import java.util.Arrays;
041import java.util.List;
042
043
044/**
045 * Simple placeholder for Markdown Nodes
046 */
047public class MarkdownDocument extends WikiDocument {
048
049    private static final long serialVersionUID = 1L;
050
051    private final Node md;
052
053    public MarkdownDocument( final Page page, final Node md ) {
054        super( page );
055        this.md = md;
056    }
057
058    public Node getMarkdownNode() {
059        return md;
060    }
061
062    /**
063     * Configuration options for MarkdownRenderers.
064     *
065     * @param context current wiki context
066     * @return configuration options for MarkdownRenderers.
067     */
068    public static MutableDataSet options( final Context context, final boolean isImageInlining, final List< Pattern > inlineImagePatterns ) {
069        final MutableDataSet options = new MutableDataSet();
070        options.setFrom( ParserEmulationProfile.COMMONMARK );
071        options.set( AttributesExtension.ASSIGN_TEXT_ATTRIBUTES, true );
072        // align style of Markdown's footnotes extension with jspwiki footnotes refs
073        options.set( FootnoteExtension.FOOTNOTE_LINK_REF_CLASS, MarkupParser.CLASS_FOOTNOTE_REF );
074        options.set( HtmlRenderer.ESCAPE_HTML, !context.getBooleanWikiProperty( MarkupParser.PROP_ALLOWHTML, false ) );
075        options.set( Parser.EXTENSIONS, Arrays.asList( new Extension[] { new MarkdownForJSPWikiExtension( context, isImageInlining, inlineImagePatterns ),
076                                                                         AttributesExtension.create(),
077                                                                         DefinitionExtension.create(),
078                                                                         FootnoteExtension.create(),
079                                                                         TablesExtension.create(),
080                                                                         TocExtension.create(),
081                                                                         // must be registered last, so its attribute provider sees (and can strip)
082                                                                         // the author-supplied attributes assigned by AttributesExtension
083                                                                         AttributeSanitizerExtension.create() } ) );
084        return options;
085    }
086
087}