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