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.htmltowiki.syntax; 020 021import org.apache.commons.lang3.StringUtils; 022import org.apache.commons.text.StringEscapeUtils; 023import org.apache.wiki.htmltowiki.XHtmlElementToWikiTranslator; 024import org.jdom2.Element; 025 026import java.io.PrintWriter; 027import java.util.HashMap; 028import java.util.Map; 029 030 031/** 032 * Wiki syntax helper operations 033 */ 034public class MarkupHelper { 035 036 public static void printUnescaped( final PrintWriter out, final String s ) { 037 out.print( StringEscapeUtils.unescapeHtml4( s ) ); 038 } 039 040 /** 041 * Checks if the link points to a footnote. 042 */ 043 public static boolean isFootnoteLink( final String ref ) { 044 return ref.startsWith( "#" ); 045 } 046 047 /** 048 * Checks if the link points to an undefined page. 049 */ 050 public static boolean isUndefinedPageLink( final Element a ) { 051 final String classVal = a.getAttributeValue( "class" ); 052 return "createpage".equals( classVal ); 053 } 054 055 public static boolean isHtmlBaseDiv( final XHtmlElementToWikiTranslator.ElementDecoratorData dto ) { 056 return "div".equals( dto.htmlBase ); 057 } 058 059 public static boolean isHtmlBaseSpan( final XHtmlElementToWikiTranslator.ElementDecoratorData dto ) { 060 return "span".equals( dto.htmlBase ); 061 } 062 063 /** 064 * Returns a Map containing the valid augmented wiki link attributes. 065 */ 066 public static Map< String, String > getAugmentedWikiLinkAttributes( final Element a ) { 067 final Map< String, String > attributesMap = new HashMap<>(); 068 final String cssClass = a.getAttributeValue( "class" ); 069 if( StringUtils.isNotEmpty( cssClass ) 070 && !cssClass.matches( "wikipage|createpage|external|interwiki|attachment" ) ) { 071 attributesMap.put( "class", cssClass.replace( "'", "\"" ) ); 072 } 073 addAttributeIfPresent( a, attributesMap, "accesskey" ); 074 addAttributeIfPresent( a, attributesMap, "charset" ); 075 addAttributeIfPresent( a, attributesMap, "dir" ); 076 addAttributeIfPresent( a, attributesMap, "hreflang" ); 077 addAttributeIfPresent( a, attributesMap, "id" ); 078 addAttributeIfPresent( a, attributesMap, "lang" ); 079 addAttributeIfPresent( a, attributesMap, "rel" ); 080 addAttributeIfPresent( a, attributesMap, "rev" ); 081 addAttributeIfPresent( a, attributesMap, "style" ); 082 addAttributeIfPresent( a, attributesMap, "tabindex" ); 083 addAttributeIfPresent( a, attributesMap, "target" ); 084 addAttributeIfPresent( a, attributesMap, "title" ); 085 addAttributeIfPresent( a, attributesMap, "type" ); 086 return attributesMap; 087 } 088 089 static void addAttributeIfPresent( final Element a, final Map< String, String > attributesMap, final String attribute ) { 090 final String attr = a.getAttributeValue( attribute ); 091 if( StringUtils.isNotEmpty( attr ) ) { 092 attributesMap.put( attribute, attr.replace( "'", "\"" ) ); 093 } 094 } 095 096 /** 097 * Converts the entries in the map to a string for use in a wiki link. 098 */ 099 public static String augmentedWikiLinkMapToString( final Map< String, String > attributesMap ) { 100 final StringBuilder sb = new StringBuilder(); 101 for( final Map.Entry< String, String > entry : attributesMap.entrySet() ) { 102 final String attributeName = entry.getKey(); 103 final String attributeValue = entry.getValue(); 104 105 sb.append( " " ).append( attributeName ).append( "='" ).append( attributeValue ).append( "'" ); 106 } 107 108 return sb.toString().trim(); 109 } 110 111 public static String nameSansNbf( final Element e ) { 112 final String name = e.getAttributeValue( "name" ); 113 // remove the "nbf_" that was prepended since new one will be generated again when the xhtml is rendered. 114 if( name != null && name.startsWith( "nbf_" ) ) { 115 return name.substring( 4 ); 116 } 117 return name; 118 } 119 120}