From db3db0fdb93f3f3617a24a2102f9ca535db806b6 Mon Sep 17 00:00:00 2001 From: low012 Date: Sun, 21 Nov 2010 00:13:08 +0000 Subject: [PATCH] *) trying to make this class less confusing (probably failing) git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7326 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/data/wiki/wikiCode.java | 268 +++++++++++++---------- 1 file changed, 148 insertions(+), 120 deletions(-) diff --git a/source/de/anomic/data/wiki/wikiCode.java b/source/de/anomic/data/wiki/wikiCode.java index 4a985e382..294b2c3cb 100644 --- a/source/de/anomic/data/wiki/wikiCode.java +++ b/source/de/anomic/data/wiki/wikiCode.java @@ -46,6 +46,47 @@ public class wikiCode extends abstractWikiParser implements wikiParser { private static final String PIPE_ESCAPED = "|"; private static final String REGEX_NOT_CHAR_NUM_OR_UNDERSCORE = "[^a-zA-Z0-9_]"; + private static enum Tags { + HEADLINE_1("=", "

", "

"), + HEADLINE_2("==", "

", "

"), + HEADLINE_3("===", "

", "

"), + HEADLINE_4("====", "

", "

"), + HEADLINE_5("=====", "
", "
"), + HEADLINE_6("======", "
", "
"), + + EMPHASIZE_1("\'\'", "", ""), + EMPHASIZE_2("\'\'\'", "", ""), + EMPHASIZE_3("\'\'\'\'\'", "", ""), + + STRIKE("<s>", "</s>", "", ""); + + final String openHTML; + final String closeHTML; + final String openWiki; + final String closeWiki; + + final int openWikiLength; + final int closeWikiLength; + + Tags(final String openWiki, final String closeWiki, final String openHTML, final String closeHTML) { + if (openHTML == null || closeHTML == null || openWiki == null || closeWiki == null) { + throw new IllegalArgumentException(""); + } + + this.openHTML = openHTML; + this.closeHTML = closeHTML; + this.openWiki = openWiki; + this.closeWiki = closeWiki; + + openWikiLength = openWiki.length(); + closeWikiLength = closeWiki.length(); + } + + Tags(final String wiki, final String openHTML, final String closeHTML) { + this(wiki, wiki, openHTML, closeHTML); + } + } + private static final String HTML_OPEN_DEFINITION_DESCRIPTION = "
"; private static final String HTML_CLOSE_DEFINITION_DESCRIPTION = "
"; private static final String HTML_OPEN_DEFINITION_ITEM = "
"; @@ -66,17 +107,6 @@ public class wikiCode extends abstractWikiParser implements wikiParser { private static final String WIKI_CLOSE_EXTERNAL_LINK = "]"; private static final String WIKI_OPEN_EXTERNAL_LINK = "["; private static final String WIKI_CLOSE_PRE_ESCAPED = "</pre>"; - private static final String WIKI_OPEN_STRIKE = "<s>"; - private static final String WIKI_CLOSE_STRIKE = "</s>"; - private static final String WIKI_EMPHASIZE_1 = "\'\'"; - private static final String WIKI_EMPHASIZE_2 = "\'\'\'"; - private static final String WIKI_EMPHASIZE_3 = "\'\'\'\'\'"; - private static final String WIKI_HEADLINE_TAG_1 = "="; - private static final String WIKI_HEADLINE_TAG_2 = "=="; - private static final String WIKI_HEADLINE_TAG_3 = "==="; - private static final String WIKI_HEADLINE_TAG_4 = "===="; - private static final String WIKI_HEADLINE_TAG_5 = "====="; - private static final String WIKI_HEADLINE_TAG_6 = "======"; private static final String WIKI_HR_LINE = "----"; private static final String WIKI_IMAGE = "Image:"; private static final String WIKI_OPEN_PRE_ESCAPED = "<pre>"; @@ -109,7 +139,13 @@ public class wikiCode extends abstractWikiParser implements wikiParser { private final static Map PROPERTY_VALUES = new HashMap(); /** Tags for different types of headlines in wikiCode. */ - private final static String[] HEADLINE_TAGS = new String[]{WIKI_HEADLINE_TAG_6, WIKI_HEADLINE_TAG_5, WIKI_HEADLINE_TAG_4, WIKI_HEADLINE_TAG_3, WIKI_HEADLINE_TAG_2, WIKI_HEADLINE_TAG_1}; + private final static String[] HEADLINE_TAGS = + new String[]{Tags.HEADLINE_6.openWiki, + Tags.HEADLINE_5.openWiki, + Tags.HEADLINE_4.openWiki, + Tags.HEADLINE_3.openWiki, + Tags.HEADLINE_2.openWiki, + Tags.HEADLINE_1.openWiki}; private final static char[] HEADLINE_LEVEL = new char[]{ONE, TWO, THREE, FOUR, FIVE, SIX}; @@ -600,68 +636,70 @@ public class wikiCode extends abstractWikiParser implements wikiParser { * @return HTML fragment */ private String processPreformattedText(String line) { - final int positionOfOpeningTag = line.indexOf(WIKI_OPEN_PRE_ESCAPED); - final int positionOfClosingTag = line.indexOf(WIKI_CLOSE_PRE_ESCAPED); - //both
 and 
in the same line - if ((positionOfOpeningTag >= 0) && (positionOfClosingTag > 0) && !escaped) { - if (positionOfOpeningTag < positionOfClosingTag) { - final StringBuilder preformattedText = new StringBuilder(); - preformattedText.append("
");
-                preformattedText.append(line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_PRE_ESCAPED, positionOfClosingTag));
-                preformattedText.append("
"); - - line = processLineOfWikiCode(line.substring(0, positionOfOpeningTag).replaceAll("!pre!", "!pre!!") + "!pre!txt!" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_PRE_ESCAPED).replaceAll("!pre!", "!pre!!")); - line = line.replaceAll("!pre!txt!", preformattedText.toString().replaceAll("!pre!", "!pre!!")); + if (!escaped) { + final int positionOfOpeningTag = line.indexOf(WIKI_OPEN_PRE_ESCAPED); + final int positionOfClosingTag = line.indexOf(WIKI_CLOSE_PRE_ESCAPED); + //both
 and 
in the same line + if (positionOfOpeningTag >= 0 && positionOfClosingTag > 0) { + if (positionOfOpeningTag < positionOfClosingTag) { + final StringBuilder preformattedText = new StringBuilder(); + preformattedText.append("
");
+                    preformattedText.append(line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_PRE_ESCAPED, positionOfClosingTag));
+                    preformattedText.append("
"); + + line = processLineOfWikiCode(line.substring(0, positionOfOpeningTag).replaceAll("!pre!", "!pre!!") + "!pre!txt!" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_PRE_ESCAPED).replaceAll("!pre!", "!pre!!")); + line = line.replaceAll("!pre!txt!", preformattedText.toString().replaceAll("!pre!", "!pre!!")); + line = line.replaceAll("!pre!!", "!pre!"); + } //handles cases like
 
 
that would cause an exception otherwise + else { + processingPreformattedText = true; + final String temp1 = processLineOfWikiCode(line.substring(0, positionOfOpeningTag - 1).replaceAll("!tmp!", "!tmp!!") + "!tmp!txt!"); + noList = true; + final String temp2 = processLineOfWikiCode(line.substring(positionOfOpeningTag)); + noList = false; + line = temp1.replaceAll("!tmp!txt!", temp2); + line = line.replaceAll("!tmp!!", "!tmp!"); + processingPreformattedText = false; + } + } //start
+            else if (positionOfOpeningTag >= 0 && !preformattedSpanning) {
+                processingPreformattedText = true;    //prevent surplus line breaks
+                final StringBuilder openBlockQuoteTags = new StringBuilder();  //gets filled with 
s as needed + String preformattedText = "
" + line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_PRE_ESCAPED);
+                preformattedText = preformattedText.replaceAll("!pre!", "!pre!!");
+                //taking care of indented lines
+                while (preindented < positionOfOpeningTag && positionOfOpeningTag < line.length() &&
+                        line.substring(preindented, positionOfOpeningTag).charAt(0) == WIKI_INDENTION) {
+                    preindented++;
+                    openBlockQuoteTags.append(HTML_OPEN_BLOCKQUOTE);
+                }
+                line = processLineOfWikiCode(line.substring(preindented, positionOfOpeningTag).replaceAll("!pre!", "!pre!!") + "!pre!txt!");
+                line = openBlockQuoteTags + line.replaceAll("!pre!txt!", preformattedText);
+                line = line.replaceAll("!pre!!", "!pre!");
+                preformattedSpanning = true;
+            } //end 
+ else if (positionOfClosingTag >= 0 && preformattedSpanning) { + preformattedSpanning = false; + final StringBuilder endBlockQuoteTags = new StringBuilder(); //gets filled with
s as needed + String preformattedText = line.substring(0, positionOfClosingTag) + "
"; + preformattedText = preformattedText.replaceAll("!pre!", "!pre!!"); + //taking care of indented lines + while (preindented > 0) { + endBlockQuoteTags.append(HTML_CLOSE_BLOCKQUOTE); + preindented--; + } + line = processLineOfWikiCode("!pre!txt!" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_PRE_ESCAPED).replaceAll("!pre!", "!pre!!")); + line = line.replaceAll("!pre!txt!", preformattedText) + endBlockQuoteTags; line = line.replaceAll("!pre!!", "!pre!"); - } //handles cases like
 
 
that would cause an exception otherwise - else { - processingPreformattedText = true; - final String temp1 = processLineOfWikiCode(line.substring(0, positionOfOpeningTag - 1).replaceAll("!tmp!", "!tmp!!") + "!tmp!txt!"); - noList = true; - final String temp2 = processLineOfWikiCode(line.substring(positionOfOpeningTag)); - noList = false; - line = temp1.replaceAll("!tmp!txt!", temp2); - line = line.replaceAll("!tmp!!", "!tmp!"); processingPreformattedText = false; + } //Getting rid of surplus + else if (positionOfOpeningTag >= 0 && !preformattedSpanning) { + int posTag; + while ((posTag = line.indexOf(WIKI_CLOSE_PRE_ESCAPED)) >= 0) { + line = line.substring(0, posTag) + line.substring(posTag + LEN_WIKI_CLOSE_PRE_ESCAPED); + } + line = processLineOfWikiCode(line); } - } //start
-        else if ((positionOfOpeningTag >= 0) && !preformattedSpanning && !escaped) {
-            processingPreformattedText = true;    //prevent surplus line breaks
-            final StringBuilder openBlockQuoteTags = new StringBuilder();  //gets filled with 
s as needed - String preformattedText = "
" + line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_PRE_ESCAPED);
-            preformattedText = preformattedText.replaceAll("!pre!", "!pre!!");
-            //taking care of indented lines
-            while (preindented < positionOfOpeningTag && positionOfOpeningTag < line.length() &&
-                    line.substring(preindented, positionOfOpeningTag).charAt(0) == WIKI_INDENTION) {
-                preindented++;
-                openBlockQuoteTags.append(HTML_OPEN_BLOCKQUOTE);
-            }
-            line = processLineOfWikiCode(line.substring(preindented, positionOfOpeningTag).replaceAll("!pre!", "!pre!!") + "!pre!txt!");
-            line = openBlockQuoteTags + line.replaceAll("!pre!txt!", preformattedText);
-            line = line.replaceAll("!pre!!", "!pre!");
-            preformattedSpanning = true;
-        } //end 
- else if ((positionOfClosingTag >= 0) && preformattedSpanning && !escaped) { - preformattedSpanning = false; - final StringBuilder endBlockQuoteTags = new StringBuilder(); //gets filled with
s as needed - String preformattedText = line.substring(0, positionOfClosingTag) + "
"; - preformattedText = preformattedText.replaceAll("!pre!", "!pre!!"); - //taking care of indented lines - while (preindented > 0) { - endBlockQuoteTags.append(HTML_CLOSE_BLOCKQUOTE); - preindented--; - } - line = processLineOfWikiCode("!pre!txt!" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_PRE_ESCAPED).replaceAll("!pre!", "!pre!!")); - line = line.replaceAll("!pre!txt!", preformattedText) + endBlockQuoteTags; - line = line.replaceAll("!pre!!", "!pre!"); - processingPreformattedText = false; - } //Getting rid of surplus - else if ((positionOfOpeningTag >= 0) && !preformattedSpanning && !escaped) { - int posTag; - while ((posTag = line.indexOf(WIKI_CLOSE_PRE_ESCAPED)) >= 0) { - line = line.substring(0, posTag) + line.substring(posTag + LEN_WIKI_CLOSE_PRE_ESCAPED); - } - line = processLineOfWikiCode(line); } return line; } @@ -698,17 +736,17 @@ public class wikiCode extends abstractWikiParser implements wikiParser { if (j >= tableOfContentElements.size()) { break; } - String d = tableOfContentElements.get(j); + final String d = tableOfContentElements.get(j); if (d == null || d.length() < 1) { continue; } - String a = d.substring(1).replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); - String b = element.substring(1).replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); + final String a = d.substring(1).replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); + final String b = element.substring(1).replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); if (a.equals(b)) { doubles++; } } - //if there are doubles, create anchor sextension + //if there are doubles, create anchor extension if (doubles > 0) { anchorext = "_" + (doubles + 1); } @@ -819,41 +857,31 @@ public class wikiCode extends abstractWikiParser implements wikiParser { return directory; } - /** Replaces two occurences of a substring in a string by a pair of strings if - * that substring occurs twice in the string. This method is not greedy! You'll - * have to run it in a loop if you want to replace all occurences of the substring. - * This method provides special treatment for headlines. - * @param input the string that something is to be replaced in - * @param pat substring to be replaced - * @param repl1 string substring gets replaced by on uneven occurences - * @param repl2 string substring gets replaced by on even occurences + /** + * Replaces the wiki representation of tags with the HTML representation. + * @param input String which potentially contains tags to be replaced. + * @param tags tags to be replaced. + * @return input String with replaced tags. */ - //[MN] - private String pairReplace(String input, final String pat, final String repl1, final String repl2) { - return pairReplace(input, pat, pat, repl1, repl2); - } - - private String pairReplace(String input, final String pat1, final String pat2, - final String repl1, final String repl2) { + private String tagReplace(String input, final Tags tags) { String direlem = null; //string to keep headlines until they get added to List dirElements + int firstPosition; final int secondPosition; - final int pat1Len = pat1.length(); - final int pat2Len = pat2.length(); //replace pattern if a pair of the pattern can be found in the line - if (((firstPosition = input.indexOf(pat1)) >= 0) && ((secondPosition = input.indexOf(pat2, firstPosition + pat1Len)) >= 0)) { + if (((firstPosition = input.indexOf(tags.openWiki)) >= 0) && + ((secondPosition = input.indexOf(tags.closeWiki, firstPosition + tags.openWikiLength)) >= 0)) { //extra treatment for headlines - if (Arrays.binarySearch(HEADLINE_TAGS, pat1) >= 0) { + if (Arrays.binarySearch(HEADLINE_TAGS, tags.openWiki) >= 0) { //add anchor and create headline - direlem = input.substring(firstPosition + pat1Len, secondPosition); - if (direlem != null) { + if ((direlem = input.substring(firstPosition + tags.openWikiLength, secondPosition)) != null) { //counting double headlines int doubles = 0; - for (int i = 0; i < tableOfContentElements.size(); i++) { + for (final String tableOfContentElement : tableOfContentElements) { // no element with null value should ever be in directory - assert (tableOfContentElements.get(i) != null); + assert (tableOfContentElement != null); - if (tableOfContentElements.size() > i && tableOfContentElements.get(i).substring(1).equals(direlem)) { + if (tableOfContentElement.substring(1).equals(direlem)) { doubles++; } } @@ -862,22 +890,22 @@ public class wikiCode extends abstractWikiParser implements wikiParser { if (doubles > 0) { anchor = anchor + "_" + (doubles + 1); } - input = input.substring(0, firstPosition) + "" + repl1 - + direlem + repl2 + input.substring(secondPosition + pat2Len); + input = input.substring(0, firstPosition) + "" + tags.openHTML + + direlem + tags.closeHTML + input.substring(secondPosition + tags.closeWikiLength); //add headlines to list of headlines (so TOC can be created) - if (Arrays.binarySearch(HEADLINE_TAGS, pat1) >= 0) { - tableOfContentElements.add((pat1Len - 1) + direlem); + if (Arrays.binarySearch(HEADLINE_TAGS, tags.openWiki) >= 0) { + tableOfContentElements.add((tags.openWikiLength - 1) + direlem); } } } else { - input = input.substring(0, firstPosition) + repl1 - + (input.substring(firstPosition + pat1Len, secondPosition)) + repl2 - + input.substring(secondPosition + pat2Len); + input = input.substring(0, firstPosition) + tags.openHTML + + (input.substring(firstPosition + tags.openWikiLength, secondPosition)) + tags.closeHTML + + input.substring(secondPosition + tags.closeWikiLength); } } //recursion if another pair of the pattern can still be found in the line - if (((firstPosition = input.indexOf(pat1)) >= 0) && (input.indexOf(pat2, firstPosition + pat1Len) >= 0)) { - input = pairReplace(input, pat1, pat2, repl1, repl2); + if (((firstPosition = input.indexOf(tags.openWiki)) >= 0) && (input.indexOf(tags.closeWiki, firstPosition + tags.openWikiLength) >= 0)) { + input = tagReplace(input, tags); } return input; } @@ -925,18 +953,18 @@ public class wikiCode extends abstractWikiParser implements wikiParser { // end contrib [MN] // format headers - line = pairReplace(line, WIKI_HEADLINE_TAG_6, "
", "
"); - line = pairReplace(line, WIKI_HEADLINE_TAG_5, "
", "
"); - line = pairReplace(line, WIKI_HEADLINE_TAG_4, "

", "

"); - line = pairReplace(line, WIKI_HEADLINE_TAG_3, "

", "

"); - line = pairReplace(line, WIKI_HEADLINE_TAG_2, "

", "

"); - line = pairReplace(line, WIKI_HEADLINE_TAG_1, "

", "

"); - - line = pairReplace(line, WIKI_EMPHASIZE_3, "", ""); - line = pairReplace(line, WIKI_EMPHASIZE_2, "", ""); - line = pairReplace(line, WIKI_EMPHASIZE_1, "", ""); - - line = pairReplace(line, WIKI_OPEN_STRIKE, WIKI_CLOSE_STRIKE, "", ""); + line = tagReplace(line, Tags.HEADLINE_6); + line = tagReplace(line, Tags.HEADLINE_5); + line = tagReplace(line, Tags.HEADLINE_4); + line = tagReplace(line, Tags.HEADLINE_3); + line = tagReplace(line, Tags.HEADLINE_2); + line = tagReplace(line, Tags.HEADLINE_1); + + line = tagReplace(line, Tags.EMPHASIZE_3); + line = tagReplace(line, Tags.EMPHASIZE_2); + line = tagReplace(line, Tags.EMPHASIZE_1); + + line = tagReplace(line, Tags.STRIKE); line = processUnorderedList(line); line = processOrderedList(line);