*) cleaning up code

*) fixed a bug concerning [= =] and <pre></pre> in tables


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2018 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
low012 19 years ago
parent 9324425165
commit 23cdf319be

@ -64,6 +64,7 @@ public class wikiCode {
private String defListLevel="";
private plasmaSwitchboard sb;
private boolean cellprocessing=false; // needed for prevention of double-execution of replaceHTML
private boolean defList = false; //needed for definition lists
private boolean escape = false; //needed for escape
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
@ -495,185 +496,13 @@ public class wikiCode {
return result;
}
/** This method creates a directory for a wiki page.
* @return directory of the wiki
*/
//method contributed by [MN]
private String directory(){
String directory = "";
String element;
int s = 0;
int level = 1;
int level1 = 0;
int level2 = 0;
int level3 = 0;
int doubles = 0;
String anchorext = "";
if((s=dirElements.size())>2){
for(int i=0;i<s;i++){
element = dirElements.get(i).toString();
//counting double headlines
doubles = 0;
for(int j=0;j<i;j++){
if(dirElements.get(j).toString().substring(1).replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","").equals(element.substring(1).replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]",""))){
doubles++;
}
}
//if there are doubles, create anchorextension
if(doubles>0){
anchorext = "_" + (doubles+1);
}
if (element.startsWith("3")){
if(level<3){
level = 3;
level3 = 0;
}
level3++;
String temp = element.substring(1);
element=level1+"."+level2+"."+level3+" "+temp;
directory = directory + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br>\n";
}
else if (element.startsWith("2")){
if(level==1){
level2 = 0;
level = 2;
}
if(level==3){
level = 2;
}
level2++;
String temp = element.substring(1);
element=level1+"."+level2+" "+temp;
directory = directory + "&nbsp;&nbsp;<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br>\n";
}
else if (element.startsWith("1")) {
if (level>1) {
level = 1;
level2 = 0;
level3 = 0;
}
level1++;
String temp = element.substring(1);
element=level1+". "+temp;
directory = directory + "<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br>\n";
}
anchorext="";
}
directory = "<table><tr><td><div class=\"WikiTOCBox\">\n" + directory + "</div></td></tr></table>\n";
}
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
/** This method processes unordered lists.
*/
//[MN]
private String pairReplace(String input, String pat, String repl1, String repl2){
String direlem = ""; //string to keep headlines until they get added to List dirElements
//contributed by [AS] put into it's own method by [MN]
private String unorderedList(String result){
int p0 = 0;
int p1 = 0;
int l = pat.length();
if ((p0 = input.indexOf(pat)) >= 0) {
p1 = input.indexOf(pat, p0 + l);
if (p1 >= 0) {
//extra treatment for headlines
if((pat.equals("===="))||(pat.equals("==="))||(pat.equals("=="))){
//add anchor and create headline
direlem = input.substring(p0 + l, p1);
//counting double headlines
int doubles = 0;
for(int i=0;i<headlines;i++){
if(dirElements.get(i).toString().substring(1).equals(direlem)){
doubles++;
}
}
String anchor = direlem.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]",""); //replace blanks with underscores and delete everything thats not a regular character, a number or _
//if there are doubles, add underscore and number of doubles plus one
if(doubles>0){
anchor = anchor + "_" + (doubles+1);
}
input = input.substring(0, p0) + "<a name=\""+anchor+"\"></a>" + repl1 +
direlem + repl2 + input.substring(p1 + l);
//add headlines to list of headlines (so TOC can be created)
if(pat.equals("====")) dirElements.add("3"+direlem);
else if(pat.equals("===")) dirElements.add("2"+direlem);
else if(pat.equals("==")) dirElements.add("1"+direlem);
headlines++;
}
else{
input = input.substring(0, p0) + repl1 +
(direlem = input.substring(p0 + l, p1)) + repl2 +
input.substring(p1 + l);
}
}
}
return input;
}
/** Replaces wiki tags with HTML tags.
* @param result a line of text
* @param switchboard
* @return the line of text with HTML tags instead of wiki tags
*/
public String transformLine(String result, plasmaSwitchboard switchboard) {
// transform page
int p0, p1;
boolean defList = false; //needed for definition lists
if (!replacedHTML){
result = replaceHTMLonly(result);
replacedHTML = true;
}
if (!replacedCharacters){
result = replaceCharacters(result);
replacedCharacters = true;
}
//check if line contains any escape symbol or tag for preformatted text
//or if we are in an esacpe sequence already or if we are in a preformated text
//if that's the case the program will continue further below
//(see code for [= and =] and <pre> and </pre>) [MN]
if((result.indexOf("[=")<0)&&(result.indexOf("=]")<0)&&(!escapeSpan)&&
(result.indexOf("&lt;pre&gt;")<0)&&(result.indexOf("&lt;/pre&gt;")<0)&&(!preformattedSpan)){
//tables first -> wiki-tags in cells can be treated after that
result = this.processTable(result, switchboard);
// format lines
if (result.startsWith(" ")) result = "<tt>" + result + "</tt>";
if (result.startsWith("----")) result = "<hr>";
// citings contributed by [MN]
if(result.startsWith(":")){
String head = "";
String tail = "";
while(result.startsWith(":")){
head = head + "<blockquote>";
tail = tail + "</blockquote>";
result = result.substring(1);
}
result = head + result + tail;
}
// end contrib [MN]
// format headers
result = pairReplace(result,"====","<h4>","</h4>");
result = pairReplace(result,"===","<h3>","</h3>");
result = pairReplace(result,"==","<h2>","</h2>");
result = pairReplace(result,"'''''","<b><i>","</i></b>");
result = pairReplace(result,"'''","<b>","</b>");
result = pairReplace(result,"''","<i>","</i>");
//* unorderd Lists contributed by [AS]
//** Sublist
//contributed by [AS]
if(result.startsWith(ListLevel + "*")){ //more stars
p0 = result.indexOf(ListLevel);
p1 = result.length();
@ -709,10 +538,16 @@ public class wikiCode {
result = tmp + result.substring(p0, p1);
}
}
//end contrib [AS]
return result;
}
result = orderedList(result);
//* definition Lists contributed by [MN] based on unordered list code by [AS]
/** This method processes definition lists.
*/
//contributed by [MN] based on unordered list code by [AS]
private String definitionList(String result){
int p0 = 0;
int p1 = 0;
if(result.startsWith(defListLevel + ";")){ //more semicolons
String dt = "";
String dd = "";
@ -758,17 +593,23 @@ public class wikiCode {
result = tmp + "<dt>" + dt + "</dt>" + "<dd>" + dd;
defList = true;
}
}else{
result = tmp + result.substring(p0, p1);
}
}
// end contrib [MN]
return result;
}
/** This method processes links and images.
*/
//contributed by [AS] except where stated otherwise
private String linksAndImages(String result, plasmaSwitchboard switchboard){
// create links
String kl, kv, alt, align;
int p;
int p0 = 0;
int p1 = 0;
// internal links and images
while ((p0 = result.indexOf("[[")) >= 0) {
p1 = result.indexOf("]]", p0 + 2);
@ -787,7 +628,6 @@ public class wikiCode {
if ((p = kl.indexOf("|")) > 0) {
kv = kl.substring(p + 1);
kl = kl.substring(0, p);
// if there are 2 arguments, write them into ALIGN and ALT
if ((p = kv.indexOf("|")) > 0) {
align = kv.substring(0, p);
@ -805,7 +645,6 @@ public class wikiCode {
else align = "";
alt = " alt=\"" + kv.substring(p + 1) + "\"";
}
// if there is just one, put it into ALT
else
alt = " alt=\"" + kv + "\"";
@ -862,6 +701,191 @@ public class wikiCode {
}
result = result.substring(0, p0) + "<a class=\"extern\" href=\"" + kl + "\">" + kv + "</a>" + result.substring(p1 + 1);
}
return result;
}
/** This method creates a directory for a wiki page.
* @return directory of the wiki
*/
//method contributed by [MN]
private String directory(){
String directory = "";
String element;
int s = 0;
int level = 1;
int level1 = 0;
int level2 = 0;
int level3 = 0;
int doubles = 0;
String anchorext = "";
if((s=dirElements.size())>2){
for(int i=0;i<s;i++){
element = dirElements.get(i).toString();
//counting double headlines
doubles = 0;
for(int j=0;j<i;j++){
if(dirElements.get(j).toString().substring(1).replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","").equals(element.substring(1).replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]",""))){
doubles++;
}
}
//if there are doubles, create anchorextension
if(doubles>0){
anchorext = "_" + (doubles+1);
}
if (element.startsWith("3")){
if(level<3){
level = 3;
level3 = 0;
}
level3++;
String temp = element.substring(1);
element=level1+"."+level2+"."+level3+" "+temp;
directory = directory + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br>\n";
}
else if (element.startsWith("2")){
if(level==1){
level2 = 0;
level = 2;
}
if(level==3){
level = 2;
}
level2++;
String temp = element.substring(1);
element=level1+"."+level2+" "+temp;
directory = directory + "&nbsp;&nbsp;<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br>\n";
}
else if (element.startsWith("1")) {
if (level>1) {
level = 1;
level2 = 0;
level3 = 0;
}
level1++;
String temp = element.substring(1);
element=level1+". "+temp;
directory = directory + "<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br>\n";
}
anchorext="";
}
directory = "<table><tr><td><div class=\"WikiTOCBox\">\n" + directory + "</div></td></tr></table>\n";
}
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
*/
//[MN]
private String pairReplace(String input, String pat, String repl1, String repl2){
String direlem = ""; //string to keep headlines until they get added to List dirElements
int p0 = 0;
int p1 = 0;
int l = pat.length();
if ((p0 = input.indexOf(pat)) >= 0) {
p1 = input.indexOf(pat, p0 + l);
if (p1 >= 0) {
//extra treatment for headlines
if((pat.equals("===="))||(pat.equals("==="))||(pat.equals("=="))){
//add anchor and create headline
direlem = input.substring(p0 + l, p1);
//counting double headlines
int doubles = 0;
for(int i=0;i<headlines;i++){
if(dirElements.get(i).toString().substring(1).equals(direlem)){
doubles++;
}
}
String anchor = direlem.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]",""); //replace blanks with underscores and delete everything thats not a regular character, a number or _
//if there are doubles, add underscore and number of doubles plus one
if(doubles>0){
anchor = anchor + "_" + (doubles+1);
}
input = input.substring(0, p0) + "<a name=\""+anchor+"\"></a>" + repl1 +
direlem + repl2 + input.substring(p1 + l);
//add headlines to list of headlines (so TOC can be created)
if(pat.equals("====")) dirElements.add("3"+direlem);
else if(pat.equals("===")) dirElements.add("2"+direlem);
else if(pat.equals("==")) dirElements.add("1"+direlem);
headlines++;
}
else{
input = input.substring(0, p0) + repl1 +
(direlem = input.substring(p0 + l, p1)) + repl2 +
input.substring(p1 + l);
}
}
}
return input;
}
/** Replaces wiki tags with HTML tags.
* @param result a line of text
* @param switchboard
* @return the line of text with HTML tags instead of wiki tags
*/
public String transformLine(String result, plasmaSwitchboard switchboard) {
// transform page
int p0, p1;
if (!replacedHTML){
result = replaceHTMLonly(result);
replacedHTML = true;
}
if (!replacedCharacters){
result = replaceCharacters(result);
replacedCharacters = true;
}
//check if line contains any escape symbol or tag for preformatted text
//or if we are in an esacpe sequence already or if we are in a preformated text
//if that's the case the program will continue further below
//(see code for [= and =] and <pre> and </pre>) [MN]
if((result.indexOf("[=")<0)&&(result.indexOf("=]")<0)&&(!escapeSpan)&&
(result.indexOf("&lt;pre&gt;")<0)&&(result.indexOf("&lt;/pre&gt;")<0)&&(!preformattedSpan)){
//tables first -> wiki-tags in cells can be treated after that
result = processTable(result, switchboard);
// format lines
if (result.startsWith(" ")) result = "<tt>" + result + "</tt>";
if (result.startsWith("----")) result = "<hr>";
// citings contributed by [MN]
if(result.startsWith(":")){
String head = "";
String tail = "";
while(result.startsWith(":")){
head = head + "<blockquote>";
tail = tail + "</blockquote>";
result = result.substring(1);
}
result = head + result + tail;
}
// end contrib [MN]
// format headers
result = pairReplace(result,"====","<h4>","</h4>");
result = pairReplace(result,"===","<h3>","</h3>");
result = pairReplace(result,"==","<h2>","</h2>");
result = pairReplace(result,"'''''","<b><i>","</i></b>");
result = pairReplace(result,"'''","<b>","</b>");
result = pairReplace(result,"''","<i>","</i>");
result = unorderedList(result);
result = orderedList(result);
result = definitionList(result);
result = linksAndImages(result, switchboard);
}
//escape code ([=...=]) contributed by [MN]
@ -912,6 +936,13 @@ public class wikiCode {
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]
@ -955,6 +986,13 @@ public class wikiCode {
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;

Loading…
Cancel
Save