/*
* [HtmlReflow.java]
*
* Summary: reflows/tidies HTML.
*
* 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 2010-01-12 initial release.
*/
package com.mindprod.htmlreflow;
import com.mindprod.commandline.CommandLine;
import com.mindprod.common18.EIO;
import com.mindprod.filter.AllButSVNDirectoriesFilter;
import com.mindprod.filter.ExtensionListFilter;
import com.mindprod.hunkio.HunkIO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import static java.lang.System.*;
/**
* reflows/tidies HTML.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2010-01-12 initial release.
* @since 2010-01-12
*/
public class HtmlReflow
{
private static final int FIRST_COPYRIGHT_YEAR = 2010;
/**
* undisplayed copyright notice
*/
@SuppressWarnings( { "UnusedDeclaration" } )
private static final String EMBEDDED_COPYRIGHT =
"Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com";
/**
* date this version was released.
*/
@SuppressWarnings( { "UnusedDeclaration" } )
private static final String RELEASE_DATE = "2010-01-12";
/**
* embedded version string.
*/
@SuppressWarnings( { "UnusedDeclaration" } )
private static final String VERSION_STRING = "1.0";
/**
* processor that tidies the HTML
*/
private static Reflower reflower;
/**
* reflows HTML files.
*
* @param args names of files to process, dirs, files, -s, *.*, no wildcards.
*/
public static void main( String[] args )
{
// get the configuration parameters via the Preferences API.
try
{
Persist.restoreConfig();
}
catch ( BackingStoreException e )
{
err.println( "HtmlReflow configuration missing or corrupt. Please rerun Configure." );
System.exit( 1 );
}
final int desiredLineLength = Persist.getDesiredLineLength();
final int maxLineLength = Persist.getMaxLineLength();
final String lineSeparator = Persist.getLineSeparator();
final TagConfig[] tags = Persist.getTags();
reflower = new Reflower( desiredLineLength, maxLineLength, lineSeparator, tags );
// gather all the files mentioned on the command line.
// either directories, files, *.*, with -s and subdirs option.
out.println( "Gathering html files to reflow..." );
CommandLine wantedFiles = new CommandLine( args,
new AllButSVNDirectoriesFilter(),
new ExtensionListFilter( "html" ) );
for ( File file : wantedFiles )
{
try
{
reflowFile( false, file /* not quiet */ );
}
catch ( FileNotFoundException e )
{
err.println( "Error: "
+ EIO.getCanOrAbsPath( file )
+ " not found." );
}
catch ( Exception e )
{
err.println();
e.printStackTrace( err );
err.println( " in file "
+ EIO.getCanOrAbsPath( file ) );
err.println();
}
} // end for
} // end main
/**
* compact and tidy one file.
*
* @param quiet true if want progress messages suppressed
* @param fileBeingProcessed the file currently being processed.
*
* @throws java.io.IOException Suppress IntelliJ Code Analyse that wants to make this private.
* @noinspection WeakerAccess, SameParameterValue, StringEquality
*/
public static void reflowFile( boolean quiet, File fileBeingProcessed
) throws IOException
{
if ( !quiet )
{
out.print( " reflowing " + fileBeingProcessed.getName() + " " );
}
if ( !( fileBeingProcessed.getName().endsWith( ".html" )
|| fileBeingProcessed
.getName().endsWith( ".htm" ) ) )
{
err.println( "Cannot reflow: "
+ fileBeingProcessed.getName()
+ "not .html file" );
return;
}
final String big = HunkIO.readEntireFile( fileBeingProcessed );
final String result = HTMLState.tidy( big );
// use == not equals() because compare already done in compactStringKeepingMacrosAndComments.
if ( result == big )
{
// nothing changed. No need to write results.
if ( !quiet )
{
out.println( "-" );
}
return;
}
// generate output into a temporary file until we are sure all is ok.
// create a temp file in the same directory as filename
if ( !quiet )
{
out.println( "*" );
}
final File tempFile =
HunkIO.createTempFile( "temp_", ".tmp", fileBeingProcessed );
FileWriter emit = new FileWriter( tempFile );
emit.write( result );
emit.close();
HunkIO.deleteAndRename( tempFile, fileBeingProcessed );
}
}