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