Author: ltheussl
Date: Mon Nov 19 02:37:51 2007
New Revision: 596258
URL: http://svn.apache.org/viewvc?rev=596258&view=rev
Log:
[DOXIA-178] fix nested wiki formats (e.g. bold italic)
Submitted by: Dave Syer
Added:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/resources/nested-format.confluence
Modified:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/AbstractFatherBlock.java
maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ChildBlocksBuilder.java
maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ParagraphBlockParser.java
maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/list/TreeListBuilder.java
maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/java/org/apache/maven/doxia/module/confluence/ConfluenceParserTest.java
Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/AbstractFatherBlock.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/AbstractFatherBlock.java?rev=596258&r1=596257&r2=596258&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/AbstractFatherBlock.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/AbstractFatherBlock.java
Mon Nov 19 02:37:51 2007
@@ -19,9 +19,8 @@
* under the License.
*/
-import java.util.Arrays;
-import java.util.List;
import java.util.Iterator;
+import java.util.List;
import org.apache.maven.doxia.sink.Sink;
Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ChildBlocksBuilder.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ChildBlocksBuilder.java?rev=596258&r1=596257&r2=596258&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ChildBlocksBuilder.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ChildBlocksBuilder.java
Mon Nov 19 02:37:51 2007
@@ -20,36 +20,48 @@
*/
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import org.codehaus.plexus.util.StringUtils;
/**
* Re-usable builder that can be used to generate paragraph and list item text from a string
containing all the content
- * and wiki formatting.
- *
+ * and wiki formatting. This class is intentionally stateful, but cheap to create, so create
one as needed and keep it
+ * on the stack to preserve stateless behaviour in the caller.
+ *
* @author Dave Syer
*/
public class ChildBlocksBuilder
{
+ private boolean insideBold = false;
+
+ private boolean insideItalic = false;
+
+ private boolean insideLink = false;
+
+ private List blocks = new ArrayList();
+
+ private StringBuffer text = new StringBuffer();
+
+ private String input;
+
+ private boolean insideMonospaced;
+
+ public ChildBlocksBuilder( String input )
+ {
+ this.input = input;
+ }
+
/**
* Utility method to convert marked up content into blocks for rendering.
- *
+ *
* @param input a String with no line breaks
* @return a list of Blocks that can be used to render it
*/
- public List getBlocks( String input )
+ public List getBlocks()
{
-
- boolean insideBold = false;
- boolean insideItalic = false;
- boolean insideLink = false;
-
- List blocks = new ArrayList();
-
- StringBuffer text = new StringBuffer();
+ List specialBlocks = new ArrayList();
for ( int i = 0; i < input.length(); i++ )
{
@@ -60,13 +72,13 @@
case '*':
if ( insideBold )
{
- TextBlock tb = new TextBlock( text.toString() );
- blocks.add( new BoldBlock( Arrays.asList( new Block[] { tb } ) )
);
+ insideBold = false;
+ specialBlocks = getList( new BoldBlock( getChildren(text, specialBlocks)
), specialBlocks );
text = new StringBuffer();
}
else
{
- text = addTextBlockIfNecessary( blocks, text );
+ text = addTextBlockIfNecessary( blocks, specialBlocks, text );
insideBold = true;
}
@@ -74,20 +86,20 @@
case '_':
if ( insideItalic )
{
- TextBlock tb = new TextBlock( text.toString() );
- blocks.add( new ItalicBlock( Arrays.asList( new Block[] { tb } )
) );
+ insideItalic = false;
+ specialBlocks = getList( new ItalicBlock( getChildren( text, specialBlocks
) ), specialBlocks );
text = new StringBuffer();
}
else
{
- text = addTextBlockIfNecessary( blocks, text );
+ text = addTextBlockIfNecessary( blocks, specialBlocks, text );
insideItalic = true;
}
break;
case '[':
insideLink = true;
- text = addTextBlockIfNecessary( blocks, text );
+ text = addTextBlockIfNecessary( blocks, specialBlocks, text );
break;
case ']':
if ( insideLink )
@@ -113,17 +125,20 @@
}
text = new StringBuffer();
+ insideLink = false;
}
break;
case '{':
+ text = addTextBlockIfNecessary( blocks, specialBlocks, text );
+
if ( charAt( input, i ) == '{' ) // it's monospaced
{
i++;
+ insideMonospaced = true;
}
// else it's a confluence macro...
- text = addTextBlockIfNecessary( blocks, text );
break;
case '}':
@@ -133,8 +148,8 @@
if ( charAt( input, i ) == '}' )
{
i++;
- TextBlock tb = new TextBlock( text.toString() );
- blocks.add( new MonospaceBlock( Arrays.asList( new Block[] { tb }
) ) );
+ insideMonospaced = false;
+ specialBlocks = getList( new MonospaceBlock( getChildren( text, specialBlocks
) ), specialBlocks );
text = new StringBuffer();
}
else
@@ -159,7 +174,7 @@
if ( charAt( input, i ) == '\\' )
{
i++;
- text = addTextBlockIfNecessary( blocks, text );
+ text = addTextBlockIfNecessary( blocks, specialBlocks, text );
blocks.add( new LinebreakBlock() );
}
else
@@ -172,6 +187,16 @@
default:
text.append( c );
}
+
+ if ( !specialBlocks.isEmpty() )
+ {
+ if ( !insideItalic && !insideBold && !insideMonospaced )
+ {
+ blocks.addAll( specialBlocks );
+ specialBlocks.clear();
+ }
+ }
+
}
if ( text.length() > 0 )
@@ -182,18 +207,69 @@
return blocks;
}
+ private List getList( Block block, List currentBlocks )
+ {
+ List list = new ArrayList();
+
+ if ( insideBold || insideItalic || insideMonospaced )
+ {
+ list.addAll( currentBlocks );
+ }
+
+ list.add( block );
+
+ return list;
+ }
+
+ private List getChildren( StringBuffer buffer, List currentBlocks )
+ {
+ String text = buffer.toString().trim();
+
+ if ( currentBlocks.isEmpty() && StringUtils.isEmpty( text ) )
+ {
+ return new ArrayList();
+ }
+
+ ArrayList list = new ArrayList();
+
+ if ( !insideBold && !insideItalic && !insideMonospaced )
+ {
+ list.addAll( currentBlocks );
+ }
+
+ if ( StringUtils.isEmpty( text ) )
+ {
+ return list;
+ }
+
+ list.add( new TextBlock(text) );
+
+ return list;
+ }
+
private static char charAt( String input, int i )
{
return input.length() > i + 1 ? input.charAt( i + 1 ) : '\0';
}
- private StringBuffer addTextBlockIfNecessary( List blocks, StringBuffer text )
+ private StringBuffer addTextBlockIfNecessary( List blocks, List specialBlocks, StringBuffer
text )
{
if ( text.length() == 0 )
{
return text;
}
- blocks.add( new TextBlock( text.toString() ) );
+
+ TextBlock textBlock = new TextBlock( text.toString() );
+
+ if ( !insideBold && !insideItalic && !insideMonospaced )
+ {
+ blocks.add( textBlock );
+ }
+ else
+ {
+ specialBlocks.add( textBlock );
+ }
+
return new StringBuffer();
}
Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ParagraphBlockParser.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ParagraphBlockParser.java?rev=596258&r1=596257&r2=596258&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ParagraphBlockParser.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/ParagraphBlockParser.java
Mon Nov 19 02:37:51 2007
@@ -43,8 +43,8 @@
throws ParseException
{
- ChildBlocksBuilder builder = new ChildBlocksBuilder();
- return new ParagraphBlock( builder.getBlocks( appendUntilEmptyLine( line, source
) ) );
+ ChildBlocksBuilder builder = new ChildBlocksBuilder( appendUntilEmptyLine( line,
source ) );
+ return new ParagraphBlock( builder.getBlocks() );
}
/**
Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/list/TreeListBuilder.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/list/TreeListBuilder.java?rev=596258&r1=596257&r2=596258&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/list/TreeListBuilder.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/main/java/org/apache/maven/doxia/module/confluence/parser/list/TreeListBuilder.java
Mon Nov 19 02:37:51 2007
@@ -118,7 +118,7 @@
if ( child.getFather() != null )
{
- childBlocks.addAll( new ChildBlocksBuilder().getBlocks( child.getText() )
);
+ childBlocks.addAll( new ChildBlocksBuilder( child.getText() ).getBlocks()
);
}
if ( child.getChildren().size() != 0 )
Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/java/org/apache/maven/doxia/module/confluence/ConfluenceParserTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/java/org/apache/maven/doxia/module/confluence/ConfluenceParserTest.java?rev=596258&r1=596257&r2=596258&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/java/org/apache/maven/doxia/module/confluence/ConfluenceParserTest.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/java/org/apache/maven/doxia/module/confluence/ConfluenceParserTest.java
Mon Nov 19 02:37:51 2007
@@ -291,6 +291,28 @@
assertEquals( 2, result.split( "end:sectionTitle2\n" ).length );
}
+ /** @throws Exception */
+ public void testNestedFormats()
+ throws Exception
+ {
+ String result = locateAndParseTestSourceFile( "nested-format" );
+
+ assertContainsLines( result, "begin:bold\nbegin:italic\ntext: bold italic\nend:italic"
);
+ assertContainsLines( result, "begin:italic\nbegin:bold\ntext: italic bold\nend:bold"
);
+ assertContainsLines( result, "begin:bold\nbegin:monospaced\ntext: bold monospaced\nend:monospaced"
);
+ assertContainsLines( result, "text: A paragraph with \nbegin:bold\ntext: bold \nbegin:italic\ntext:
italic\nend:italic" );
+ assertContainsLines( result, "begin:italic\ntext: italic \nbegin:bold\ntext: bold\nend:bold"
);
+ assertContainsLines( result, "begin:bold\ntext: bold \nbegin:monospaced\ntext: monospaced\nend:monospaced"
);
+ // 2 paragraphs in the input...
+ assertEquals( 3, result.split( "end:paragraph\n" ).length );
+ // 6 bolds in the input...
+ assertEquals( 7, result.split( "end:bold\n" ).length );
+ // 4 italics in the input...
+ assertEquals( 5, result.split( "end:italic\n" ).length );
+ // 2 monospaced in the input...
+ assertEquals( 3, result.split( "end:monospaced\n" ).length );
+ }
+
private void assertContainsLines( String message, String result, String lines )
{
lines = StringUtils.replace( lines, "\n", EOL );
Added: maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/resources/nested-format.confluence
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/resources/nested-format.confluence?rev=596258&view=auto
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/resources/nested-format.confluence
(added)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-confluence/src/test/resources/nested-format.confluence
Mon Nov 19 02:37:51 2007
@@ -0,0 +1,6 @@
+
+A paragraph with *_bold italic_* _*italic bold*_ *{{bold monospaced}}*
+
+A paragraph with *bold _italic_* _italic *bold*_ *bold {{monospaced}}*
+
+
|