/* * [HTMLTouchUp.java] * * Summary: Embeds <br> and <p> entities in HTML output. * * Copyright: (c) 2002-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: * 4.5 2009-02-26 add both Java string quoting and plain for Java search/regexes. */ package com.mindprod.quoter; /** * Embeds <br> and <p> entities in HTML output. * * @author Roedy Green, Canadian Mind Products * @version 4.5 2009-02-26 add both Java string quoting and plain for Java search/regexes. * @since 2002-06-19 */ final class HTMLTouchUp extends TextProcessor { /** * Embeds *
* chars in HTML output. * * @param raw text to be tidied * * @return String representing the cooked output */ public String process( String raw ) { assert raw != null : "HTMLTouchUp.process raw must not be null"; // We are processing the entire clipboard in one fell swoop // not just one line of it. // check for runs of blank lines, convert to new paragraph int pendingNlCount = 0; StringBuilder result = new StringBuilder( raw.length() + raw.length() / 4 ); result.append( "" );
for ( int i = 0; i < raw.length(); i++ )
{
char c = raw.charAt( i );
if ( c == '\n' )
{
pendingNlCount++;
}
else
{
switch ( pendingNlCount )
{
case 0:
/* do nothing */
break;
case 1:
result.append( "\n
\n" );
break;
default:
/* treat 3 or more like 2 */
case 2:
result.append( "
" ); break; } pendingNlCount = 0; result.append( c ); } } // end for result.append( "
\n" ); return result.toString(); } // end process }