/*
* [CompactAllPosters.java]
*
* Summary: Compact AllPosters.com posters web pages.
*
* 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-12-27 initial version.
*/
package com.mindprod.poster;
import com.mindprod.fastcat.FastCat;
import com.mindprod.hunkio.HunkIO;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.*;
/**
* Compact AllPosters.com posters web pages.
*
* Will leave macros unexpanded.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2012-12-27 initial version.
* @since 2012-12-27
*/
abstract class CompactAllPosters
{
/**
* number of thumbnail pages, e.g. named allposters.html allposters1.htm ...
*/
private static final int ALL_POSTERS_COM_THUMBNAIL_PAGE_COUNT = 6;
/**
* how many columns of posters
*/
private static final int MAX_COLS = 4;
/**
* max rows we place per HTML page.
*/
private static final int MAX_ROWS_PER_PAGE = 8;
/**
* where artcom.html and allposters.html live
*/
private static final String PROVIDER_DIR = "E:/mindprod/jgloss/";
/**
* search for a macro
* e.g.
* or
*/
private static final Pattern MACRO_FINDER = Pattern.compile(
"" );
/**
* remove individual empty slots
*/
static void compactAllPosters()
{
try
{
final int size = ALL_POSTERS_COM_THUMBNAIL_PAGE_COUNT * MAX_ROWS_PER_PAGE
* MAX_COLS;
final ArrayList allMacros = new ArrayList<>( size );
// pages numbered starting at 1 not 0.
for ( int pageNo = 1; pageNo <= ALL_POSTERS_COM_THUMBNAIL_PAGE_COUNT; pageNo++ )
{
final String page = HunkIO.readEntireFile( new File( getHTMLFilename( pageNo ) ) );
final Matcher m = MACRO_FINDER.matcher( page );
while ( m.find() )
{
// hoover up every AllPosters macro, leaving off the lead
allMacros.add( m.group( 1 ) );
}
}
// squirt out macros, with no holes.
int mi = 0; // macro index, which macro to emit
for ( int pageNo = 1; pageNo <= ALL_POSTERS_COM_THUMBNAIL_PAGE_COUNT; pageNo++ )
{
final String page = HunkIO.readEntireFile( new File( getHTMLFilename( pageNo ) ) );
// insert between ...
int start = page.indexOf( "" );
if ( start < 0 )
{
throw new IllegalArgumentException( "missing on " + getHTMLFilename( pageNo ) );
}
start += "".length();
final int end = page.lastIndexOf( "" );
if ( end < 0 )
{
throw new IllegalArgumentException( "missing on " + getHTMLFilename( pageNo ) );
}
int rows = MAX_ROWS_PER_PAGE;
// if this is the last page make extra rows if needed.
if ( pageNo == ALL_POSTERS_COM_THUMBNAIL_PAGE_COUNT && allMacros.size() - mi > MAX_ROWS_PER_PAGE *
MAX_COLS )
{
err.println( "WARNING: too many rows on page " + getHTMLFilename( pageNo + 1 ) );
err.println(
"Create another overflow page soon, containing just ..... ...."
);
err.println( "For now, the last overflow page will have excess lines." );
err.println( "Update ConfigurationForMindprod to add a page to the cycle." );
err.println(
"Also increment ALL_POSTERS_COM_THUMBNAIL_PAGE_COUNT in " +
"program."
);
rows = ( allMacros.size() - mi + MAX_COLS - 1 ) / MAX_COLS;
}
final FastCat sb = new FastCat( rows * MAX_COLS * 3 + rows * 2 + 2 );
// append material prior to block of poster macros.
sb.append( page.substring( 0, start ) );
for ( int row = 0; row < rows; row++ )
{
if ( mi >= allMacros.size() )
{
if ( row == 0 )
{
// create a dummy row to keep HTML validator happy.
sb.append( "\n |
\n" );
}
break;
}
sb.append( "\n" );
for ( int col = 0; col < MAX_COLS; col++ )
{
if ( mi >= allMacros.size() )
{
break;
}
sb.append( " | \n" );
mi++;
}
sb.append( "
\n" );
}
sb.append( page.substring( end ) );
HunkIO.writeEntireFile( new File( getHTMLFilename( pageNo ) ), sb.toString() );
}
}
catch ( IOException e )
{
err.println( e.getMessage() + " cannot compact Allposters.com posters" );
}
}
/**
* get html filename of the i-th extra file where 1 is the base recent file
*
* @param i which of many poster display HTML files wanted.
*
* @return URL of ith HTML of posters
*/
private static String getHTMLFilename( final int i )
{
return PROVIDER_DIR + providerName() + ( i == 1 ? "" : i ) + ".html";
}
private static String providerName()
{
return "allposterscom";
}
}