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