*) cleaned up code

*) fixed a bug where a line like <pre><pre>text</pre></pre><pre>text</pre> caused an exception
*) fixed a bug where a line like [=[=text=]=][=text=] caused an exception


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2023 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
low012 19 years ago
parent 00a5d435e2
commit 4639c0ac6d

@ -6,7 +6,7 @@
// Frankfurt, Germany, 2004 // Frankfurt, Germany, 2004
// //
// This file ist contributed by Alexander Schier // This file ist contributed by Alexander Schier
// last major change: 08.03.2006 // last major change: 17.04.2006
// //
// This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
@ -57,27 +57,30 @@ import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.server.serverCore; import de.anomic.server.serverCore;
import de.anomic.yacy.yacyCore; import de.anomic.yacy.yacyCore;
/** This class provides methods to handle texts that have been posted in the yacyWiki. */ /** This class provides methods to handle texts that have been posted in the yacyWiki or other
* parts of YaCy that use this class, like the blog or the profile.
*/
public class wikiCode { public class wikiCode {
private String numListLevel=""; private String numListLevel="";
private String ListLevel=""; private String ListLevel="";
private String defListLevel=""; private String defListLevel="";
private plasmaSwitchboard sb; private plasmaSwitchboard sb;
private boolean cellprocessing=false; // needed for prevention of double-execution of replaceHTML private boolean cellprocessing=false; //needed for prevention of double-execution of replaceHTML
private boolean defList = false; //needed for definition lists private boolean defList = false; //needed for definition lists
private boolean escape = false; //needed for escape private boolean escape = false; //needed for escape
private boolean escaped = false; //needed for <pre> not getting in the way private boolean escaped = false; //needed for <pre> not getting in the way
private boolean escapeSpan = false; //needed for escape symbols [= and =] spanning over several lines private boolean escapeSpan = false; //needed for escape symbols [= and =] spanning over several lines
private boolean newrowstart=false; // needed for the first row not to be empty private boolean newrowstart=false; //needed for the first row not to be empty
private boolean nolist = false; //needed for handling of [= and <pre> in lists
private boolean preformatted = false; //needed for preformatted text private boolean preformatted = false; //needed for preformatted text
private boolean preformattedSpan = false; //needed for <pre> and </pre> spanning over several lines private boolean preformattedSpan = false; //needed for <pre> and </pre> spanning over several lines
private boolean replacedHTML = false; //indicates if method replaceHTML has been used with line already private boolean replacedHTML = false; //indicates if method replaceHTML has been used with line already
private boolean replacedCharacters = false; //indicates if method replaceCharachters has been used with line private boolean replacedCharacters = false; //indicates if method replaceCharachters has been used with line
private boolean table=false; // needed for tables, because they reach over several lines private boolean table=false; //needed for tables, because they reach over several lines
private int preindented = 0; //needed for indented <pre>s private int preindented = 0; //needed for indented <pre>s
private int escindented = 0; //needed for indented [=s private int escindented = 0; //needed for indented [=s
private int headlines = 0; //number of headlines in page private int headlines = 0; //number of headlines in page
private ArrayList dirElements = new ArrayList(); //List of headlines used to create diectory of page private ArrayList dirElements = new ArrayList(); //list of headlines used to create diectory of page
/** Constructor of the class wikiCode */ /** Constructor of the class wikiCode */
public wikiCode(plasmaSwitchboard switchboard){ public wikiCode(plasmaSwitchboard switchboard){
@ -453,6 +456,7 @@ public class wikiCode {
/** This method processes ordered lists. /** This method processes ordered lists.
*/ */
private String orderedList(String result){ private String orderedList(String result){
if(!nolist){ //lists only get processed if not forbidden (see code for [= and <pre>). [MN]
int p0 = 0; int p0 = 0;
int p1 = 0; int p1 = 0;
//# sorted Lists contributed by [AS] //# sorted Lists contributed by [AS]
@ -493,6 +497,7 @@ public class wikiCode {
} }
} }
// end contrib [AS] // end contrib [AS]
}
return result; return result;
} }
@ -500,6 +505,7 @@ public class wikiCode {
*/ */
//contributed by [AS] put into it's own method by [MN] //contributed by [AS] put into it's own method by [MN]
private String unorderedList(String result){ private String unorderedList(String result){
if(!nolist){ //lists only get processed if not forbidden (see code for [= and <pre>). [MN]
int p0 = 0; int p0 = 0;
int p1 = 0; int p1 = 0;
//contributed by [AS] //contributed by [AS]
@ -521,7 +527,7 @@ public class wikiCode {
int i = ListLevel.length(); int i = ListLevel.length();
String tmp = ""; String tmp = "";
while(! result.startsWith(ListLevel.substring(0,i)) ){ while(! result.startsWith(ListLevel.substring(0,i))){
tmp += "</ul>"; tmp += "</ul>";
i--; i--;
} }
@ -539,6 +545,7 @@ public class wikiCode {
} }
} }
//end contrib [AS] //end contrib [AS]
}
return result; return result;
} }
@ -546,6 +553,7 @@ public class wikiCode {
*/ */
//contributed by [MN] based on unordered list code by [AS] //contributed by [MN] based on unordered list code by [AS]
private String definitionList(String result){ private String definitionList(String result){
if(!nolist){ //lists only get processed if not forbidden (see code for [= and <pre>). [MN]
int p0 = 0; int p0 = 0;
int p1 = 0; int p1 = 0;
if(result.startsWith(defListLevel + ";")){ //more semicolons if(result.startsWith(defListLevel + ";")){ //more semicolons
@ -597,6 +605,7 @@ public class wikiCode {
result = tmp + result.substring(p0, p1); result = tmp + result.substring(p0, p1);
} }
} }
}
return result; return result;
} }
@ -704,6 +713,141 @@ public class wikiCode {
return result; return result;
} }
/** This method handles the escape tags [= =] */
//contributed by [MN]
private String escapeTag(String result, plasmaSwitchboard switchboard){
int p0 = 0;
int p1 = 0;
//both [= and =] in the same line
if(((p0 = result.indexOf("[="))>=0)&&((p1 = result.indexOf("=]"))>0)&&(!(preformatted))){
if(p0<p1){
String escapeText = result.substring(p0+2,p1);
result = transformLine(result.substring(0,p0).replaceAll("!esc!", "!esc!!")+"!esc!txt!"+result.substring(p1+2).replaceAll("!esc!", "!esc!!"), switchboard);
result = result.replaceAll("!esc!txt!", escapeText);
result = result.replaceAll("!esc!!", "!esc!");
}
//handles cases like [=[= =]=] [= =] that would cause an exception otherwise
else{
escape = true;
String temp1 = transformLine(result.substring(0,p0-1).replaceAll("!tmp!","!tmp!!")+"!tmp!txt!", switchboard);
nolist = true;
String temp2 = transformLine(result.substring(p0), switchboard);
nolist = false;
result = temp1.replaceAll("!tmp!txt!",temp2);
result = result.replaceAll("!tmp!!", "!tmp!");
escape = false;
}
}
//start [=
else if(((p0 = result.indexOf("[="))>=0)&&(!escapeSpan)&&(!preformatted)){
escape = true; //prevent surplus line breaks
escaped = true; //prevents <pre> being parsed
String bq = ""; //gets filled with <blockquote>s as needed
String escapeText = result.substring(p0+2);
//taking care of indented lines
while(result.substring(escindented,p0).startsWith(":")){
escindented++;
bq = bq + "<blockquote>";
}
result = transformLine(result.substring(escindented,p0).replaceAll("!esc!", "!esc!!")+"!esc!txt!", switchboard);
result = bq + result.replaceAll("!esc!txt!", escapeText);
escape = false;
escapeSpan = true;
}
//end =]
else if(((p0 = result.indexOf("=]"))>=0)&&(escapeSpan)&&(!preformatted)){
escapeSpan = false;
String bq = ""; //gets filled with </blockquote>s as neede
String escapeText = result.substring(0,p0);
//taking care of indented lines
while(escindented > 0){
bq = bq + "</blockquote>";
escindented--;
}
result = transformLine("!esc!txt!"+result.substring(p0+2).replaceAll("!esc!", "!esc!!"), switchboard);
result = result.replaceAll("!esc!txt!", escapeText) + bq;
escaped = false;
}
//Getting rid of surplus =]
else if (((p0 = result.indexOf("=]"))>=0)&&(!escapeSpan)&&(!preformatted)){
while((p0 = result.indexOf("=]"))>=0){
result = result.substring(0,p0)+result.substring(p0+2);
}
result = transformLine(result, switchboard);
}
return result;
}
/** This method handles the preformatted tags <pre> </pre> */
//contributed by [MN]
private String preformattedTag(String result, plasmaSwitchboard switchboard){
int p0 = 0;
int p1 = 0;
//implementation very similar to escape code (see above)
//both <pre> and </pre> in the same line
if(((p0=result.indexOf("&lt;pre&gt;"))>=0)&&((p1=result.indexOf("&lt;/pre&gt;"))>0)&&(!(escaped))){
if(p0<p1){
String preformattedText = "<pre style=\"border:dotted;border-width:thin\">"+result.substring(p0+11,p1)+"</pre>";
result = transformLine(result.substring(0,p0).replaceAll("!pre!", "!pre!!")+"!pre!txt!"+result.substring(p1+12).replaceAll("!pre!", "!pre!!"), switchboard);
result = result.replaceAll("!pre!txt!", preformattedText);
result = result.replaceAll("!pre!!", "!pre!");
}
//handles cases like <pre><pre> </pre></pre> <pre> </pre> that would cause an exception otherwise
else{
preformatted = true;
String temp1 = transformLine(result.substring(0,p0-1).replaceAll("!tmp!","!tmp!!")+"!tmp!txt!", switchboard);
nolist = true;
String temp2 = transformLine(result.substring(p0), switchboard);
nolist = false;
result = temp1.replaceAll("!tmp!txt!",temp2);
result = result.replaceAll("!tmp!!", "!tmp!");
preformatted = false;
}
}
//start <pre>
else if(((p0 = result.indexOf("&lt;pre&gt;"))>=0)&&(!preformattedSpan)&&(!escaped)){
preformatted = true; //prevent surplus line breaks
String bq =""; //gets filled with <blockquote>s as needed
String preformattedText = "<pre style=\"border:dotted;border-width:thin\">"+result.substring(p0+11);
//taking care of indented lines
while(result.substring(preindented,p0).startsWith(":")){
preindented++;
bq = bq + "<blockquote>";
}
result = transformLine(result.substring(preindented,p0).replaceAll("!pre!", "!pre!!")+"!pre!txt!", switchboard);
result = bq + result.replaceAll("!pre!txt!", preformattedText);
result = result.replaceAll("!pre!!", "!pre!");
preformattedSpan = true;
}
//end </pre>
else if(((p0 = result.indexOf("&lt;/pre&gt;"))>=0)&&(preformattedSpan)&&(!escaped)){
preformattedSpan = false;
String bq = ""; //gets filled with </blockquote>s as needed
String preformattedText = result.substring(0,p0)+"</pre>";
//taking care of indented lines
while (preindented > 0){
bq = bq + "</blockquote>";
preindented--;
}
result = transformLine("!pre!txt!"+result.substring(p0+12).replaceAll("!pre!", "!pre!!"), switchboard);
result = result.replaceAll("!pre!txt!", preformattedText) + bq;
result = result.replaceAll("!pre!!", "!pre!");
preformatted = false;
}
//Getting rid of surplus </pre>
else if (((p0 = result.indexOf("&lt;/pre&gt;"))>=0)&&(!preformattedSpan)&&(!escaped)){
while((p0 = result.indexOf("&lt;/pre&gt;"))>=0){
result = result.substring(0,p0)+result.substring(p0+12);
}
result = transformLine(result, switchboard);
}
return result;
}
/** This method creates a directory for a wiki page. /** This method creates a directory for a wiki page.
* @return directory of the wiki * @return directory of the wiki
*/ */
@ -832,24 +976,27 @@ public class wikiCode {
* @return the line of text with HTML tags instead of wiki tags * @return the line of text with HTML tags instead of wiki tags
*/ */
public String transformLine(String result, plasmaSwitchboard switchboard) { public String transformLine(String result, plasmaSwitchboard switchboard) {
// transform page //If HTML has not bee replaced yet (can happen if method gets called in recursion), replace now!
int p0, p1;
if (!replacedHTML){ if (!replacedHTML){
result = replaceHTMLonly(result); result = replaceHTMLonly(result);
replacedHTML = true; replacedHTML = true;
} }
//If special characters have not bee replaced yet, replace now!
if (!replacedCharacters){ if (!replacedCharacters){
result = replaceCharacters(result); result = replaceCharacters(result);
replacedCharacters = true; replacedCharacters = true;
} }
//check if line contains any escape symbol or tag for preformatted text //check if line contains escape symbols([= =]) or if we are in an escape sequence already.
//or if we are in an esacpe sequence already or if we are in a preformated text if ((result.indexOf("[=")>=0)||(result.indexOf("=]")>=0)||(escapeSpan)){
//if that's the case the program will continue further below result = escapeTag(result, switchboard);
//(see code for [= and =] and <pre> and </pre>) [MN] }
if((result.indexOf("[=")<0)&&(result.indexOf("=]")<0)&&(!escapeSpan)&& //check if line contains preformatted symbols or if we are in a preformatted sequence already.
(result.indexOf("&lt;pre&gt;")<0)&&(result.indexOf("&lt;/pre&gt;")<0)&&(!preformattedSpan)){ else if ((result.indexOf("&lt;pre&gt;")>=0)||(result.indexOf("&lt;/pre&gt;")>=0)||(preformattedSpan)){
result = preformattedTag(result, switchboard);
}
//transform page as usual
else {
//tables first -> wiki-tags in cells can be treated after that //tables first -> wiki-tags in cells can be treated after that
result = processTable(result, switchboard); result = processTable(result, switchboard);
@ -888,114 +1035,7 @@ public class wikiCode {
} }
//escape code ([=...=]) contributed by [MN] if (!preformatted) replacedHTML = false;
//both [= and =] in the same line
else if(((p0 = result.indexOf("[="))>=0)&&((p1 = result.indexOf("=]"))>0)&&(!(preformatted))){
String escapeText = result.substring(p0+2,p1);
//BUGS TO BE FIXED: [=[=text=]=] does not work properly:
//[=[= undx=]x=] should resolve as [= undx=]x, but resolves as [= undxx=]
//ALSO [=[= und =]=] [= und =] leads to an exception
//
//handlicg cases where the text inside [= and =] also contains
//[= and =]. Example: [=[=...=]=]
result = transformLine(result.substring(0,p0).replaceAll("!esc!", "!esc!!")+"!esc!txt!"+result.substring(p1+2).replaceAll("!esc!", "!esc!!"), switchboard);
result = result.replaceAll("!esc!txt!", escapeText);
result = result.replaceAll("!esc!!", "!esc!");
}
//start [=
else if(((p0 = result.indexOf("[="))>=0)&&(!escapeSpan)&&(!preformatted)){
escape = true; //prevent surplus line breaks
escaped = true; //prevents <pre> being parsed
String bq = ""; //gets filled with <blockquote>s as needed
String escapeText = result.substring(p0+2);
//taking care of indented lines
while(result.substring(escindented,p0).startsWith(":")){
escindented++;
bq = bq + "<blockquote>";
}
result = transformLine(result.substring(escindented,p0).replaceAll("!esc!", "!esc!!")+"!esc!txt!", switchboard);
result = bq + result.replaceAll("!esc!txt!", escapeText);
escape = false;
escapeSpan = true;
}
//end =]
else if(((p0 = result.indexOf("=]"))>=0)&&(escapeSpan)&&(!preformatted)){
escapeSpan = false;
String bq = ""; //gets filled with </blockquote>s as neede
String escapeText = result.substring(0,p0);
//taking care of indented lines
while(escindented > 0){
bq = bq + "</blockquote>";
escindented--;
}
result = transformLine("!esc!txt!"+result.substring(p0+2).replaceAll("!esc!", "!esc!!"), switchboard);
result = result.replaceAll("!esc!txt!", escapeText) + bq;
escaped = false;
}
//Getting rid of surplus =]
else if (((p0 = result.indexOf("=]"))>=0)&&(!escapeSpan)&&(!preformatted)){
while((p0 = result.indexOf("=]"))>=0){
result = result.substring(0,p0)+result.substring(p0+2);
}
result = transformLine(result, switchboard);
}
//end contrib [MN]
//preformatted code (<pre>...</pre>) contributed by [MN]
//implementation very similar to escape code (see above)
//both <pre> and </pre> in the same line
else if(((p0 = result.indexOf("&lt;pre&gt;"))>=0)&&((p1 = result.indexOf("&lt;/pre&gt;"))>0)&&(!(escaped))){
String preformattedText = "<pre style=\"border:dotted;border-width:thin\">"+result.substring(p0+11,p1)+"</pre>";
result = transformLine(result.substring(0,p0).replaceAll("!pre!", "!pre!!")+"!pre!txt!"+result.substring(p1+12).replaceAll("!pre!", "!pre!!"), switchboard);
result = result.replaceAll("!pre!txt!", preformattedText);
result = result.replaceAll("!pre!!", "!pre!");
}
//start <pre>
else if(((p0 = result.indexOf("&lt;pre&gt;"))>=0)&&(!preformattedSpan)&&(!escaped)){
preformatted = true; //prevent surplus line breaks
String bq =""; //gets filled with <blockquote>s as needed
String preformattedText = "<pre style=\"border:dotted;border-width:thin\">"+result.substring(p0+11);
//taking care of indented lines
while(result.substring(preindented,p0).startsWith(":")){
preindented++;
bq = bq + "<blockquote>";
}
result = transformLine(result.substring(preindented,p0).replaceAll("!pre!", "!pre!!")+"!pre!txt!", switchboard);
result = bq + result.replaceAll("!pre!txt!", preformattedText);
result = result.replaceAll("!pre!!", "!pre!");
preformattedSpan = true;
}
//end </pre>
else if(((p0 = result.indexOf("&lt;/pre&gt;"))>=0)&&(preformattedSpan)&&(!escaped)){
preformattedSpan = false;
String bq = ""; //gets filled with </blockquote>s as needed
String preformattedText = result.substring(0,p0)+"</pre>";
//taking care of indented lines
while (preindented > 0){
bq = bq + "</blockquote>";
preindented--;
}
result = transformLine("!pre!txt!"+result.substring(p0+12).replaceAll("!pre!", "!pre!!"), switchboard);
result = result.replaceAll("!pre!txt!", preformattedText) + bq;
result = result.replaceAll("!pre!!", "!pre!");
preformatted = false;
}
//Getting rid of surplus </pre>
else if (((p0 = result.indexOf("&lt;/pre&gt;"))>=0)&&(!preformattedSpan)&&(!escaped)){
while((p0 = result.indexOf("&lt;/pre&gt;"))>=0){
result = result.substring(0,p0)+result.substring(p0+12);
}
result = transformLine(result, switchboard);
}
//end contrib [MN]
replacedHTML = false;
replacedCharacters = false; replacedCharacters = false;
if ((result.endsWith("</li>"))||(defList)||(escape)||(preformatted)||(table)||(cellprocessing)) return result; if ((result.endsWith("</li>"))||(defList)||(escape)||(preformatted)||(table)||(cellprocessing)) return result;
return result + "<br>"; return result + "<br>";

Loading…
Cancel
Save