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