šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java
changeset 92 8d34f2020884
parent 87 25dec6931f18
child 133 8628ef19f353
     1.1 --- a/šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java	Wed Feb 08 17:58:17 2012 +0100
     1.2 +++ b/šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java	Thu Feb 09 12:54:49 2012 +0100
     1.3 @@ -1,46 +1,75 @@
     1.4  /**
     1.5   * XML Web generátor – program na generování webových stránek
     1.6   * Copyright © 2012 František Kučera (frantovo.cz)
     1.7 - * 
     1.8 + *
     1.9   * This program is free software: you can redistribute it and/or modify
    1.10   * it under the terms of the GNU General Public License as published by
    1.11   * the Free Software Foundation, either version 3 of the License, or
    1.12   * (at your option) any later version.
    1.13 - * 
    1.14 + *
    1.15   * This program is distributed in the hope that it will be useful,
    1.16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.19   * GNU General Public License for more details.
    1.20 - * 
    1.21 + *
    1.22   * You should have received a copy of the GNU General Public License
    1.23 - * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.24 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.25   */
    1.26  package cz.frantovo.xmlWebGenerator.makra;
    1.27  
    1.28  import java.io.IOException;
    1.29  import java.io.PrintStream;
    1.30  import static cz.frantovo.xmlWebGenerator.NástrojeCLI.*;
    1.31 +import java.io.BufferedReader;
    1.32 +import java.io.InputStreamReader;
    1.33 +import java.io.OutputStreamWriter;
    1.34 +import java.net.URL;
    1.35 +import java.net.URLConnection;
    1.36 +import java.net.URLEncoder;
    1.37  
    1.38  /**
    1.39   * Wiki syntaxe
    1.40 - * 
    1.41 + *
    1.42   * @author František Kučera (frantovo.cz)
    1.43   */
    1.44  public class Wiki {
    1.45  
    1.46 +	public enum SYNTAXE {
    1.47 +
    1.48 +		markdown,
    1.49 +		texy
    1.50 +	}
    1.51  	private static final String PŘÍKAZ_MARKDOWN = "markdown";
    1.52 +	/**
    1.53 +	 * Zde číhá tento PHP skript:
    1.54 +	 * https://hg.frantovo.cz/nekurak.net/file/tip/php/texy/http/index.php
    1.55 +	 */
    1.56 +	private static final String URL_TEXY = "http://nekurak.net/texy/http/";
    1.57  
    1.58  	/**
    1.59  	 * Převede text ve wiki syntaxi do XHTML.
    1.60 +	 *
    1.61  	 * @param wiki vstupní text v dané wiki syntaxi
    1.62  	 * @param syntaxe null nebo volitelně syntaxe (markdown, texy)
    1.63  	 * @return naformátované XHTML
    1.64 -	 * TODO: 
    1.65 -	 *	- vracet místo textu instanci com.icl.saxon.om.NodeInfo http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
    1.66 -	 *  - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost markdownu)
    1.67 -	 
    1.68 +	 * TODO:
    1.69 +	 * - vracet místo textu instanci com.icl.saxon.om.NodeInfo
    1.70 +	 * http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
    1.71 +	 * - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost
    1.72 +	 * markdownu případně texy)
    1.73 +	 *
    1.74  	 */
    1.75  	public static String formátujWiki(String wiki, String syntaxe) throws IOException {
    1.76 +		if (syntaxe == null || SYNTAXE.valueOf(syntaxe) == SYNTAXE.markdown) {
    1.77 +			return formátujMarkdown(wiki);
    1.78 +		} else if (SYNTAXE.valueOf(syntaxe) == SYNTAXE.texy) {
    1.79 +			return formátujTexy(wiki);
    1.80 +		} else {
    1.81 +			throw new IllegalArgumentException("Syntaxe není podporovaná: " + syntaxe);
    1.82 +		}
    1.83 +	}
    1.84 +
    1.85 +	private static String formátujMarkdown(String wiki) throws IOException {
    1.86  		if (isPříkazDostupný(PŘÍKAZ_MARKDOWN)) {
    1.87  			Runtime r = Runtime.getRuntime();
    1.88  			Process p = r.exec(new String[]{PŘÍKAZ_MARKDOWN});
    1.89 @@ -70,5 +99,63 @@
    1.90  			return null;
    1.91  		}
    1.92  	}
    1.93 +
    1.94 +	/**
    1.95 +	 * Texy! syntaxe je experimentální a oficiálně nepodporovaná.
    1.96 +	 *
    1.97 +	 * TODO: až bude balíček texy pro GNU/Linuxové distribuce:
    1.98 +	 * http://forum.texy.info/cs/873-balicek-pro-linuxove-distribuce
    1.99 +	 * řešit stejně jako Markdown.
   1.100 +	 */
   1.101 +	private static String formátujTexy(String wiki) throws IOException {
   1.102 +		System.out.println("Pozor: Texy! wiki syntaxe je experimentální a oficiálně nepodporovaná.");
   1.103 +		System.out.println("Pozor: používáte na vlastní nebezpečí!");
   1.104 +		System.out.println("Pozor: text k interpretování bude odeslán na vzdálené URL: " + URL_TEXY);
   1.105 +		System.out.println("Pokračovat? [a/N]");
   1.106 +		int pokračovat = System.in.read();
   1.107 +
   1.108 +		if (pokračovat == 'a') {
   1.109 +			OutputStreamWriter požadavek = null;
   1.110 +			BufferedReader odpověď = null;
   1.111 +			final String kódování = "UTF-8";
   1.112 +			try {
   1.113 +				URL url = new URL(URL_TEXY);
   1.114 +				URLConnection spojeni = url.openConnection();
   1.115 +				spojeni.setDoOutput(true);
   1.116 +
   1.117 +				/** Odešleme data */
   1.118 +				požadavek = new OutputStreamWriter(spojeni.getOutputStream());
   1.119 +				požadavek.write(URLEncoder.encode(wiki, kódování));
   1.120 +				požadavek.flush();
   1.121 +
   1.122 +				/** Přijmeme odpověď */
   1.123 +				odpověď = new BufferedReader(new InputStreamReader(spojeni.getInputStream(), kódování));
   1.124 +				StringBuilder vysledek = new StringBuilder();
   1.125 +				String radka;
   1.126 +				while ((radka = odpověď.readLine()) != null) {
   1.127 +					vysledek.append(radka);
   1.128 +					vysledek.append("\n");
   1.129 +				}
   1.130 +
   1.131 +				return vysledek.toString();
   1.132 +			} catch (Exception e) {
   1.133 +				throw new RuntimeException("Chyba při zpracovávání Texy! syntaxe: " + wiki, e);
   1.134 +			} finally {
   1.135 +				try {
   1.136 +					požadavek.close();
   1.137 +				} catch (IOException e) {
   1.138 +					e.printStackTrace(System.err);
   1.139 +				}
   1.140 +				try {
   1.141 +					odpověď.close();
   1.142 +				} catch (IOException e) {
   1.143 +					e.printStackTrace(System.err);
   1.144 +				}
   1.145 +			}
   1.146 +		} else {
   1.147 +			String hláška = "Texy! wiki syntaxe nebyla interpretována. Zdrojový text nebyl nikam odeslán.";
   1.148 +			System.out.println(hláška);
   1.149 +			return "<!-- " + hláška + " -->";
   1.150 +		}
   1.151 +	}
   1.152  }
   1.153 -