Wiki
程式作品C 語言JavaC#JavaScript常用函數文字處理遊戲程式衛星定位系統程式資料結構網路程式自然語言人工智慧機率統計資訊安全等待完成訊息相關網站參考文獻最新修改簡體版English |
專案下載:wiki.zip import java.io.*; import java.util.*; import java.util.regex.*; import java.net.*; class Wiki { int MODE = Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE; String mode = "paper"; // or mode = "book" String[] wikiRules; // String head="", tail=""; String defaultTemplate = ""; String encode = ""; Stack<String> templateStack = new Stack(); public static void main(String[] args) throws Exception { String path = "."; if (args.length > 0) path = args[0]; File dir = new File(path); Wiki wiki= new Wiki(); // wiki.head = STR.file2text("head.htm")+"<wiki>\n"; // wiki.tail = "\n</wiki>"+STR.file2text("tail.htm"); wiki.defaultTemplate = STR.file2text("template.htm"); if (args.length > 1) wiki.encode = args[1]; int maxLevels = 9999; if (args.length > 2) maxLevels = Integer.parseInt(args[2]); wiki.dir2html(dir.getAbsoluteFile(), maxLevels); } public String getTemplate() { String template = ""; if (templateStack.isEmpty()) template = defaultTemplate; else template = templateStack.peek(); return template; } public void dir2html(File pDir, int maxLevels) throws Exception { if (maxLevels < 0) return; if (pDir.isFile()) { if (!pDir.getName().endsWith(".wiki")) return; System.out.println(pDir.getName()); // String wiki = text2wiki(pDir, STR.file2text(pDir.getAbsolutePath())); String wiki = text2wiki(pDir); // String html = wiki2html(head+wiki+tail); String embedHtml = wiki2html("<wiki>\n"+wiki+"</wiki>"); String html = STR.replace(getTemplate(), "<?EMBED_HTML?>", embedHtml); STR.text2file(html, STR.replace(pDir.getPath(), ".wiki", ".htm"), encode); } else if (pDir.isDirectory()) { String templatePath = pDir.getCanonicalPath()+"/template.htm"; File templateFile = new File(templatePath); if (templateFile.exists()) { String template = STR.file2text(templatePath); templateStack.push(template); System.out.println("using template : "+templatePath); } File[] files = pDir.listFiles(); for (int i=0; i<files.length; i++) dir2html(files[i], maxLevels-1); if (templateFile.exists()) { templateStack.pop(); } } } public String text2wiki(File pFile) throws Exception { String text = STR.file2text(pFile.getAbsolutePath()); return text; // return text2wiki(pFile, text); } /* public String text2wiki(File pFile, String text) throws Exception { pFile = pFile.getAbsoluteFile(); StringBuffer rzWiki = new StringBuffer(); File parent = pFile.getParentFile(); File[] files = pFile.getParentFile().listFiles(); TreeMap fileMap = new TreeMap(); for (int fi=0; fi<files.length; fi++) { if (files[fi].isDirectory()) continue; String name = files[fi].getName().toLowerCase(); name = STR.last(name, "\\"); if (!name.endsWith(".gif") && !name.endsWith(".jpg")) fileMap.put(name, files[fi]); } // System.out.println(fileMap.keySet()); boolean isInRef = false; for (int i=0; i<text.length();) { String match = null; if (i+2 <= text.length()) { String char2 = text.substring(i,i+2).toLowerCase(); if (char2.equals("[[")) isInRef = true; if (char2.equals("]]")) isInRef = false; if (!isInRef) { Map hitMap = fileMap.subMap(char2, char2+Character.MAX_VALUE); if (hitMap != null) { Object[] names = hitMap.keySet().toArray(); for (int ni=0; ni<names.length; ni++) { String name = (String) names[ni]; String fileName = pFile.getName().toLowerCase(); name = STR.replace(name, ".wiki", ".htm"); fileName = STR.replace(fileName, ".wiki", ".htm"); if (fileName.equals(name)) continue; // name = STR.replace(name, ".htm", ""); if (i+name.length() > text.length()) continue; String token = text.substring(i, i+name.length()); char nextCh = ' '; if (i+name.length()<text.length()) nextCh = text.charAt(i+name.length()); if (token.toLowerCase().equals(name)) { // && Character.isSpace(nextCh) if (match == null) match = token; else if (match.length() < token.length()) match = token; } } } } } if (match != null) { rzWiki.append("[["+match+"]]"); i+=match.length(); } else { rzWiki.append(text.charAt(i)); i++; } } return rzWiki.toString(); } */ public Wiki() throws Exception { wikiRules = text2rules("wikiRules.txt"); System.out.println(wikiRules); } public String wiki2fullhtml(String wikiText) throws Exception { return STR.replace(getTemplate(), "<?EMBED_HTML?>", wiki2html(wikiText)); } public String wiki2html(String wikiText) throws Exception { wikiText = code2html(wikiText); wikiText = math2html(wikiText); wikiText = xml2html(wikiText); wikiText = translate(wikiText, "<wiki>(.*?)</wiki>", wikiRules); wikiText = csv2html(wikiText); if (encode != "") wikiText = "<META http-equiv=Content-Type content=\"text/html; charset="+encode+"\">\n"+wikiText; return wikiText; } public String[] text2rules(String pFileName) throws Exception { String[] rzRules = STR.file2text(pFileName).split("\n"); for (int i=0; i<rzRules.length; i++) { rzRules[i] = STR.replace(rzRules[i], "\\n", "\n"); // System.out.println(rzRules[i]); } return rzRules; } public String translate(String pText, String pPattern, String[] rules) throws Exception { StringBuffer rzText = new StringBuffer(); Pattern p = Pattern.compile(pPattern, MODE); Matcher m = p.matcher(pText); int lastIdx = 0; while (m.find()) { String lastBlock = pText.substring(lastIdx, m.start()); rzText.append(lastBlock); String block = m.group(1); for (int ri=0; ri<rules.length; ri++) { if (rules[ri].startsWith("//")) continue; String fromPat = STR.head(rules[ri], "\t"); String toPat = STR.tail(rules[ri], "\t"); block = transform(block, fromPat, toPat); block = transform(block, fromPat, toPat); block = REGEX.transform(block, "\\^\\((.*?)\\)", "<sup>(?1)</sup>"); block = REGEX.transform(block, "_\\((.*?)\\)", "<sub>(?1)</sub>"); } rzText.append(block); lastIdx = m.end(); } rzText.append(pText.substring(lastIdx)); return rzText.toString(); } public String transform(String pText, String fromPat, String toPat) throws Exception { StringBuffer rzText = new StringBuffer(); Pattern p = Pattern.compile(fromPat, MODE); Matcher m = p.matcher(pText); int lastIdx = 0; while (m.find()) { String lastBlock = pText.substring(lastIdx, m.start()); rzText.append(lastBlock); String pat = toPat; // System.out.println("fromPat="+fromPat+" toPat="+pat+" match="+m.group(0)); for (int gi=1; gi<=m.groupCount(); gi++) { String group = m.group(gi); if (group == null) group = ""; String toGroup = group; pat = STR.replace(pat, "(?"+gi+")", toGroup); pat = STR.replace(pat, "(?e"+gi+")", escape(toGroup)); // transform (?e1) to encode(?e1). } // System.out.println("pat="+pat); rzText.append(pat); lastIdx = m.end(); } rzText.append(pText.substring(lastIdx)); return rzText.toString(); } public String escape(String str) { try { if (str.indexOf("?")>=0) { return str; } else if (str.indexOf("/")>=0) { String head = STR.noLast(str, "/"); String last = STR.last(str, "/"); return head+"/"+URLEncoder.encode(last, "UTF-8"); } else return URLEncoder.encode(str, "UTF-8"); } catch (Exception e) { return str; } } public String math2html(String pText) throws Exception { StringBuffer rzText = new StringBuffer(); Pattern p = Pattern.compile("<math>\\s*(.*?)\\s*</math>", MODE); Matcher m = p.matcher(pText); int lastIdx = 0; while (m.find()) { String lastBlock = pText.substring(lastIdx, m.start()); rzText.append(lastBlock); String block = m.group(1); String mathHtml = convertMath(block); rzText.append(mathHtml); lastIdx = m.end(); } rzText.append(pText.substring(lastIdx)); return rzText.toString(); } /* public String[] symbols = ("alpha;beta;gamma;delta;phi;psi;tau;omega;sigma;theta;"+ "Alpha;Beta;Gamma;Delta;Phi;Psi;Tau;Omega;Sigma;Theta;"+ "and;or;int;sum;oplus;otimes;prod;cup;cap;sub;sup;infin").split(";"); */ public String convertMath(String exp) throws Exception { String html=exp.trim(); if (!exp.startsWith("[:")) html = "[:"+exp+":]"; // System.out.println("exp="+html); html = html.replaceAll("\n", ":]\n[:"); html = html.replaceAll("\\[:", "<table border=0><tr><td>\n"); html = html.replaceAll(":\\]", "\n</td></tr></table>"); html = html.replaceAll(":", "</td><td>"); html = html.replaceAll("//", "<div class=line></div>"); html = html.replaceAll("\\|\\|", "| |"); html = html.replaceAll("\\|", "</div><div>"); html = html.replaceAll("\\[", "</td><td><div>"); html = html.replaceAll("\\]", "</div></td><td>"); html = REGEX.transform(html, "\\^\\((.*?)\\)", "<sup>(?1)</sup>"); html = REGEX.transform(html, "_\\((.*?)\\)", "<sub>(?1)</sub>"); html = REGEX.transform(html, "\\(%(.*?)%\\)", "<small>(?1)</small>"); html = REGEX.transform(html, "\\(\\+(.*?)\\+\\)", "<big>(?1)</big>"); html = html.replaceAll("\\∑", "<font style=\"font-size:200%\">∑</font>"); html = html.replaceAll("\\∫", "<font style=\"font-size:200%\">∫</font>"); html = html.replaceAll("\\&lmat;","<font style=\"font-size:500%;font-family:Courier\">[</font>"); html = html.replaceAll("\\&rmat;","<font style=\"font-size:500%;font-family:Courier\">]</font>"); /* for (int i=0; i<symbols.length; i++) html = html.replaceAll(symbols[i], "&"+symbols[i]+";");*/ // System.out.println("html="+html); return "<center><table><tr><td>"+html+"</td></tr></table></center>"; } public String code2html(String pText) throws Exception { StringBuffer rzText = new StringBuffer(); Pattern p = Pattern.compile("<code>(.*?)</code>", MODE); Matcher m = p.matcher(pText); int lastIdx = 0; while (m.find()) { String lastBlock = pText.substring(lastIdx, m.start()); rzText.append(lastBlock); String block = m.group(1); block = STR.replace(block, "&", "&"); block = STR.replace(block, "<", "<"); block = STR.replace(block, ">", ">"); rzText.append("<pre>"+block+"</pre>"); lastIdx = m.end(); } rzText.append(pText.substring(lastIdx)); return rzText.toString(); } public String csv2html(String pText) throws Exception { StringBuffer rzText = new StringBuffer(); pText = pText.replaceAll("\\\\\\+\\s*\n", " <BR/> "); Pattern p = Pattern.compile("<csv>\\s*(.*?)\\s*</csv>", MODE); Matcher m = p.matcher(pText); int lastIdx = 0; while (m.find()) { String lastBlock = pText.substring(lastIdx, m.start()); rzText.append(lastBlock); String block = m.group(1); String[] lines = block.split("\n"); // System.out.println(Arrays.asList(lines)); rzText.append("<table width=100% class=box>\n"); String[] fields = null; for (int li=0; li<lines.length; li++) { String line = STR.replace(lines[li], "\",\"", "\\A"); line = STR.replace(line, ",", " , "); fields = line.split(","); rzText.append("<tr>"); for (int fi=0; fi<fields.length; fi++) { String field = STR.replace(fields[fi], "\\A", ","); String value = STR.head(field, "!"); String attribute = STR.tail(field, "!"); if (attribute==null) attribute = ""; attribute = attribute.trim(); if (li==0) rzText.append("<th class=csvth "+attribute+">"+value+"</th>"); else if (fi == 0) rzText.append("<td class=csvch "+attribute+">"+value+"</td>"); else rzText.append("<td class=csvtd "+attribute+">"+value+"</td>"); } rzText.append("</tr>\n"); } rzText.append("</table>"); lastIdx = m.end(); } rzText.append(pText.substring(lastIdx)); return rzText.toString(); } public String xml2html(String pText) throws Exception { StringBuffer rzText = new StringBuffer(); Pattern p = Pattern.compile("<xml>\\s*(.*?)\\s*</xml>", MODE); Matcher m = p.matcher(pText); int lastIdx = 0; while (m.find()) { String lastBlock = pText.substring(lastIdx, m.start()); rzText.append(lastBlock); String xml = m.group(1); rzText.append(XML.xml2html(xml)); lastIdx = m.end(); } rzText.append(pText.substring(lastIdx)); return rzText.toString(); } } |
page revision: 1, last edited: 29 Oct 2010 05:21
Post preview:
Close preview