/* * [TidyBookMacros.java] * * Summary: Put Book macros into canonical form. Ensures title consistent. Leaves in stripGenerated form. * * Copyright: (c) 2012-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 2012-02-18 initial version */ package com.mindprod.stores; import com.mindprod.common18.EIO; import com.mindprod.common18.Misc; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.macro.Global; import com.mindprod.htmlmacros.support.ConfigurationForMindprod; import com.mindprod.hunkio.HunkIO; import com.mindprod.isbn.ISBNValidate; import java.io.File; import java.io.IOException; import java.util.regex.Pattern; import static java.lang.System.*; /** * Put Book macros into canonical form. Ensures title consistent. Leaves in stripGenerated form. *

* Encloses text fields is {}. Commalists are plain. *

* Use -dry to not actually change the files. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2012-02-18 initial version * @since 2012-02-18 */ public class TidyBookMacros extends ManipulateMacros { // TidyBookMacros: reflow Book macros. Update BookHead with recent title and published. Add missing published to BookHead. // RefreshInserts: refresh data on Insert macros from book page. Add missing published to book Insert. // InsertBookMarkers: put between books to make it easier to sort. One shot. // SortBooks: sort book Inserts on page so most recently published are first. // ProofreadInserts: check that sort worked. /** * true if want extra debugging information */ private static final boolean DEBUGGING = false; /** * how to use from command line */ private static final String USAGE = "\nTidyBookMacros.exe Configuration"; /** * split isbns at comma, gets rid of spaces around comma */ private static final Pattern SPLIT_ON_COMMA = Pattern.compile( "\\s*,\\s*" ); /** * where Book files are kept */ private static File bookDir; /** * replace the Book macro with a stripped-down Insert macro * * @param isbn13 EAN to use in Insert macro * @param paperback13 isbn for paperback * @param hardcover13 isbn for hardcover * @param kindle asin for kindle * @param eBook isbn for eBook * @param nook isbn for bn nook * @param kobo isbn for kobo ebook * @param webBook Safari read on web book * @param audio isbn for audio * @param title title * @param author author * @param birth yyyy-mm-dd * @param death yyyy-mm-dd * @param publisher publisher * @param published yyyy-mm-dd * @param notes comments about book * * @return generated Tidied Book macro */ private static String buildTidiedBookFile( final String isbn13, final String paperback13, final String hardcover13, final String kindle, final String eBook, final String webBook, final String audio, final String title, final String author, final String birth, final String death, final String publisher, final String published, final String notes ) { final FastCat sb = new FastCat( 55 ); sb.append( "\n" ); sb.append( "\n" ); sb.append( "\n" ); return sb.toString(); } /** * tidy a comma list of isbns * * @param isbnList comma-separated list of isbns */ private static String tidyISBNList( String isbnList ) { if ( ST.isEmpty( isbnList ) ) { return ""; } // comma list final String[] isbns = SPLIT_ON_COMMA.split( isbnList ); for ( int i = 0; i < isbns.length; i++ ) { if ( ST.isEmpty( isbns[ i ] ) ) { isbns[ i ] = null; } else { isbns[ i ] = ISBNValidate.tidyISBN10or13RemovingDashes( isbns[ i ] ); } } // glue it back together with , final FastCat sb = new FastCat( isbns.length ); for ( String isbn : isbns ) { if ( isbn != null ) { sb.append( isbn ); } } return sb.toCommaList(); } /** * We regeneneraged BookHead, Book and Foot, purely from info in Book. * * @param args not used * * @throws java.io.IOException if trouble reading or writing files containing Book macros. */ public static void main( String[] args ) throws IOException { Global.installConfiguration( new ConfigurationForMindprod() ); final File webrootDir = new File( Global.configuration.getLocalWebrootWithSlashes() ); bookDir = new File( webrootDir, "book" ); String[] files = bookDir.list( new BookMacroFileFilter() ); for ( String fileName : files ) { if ( fileName.equals( "9781593277574.html" ) ) { out.println( "turn on debug" ); } File sourceFile = new File( bookDir, fileName ); try { final String big = HunkIO.readEntireFile( sourceFile, HunkIO.UTF8 ); final String bookMacro = findFirstMacroContents( big, "Book" ); if ( bookMacro == null ) { err.println( "missing