šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java
changeset 76 c7746d95283d
child 87 25dec6931f18
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java	Sun Jan 08 16:52:30 2012 +0100
     1.3 @@ -0,0 +1,73 @@
     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 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + * 
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + * GNU General Public License for more details.
    1.17 + * 
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.xmlWebGenerator.makra;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.PrintStream;
    1.25 +import static cz.frantovo.xmlWebGenerator.NástrojeCLI.*;
    1.26 +
    1.27 +/**
    1.28 + * Wiki syntaxe
    1.29 + * 
    1.30 + * @author František Kučera (frantovo.cz)
    1.31 + */
    1.32 +public class Wiki {
    1.33 +
    1.34 +	private static final String PŘÍKAZ_MARKDOWN = "markdown";
    1.35 +
    1.36 +	/**
    1.37 +	 * Převede text ve wiki syntaxi do XHTML.
    1.38 +	 * @param wiki vstupní text v dané wiki syntaxi
    1.39 +	 * @param syntaxe null nebo volitelně syntaxe (markdown, texy)
    1.40 +	 * @return naformátované XHTML
    1.41 +	 * TODO: 
    1.42 +	 *	- vracet místo textu instanci com.icl.saxon.om.NodeInfo http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
    1.43 +	 *  - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost markdownu)
    1.44 +	 
    1.45 +	 */
    1.46 +	public static String formátujWiki(String wiki, String syntaxe) throws IOException {
    1.47 +		if (isPříkazDostupný(PŘÍKAZ_MARKDOWN)) {
    1.48 +			Runtime r = Runtime.getRuntime();
    1.49 +			Process p = r.exec(new String[]{PŘÍKAZ_MARKDOWN});
    1.50 +
    1.51 +			/**
    1.52 +			 * TODO: oříznout mezery na začátcích řádků, pokud je jich všude stejně?
    1.53 +			 * (odsazení v XML)
    1.54 +			 */
    1.55 +			PrintStream vstupProcesu = new PrintStream(p.getOutputStream());
    1.56 +			vstupProcesu.print(wiki);
    1.57 +			vstupProcesu.close();
    1.58 +
    1.59 +			String chyby = načtiProud(p.getErrorStream());
    1.60 +			String xhtml = načtiProud(p.getInputStream());
    1.61 +
    1.62 +			if (chyby.length() == 0) {
    1.63 +				return xhtml;
    1.64 +			} else {
    1.65 +				System.err.print("Při zpracování wiki syntaxe došlo k chybě: " + chyby);
    1.66 +				return null;
    1.67 +			}
    1.68 +		} else {
    1.69 +			System.err.println("Příkaz " + PŘÍKAZ_MARKDOWN + " není na vašem systému dostupný → nelze formátovat texty ve wiki syntaxi.");
    1.70 +			System.err.println("Můžete ho nainstalovat pomocí:");
    1.71 +			System.err.println("\t$ aptitude install markdown         # (Debian/Ubuntu)");
    1.72 +			System.err.println("\t$ yum install perl-Text-Markdown    # (Fedora/RedHat)");
    1.73 +			return null;
    1.74 +		}
    1.75 +	}
    1.76 +}