1 /* 2 * Copyright 2011 - Doxia :: Include Macro - Juergen Kellerer 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.tinyjee.maven.dim.extensions; 18 19 import java.util.Map; 20 21 import static org.tinyjee.maven.dim.IncludeMacroSignature.PARAM_SOURCE_CONTENT; 22 23 /** 24 * This extension adds an inline CSS definition to the current page. 25 * Use this extension to define short-lived style classes or adjust existing styles for just one page. 26 * 27 * @author Juergen_Kellerer, 2011-11-06 28 */ 29 public class InlineCssParameterTransformer extends AbstractParameterTransformer { 30 31 /** 32 * Enables this extension and sets the content to place inside the inlined "<style>" element. 33 * <p/> 34 * <b>Note:</b> Setting this property has no effect in case of "<code>source</code>" or "<code>source-class</code>" were 35 * specified with the macro call. This behaviour ensures that the parameter "<code>inline-css</code>" can still be used 36 * with ordinary macro calls where a source or source-class was set. 37 * <p/> 38 * Using this parameter has no effect when the used Sink does not support HTML markup. 39 * <p/> 40 * This parameter supports the usage of "{@code classpath:/path/to/css/to/inline}" to load the css from the classpath instead 41 * of defining it with the macro call. 42 * <p/> 43 * As the default brace style of "{}" is used to call macros in APT, the extension supports substituting the default CSS braces 44 * with "[]". This brace style is optional but it must be used when more than one style class is defined with the macro call 45 * as the APT parser swallows the "}" character, leading to invalid results. 46 * <p/> 47 * Examples:<ul> 48 * <li>"<code>%{include|inline-css=.my-class { background-color:red; }}</code>"</li> 49 * <li>"<code>%{include|inline-css=.section th { background-color:#999999; color:white; }}</code>" 50 * - changes the appearance of table headers inside sections.</li> 51 * <li>"<code>%{include|inline-css=.white [ color:white; ] .red [ color:red; ]}</code>" 52 * - Define more than one class at once using [] instead of {}.</li> 53 * </ul> 54 */ 55 public static final String PARAM_INLINE_CSS = "inline-css"; 56 57 @Override 58 protected boolean doTransformParameters(Map<String, Object> requestParams) { 59 String inlineCss = (String) requestParams.get(PARAM_INLINE_CSS); 60 if (inlineCss != null) { 61 // Using velocity instead of a direct call to "attachCss()" as parameter transformers shouldn't do anything else than 62 // what they are named after. 63 requestParams.put(PARAM_SOURCE_CONTENT, "#set($success = $globals.attachCss(null, ${inline-css}))"); 64 65 inlineCss = inlineCss.replace('[', '{').replace(']', '}').trim(); 66 if (!inlineCss.endsWith("}")) inlineCss += "}"; 67 requestParams.put(PARAM_INLINE_CSS, inlineCss); 68 69 return true; 70 } 71 return false; 72 } 73 }