001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.utils;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.util.StringTokenizer;
022:
023: /**
024: * This class translates TiddlyWiki markup to MediaWiki markup.
025: * @author Michael Greifeneder mikegr@gmx.net
026: */
027: public class TiddlyWiki2MediaWikiTranslator {
028:
029: private static final WikiLogger logger = WikiLogger
030: .getLogger(TiddlyWiki2MediaWikiTranslator.class.getName());
031:
032: public static String newline = System.getProperty("line.separator");
033:
034: public String translate(String wikicode) throws IOException {
035: String replaced = wikicode.replaceAll("\\\\n", "\n");
036: replaced = insertBreaks(replaced);
037: logger.info("Content with breaks?: " + replaced);
038: replaced = tables(headers(wikiLinks(replaced)));
039: return replaced;
040: }
041:
042: private String tables(String wikicode) throws IOException {
043: BufferedReader reader = new BufferedReader(new StringReader(
044: wikicode));
045: StringBuffer output = new StringBuffer();
046: boolean inTable = false;
047: String line = reader.readLine();
048: while (line != null) {
049: if (inTable) {
050: if (line.startsWith("|")) {
051: output.append("|-"); //new row
052: output.append(newline);
053: output.append(translateTableLine(line));
054: } else {
055: output.append("|}");
056: output.append(newline);
057: output.append(line);
058: inTable = false;
059: }
060: } else {
061: if (line.startsWith("|")) {
062: output.append("{|");
063: output.append(newline);
064: inTable = true;
065: output.append(translateTableLine(line));
066: } else {
067: output.append(line);
068: }
069: }
070: line = reader.readLine();
071: if (line != null) {
072: output.append(newline);
073: }
074: }
075: if (inTable) {
076: output.append(newline);
077: output.append("|}");
078: }
079: return output.toString();
080: }
081:
082: private String translateTableLine(String line) {
083: String[] tokens = line.split("\\|");
084: StringBuffer output = new StringBuffer();
085: output.append("|");
086: for (int i = 0; i < tokens.length; i++) {
087: String token = tokens[i];
088: if (i > 0) {
089: output.append("||");
090: }
091: if (token.startsWith("!")) { //headers bold
092: output.append("'''");
093: }
094: output.append(token);
095: if (token.startsWith("!")) {
096: output.append("'''");
097: }
098: }
099: return output.toString();
100: }
101:
102: private String headers(String wikicode) throws IOException {
103: BufferedReader reader = new BufferedReader(new StringReader(
104: wikicode));
105: String line = null;
106: StringBuffer output = new StringBuffer();
107: while ((line = reader.readLine()) != null) {
108: int i = 0;
109: while (line.length() > i && line.charAt(i) == '!') {
110: output.append('=');
111: i++;
112: }
113: output.append(line.substring(i));
114: for (int j = 0; j < i; j++) {
115: output.append('=');
116: }
117: output.append(newline);
118: }
119: return output.toString();
120: }
121:
122: public String wikiLinks(String wikicode) {
123: StringBuffer output = new StringBuffer();
124: StringTokenizer tokenizer = new StringTokenizer(wikicode,
125: " \t\n\r\f<>", true);
126: while (tokenizer.hasMoreTokens()) {
127: String token = tokenizer.nextToken();
128: if (token.length() > 0
129: && Character.isUpperCase(token.charAt(0))) {
130: String rest = token.substring(1);
131: if (!rest.toLowerCase().equals(rest)) {
132: output.append("[[");
133: output.append(token);
134: output.append("]]");
135: } else {
136: output.append(token);
137: }
138: } else {
139: output.append(token);
140: }
141: }
142: return output.toString();
143: }
144:
145: public String insertBreaks(String wikicode) throws IOException {
146: BufferedReader reader = new BufferedReader(new StringReader(
147: wikicode));
148: StringBuffer output = new StringBuffer();
149: boolean isLetterLast = false;
150: boolean isLetterNow = false;
151: String line = reader.readLine();
152: String lastLine = null;
153: while (line != null) {
154: isLetterLast = isLetterNow;
155: if (line != null && line.length() > 0
156: && Character.isLetterOrDigit(line.charAt(0))) {
157: isLetterNow = true;
158: } else {
159: isLetterNow = false;
160: }
161: if (lastLine != null) {
162: output.append(lastLine);
163: }
164: if (isLetterLast && isLetterNow) {
165: output.append("<br/>");
166: }
167: if (lastLine != null) {
168: output.append(newline);
169: }
170: lastLine = line;
171: line = reader.readLine();
172: if (line == null) {
173: output.append(lastLine);
174: }
175: }
176: return output.toString();
177: }
178: }
|