001: package vqwiki.lex;
002:
003: import org.apache.log4j.Logger;
004: import vqwiki.Environment;
005:
006: public class ExLayoutLexConvert {
007: private boolean even = false;
008:
009: private boolean list_ul = false;
010:
011: private int list_level = 0;
012:
013: private boolean multitableheader = false;
014:
015: protected static Logger cat = Logger
016: .getLogger(ExLayoutLexConvert.class);
017:
018: protected String virtualWiki;
019:
020: public void setVirtualWiki(String vWiki) {
021: this .virtualWiki = vWiki;
022: }
023:
024: public String onTableCell(String text) {
025: StringBuffer sbuf = new StringBuffer();
026: sbuf.append("<tr "
027: + (even ? "class=\"even\"" : "class=\"odd\"") + ">");
028: even = !even;
029: int startindex = 0;
030: int endindex = 0;
031: while ((startindex = text.indexOf("||", startindex)) != -1) {
032: endindex = text.indexOf("||", startindex + 2);
033: if (endindex != -1) {
034: sbuf.append("<td>");
035: sbuf.append(text.substring(startindex + 2, endindex));
036: sbuf.append("</td>");
037: startindex = endindex;
038: } else {
039: startindex += 2;
040: }
041: }
042: sbuf.append("</tr>\n");
043: return sbuf.toString();
044: }
045:
046: public String onTableEnd() {
047: StringBuffer sbuf = new StringBuffer();
048: sbuf.append("</tbody></table>\n");
049: return sbuf.toString();
050: }
051:
052: public String onTableEOF() {
053: return "</tbody></table>\n";
054: }
055:
056: public String onTableStart(String text) {
057: even = false;
058: StringBuffer sbuf = new StringBuffer();
059: sbuf.append("<table><tbody>\n");
060: sbuf.append(onTableCell(text));
061: return sbuf.toString();
062: }
063:
064: public String onTableStartWithHeader(String text) {
065: even = false;
066: StringBuffer sbuf2 = new StringBuffer(text);
067: int index = 0;
068: while (-1 != (index = sbuf2.indexOf("|!"))) {
069: sbuf2.replace(index, index + 2, "||");
070: }
071: text = sbuf2.toString();
072: StringBuffer sbuf = new StringBuffer();
073: sbuf.append("<table>\n");
074: sbuf.append("<thead>\n");
075: sbuf.append(onTableCell(text));
076: sbuf.append("</thead><tbody>\n");
077: return sbuf.toString();
078: }
079:
080: public String onListBegin(String text) {
081: text = text.trim();
082: StringBuffer sbuf = new StringBuffer();
083: // decide ordered or unordered list.
084: if (text.charAt(0) == '*') {
085: list_ul = true;
086: } else {
087: list_ul = false;
088: }
089: list_level = getListLevel(text);
090: for (int i = list_level; i > 0; i--) {
091: sbuf.append(list_ul ? "<ul>\n" : "<ol>\n");
092: }
093: sbuf.append("<li>");
094: sbuf.append(text.substring(list_level));
095: return sbuf.toString();
096: }
097:
098: public String onListEnd() {
099: StringBuffer sbuf = new StringBuffer();
100: sbuf.append("</li>");
101: for (int i = list_level; i > 0; i--) {
102: sbuf.append(list_ul ? "</ul>\n" : "</ol>\n");
103: }
104: return sbuf.toString();
105: }
106:
107: public String onListEOF() {
108: return onListEnd();
109: }
110:
111: public String onListLine(String text) {
112: text = text.trim();
113: StringBuffer sbuf = new StringBuffer();
114: sbuf.append("</li>");
115: int count = getListLevel(text);
116: int diff = count - list_level;
117: if (diff != 0) {
118: String html;
119: // First decide to open or close lists.
120: if (diff > 0) {
121: html = list_ul ? "<ul>\n" : "<ol>\n";
122: } else {
123: html = list_ul ? "</ul>\n" : "</ol>\n";
124: }
125: // make the diff positive for the for-loop.
126: diff = diff > 0 ? diff : -diff;
127: // Print open or closing lists.
128: while (diff-- > 0) {
129: sbuf.append(html);
130: }
131: // change List-level global.
132: list_level = count;
133: }
134: sbuf.append("<li>");
135: sbuf.append(text.substring(list_level));
136: return sbuf.toString();
137: }
138:
139: private int getListLevel(String text) {
140: // get the level of the list entry.
141: int count = 0;
142: while ((text.charAt(count) == '*')
143: || (text.charAt(count) == '#')) {
144: count++;
145: if (count >= text.length())
146: break;
147: }
148: return count;
149: }
150:
151: public String onNewLine() {
152: cat.debug("{newline}");
153: StringBuffer buffer = new StringBuffer();
154: for (int i = 0; i < Environment.getInstance().getIntSetting(
155: Environment.PROPERTY_NEW_LINE_BREAKS); i++)
156: buffer.append("<br/>");
157: buffer.append("\n");
158: return buffer.toString();
159: }
160:
161: public String onPreFormatBegin() {
162: cat.debug("@@@@{newline} entering PRE");
163: return "<pre>";
164: }
165:
166: public String onPreFormatEnd() {
167: cat.debug("{newline}x2 leaving pre");
168: return "</pre>\n";
169: }
170:
171: public String onRemoveWhitespace(String text) {
172: StringBuffer buffer = new StringBuffer();
173: for (int i = 0; i < text.length(); i++) {
174: buffer.append((int) text.charAt(i));
175: }
176: cat.debug("{whitespace} " + buffer.toString());
177: return " ";
178: }
179:
180: public String onKeepWhitespace(String text) {
181: cat.debug("PRE, EXTERNAL {whitespace}");
182: return text;
183: }
184:
185: public String onHorizontalRuler() {
186: cat.debug("{hr}");
187: return "\n<hr>\n";
188: }
189:
190: public String onMultiTableStart(String text) {
191: multitableheader = false;
192: StringBuffer sbuf = new StringBuffer();
193: sbuf.append("<table><tbody>\n");
194: sbuf.append("<tr class=\"odd\">");
195: // because the first row is odd, we need to set this
196: // to even for the next row.
197: even = true;
198: sbuf.append(onMultiTableCell(text));
199: return sbuf.toString();
200: }
201:
202: public String onMultiTableRow(String text) {
203: StringBuffer sbuf = new StringBuffer();
204: sbuf.append("</tr>\n");
205: if (multitableheader) {
206: sbuf.append("</thead>\n");
207: sbuf.append("<tbody>\n");
208: multitableheader = false;
209: }
210: sbuf.append("<tr "
211: + (even ? "class=\"even\"" : "class=\"odd\"") + ">");
212: even = !even;
213: return sbuf.toString();
214: }
215:
216: public String onMultiTableCell(String text) {
217: StringBuffer sbuf = new StringBuffer();
218: sbuf.append("<td>");
219: sbuf.append(text.substring(2));
220: sbuf.append("</td>");
221: return sbuf.toString();
222: }
223:
224: public String onMultiTableEnd() {
225: StringBuffer sbuf = new StringBuffer();
226: sbuf.append("</tr>");
227: if (multitableheader) {
228: sbuf.append("</thead>\n");
229: } else {
230: sbuf.append("</tbody>\n");
231: }
232: sbuf.append("</table>\n");
233: return sbuf.toString();
234: }
235:
236: public String onMultiTableStartWithHeader(String text) {
237: multitableheader = true;
238: StringBuffer sbuf = new StringBuffer();
239: sbuf.append("<table>\n");
240: sbuf.append("<thead>\n");
241: sbuf.append("<tr class=\"odd\">");
242: // because the first row is odd, we need to set this
243: // to even for the next row.
244: even = true;
245: sbuf.append(onMultiTableCell(text));
246: return sbuf.toString();
247: }
248:
249: public String onMultiTableEOF() {
250: return onMultiTableEnd();
251: }
252: }
|