/* * [GoogleCannedSearch.java] * * Summary: Expands HTML GoogleCannedSearch macro. * * Copyright: (c) 2010-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.0 2013-03-13 initial version */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.support.BuildImage; import com.mindprod.htmlmacros.support.ImageAlignment; import com.mindprod.htmlmacros.support.Tools; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import static com.mindprod.htmlmacros.macro.Global.configuration; import static java.lang.System.*; /** * Expands HTML GoogleCannedSearch macro. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-12-21 initial version * @see GoogleAdjustableSearch * @see GoogleRegisteredSearch * @since 2010 */ public final class GoogleCannedSearch extends Macro { /** * how to use the macro. web-search web for text image-searchimages. * web=search for documents image=search for pictures * label=text to label box, null Google Icon */ private static final String USAGE = "\nUsage: GoogleCannedSearch {web/image} {\"keywords\"} {label}"; /** * Remove any punctuation from the string. This is not an entity strip. * It is used to simplify text for Google searches. * * @param s string to strip. * * @return string with punctuation replaced with blanks: */ @SuppressWarnings( { "ConstantConditions" } ) private static String stripPunctuation( String s ) { return ST.stripNaughtyCharacters( s, "\"'\''#$%&()*+=-/:;<=>?@[]{}^~\u2018\u2019\u201c\u201d" ); } /** * generate HTML for a canned google search. This search is ad hoc, not pre-registered with Google. * Code used to generate Google button for page in header, and in stores to search other bookstores. * * @param isImageSearch true if want an image search. * @param literalKeywoards * @param keywords what to search for. punctuation will be stripped. * @param label text to label the link, null use Google Icon * @param fileBeingDistributed where this markup will be embedded. * * @return generated HTML */ public static String expand( final boolean isImageSearch, String literalKeywords, String keywords, String label, final File fileBeingDistributed ) { if ( literalKeywords == null ) { literalKeywords = ""; } if ( keywords == null ) { keywords = ""; } keywords = stripPunctuation( Tools.flatten( keywords ) ); // collapse multiple spaces. Remove lead trail space. keywords = ST.condense( keywords.trim() + " " + literalKeywords ); // URLencode to deal with spaces and punctuation try { // this is not a different charset, it is armouring a few chars keywords = URLEncoder.encode( keywords, "UTF-8" ); } catch ( UnsupportedEncodingException e ) { err.println( "fatal error. Missing UTF-8 support." ); System.exit( 2 ); } final String cssClass; if ( label == null ) { // no label, get big icon instead. label = BuildImage.buildImgTag( "navigate/googlesearch.png", "Google search web for more information on this topic", ImageAlignment.middle, null/* no cssClass */, fileBeingDistributed ); cssClass = "plain"; } else { cssClass = "googlesearch"; /* get smal icon with label */ } // search web with google for this topic // https://www.google.com/search?q=The+NetRexx+Language final FastCat sb = new FastCat( 8 ); sb.append( "" ); sb.append( label ); sb.append( "" ); return sb.toString(); } /** * Generate a simple google search referral. The real work is done by the amazon.Book class * * @param parms GoogleAdjustableSearch keywords * @param quiet true if want output suppressed. * @param verbose @return expanded macro */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { // indicate we handled an Book macro on the console out.print( "G" ); } if ( parms.length != 3 ) { throw new IllegalArgumentException( USAGE ); } if ( configuration.isKindle() ) { return ""; } else { final boolean isImageSearch = parms[ 0 ].equals( "image" ); final String keywords = parms[ 1 ]; final String label = parms[ 2 ]; // null filled in when call by other classes return expand( isImageSearch, null, keywords, label, fileBeingDistributed ); } } }