/* * [SortQuotes.java] * * Summary: Sort quotes. Does not deDup. Standalone. * * Copyright: (c) 2008-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 2008-06-08 */ package com.mindprod.htmlmacros; import com.mindprod.common18.Misc; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.macro.Global; import com.mindprod.htmlmacros.support.ConfigurationForMindprod; import com.mindprod.htmlmacros.support.QuoteFlock; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * Sort quotes. Does not deDup. Standalone. *

* SortQuotes will accept either stripgenerated or expanded html. * However, it generates unexpanded headers. * The bodies of the quotes will be left stripgenerated or expanded as they were. * We used to run this before every macro expansion. Now it is does less often, * in tquotes.btm when the quotes are tidied and verified. * Gets its list of Flocks from the QuoteFlock enum. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2008-06-08 * @see com.mindprod.htmlmacros.support.QuoteFlock * @since 2008-06-08 */ public class SortQuotes { /** * true if want extra debugging output */ private static final boolean DEBUGGING = false; /** * what we look for surrounding text of interest */ private static final String CLOSE_TAG = ""; /** * what we look for surrounding text of interest */ private static final String OPEN_TAG = " quotes = new ArrayList<>( 10000 ); /** * pattern of quote already handled, used to extract author * Should ignore multiple famous inside quote. */ private static final Pattern AUTHOR = Pattern.compile( ".+(.+?)", Pattern.DOTALL ); /** * pattern to check that quote ends with suitable HTML */ private static final Pattern TEXT_TAIL = Pattern.compile( ".+(?:

||||||
| -->)" + "\\s*(
)?\\s*~\\s*", Pattern.DOTALL ); /** * true when sorting first batch of quotes */ private static boolean first = true; /** * sort ALL quotations to be used for random quotes in all flocks. * Use in frequeuncy mode to pull quotes to top containing a magic * word. * * @param args Command line: [plain] [frequency word] [length] [weight] * Sorts all quotes from configuration. * * @throws java.io.IOException if cannot read quotations */ public static void main( String[] args ) throws IOException { Global.installConfiguration( new ConfigurationForMindprod() ); int errorCount = 0; out.println( "Sorting " + QuoteFlock.values().length + " quotation libraries" ); for ( QuoteFlock f : QuoteFlock.values() ) { File file = f.getFile(); try { final String big = HunkIO.readEntireFile( file ); int start0 = 0; int start1; while ( ( start1 = big.indexOf( OPEN_TAG, start0 ) ) >= 0 ) { final int start2 = start1 + OPEN_TAG.length(); final int end1 = big.indexOf( CLOSE_TAG, start2 ); if ( end1 < 0 ) { throw new IllegalArgumentException( ">>>missing " ); } final int end2 = end1 + CLOSE_TAG.length(); final String rawBlockQuote = big.substring( start1, end2 ); // process text
// Test number of quotes in collected, // duplicates // and format of followed by ~: " + file .toString() + " " + rawBlockQuote ); errorCount++; } final Matcher a = AUTHOR.matcher( rawBlockQuote ); if ( a.lookingAt() && a.groupCount() == 1 ) { quotes.add( new SortableQuote( a.group( 1 ), rawBlockQuote ) ); } else { out.println( "authorless quote: " + file.toString() + " " + rawBlockQuote ); errorCount++; // file it under blank for now quotes.add( new SortableQuote( "", rawBlockQuote ) ); } start0 = end2; } // end while } catch ( IOException e ) { e.printStackTrace( err ); err.println( "Fatal error: unable to open " + file + " possibly locked in the editor." ); System.exit( 2 ); } { final String order = args.length == 0 ? "plain" : args[ 0 ].toLowerCase(); switch ( order ) { case "plain": case "alpha": { if ( first ) { out.println( "Sort quotes in plain order" ); } Collections.sort( quotes ); break; } // we have collected quotes for one file. case "frequency": { if ( first ) { out.println( "Sort quotes by frequency " + args[ 1 ] ); } Collections.sort( quotes, new ByFrequency( args[ 1 ] ) ); break; } case "length": { if ( first ) { out.println( "Sort quotes in length order" ); } Collections.sort( quotes, new ByLength() ); break; } case "date": { if ( first ) { out.println( "Sort quotes in date order" ); } Collections.sort( quotes, new ByDate() ); break; } case "weight": { if ( first ) { out.println( "Sort quotes in weight order" ); } Collections.sort( quotes, new ByWeight() ); break; } default: { if ( first ) { err.println( "no keyword on command line: plain, frequency word, length, date or weight" ); for ( String arg : args ) { err.println( arg ); } } System.exit( 1 ); break; } } first = false; } if ( DEBUGGING ) { out.println( "Sorting " + quotes.size() + " " + f.getDescription() ); } // regenerate head and tail final FastCat sb = new FastCat( quotes.size() * 2 + 6 ); sb.append( "\n" ); sb.append( "

Quotations are selected from this pool (and other quotation pools)\n" + "in a pseudorandom way every hour\n" + "and inserted at the top and bottom of some of the major pages on this website.\n" + "Feel free to copy any of these quotes and paste them for whatever purpose you please, \n" + "including on your own website, blog, social media page or forum debate posts.

\n" ); for ( SortableQuote s : quotes ) { sb.append( s.getQuotation() ); sb.append( "\n" ); } sb.append( "\n" ); HunkIO.writeEntireFile( file, sb.toString() ); // prepare for next file of quotes quotes.clear(); } // end for to process each file if ( errorCount != 0 ) { out.println( errorCount + " errors in quotations" ); } Misc.trackLastThread(); System.exit( 0 ); } }