franta-hg@76: /** franta-hg@76: * XML Web generátor – program na generování webových stránek franta-hg@76: * Copyright © 2012 František Kučera (frantovo.cz) franta-hg@76: * franta-hg@76: * This program is free software: you can redistribute it and/or modify franta-hg@76: * it under the terms of the GNU General Public License as published by franta-hg@76: * the Free Software Foundation, either version 3 of the License, or franta-hg@76: * (at your option) any later version. franta-hg@76: * franta-hg@76: * This program is distributed in the hope that it will be useful, franta-hg@76: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@76: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@76: * GNU General Public License for more details. franta-hg@76: * franta-hg@76: * You should have received a copy of the GNU General Public License franta-hg@76: * along with this program. If not, see . franta-hg@76: */ franta-hg@76: package cz.frantovo.xmlWebGenerator.makra; franta-hg@76: franta-hg@76: import java.io.IOException; franta-hg@76: import java.io.PrintStream; franta-hg@76: import static cz.frantovo.xmlWebGenerator.NástrojeCLI.*; franta-hg@76: franta-hg@76: /** franta-hg@76: * Zvýrazňování syntaxe franta-hg@76: * franta-hg@76: * @author František Kučera (frantovo.cz) franta-hg@76: */ franta-hg@76: public class Pre { franta-hg@76: franta-hg@76: private static final String PŘÍKAZ_PYGMENTIZE = "pygmentize"; franta-hg@76: franta-hg@76: /** franta-hg@76: * Zvýrazňuje syntaxi zdrojového kódu. Používá k tomu externí program/knihovnu pygmentize. franta-hg@76: * @param zdroják zdrojový kód, který předáme příkazu pygmentize na standardním vstupu franta-hg@76: * @param jazyk předáme příkazu pygmentize jako parametr -l <lexer> franta-hg@76: * @return zvýrazněný text nebo null, pokud došlo k chybě. franta-hg@76: * TODO: franta-hg@76: * - vracet místo textu instanci com.icl.saxon.om.NodeInfo http://saxon.sourceforge.net/saxon6.5.3/extensibility.html franta-hg@76: * - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost pygmentize) franta-hg@76: */ franta-hg@76: public static String zvýrazniSyntaxi(String zdroják, String jazyk) throws IOException, InterruptedException { franta-hg@76: if (jazyk == null || jazyk.length() == 0) { franta-hg@76: System.err.println("Není vyplněn atribut „jazyk“ → není jasné, jak se má zvýrazňovat."); franta-hg@76: return null; franta-hg@76: } else if (isPříkazDostupný(PŘÍKAZ_PYGMENTIZE)) { franta-hg@76: Runtime r = Runtime.getRuntime(); franta-hg@76: Process p = r.exec(new String[]{PŘÍKAZ_PYGMENTIZE, "-f", "html", "-l", jazyk}); franta-hg@76: franta-hg@76: PrintStream vstupProcesu = new PrintStream(p.getOutputStream()); franta-hg@76: vstupProcesu.print(zdroják); franta-hg@76: vstupProcesu.close(); franta-hg@76: franta-hg@76: String výsledek = načtiProud(p.getInputStream()); franta-hg@76: String chyby = načtiProud(p.getErrorStream()); franta-hg@76: franta-hg@76: p.waitFor(); franta-hg@76: franta-hg@76: if (chyby.length() == 0) { franta-hg@76: // Pozor: pygmentize má i při chybě návratový kód 0 → je potřeba kontrolovat chybový výstup. franta-hg@76: return výsledek; franta-hg@76: } else { franta-hg@76: System.err.print("Při zvýrazňování syntaxe došlo k chybě: " + chyby); franta-hg@76: return null; franta-hg@76: } franta-hg@76: } else { franta-hg@76: System.err.println("Příkaz " + PŘÍKAZ_PYGMENTIZE + " není na vašem systému dostupný → zvýrazňování syntaxe nebude fungovat."); franta-hg@76: System.err.println("Můžete ho nainstalovat pomocí:"); franta-hg@76: System.err.println("\t$ aptitude install python-pygments # (Debian/Ubuntu)"); franta-hg@76: System.err.println("\t$ yum install python-pygments # (Fedora/RedHat)"); franta-hg@76: return null; franta-hg@76: } franta-hg@76: } franta-hg@76: franta-hg@76: /** franta-hg@76: * Vygeneruje CSS styl pro zvýrazňování syntaxe. franta-hg@76: * @return obsah CSS souboru nebo null, pokud generování nebylo možné franta-hg@76: */ franta-hg@76: public static String generujCssSyntaxe() throws IOException, InterruptedException { franta-hg@76: if (isPříkazDostupný(PŘÍKAZ_PYGMENTIZE)) { franta-hg@76: Runtime r = Runtime.getRuntime(); franta-hg@76: Process p = r.exec(new String[]{PŘÍKAZ_PYGMENTIZE, "-S", "default", "-f", "html"}); franta-hg@76: return načtiProud(p.getInputStream()); franta-hg@76: } else { franta-hg@76: return null; franta-hg@76: } franta-hg@76: } franta-hg@76: } franta-hg@87: