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