*) removed description of removed commands from wikiHelp ([= =])

*) used format function of Netbeans for wikiCode to make it more readable, no functional changes

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5907 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
low012 16 years ago
parent eacf95213a
commit ff5f82d780

@ -134,15 +134,6 @@
</td> </td>
</tr> </tr>
<tr valign="top">
<td class="tt">
[= text =]
</td>
<td>
The escape tags will cause all tags in the text between the starting and the closing tag to not be treated as wiki-code.
</td>
</tr>
<tr valign="top"> <tr valign="top">
<td class="tt"> <td class="tt">
&lt;pre&gt; text &lt;/pre&gt; &lt;pre&gt; text &lt;/pre&gt;

@ -24,7 +24,6 @@
// Contains contributions from Alexander Schier [AS] // Contains contributions from Alexander Schier [AS]
// Franz Brausze [FB] and Marc Nause [MN] // Franz Brausze [FB] and Marc Nause [MN]
package de.anomic.data.wiki; package de.anomic.data.wiki;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -37,34 +36,35 @@ import de.anomic.htmlFilter.htmlFilterCharacterCoding;
import de.anomic.server.serverCore; import de.anomic.server.serverCore;
/** This class provides methods to handle texts that have been posted in the yacyWiki or other /** 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. * parts of YaCy that use this class, like the blog or the profile.
*/ */
public class wikiCode extends abstractWikiParser implements wikiParser { public class wikiCode extends abstractWikiParser implements wikiParser {
/* Table properties */ /* Table properties */
private static final String[] tps = { "rowspan", "colspan", "vspace", "hspace", "cellspacing", "cellpadding", "border" }; private static final String[] tps = {"rowspan", "colspan", "vspace", "hspace", "cellspacing", "cellpadding", "border"};
private static final HashMap/* <String,String[]> */<String, String[]> ps = new HashMap<String, String[]>(); private static final HashMap<String, String[]> ps = new HashMap<String, String[]>();
static { static {
Arrays.sort(tps); Arrays.sort(tps);
String[] array; String[] array;
Arrays.sort(array = new String[] { "void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border" }); Arrays.sort(array = new String[]{"void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"});
ps.put("frame", array); ps.put("frame", array);
Arrays.sort(array = new String[] { "none", "groups", "rows", "cols", "all" }); Arrays.sort(array = new String[]{"none", "groups", "rows", "cols", "all"});
ps.put("rules", array); ps.put("rules", array);
Arrays.sort(array = new String[] { "top", "middle", "bottom", "baseline" }); Arrays.sort(array = new String[]{"top", "middle", "bottom", "baseline"});
ps.put("valign", array); ps.put("valign", array);
Arrays.sort(array = new String[] { "left", "right", "center" }); Arrays.sort(array = new String[]{"left", "right", "center"});
ps.put("align", array); ps.put("align", array);
} }
private String numListLevel = "";
private String numListLevel=""; private String ListLevel = "";
private String ListLevel=""; private String defListLevel = "";
private String defListLevel=""; 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 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 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
@ -74,7 +74,7 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
private final ArrayList<String> dirElements = new ArrayList<String>(); //list of headlines used to create diectory of page private final ArrayList<String> dirElements = new ArrayList<String>(); //list of headlines used to create diectory of page
/** Constructor of the class wikiCode */ /** Constructor of the class wikiCode */
public wikiCode(String address){ public wikiCode(String address) {
super(address); super(address);
} }
@ -83,17 +83,18 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
final int length) throws IOException { final int length) throws IOException {
final StringBuilder out = new StringBuilder(length); final StringBuilder out = new StringBuilder(length);
String line; String line;
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null) {
out.append(transformLine(line)).append(serverCore.CRLF_STRING); out.append(transformLine(line)).append(serverCore.CRLF_STRING);
}
return out.insert(0, directory()).toString(); return out.insert(0, directory()).toString();
} }
/** This method processes tables in the wiki code. /** This method processes tables in the wiki code.
* @param a string that might contain parts of a table * @param a string that might contain parts of a table
* @return a string with wiki code of parts of table replaced by HTML code for table * @return a string with wiki code of parts of table replaced by HTML code for table
*/ */
//[FB], changes by [MN] //[FB], changes by [MN]
private String processTable(String result){ private String processTable(String result) {
//some variables that make it easier to change codes for the table //some variables that make it easier to change codes for the table
String line = ""; String line = "";
final String tableStart = "&#123;&#124;"; // {| final String tableStart = "&#123;&#124;"; // {|
@ -107,97 +108,89 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
final int lenAttribDivider = attribDivider.length(); final int lenAttribDivider = attribDivider.length();
if ((result.startsWith(tableStart)) && (!table)) { if ((result.startsWith(tableStart)) && (!table)) {
table=true; table = true;
newrowstart=true; newrowstart = true;
line="<table"; line = "<table";
if (result.trim().length()>lenTableStart) { if (result.trim().length() > lenTableStart) {
line+=parseTableProperties(result.substring(lenTableStart).trim()).toString(); line += parseTableProperties(result.substring(lenTableStart).trim()).toString();
} }
line+=">"; line += ">";
result=line; result = line;
} } else if (result.startsWith(newLine) && (table)) { // new row
else if (result.startsWith(newLine) && (table)) { // new row
if (!newrowstart) { if (!newrowstart) {
line+="\t</tr>\n"; line += "\t</tr>\n";
} else { } else {
newrowstart=false; newrowstart = false;
} }
line=line+"\t<tr>"; line = line + "\t<tr>";
result=line; result = line;
} } else if ((result.startsWith(cellDivider)) && (table)) {
else if ((result.startsWith(cellDivider)) && (table)) { line += "\t\t<td";
line+="\t\t<td"; final int cellEnd = (result.indexOf(cellDivider, lenCellDivider) > 0) ? (result.indexOf(cellDivider, lenCellDivider)) : (result.length());
final int cellEnd=(result.indexOf(cellDivider,lenCellDivider)>0)?(result.indexOf(cellDivider,lenCellDivider)):(result.length()); int propEnd = result.indexOf(attribDivider, lenCellDivider);
int propEnd = result.indexOf(attribDivider,lenCellDivider); final int occImage = result.indexOf("[[Image:", lenCellDivider);
final int occImage = result.indexOf("[[Image:",lenCellDivider); final int occEscape = result.indexOf("[=", lenCellDivider);
final int occEscape = result.indexOf("[=",lenCellDivider);
//If resultOf("[[Image:") is less than propEnd, that means that there is no //If resultOf("[[Image:") is less than propEnd, that means that there is no
//property for this cell, only an image. Without this, YaCy could get confused //property for this cell, only an image. Without this, YaCy could get confused
//by a | in [[Image:picture.png|alt-text]] or [[Image:picture.png|alt-text]] //by a | in [[Image:picture.png|alt-text]] or [[Image:picture.png|alt-text]]
//Same for [= (part of [= =]) //Same for [= (part of [= =])
if((propEnd > lenCellDivider) if ((propEnd > lenCellDivider) && ((occImage > propEnd) || (occImage < 0)) && ((occEscape > propEnd) || (occEscape < 0))) {
&&((occImage > propEnd)||( occImage < 0)) propEnd = result.indexOf(attribDivider, lenCellDivider) + lenAttribDivider;
&&((occEscape> propEnd)||( occEscape < 0)) } else {
){
propEnd = result.indexOf(attribDivider,lenCellDivider)+lenAttribDivider;
}
else {
propEnd = cellEnd; propEnd = cellEnd;
} }
// both point at same place => new line // both point at same place => new line
if (propEnd==cellEnd) { if (propEnd == cellEnd) {
propEnd=lenCellDivider; propEnd = lenCellDivider;
} } else {
else { line += parseTableProperties(result.substring(lenCellDivider, propEnd - lenAttribDivider).trim()).toString();
line+=parseTableProperties(result.substring(lenCellDivider,propEnd-lenAttribDivider).trim()).toString();
} }
// quick&dirty fix [MN] // quick&dirty fix [MN]
if(propEnd > cellEnd){ if (propEnd > cellEnd) {
propEnd = lenCellDivider; propEnd = lenCellDivider;
} }
table=false; cellprocessing=true; table = false;
line+=">"+processTable(result.substring(propEnd,cellEnd).trim())+"</td>"; cellprocessing = true;
table=true; cellprocessing=false; line += ">" + processTable(result.substring(propEnd, cellEnd).trim()) + "</td>";
if (cellEnd<result.length()) { table = true;
line+="\n"+processTable(result.substring(cellEnd)); cellprocessing = false;
if (cellEnd < result.length()) {
line += "\n" + processTable(result.substring(cellEnd));
} }
result=line; result = line;
} } else if (result.startsWith(tableEnd) && (table)) { // Table end
else if (result.startsWith(tableEnd) && (table)) { // Table end table = false;
table=false; line += "\t</tr>\n</table>" + result.substring(lenTableEnd);
line+="\t</tr>\n</table>"+result.substring(lenTableEnd); result = line;
result=line;
} }
return result; return result;
} }
// contributed by [MN], changes by [FB] // contributed by [MN], changes by [FB]
/** This method takes possible table properties and tests if they are valid. /** This method takes possible table properties and tests if they are valid.
* Valid in this case means if they are a property for the table, tr or td * Valid in this case means if they are a property for the table, tr or td
* tag as stated in the HTML Pocket Reference by Jennifer Niederst (1st edition) * tag as stated in the HTML Pocket Reference by Jennifer Niederst (1st edition)
* The method is important to avoid XSS attacks on the wiki via table properties. * The method is important to avoid XSS attacks on the wiki via table properties.
* @param properties A string that may contain several table properties and/or junk. * @param properties A string that may contain several table properties and/or junk.
* @return A string that only contains table properties. * @return A string that only contains table properties.
*/ */
private static StringBuilder parseTableProperties(final String properties) { private static StringBuilder parseTableProperties(final String properties) {
final String[] values = properties.replaceAll("&quot;", "").split("[= ]"); //splitting the string at = and blanks final String[] values = properties.replaceAll("&quot;", "").split("[= ]"); //splitting the string at = and blanks
final StringBuilder sb = new StringBuilder(properties.length()); final StringBuilder sb = new StringBuilder(properties.length());
String key, value; String key, value;
String[] posVals; String[] posVals;
final int numberofvalues = values.length; final int numberofvalues = values.length;
for (int i=0; i<numberofvalues; i++) { for (int i = 0; i < numberofvalues; i++) {
key = values[i].trim(); key = values[i].trim();
if (key.equals("nowrap")) { if (key.equals("nowrap")) {
addPair("nowrap", "nowrap", sb); addPair("nowrap", "nowrap", sb);
} else if (i + 1 < numberofvalues) { } else if (i + 1 < numberofvalues) {
value = values[++i].trim(); value = values[++i].trim();
if ( if ((key.equals("summary")) ||
(key.equals("summary")) ||
(key.equals("bgcolor") && value.matches("#{0,1}[0-9a-fA-F]{1,6}|[a-zA-Z]{3,}")) || (key.equals("bgcolor") && value.matches("#{0,1}[0-9a-fA-F]{1,6}|[a-zA-Z]{3,}")) ||
((key.equals("width") || key.equals("height")) && value.matches("\\d+%{0,1}")) || ((key.equals("width") || key.equals("height")) && value.matches("\\d+%{0,1}")) ||
((posVals = ps.get(key)) != null && Arrays.binarySearch(posVals, value) >= 0) || ((posVals = ps.get(key)) != null && Arrays.binarySearch(posVals, value) >= 0) ||
(Arrays.binarySearch(tps, key) >= 0 && value.matches("\\d+")) (Arrays.binarySearch(tps, key) >= 0 && value.matches("\\d+"))) {
) {
addPair(key, value, sb); addPair(key, value, sb);
} }
} }
@ -210,157 +203,157 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
} }
/** 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] 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]
//## Sublist //## Sublist
if(result.startsWith(numListLevel + "#")){ //more # if (result.startsWith(numListLevel + "#")) { //more #
p0 = result.indexOf(numListLevel); p0 = result.indexOf(numListLevel);
p1 = result.length(); p1 = result.length();
result = "<ol>" + serverCore.CRLF_STRING + result = "<ol>" + serverCore.CRLF_STRING +
"<li>" + "<li>" +
result.substring(numListLevel.length() + 1, p1) + result.substring(numListLevel.length() + 1, p1) +
"</li>"; "</li>";
numListLevel += "#"; numListLevel += "#";
}else if(numListLevel.length() > 0 && result.startsWith(numListLevel)){ //equal number of # } else if (numListLevel.length() > 0 && result.startsWith(numListLevel)) { //equal number of #
p0 = result.indexOf(numListLevel); p0 = result.indexOf(numListLevel);
p1 = result.length(); p1 = result.length();
result = "<li>" + result = "<li>" +
result.substring(numListLevel.length(), p1) + result.substring(numListLevel.length(), p1) +
"</li>"; "</li>";
}else if(numListLevel.length() > 0){ //less # } else if (numListLevel.length() > 0) { //less #
int i = numListLevel.length(); int i = numListLevel.length();
String tmp = ""; String tmp = "";
while(! result.startsWith(numListLevel.substring(0,i)) ){ while (!result.startsWith(numListLevel.substring(0, i))) {
tmp += "</ol>"; tmp += "</ol>";
i--; i--;
} }
numListLevel = numListLevel.substring(0,i); numListLevel = numListLevel.substring(0, i);
p0 = numListLevel.length(); p0 = numListLevel.length();
p1 = result.length(); p1 = result.length();
if(numListLevel.length() > 0){ if (numListLevel.length() > 0) {
result = tmp + result = tmp +
"<li>" + "<li>" +
result.substring(p0, p1) + result.substring(p0, p1) +
"</li>"; "</li>";
}else{ } else {
result = tmp + result.substring(p0, p1); result = tmp + result.substring(p0, p1);
} }
} }
// end contrib [AS] // end contrib [AS]
} }
return result; return result;
} }
/** This method processes unordered lists. /** This method processes unordered lists.
*/ */
//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] 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]
if(result.startsWith(ListLevel + "*")){ //more stars if (result.startsWith(ListLevel + "*")) { //more stars
p0 = result.indexOf(ListLevel); p0 = result.indexOf(ListLevel);
p1 = result.length(); p1 = result.length();
result = "<ul>" + serverCore.CRLF_STRING + result = "<ul>" + serverCore.CRLF_STRING +
"<li>" + "<li>" +
result.substring(ListLevel.length() + 1, p1) + result.substring(ListLevel.length() + 1, p1) +
"</li>"; "</li>";
ListLevel += "*"; ListLevel += "*";
}else if(ListLevel.length() > 0 && result.startsWith(ListLevel)){ //equal number of stars } else if (ListLevel.length() > 0 && result.startsWith(ListLevel)) { //equal number of stars
p0 = result.indexOf(ListLevel); p0 = result.indexOf(ListLevel);
p1 = result.length(); p1 = result.length();
result = "<li>" + result = "<li>" +
result.substring(ListLevel.length(), p1) + result.substring(ListLevel.length(), p1) +
"</li>"; "</li>";
}else if(ListLevel.length() > 0){ //less stars } else if (ListLevel.length() > 0) { //less stars
int i = ListLevel.length(); int i = ListLevel.length();
String tmp = ""; String tmp = "";
while (ListLevel.length() >= i && !result.startsWith(ListLevel.substring(0,i))) { while (ListLevel.length() >= i && !result.startsWith(ListLevel.substring(0, i))) {
tmp += "</ul>"; tmp += "</ul>";
i--; i--;
} }
p0 = ListLevel.length(); p0 = ListLevel.length();
if (i < p0) { if (i < p0) {
ListLevel = ListLevel.substring(0,i); ListLevel = ListLevel.substring(0, i);
p0 = ListLevel.length(); p0 = ListLevel.length();
} }
p1 = result.length(); p1 = result.length();
if (ListLevel.length() > 0) { if (ListLevel.length() > 0) {
result = tmp + result = tmp +
"<li>" + "<li>" +
result.substring(p0, p1) + result.substring(p0, p1) +
"</li>"; "</li>";
}else{ } else {
result = tmp + result.substring(p0, p1); result = tmp + result.substring(p0, p1);
} }
} }
//end contrib [AS] //end contrib [AS]
} }
return result; return result;
} }
/** This method processes definition lists. /** This method processes definition lists.
*/ */
//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] 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
String dt = ""; String dt = "";
String dd = ""; String dd = "";
p0 = result.indexOf(defListLevel); p0 = result.indexOf(defListLevel);
p1 = result.length(); p1 = result.length();
final String resultCopy = result.substring(defListLevel.length() + 1, p1); final String resultCopy = result.substring(defListLevel.length() + 1, p1);
if((p0 = resultCopy.indexOf(":")) > 0){ if ((p0 = resultCopy.indexOf(":")) > 0) {
dt = resultCopy.substring(0,p0); dt = resultCopy.substring(0, p0);
dd = resultCopy.substring(p0+1); dd = resultCopy.substring(p0 + 1);
result = "<dl>" + "<dt>" + dt + "</dt>" + "<dd>" + dd; result = "<dl>" + "<dt>" + dt + "</dt>" + "<dd>" + dd;
defList = true; defList = true;
} }
defListLevel += ";"; defListLevel += ";";
}else if(defListLevel.length() > 0 && result.startsWith(defListLevel)){ //equal number of semicolons } else if (defListLevel.length() > 0 && result.startsWith(defListLevel)) { //equal number of semicolons
String dt = ""; String dt = "";
String dd = ""; String dd = "";
p0 = result.indexOf(defListLevel); p0 = result.indexOf(defListLevel);
p1 = result.length(); p1 = result.length();
final String resultCopy = result.substring(defListLevel.length(), p1); final String resultCopy = result.substring(defListLevel.length(), p1);
if((p0 = resultCopy.indexOf(":")) > 0){ if ((p0 = resultCopy.indexOf(":")) > 0) {
dt = resultCopy.substring(0,p0); dt = resultCopy.substring(0, p0);
dd = resultCopy.substring(p0+1); dd = resultCopy.substring(p0 + 1);
result = "<dt>" + dt + "</dt>" + "<dd>" + dd; result = "<dt>" + dt + "</dt>" + "<dd>" + dd;
defList = true; defList = true;
} }
}else if(defListLevel.length() > 0){ //less semicolons } else if (defListLevel.length() > 0) { //less semicolons
String dt = ""; String dt = "";
String dd = ""; String dd = "";
int i = defListLevel.length(); int i = defListLevel.length();
String tmp = ""; String tmp = "";
while(! result.startsWith(defListLevel.substring(0,i)) ){ while (!result.startsWith(defListLevel.substring(0, i))) {
tmp += "</dd></dl>"; tmp += "</dd></dl>";
i--; i--;
} }
defListLevel = defListLevel.substring(0,i); defListLevel = defListLevel.substring(0, i);
p0 = defListLevel.length(); p0 = defListLevel.length();
p1 = result.length(); p1 = result.length();
if(defListLevel.length() > 0){ if (defListLevel.length() > 0) {
final String resultCopy = result.substring(p0, p1); final String resultCopy = result.substring(p0, p1);
if((p0 = resultCopy.indexOf(":")) > 0){ if ((p0 = resultCopy.indexOf(":")) > 0) {
dt = resultCopy.substring(0,p0); dt = resultCopy.substring(0, p0);
dd = resultCopy.substring(p0+1); dd = resultCopy.substring(p0 + 1);
result = tmp + "<dt>" + dt + "</dt>" + "<dd>" + dd; result = tmp + "<dt>" + dt + "</dt>" + "<dd>" + dd;
defList = true; defList = true;
} }
}else{ } else {
result = tmp + result.substring(p0, p1); result = tmp + result.substring(p0, p1);
} }
} }
@ -369,7 +362,7 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
} }
/** This method processes links and images. /** This method processes links and images.
*/ */
//contributed by [AS] except where stated otherwise //contributed by [AS] except where stated otherwise
private String linksAndImages(String result) { private String linksAndImages(String result) {
@ -381,7 +374,9 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
// internal links and images // internal links and images
while ((p0 = result.indexOf("[[")) >= 0) { while ((p0 = result.indexOf("[[")) >= 0) {
p1 = result.indexOf("]]", p0 + 2); p1 = result.indexOf("]]", p0 + 2);
if (p1 <= p0) break; if (p1 <= p0) {
break;
}
kl = result.substring(p0 + 2, p1); kl = result.substring(p0 + 2, p1);
// this is the part of the code that's responsible for images // this is the part of the code that's responsible for images
@ -401,21 +396,21 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
align = kv.substring(0, p); align = kv.substring(0, p);
//checking validity of value for align. Only non browser specific //checking validity of value for align. Only non browser specific
//values get supported. Not supported: absmiddle, baseline, texttop //values get supported. Not supported: absmiddle, baseline, texttop
if ((align.equals("bottom"))|| if ((align.equals("bottom")) ||
(align.equals("center"))|| (align.equals("center")) ||
(align.equals("left"))|| (align.equals("left")) ||
(align.equals("middle"))|| (align.equals("middle")) ||
(align.equals("right"))|| (align.equals("right")) ||
(align.equals("top"))) (align.equals("top"))) {
{
align = " align=\"" + align + "\""; align = " align=\"" + align + "\"";
} else {
align = "";
} }
else align = "";
alt = " alt=\"" + kv.substring(p + 6) + "\""; alt = " alt=\"" + kv.substring(p + 6) + "\"";
} } // if there is just one, put it into ALT
// if there is just one, put it into ALT else {
else
alt = " alt=\"" + kv + "\""; alt = " alt=\"" + kv + "\"";
}
} }
// replace incomplete URLs and make them point to http://peerip:port/... // replace incomplete URLs and make them point to http://peerip:port/...
@ -423,14 +418,12 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
// using the wikicode [[Image:share/yacy.gif]] // using the wikicode [[Image:share/yacy.gif]]
// or an image DATA/HTDOCS/grafics/kaskelix.jpg with [[Image:grafics/kaskelix.jpg]] // or an image DATA/HTDOCS/grafics/kaskelix.jpg with [[Image:grafics/kaskelix.jpg]]
// you are free to use other sub-paths of DATA/HTDOCS // you are free to use other sub-paths of DATA/HTDOCS
if (kl.indexOf("://")<1) { if (kl.indexOf("://") < 1) {
kl = "http://" + super.address + "/" + kl; kl = "http://" + super.address + "/" + kl;
} }
result = result.substring(0, p0) + "<img src=\"" + kl + "\"" + align + alt + ">" + result.substring(p1 + 2); result = result.substring(0, p0) + "<img src=\"" + kl + "\"" + align + alt + ">" + result.substring(p1 + 2);
} } // end contrib [MN]
// end contrib [MN]
// if it's no image, it might be an internal link // if it's no image, it might be an internal link
else { else {
if ((p = kl.indexOf("&#124;")) > 0) { if ((p = kl.indexOf("&#124;")) > 0) {
@ -446,13 +439,14 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
// external links // external links
while ((p0 = result.indexOf("[")) >= 0) { while ((p0 = result.indexOf("[")) >= 0) {
p1 = result.indexOf("]", p0 + 1); p1 = result.indexOf("]", p0 + 1);
if (p1 <= p0) break; if (p1 <= p0) {
break;
}
kl = result.substring(p0 + 1, p1); kl = result.substring(p0 + 1, p1);
if ((p = kl.indexOf(" ")) > 0) { if ((p = kl.indexOf(" ")) > 0) {
kv = kl.substring(p + 1); kv = kl.substring(p + 1);
kl = kl.substring(0, p); kl = kl.substring(0, p);
} } // No text for the link? -> <a href="http://www.url.com/">http://www.url.com/</a>
// No text for the link? -> <a href="http://www.url.com/">http://www.url.com/</a>
else { else {
kv = kl; kv = kl;
} }
@ -464,76 +458,70 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
if (kl.indexOf("://") < 1) { if (kl.indexOf("://") < 1) {
kl = "http://" + super.address + "/" + kl; kl = "http://" + super.address + "/" + kl;
} }
result = result.substring(0, p0) + "<a class=\"extern\" href=\"" + kl + "\">" + kv + "</a>" + result.substring(p1 + 1); result = result.substring(0, p0) + "<a class=\"extern\" href=\"" + kl + "\">" + kv + "</a>" + result.substring(p1 + 1);
} }
return result; return result;
} }
/** This method handles the preformatted tags <pre> </pre> */ /** This method handles the preformatted tags <pre> </pre> */
//contributed by [MN] //contributed by [MN]
private String preformattedTag(String result){ private String preformattedTag(String result) {
int p0 = 0; int p0 = 0;
int p1 = 0; int p1 = 0;
//implementation very similar to escape code (see above) //implementation very similar to escape code (see above)
//both <pre> and </pre> in the same line //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 = result.indexOf("&lt;pre&gt;")) >= 0) && ((p1 = result.indexOf("&lt;/pre&gt;")) > 0) && (!(escaped))) {
if(p0<p1){ if (p0 < p1) {
String preformattedText = "<pre style=\"border:dotted;border-width:thin\">"+result.substring(p0+11,p1)+"</pre>"; String preformattedText = "<pre style=\"border:dotted;border-width:thin\">" + result.substring(p0 + 11, p1) + "</pre>";
preformattedText = preformattedText.replaceAll("!pre!", "!pre!!"); preformattedText = preformattedText.replaceAll("!pre!", "!pre!!");
result = transformLine(result.substring(0,p0).replaceAll("!pre!", "!pre!!")+"!pre!txt!"+result.substring(p1+12).replaceAll("!pre!", "!pre!!")); result = transformLine(result.substring(0, p0).replaceAll("!pre!", "!pre!!") + "!pre!txt!" + result.substring(p1 + 12).replaceAll("!pre!", "!pre!!"));
result = result.replaceAll("!pre!txt!", preformattedText); result = result.replaceAll("!pre!txt!", preformattedText);
result = result.replaceAll("!pre!!", "!pre!"); result = result.replaceAll("!pre!!", "!pre!");
} } //handles cases like <pre><pre> </pre></pre> <pre> </pre> that would cause an exception otherwise
//handles cases like <pre><pre> </pre></pre> <pre> </pre> that would cause an exception otherwise else {
else{
preformatted = true; preformatted = true;
final String temp1 = transformLine(result.substring(0,p0-1).replaceAll("!tmp!","!tmp!!")+"!tmp!txt!"); final String temp1 = transformLine(result.substring(0, p0 - 1).replaceAll("!tmp!", "!tmp!!") + "!tmp!txt!");
nolist = true; nolist = true;
final String temp2 = transformLine(result.substring(p0)); final String temp2 = transformLine(result.substring(p0));
nolist = false; nolist = false;
result = temp1.replaceAll("!tmp!txt!",temp2); result = temp1.replaceAll("!tmp!txt!", temp2);
result = result.replaceAll("!tmp!!", "!tmp!"); result = result.replaceAll("!tmp!!", "!tmp!");
preformatted = false; preformatted = false;
} }
} } //start <pre>
else if (((p0 = result.indexOf("&lt;pre&gt;")) >= 0) && (!preformattedSpan) && (!escaped)) {
//start <pre>
else if(((p0 = result.indexOf("&lt;pre&gt;"))>=0)&&(!preformattedSpan)&&(!escaped)){
preformatted = true; //prevent surplus line breaks preformatted = true; //prevent surplus line breaks
String bq =""; //gets filled with <blockquote>s as needed String bq = ""; //gets filled with <blockquote>s as needed
String preformattedText = "<pre style=\"border:dotted;border-width:thin\">"+result.substring(p0+11); String preformattedText = "<pre style=\"border:dotted;border-width:thin\">" + result.substring(p0 + 11);
preformattedText = preformattedText.replaceAll("!pre!", "!pre!!"); preformattedText = preformattedText.replaceAll("!pre!", "!pre!!");
//taking care of indented lines //taking care of indented lines
while(result.substring(preindented,p0).startsWith(":")){ while (result.substring(preindented, p0).startsWith(":")) {
preindented++; preindented++;
bq = bq + "<blockquote>"; bq = bq + "<blockquote>";
} }
result = transformLine(result.substring(preindented,p0).replaceAll("!pre!", "!pre!!")+"!pre!txt!"); result = transformLine(result.substring(preindented, p0).replaceAll("!pre!", "!pre!!") + "!pre!txt!");
result = bq + result.replaceAll("!pre!txt!", preformattedText); result = bq + result.replaceAll("!pre!txt!", preformattedText);
result = result.replaceAll("!pre!!", "!pre!"); result = result.replaceAll("!pre!!", "!pre!");
preformattedSpan = true; preformattedSpan = true;
} } //end </pre>
else if (((p0 = result.indexOf("&lt;/pre&gt;")) >= 0) && (preformattedSpan) && (!escaped)) {
//end </pre>
else if(((p0 = result.indexOf("&lt;/pre&gt;"))>=0)&&(preformattedSpan)&&(!escaped)){
preformattedSpan = false; preformattedSpan = false;
String bq = ""; //gets filled with </blockquote>s as needed String bq = ""; //gets filled with </blockquote>s as needed
String preformattedText = result.substring(0,p0)+"</pre>"; String preformattedText = result.substring(0, p0) + "</pre>";
preformattedText = preformattedText.replaceAll("!pre!", "!pre!!"); preformattedText = preformattedText.replaceAll("!pre!", "!pre!!");
//taking care of indented lines //taking care of indented lines
while (preindented > 0){ while (preindented > 0) {
bq = bq + "</blockquote>"; bq = bq + "</blockquote>";
preindented--; preindented--;
} }
result = transformLine("!pre!txt!"+result.substring(p0+12).replaceAll("!pre!", "!pre!!")); result = transformLine("!pre!txt!" + result.substring(p0 + 12).replaceAll("!pre!", "!pre!!"));
result = result.replaceAll("!pre!txt!", preformattedText) + bq; result = result.replaceAll("!pre!txt!", preformattedText) + bq;
result = result.replaceAll("!pre!!", "!pre!"); result = result.replaceAll("!pre!!", "!pre!");
preformatted = false; preformatted = false;
} } //Getting rid of surplus </pre>
//Getting rid of surplus </pre> else if (((p0 = result.indexOf("&lt;/pre&gt;")) >= 0) && (!preformattedSpan) && (!escaped)) {
else if (((p0 = result.indexOf("&lt;/pre&gt;"))>=0)&&(!preformattedSpan)&&(!escaped)){ while ((p0 = result.indexOf("&lt;/pre&gt;")) >= 0) {
while((p0 = result.indexOf("&lt;/pre&gt;"))>=0){ result = result.substring(0, p0) + result.substring(p0 + 12);
result = result.substring(0,p0)+result.substring(p0+12);
} }
result = transformLine(result); result = transformLine(result);
} }
@ -541,10 +529,10 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
} }
/** 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
*/ */
//method contributed by [MN] //method contributed by [MN]
private String directory(){ private String directory() {
String directory = ""; String directory = "";
String element; String element;
int s = 0; int s = 0;
@ -556,136 +544,147 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
String anchorext = ""; String anchorext = "";
if ((s = dirElements.size()) > 2) { if ((s = dirElements.size()) > 2) {
for (int i = 0; i < s; i++) { for (int i = 0; i < s; i++) {
if (i >= dirElements.size()) break; if (i >= dirElements.size()) {
break;
}
element = dirElements.get(i); element = dirElements.get(i);
//counting double headlines //counting double headlines
doubles = 0; doubles = 0;
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
if (j >= dirElements.size()) break; if (j >= dirElements.size()) {
String d = dirElements.get(j); break;
if (d == null || d.length() < 1) continue; }
String a = d.substring(1).replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]",""); String d = dirElements.get(j);
String b = element.substring(1).replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]",""); if (d == null || d.length() < 1) {
if (a.equals(b)) doubles++; continue;
}
String a = d.substring(1).replaceAll(" ", "_").replaceAll("[^a-zA-Z0-9_]", "");
String b = element.substring(1).replaceAll(" ", "_").replaceAll("[^a-zA-Z0-9_]", "");
if (a.equals(b)) {
doubles++;
}
} }
//if there are doubles, create anchorextension //if there are doubles, create anchorextension
if(doubles>0){ if (doubles > 0) {
anchorext = "_" + (doubles+1); anchorext = "_" + (doubles + 1);
} }
if (element.startsWith("3")){ if (element.startsWith("3")) {
if(level<3){ if (level < 3) {
level = 3; level = 3;
level3 = 0; level3 = 0;
} }
level3++; level3++;
final String temp = element.substring(1); final String temp = element.substring(1);
element=level1+"."+level2+"."+level3+" "+temp; 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"; 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")) {
else if (element.startsWith("2")){ if (level == 1) {
if(level==1){
level2 = 0; level2 = 0;
level = 2; level = 2;
} }
if(level==3){ if (level == 3) {
level = 2; level = 2;
} }
level2++; level2++;
final String temp = element.substring(1); final String temp = element.substring(1);
element=level1+"."+level2+" "+temp; element = level1 + "." + level2 + " " + temp;
directory = directory + "&nbsp;&nbsp;<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br />\n"; 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")) {
else if (element.startsWith("1")) { if (level > 1) {
if (level>1) {
level = 1; level = 1;
level2 = 0; level2 = 0;
level3 = 0; level3 = 0;
} }
level1++; level1++;
final String temp = element.substring(1); final String temp = element.substring(1);
element=level1+". "+temp; element = level1 + ". " + temp;
directory = directory + "<a href=\"#"+temp.replaceAll(" ","_").replaceAll("[^a-zA-Z0-9_]","")+anchorext+"\" class=\"WikiTOC\">"+element+"</a><br />\n"; directory = directory + "<a href=\"#" + temp.replaceAll(" ", "_").replaceAll("[^a-zA-Z0-9_]", "") + anchorext + "\" class=\"WikiTOC\">" + element + "</a><br />\n";
} }
anchorext=""; anchorext = "";
} }
directory = "<table><tr><td><div class=\"WikiTOCBox\">\n" + directory + "</div></td></tr></table>\n"; directory = "<table><tr><td><div class=\"WikiTOCBox\">\n" + directory + "</div></td></tr></table>\n";
} }
// [MN] // [MN]
if(!dirElements.isEmpty()){ if (!dirElements.isEmpty()) {
dirElements.clear(); dirElements.clear();
} }
return directory; return directory;
} }
/** Replaces two occurences of a substring in a string by a pair of strings if /** 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 * 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. * have to run it in a loop if you want to replace all occurences of the substring.
* This method provides special treatment for headlines. * This method provides special treatment for headlines.
* @param input the string that something is to be replaced in * @param input the string that something is to be replaced in
* @param pat substring to be replaced * @param pat substring to be replaced
* @param repl1 string substring gets replaced by on uneven occurences * @param repl1 string substring gets replaced by on uneven occurences
* @param repl2 string substring gets replaced by on even occurences * @param repl2 string substring gets replaced by on even occurences
*/ */
//[MN] //[MN]
private String pairReplace(String input, final String pat, final String repl1, final String repl2){ private String pairReplace(String input, final String pat, final String repl1, final String repl2) {
String direlem = ""; //string to keep headlines until they get added to List dirElements String direlem = ""; //string to keep headlines until they get added to List dirElements
int p0 = 0; int p0 = 0;
int p1 = 0; int p1 = 0;
final int l = pat.length(); final int l = pat.length();
//replace pattern if a pair of the pattern can be found in the line //replace pattern if a pair of the pattern can be found in the line
if( ((p0 = input.indexOf(pat)) >= 0) && ((p1 = input.indexOf(pat, p0 + l)) >= 0) ) { if (((p0 = input.indexOf(pat)) >= 0) && ((p1 = input.indexOf(pat, p0 + l)) >= 0)) {
//extra treatment for headlines //extra treatment for headlines
if((pat.equals("===="))||(pat.equals("==="))||(pat.equals("=="))){ if ((pat.equals("====")) || (pat.equals("===")) || (pat.equals("=="))) {
//add anchor and create headline //add anchor and create headline
direlem = input.substring(p0 + l, p1); direlem = input.substring(p0 + l, p1);
//counting double headlines //counting double headlines
int doubles = 0; int doubles = 0;
for(int i=0;i<dirElements.size();i++){ for (int i = 0; i < dirElements.size(); i++) {
if (dirElements.get(i) == null) continue; if (dirElements.get(i) == null) {
if(dirElements.size() > i && dirElements.get(i).substring(1).equals(direlem)){ continue;
}
if (dirElements.size() > i && dirElements.get(i).substring(1).equals(direlem)) {
doubles++; 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 _ 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 there are doubles, add underscore and number of doubles plus one
if(doubles>0){ if (doubles > 0) {
anchor = anchor + "_" + (doubles+1); anchor = anchor + "_" + (doubles + 1);
} }
input = input.substring(0, p0) + "<a name=\""+anchor+"\"></a>" + repl1 + input = input.substring(0, p0) + "<a name=\"" + anchor + "\"></a>" + repl1 +
direlem + repl2 + input.substring(p1 + l); direlem + repl2 + input.substring(p1 + l);
//add headlines to list of headlines (so TOC can be created) //add headlines to list of headlines (so TOC can be created)
if(pat.equals("====")) dirElements.add("3"+direlem); if (pat.equals("====")) {
else if(pat.equals("===")) dirElements.add("2"+direlem); dirElements.add("3" + direlem);
else if(pat.equals("==")) dirElements.add("1"+direlem); } else if (pat.equals("===")) {
} dirElements.add("2" + direlem);
else{ } else if (pat.equals("==")) {
input = input.substring(0, p0) + repl1 + dirElements.add("1" + direlem);
(/*direlem =*/ input.substring(p0 + l, p1)) + repl2 + }
input.substring(p1 + l); } else {
input = input.substring(0, p0) + repl1 +
(/*direlem =*/input.substring(p0 + l, p1)) + repl2 +
input.substring(p1 + l);
} }
} }
//recursion if a pair of the pattern can still be found in the line //recursion if a pair of the pattern can still be found in the line
if( ((p0 = input.indexOf(pat)) >= 0) && (input.indexOf(pat,p0 + l) >= 0) ){ if (((p0 = input.indexOf(pat)) >= 0) && (input.indexOf(pat, p0 + l) >= 0)) {
input = pairReplace(input, pat, repl1, repl2); input = pairReplace(input, pat, repl1, repl2);
} }
return input; return input;
} }
/** Replaces wiki tags with HTML tags. /** Replaces wiki tags with HTML tags.
* @param result a line of text * @param result a line of text
* @param switchboard * @param switchboard
* @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) { public String transformLine(String result) {
//If HTML has not bee replaced yet (can happen if method gets called in recursion), replace now! //If HTML has not bee replaced yet (can happen if method gets called in recursion), replace now!
if (!replacedHTML || preformattedSpan){ if (!replacedHTML || preformattedSpan) {
result = htmlFilterCharacterCoding.unicode2html(result, true); result = htmlFilterCharacterCoding.unicode2html(result, true);
replacedHTML = true; replacedHTML = true;
} }
//check if line contains preformatted symbols or if we are in a preformatted sequence already. //check if line contains preformatted symbols or if we are in a preformatted sequence already.
if ((result.indexOf("&lt;pre&gt;")>=0)||(result.indexOf("&lt;/pre&gt;")>=0)||(preformattedSpan)){ if ((result.indexOf("&lt;pre&gt;") >= 0) || (result.indexOf("&lt;/pre&gt;") >= 0) || (preformattedSpan)) {
result = preformattedTag(result); result = preformattedTag(result);
} else { } else {
@ -693,14 +692,18 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
result = processTable(result); result = processTable(result);
// format lines // format lines
if (result.startsWith(" ")) result = "<tt>" + result.substring(1) + "</tt>"; if (result.startsWith(" ")) {
if (result.startsWith("----")) result = "<hr />"; result = "<tt>" + result.substring(1) + "</tt>";
}
if (result.startsWith("----")) {
result = "<hr />";
}
// citings contributed by [MN] // citings contributed by [MN]
if(result.startsWith(":")){ if (result.startsWith(":")) {
String head = ""; String head = "";
String tail = ""; String tail = "";
while(result.startsWith(":")){ while (result.startsWith(":")) {
head = head + "<blockquote>"; head = head + "<blockquote>";
tail = tail + "</blockquote>"; tail = tail + "</blockquote>";
result = result.substring(1); result = result.substring(1);
@ -710,13 +713,13 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
// end contrib [MN] // end contrib [MN]
// format headers // format headers
result = pairReplace(result,"====","<h4>","</h4>"); result = pairReplace(result, "====", "<h4>", "</h4>");
result = pairReplace(result,"===","<h3>","</h3>"); result = pairReplace(result, "===", "<h3>", "</h3>");
result = pairReplace(result,"==","<h2>","</h2>"); result = pairReplace(result, "==", "<h2>", "</h2>");
result = pairReplace(result,"'''''","<b><i>","</i></b>"); result = pairReplace(result, "'''''", "<b><i>", "</i></b>");
result = pairReplace(result,"'''","<b>","</b>"); result = pairReplace(result, "'''", "<b>", "</b>");
result = pairReplace(result,"''","<i>","</i>"); result = pairReplace(result, "''", "<i>", "</i>");
result = unorderedList(result); result = unorderedList(result);
result = orderedList(result); result = orderedList(result);
@ -726,8 +729,12 @@ public class wikiCode extends abstractWikiParser implements wikiParser {
} }
if (!preformatted) replacedHTML = false; if (!preformatted) {
if ((result.endsWith("</li>"))||(defList)||(escape)||(preformatted)||(table)||(cellprocessing)) return result; replacedHTML = false;
}
if ((result.endsWith("</li>")) || (defList) || (escape) || (preformatted) || (table) || (cellprocessing)) {
return result;
}
return result + "<br />"; return result + "<br />";
} }
} }

Loading…
Cancel
Save