/* * [Image.java] * * Summary: Generate a reference to an Image. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.8 2009-02-06 include go package in ZIP bundle. * <!-- macro Image people/bush.jpg "George Bush" right --> * <!-- macro Image people/bush.jpg caption "George Bush" right --> * <!-- macro Image people/bush.jpg link "George Bush" http://whitehouse.gov right --> caption * and alignment are * optional */ package com.mindprod.htmlmacros.macro; import com.mindprod.entities.DeEntifyStrings; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.support.BuildImage; import com.mindprod.htmlmacros.support.ImageAlignment; import static com.mindprod.htmlmacros.macro.Global.configuration; import static java.lang.System.*; /** * Generate a reference to an Image. * * @author Roedy Green, Canadian Mind Products * @version 1.8 2009-02-06 include go package in ZIP bundle. * <!-- macro Image people/bush.jpg "George Bush" right --> * <!-- macro Image people/bush.jpg caption "George Bush" right --> * <!-- macro Image people/bush.jpg link "George Bush" http://whitehouse.gov right --> caption and * alignment are * optional * @see com.mindprod.htmlmacros.support.ImageAlignment * @since 2009 */ public final class Image extends Macro { /** * suffix for a tag */ private static final String TAG_TAIL = configuration.getTagTail(); /** * how to use the macro. Link=where to jump if clicks caption. */ private static final String USAGE = "\nImage macro needs image file, [caption] altText [link url] [align]"; /** * Generate HTML for an image * * @param image URL to the image * @param hasCaption true if want image captioned with the alt text * @param hasLink true if want link on caption * @param alt alt text * @param link where to jump if user clicks the caption. * @param align alignment enumeration * * @return HTML to display that (captioned) image. */ private String expand( String image, boolean hasCaption, boolean hasLink, String alt, String link, ImageAlignment align ) { // null link will result in null css class. final String cssClassForLink = Global.assignCSSClasses.assignCSSClass( link, fileBeingDistributed ); if ( hasCaption ) { // enclose image and caption in a two-row table. final FastCat sb = new FastCat( 36 ); int width = BuildImage.getProjectImageDimensions( image )[ 0 ]; sb.append( "" ); sb.append( DeEntifyStrings.stripHTMLTags( alt ) ); sb.append( "" ); sb.append( "\n" ); sb.append( "\n" ); sb.append( "
" ); if ( hasLink ) { // make both image and caption clickable sb.append( "" ); } sb.append( BuildImage.buildImgTag( image, alt, ImageAlignment.none, null, fileBeingDistributed ) ); if ( hasLink ) { sb.append( "" ); } sb.append( "
" ); if ( hasLink ) { // make both image and caption clickable sb.append( "" ); } sb.append( alt ); if ( hasLink ) { sb.append( "" ); } sb.append( "
\n" ); return sb.toString(); } else { // no caption return BuildImage.buildImgTag( image, alt, align, null /* cssClass for Image itself */, fileBeingDistributed ); } } /** * Image given just file name in image directory typical use: Expands: brown dog * * @param parms first is image file in mindprod/image second is text for alt * @param quiet true if want output suppressed. * @param verbose @return expanded macro HTML */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "I" ); } if ( !( 2 <= parms.length && parms.length <= 5 ) ) { throw new IllegalArgumentException( USAGE ); } final String image = parms[ 0 ]; final boolean hasCaption; final boolean hasLink; final String alt; final String link; ImageAlignment align = ImageAlignment.none; try { if ( parms[ 1 ].equals( "caption" ) ) { hasCaption = true; hasLink = false; alt = parms[ 2 ]; link = null; if ( parms.length == 4 ) { // 0=url 1=caption 2=alt 3=align // see ImageAlignment align = ImageAlignment.valueOf( parms[ 3 ].toLowerCase() ); } } else if ( parms[ 1 ].equals( "link" ) ) { hasCaption = true; hasLink = true; alt = parms[ 2 ]; link = parms[ 3 ]; if ( parms.length == 5 ) { align = ImageAlignment.valueOf( parms[ 4 ].toLowerCase() ); } } else { hasCaption = false; hasLink = false; alt = parms[ 1 ]; link = null; if ( parms.length == 3 ) { align = ImageAlignment.valueOf( parms[ 2 ].toLowerCase() ); } } } catch ( IllegalArgumentException e ) { // explain more clearly if illegal enum value throw new IllegalArgumentException( "macro Image " + image + " align value bad. " + e.getMessage() ); } return expand( image, hasCaption, hasLink, alt, link, align ); } }