šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Pre.java
changeset 76 c7746d95283d
child 87 25dec6931f18
equal deleted inserted replaced
75:dbf6e5dbeecd 76:c7746d95283d
       
     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  * Zvýrazňování syntaxe
       
    26  * 
       
    27  * @author František Kučera (frantovo.cz)
       
    28  */
       
    29 public class Pre {
       
    30 
       
    31 	private static final String PŘÍKAZ_PYGMENTIZE = "pygmentize";
       
    32 
       
    33 	/**
       
    34 	 * Zvýrazňuje syntaxi zdrojového kódu. Používá k tomu externí program/knihovnu pygmentize.
       
    35 	 * @param zdroják zdrojový kód, který předáme příkazu pygmentize na standardním vstupu
       
    36 	 * @param jazyk předáme příkazu pygmentize jako parametr -l &lt;lexer&gt;
       
    37 	 * @return zvýrazněný text nebo null, pokud došlo k chybě.
       
    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 pygmentize)
       
    41 	 */
       
    42 	public static String zvýrazniSyntaxi(String zdroják, String jazyk) throws IOException, InterruptedException {
       
    43 		if (jazyk == null || jazyk.length() == 0) {
       
    44 			System.err.println("Není vyplněn atribut „jazyk“ → není jasné, jak se má zvýrazňovat.");
       
    45 			return null;
       
    46 		} else if (isPříkazDostupný(PŘÍKAZ_PYGMENTIZE)) {
       
    47 			Runtime r = Runtime.getRuntime();
       
    48 			Process p = r.exec(new String[]{PŘÍKAZ_PYGMENTIZE, "-f", "html", "-l", jazyk});
       
    49 
       
    50 			PrintStream vstupProcesu = new PrintStream(p.getOutputStream());
       
    51 			vstupProcesu.print(zdroják);
       
    52 			vstupProcesu.close();
       
    53 
       
    54 			String výsledek = načtiProud(p.getInputStream());
       
    55 			String chyby = načtiProud(p.getErrorStream());
       
    56 
       
    57 			p.waitFor();
       
    58 
       
    59 			if (chyby.length() == 0) {
       
    60 				// Pozor: pygmentize má i při chybě návratový kód 0 → je potřeba kontrolovat chybový výstup.
       
    61 				return výsledek;
       
    62 			} else {
       
    63 				System.err.print("Při zvýrazňování syntaxe došlo k chybě: " + chyby);
       
    64 				return null;
       
    65 			}
       
    66 		} else {
       
    67 			System.err.println("Příkaz " + PŘÍKAZ_PYGMENTIZE + " není na vašem systému dostupný → zvýrazňování syntaxe nebude fungovat.");
       
    68 			System.err.println("Můžete ho nainstalovat pomocí:");
       
    69 			System.err.println("\t$ aptitude install python-pygments   # (Debian/Ubuntu)");
       
    70 			System.err.println("\t$ yum install python-pygments        # (Fedora/RedHat)");
       
    71 			return null;
       
    72 		}
       
    73 	}
       
    74 
       
    75 	/**
       
    76 	 * Vygeneruje CSS styl pro zvýrazňování syntaxe.
       
    77 	 * @return obsah CSS souboru nebo null, pokud generování nebylo možné
       
    78 	 */
       
    79 	public static String generujCssSyntaxe() throws IOException, InterruptedException {
       
    80 		if (isPříkazDostupný(PŘÍKAZ_PYGMENTIZE)) {
       
    81 			Runtime r = Runtime.getRuntime();
       
    82 			Process p = r.exec(new String[]{PŘÍKAZ_PYGMENTIZE, "-S", "default", "-f", "html"});
       
    83 			return načtiProud(p.getInputStream());
       
    84 		} else {
       
    85 			return null;
       
    86 		}
       
    87 	}
       
    88 }