šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java
author František Kučera <franta-hg@frantovo.cz>
Sat Jan 14 19:10:26 2012 +0100 (2012-01-14)
changeset 87 25dec6931f18
parent 76 c7746d95283d
child 92 8d34f2020884
permissions -rw-r--r--
Lepší odsazení, tabulátory.
     1 /**
     2  * XML Web generátor – program na generování webových stránek
     3  * Copyright © 2012 František Kučera (frantovo.cz)
     4  * 
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  * 
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  * 
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.xmlWebGenerator.makra;
    19 
    20 import java.io.IOException;
    21 import java.io.PrintStream;
    22 import static cz.frantovo.xmlWebGenerator.NástrojeCLI.*;
    23 
    24 /**
    25  * Wiki syntaxe
    26  * 
    27  * @author František Kučera (frantovo.cz)
    28  */
    29 public class Wiki {
    30 
    31 	private static final String PŘÍKAZ_MARKDOWN = "markdown";
    32 
    33 	/**
    34 	 * Převede text ve wiki syntaxi do XHTML.
    35 	 * @param wiki vstupní text v dané wiki syntaxi
    36 	 * @param syntaxe null nebo volitelně syntaxe (markdown, texy)
    37 	 * @return naformátované XHTML
    38 	 * TODO: 
    39 	 *	- vracet místo textu instanci com.icl.saxon.om.NodeInfo http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
    40 	 *  - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost markdownu)
    41 	 
    42 	 */
    43 	public static String formátujWiki(String wiki, String syntaxe) throws IOException {
    44 		if (isPříkazDostupný(PŘÍKAZ_MARKDOWN)) {
    45 			Runtime r = Runtime.getRuntime();
    46 			Process p = r.exec(new String[]{PŘÍKAZ_MARKDOWN});
    47 
    48 			/**
    49 			 * TODO: oříznout mezery na začátcích řádků, pokud je jich všude stejně?
    50 			 * (odsazení v XML)
    51 			 */
    52 			PrintStream vstupProcesu = new PrintStream(p.getOutputStream());
    53 			vstupProcesu.print(wiki);
    54 			vstupProcesu.close();
    55 
    56 			String chyby = načtiProud(p.getErrorStream());
    57 			String xhtml = načtiProud(p.getInputStream());
    58 
    59 			if (chyby.length() == 0) {
    60 				return xhtml;
    61 			} else {
    62 				System.err.print("Při zpracování wiki syntaxe došlo k chybě: " + chyby);
    63 				return null;
    64 			}
    65 		} else {
    66 			System.err.println("Příkaz " + PŘÍKAZ_MARKDOWN + " není na vašem systému dostupný → nelze formátovat texty ve wiki syntaxi.");
    67 			System.err.println("Můžete ho nainstalovat pomocí:");
    68 			System.err.println("\t$ aptitude install markdown         # (Debian/Ubuntu)");
    69 			System.err.println("\t$ yum install perl-Text-Markdown    # (Fedora/RedHat)");
    70 			return null;
    71 		}
    72 	}
    73 }
    74