diff options
author | Kevin de Vlaming | 2018-12-07 21:35:39 +0000 |
---|---|---|
committer | Kevin de Vlaming | 2018-12-10 17:59:45 +0000 |
commit | 722849a165c719c8adeb3ef6c23c46cfde3e0530 (patch) | |
tree | b147c955cdcdad066123d4fc4ba6823e02867873 | |
parent | 317fdc306f8324a93dec3a2f6aeee4019bda254e (diff) | |
download | org.eclipse.mylyn.docs-722849a165c719c8adeb3ef6c23c46cfde3e0530.tar.gz org.eclipse.mylyn.docs-722849a165c719c8adeb3ef6c23c46cfde3e0530.tar.xz org.eclipse.mylyn.docs-722849a165c719c8adeb3ef6c23c46cfde3e0530.zip |
542496: Add support for lists to the Creole DocumentBuilder
Change-Id: Id4719d7f5f09836dba62f81890cb45f67b117963
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=542496
Signed-off-by: Kevin de Vlaming <kevin.devlaming@tasktop.com>
2 files changed, 178 insertions, 0 deletions
diff --git a/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/main/java/org/eclipse/mylyn/wikitext/creole/internal/CreoleDocumentBuilder.java b/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/main/java/org/eclipse/mylyn/wikitext/creole/internal/CreoleDocumentBuilder.java index 6ba516d0b..94d9d28af 100644 --- a/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/main/java/org/eclipse/mylyn/wikitext/creole/internal/CreoleDocumentBuilder.java +++ b/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/main/java/org/eclipse/mylyn/wikitext/creole/internal/CreoleDocumentBuilder.java @@ -158,6 +158,12 @@ public class CreoleDocumentBuilder extends AbstractMarkupDocumentBuilder { @Override protected Block computeBlock(BlockType type, Attributes attributes) { switch (type) { + case BULLETED_LIST: + case NUMERIC_LIST: + return new NewlineDelimitedBlock(type, doubleNewlineDelimiterCount(), 1); + case LIST_ITEM: + char prefixChar = computeCurrentListType() == BlockType.NUMERIC_LIST ? '#' : '*'; + return new ContentBlock(type, computePrefix(prefixChar, computeListLevel()) + " ", "", 1, 1); case PARAGRAPH: return new ContentBlock(type, "", "", 2, 2); //$NON-NLS-1$ //$NON-NLS-2$ default: @@ -166,6 +172,17 @@ public class CreoleDocumentBuilder extends AbstractMarkupDocumentBuilder { } } + private int doubleNewlineDelimiterCount() { + if (currentBlock != null) { + BlockType currentBlockType = currentBlock.getBlockType(); + if (currentBlockType == BlockType.LIST_ITEM || currentBlockType == BlockType.BULLETED_LIST + || currentBlockType == BlockType.NUMERIC_LIST) { + return 1; + } + } + return 2; + } + @Override protected Block computeSpan(SpanType type, Attributes attributes) { switch (type) { diff --git a/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/test/java/org/eclipse/mylyn/internal/wikitext/creole/tests/documentbuilder/CreoleDocumentBuilderListTest.java b/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/test/java/org/eclipse/mylyn/internal/wikitext/creole/tests/documentbuilder/CreoleDocumentBuilderListTest.java new file mode 100644 index 000000000..d5f98a121 --- /dev/null +++ b/wikitext/core/org.eclipse.mylyn.wikitext.creole/src/test/java/org/eclipse/mylyn/internal/wikitext/creole/tests/documentbuilder/CreoleDocumentBuilderListTest.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * Copyright (c) 2018 Tasktop Technologies. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Kevin de Vlaming - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.wikitext.creole.tests.documentbuilder; + +import org.eclipse.mylyn.wikitext.parser.Attributes; +import org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType; + +/** + * @see http://www.wikicreole.org/wiki/Elements + * @author Kevin de Vlaming + */ +public class CreoleDocumentBuilderListTest extends AbstractCreoleDocumentBuilderTest { + + public void testListBulleted() { + builder.beginDocument(); + builder.beginBlock(BlockType.BULLETED_LIST, new Attributes()); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("X"); + builder.endBlock(); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("Y"); + builder.endBlock(); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("Z"); + builder.endBlock(); + builder.endBlock(); + builder.endDocument(); + assertMarkup("* X\n* Y\n* Z\n"); + } + + public void testListNumeric() { + builder.beginDocument(); + builder.beginBlock(BlockType.NUMERIC_LIST, new Attributes()); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("One"); + builder.endBlock(); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("Two"); + builder.endBlock(); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("Three"); + builder.endBlock(); + builder.endBlock(); + builder.endDocument(); + assertMarkup("# One\n# Two\n# Three\n"); + } + + public void testListConsecutive() { + builder.beginDocument(); + builder.beginBlock(BlockType.BULLETED_LIST, new Attributes()); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("Food"); + builder.endBlock(); + builder.endBlock(); + builder.beginBlock(BlockType.NUMERIC_LIST, new Attributes()); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters("Drink"); + builder.endBlock(); + builder.endBlock(); + builder.endDocument(); + assertMarkup("* Food\n\n# Drink\n"); + } + + public void testListWithParagraphs() { + builder.beginDocument(); + builder.beginBlock(BlockType.BULLETED_LIST, new Attributes()); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.beginBlock(BlockType.PARAGRAPH, new Attributes()); + builder.characters("X"); + builder.endBlock(); + builder.endBlock(); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.beginBlock(BlockType.PARAGRAPH, new Attributes()); + builder.characters("Y"); + builder.endBlock(); + builder.endBlock(); + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.beginBlock(BlockType.PARAGRAPH, new Attributes()); + builder.characters("Z"); + builder.endBlock(); + builder.endBlock(); + builder.endBlock(); + builder.endDocument(); + assertMarkup("* X\n\n* Y\n\n* Z\n\n"); + } + + public void testBulletedListWithNestedSublist() { + builder.beginDocument(); + + builder.beginBlock(BlockType.BULLETED_LIST, new Attributes()); //begin first list + emitListItem("item 1"); + builder.beginBlock(BlockType.BULLETED_LIST, new Attributes()); //begin second list + emitListItem("item 1.A."); + emitListItem("item 1.B."); + builder.endBlock(); //close second list + emitListItem("item 2"); + builder.beginBlock(BlockType.NUMERIC_LIST, new Attributes()); //begin third list + emitListItem("item 2.A."); + emitListItem("item 2.B."); + builder.endBlock(); //close third list + builder.endBlock(); //close first list + + builder.beginBlock(BlockType.NUMERIC_LIST, new Attributes()); //begin fourth list + emitListItem("item 3"); + builder.beginBlock(BlockType.BULLETED_LIST, new Attributes()); //begin fifth list + emitListItem("item 3.A."); + emitListItem("item 3.B."); + builder.beginBlock(BlockType.NUMERIC_LIST, new Attributes()); //begin sixth list + emitListItem("item 3.B.i."); + emitListItem("item 3.B.ii."); + builder.endBlock(); // close sixth list + + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); //begin list item + builder.characters("item 3.C."); + builder.lineBreak(); + builder.characters("item 3.C. line 2"); + builder.endBlock(); // close list item + + builder.endBlock(); // close fifth list + builder.endBlock(); // close fourth list + + builder.endDocument(); + + String markup = out.toString(); + + assertEquals(// + "* item 1\n" + // + "** item 1.A.\n" + // + "** item 1.B.\n" + // + "* item 2\n" + // + "## item 2.A.\n" + // + "## item 2.B.\n" + // + "\n" + // + "# item 3\n" + // + "** item 3.A.\n" + // + "** item 3.B.\n" + // + "### item 3.B.i.\n" + // + "### item 3.B.ii.\n" + // + "** item 3.C." + // + "\\\\item 3.C. line 2\n", // + markup); + } + + private void emitListItem(String text) { + builder.beginBlock(BlockType.LIST_ITEM, new Attributes()); + builder.characters(text); + builder.endBlock(); + } + +}
\ No newline at end of file |