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.markdown.renderer;
020
021import com.vladsch.flexmark.html.HtmlWriter;
022import com.vladsch.flexmark.html.renderer.LinkType;
023import com.vladsch.flexmark.html.renderer.NodeRenderer;
024import com.vladsch.flexmark.html.renderer.NodeRendererContext;
025import com.vladsch.flexmark.html.renderer.NodeRenderingHandler;
026import com.vladsch.flexmark.html.renderer.ResolvedLink;
027import org.apache.wiki.markdown.extensions.jspwikilinks.postprocessor.WikiHtmlInline;
028import org.apache.wiki.markdown.nodes.JSPWikiLink;
029
030import java.util.HashSet;
031import java.util.Set;
032
033
034/**
035 * Flexmark {@link NodeRenderer} for {@link JSPWikiLink}s.
036 */
037public class JSPWikiLinkRenderer implements NodeRenderer {
038
039    /**
040     * {@inheritDoc}
041     *
042     * @see com.vladsch.flexmark.html.renderer.NodeRenderer#getNodeRenderingHandlers()
043     */
044    @Override
045    public Set< NodeRenderingHandler< ? > > getNodeRenderingHandlers() {
046        final HashSet< NodeRenderingHandler< ? > > set = new HashSet<>();
047        set.add( new NodeRenderingHandler<>( JSPWikiLink.class, new NodeRenderingHandler.CustomNodeRenderer<>() {
048
049            /**
050             * {@inheritDoc}
051             */
052            @Override
053            public void render( final JSPWikiLink node, final NodeRendererContext context, final HtmlWriter html ) {
054                if (context.isDoNotRenderLinks()) {
055                    context.renderChildren(node);
056                } else {
057                    // standard Link Rendering
058                    final ResolvedLink resolvedLink = context.resolveLink(LinkType.LINK, node.getUrl().unescape(), null);
059
060                    html.attr("href", resolvedLink.getUrl());
061                    if (node.getTitle().isNotNull()) {
062                        html.attr("title", node.getTitle().unescape());
063                    }
064                    html.srcPos(node.getChars()).withAttr(resolvedLink).tag("a");
065                    context.renderChildren(node);
066                    html.tag("/a");
067                }
068            }
069        } ) );
070        set.add( new NodeRenderingHandler<>( WikiHtmlInline.class, new NodeRenderingHandler.CustomNodeRenderer<>() {
071
072            /**
073             * {@inheritDoc}
074             */
075            @Override
076            public void render( final WikiHtmlInline node, final NodeRendererContext context, final HtmlWriter html ) {
077                html.raw( node.getChars().normalizeEOL() );
078            }
079        } ) );
080        return set;
081    }
082
083}