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.extensions.jspwikilinks.attributeprovider;
020
021import com.vladsch.flexmark.util.ast.Node;
022import org.apache.wiki.WikiContext;
023import org.apache.wiki.htmltowiki.XHtmlToWikiConfig;
024import org.apache.wiki.markdown.nodes.JSPWikiLink;
025import org.apache.wiki.render.RenderingManager;
026import org.apache.wiki.render.WikiRenderer;
027
028import com.vladsch.flexmark.util.html.Attributes;
029
030
031/**
032 * {@link NodeAttributeProviderState} to finish up polishing WYSIWYG editing mode. More or less equivalent to WysiwygEditingRenderer, the main difference
033 * being that in here there isn't any node removal, those nodes are simply not inserted elsewhere if WYSIWYG editing is detected.
034 */
035public class WysiwygEditingAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
036
037    private final WikiContext wikiContext;
038    private final boolean m_wysiwygEditorMode;
039
040    public WysiwygEditingAttributeProviderState( final WikiContext wikiContext ) {
041        this.wikiContext = wikiContext;
042        final Boolean wysiwygVariable = ( Boolean )wikiContext.getVariable( RenderingManager.WYSIWYG_EDITOR_MODE );
043        m_wysiwygEditorMode = wysiwygVariable != null ? wysiwygVariable.booleanValue() : false;
044    }
045
046    /**
047     * {@inheritDoc}
048     *
049     * @see NodeAttributeProviderState#setAttributes(Attributes, Node)
050     */
051    @Override
052    public void setAttributes( final Attributes attributes, final JSPWikiLink link ) {
053        if( m_wysiwygEditorMode ) {
054            if( attributes.getValue( "class" ) != null ) {
055                String href = attributes.getValue( "href" );
056                XHtmlToWikiConfig wikiConfig = new XHtmlToWikiConfig( wikiContext );
057                // Get the url for wiki page link - it's typically "Wiki.jsp?page=MyPage"
058                // or when using the ShortURLConstructor option, it's "wiki/MyPage" .
059                String wikiPageLinkUrl = wikiConfig.getWikiJspPage();
060                String editPageLinkUrl = wikiConfig.getEditJspPage();
061                if( href != null && href.startsWith( wikiPageLinkUrl ) ) {
062                    // Remove the leading url string so that users will only see the
063                    // wikipage's name when editing an existing wiki link.
064                    // For example, change "Wiki.jsp?page=MyPage" to just "MyPage".
065
066                    String newHref = href.substring( wikiPageLinkUrl.length() );
067
068                    // Convert "This%20Pagename%20Has%20Spaces" to "This Pagename Has Spaces"
069                    newHref = wikiContext.getEngine().decodeName( newHref );
070
071                    // Handle links with section anchors.
072                    // For example, we need to translate the html string "TargetPage#section-TargetPage-Heading2"
073                    // to this wiki string: "TargetPage#Heading2".
074                    attributes.replaceValue( "href", newHref.replaceFirst( WikiRenderer.LINKS_SOURCE, WikiRenderer.LINKS_TRANSLATION ) );
075                } else if( href != null && href.startsWith( editPageLinkUrl ) ) {
076                    String title = attributes.getValue( "title" );
077                    if( title != null ) {
078                        // remove the title since we don't want to eventually save the default undefined page title.
079                        attributes.replaceValue( "title", "" );
080                    }
081
082                    String newHref = href.substring( editPageLinkUrl.length() );
083                    newHref = wikiContext.getEngine().decodeName( newHref );
084
085                    attributes.replaceValue( "href", newHref );
086                }
087            }
088        }
089    }
090
091}