root/šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Pre.java @ 136:d5feb9d8ebc3

Revision 136:d5feb9d8ebc3, 3.4 KB (checked in by František Kučera <franta-hg@…>, 5 years ago)

fix license version: GNU GPLv3

Line 
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, version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package cz.frantovo.xmlWebGenerator.makra;
18
19import java.io.IOException;
20import java.io.PrintStream;
21import static cz.frantovo.xmlWebGenerator.NástrojeCLI.*;
22
23/**
24 * Zvýrazňování syntaxe
25 *
26 * @author František Kučera (frantovo.cz)
27 */
28public class Pre {
29
30        private static final String PŘÍKAZ_PYGMENTIZE = "pygmentize";
31
32        /**
33         * Zvýrazňuje syntaxi zdrojového kódu. Používá k tomu externí program/knihovnu pygmentize.
34         * @param zdroják zdrojový kód, který předáme příkazu pygmentize na standardním vstupu
35         * @param jazyk předáme příkazu pygmentize jako parametr -l &lt;lexer&gt;
36         * @return zvýrazněný text nebo null, pokud došlo k chybě.
37         * TODO:
38         *      - vracet místo textu instanci com.icl.saxon.om.NodeInfo http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
39         *  - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost pygmentize)
40         */
41        public static String zvýrazniSyntaxi(String zdroják, String jazyk) throws IOException, InterruptedException {
42                if (jazyk == null || jazyk.length() == 0) {
43                        System.err.println("Není vyplněn atribut „jazyk“ → není jasné, jak se má zvýrazňovat.");
44                        return null;
45                } else if (isPříkazDostupný(PŘÍKAZ_PYGMENTIZE)) {
46                        Runtime r = Runtime.getRuntime();
47                        Process p = r.exec(new String[]{PŘÍKAZ_PYGMENTIZE, "-f", "html", "-l", jazyk});
48
49                        PrintStream vstupProcesu = new PrintStream(p.getOutputStream());
50                        vstupProcesu.print(zdroják);
51                        vstupProcesu.close();
52
53                        String výsledek = načtiProud(p.getInputStream());
54                        String chyby = načtiProud(p.getErrorStream());
55
56                        p.waitFor();
57
58                        if (chyby.length() == 0) {
59                                // Pozor: pygmentize má i při chybě návratový kód 0 → je potřeba kontrolovat chybový výstup.
60                                return výsledek;
61                        } else {
62                                System.err.print("Při zvýrazňování syntaxe došlo k chybě: " + chyby);
63                                return null;
64                        }
65                } else {
66                        System.err.println("Příkaz " + PŘÍKAZ_PYGMENTIZE + " není na vašem systému dostupný → zvýrazňování syntaxe nebude fungovat.");
67                        System.err.println("Můžete ho nainstalovat pomocí:");
68                        System.err.println("\t$ aptitude install python-pygments   # (Debian/Ubuntu)");
69                        System.err.println("\t$ yum install python-pygments        # (Fedora/RedHat)");
70                        return null;
71                }
72        }
73
74        /**
75         * Vygeneruje CSS styl pro zvýrazňování syntaxe.
76         * @return obsah CSS souboru nebo null, pokud generování nebylo možné
77         */
78        public static String generujCssSyntaxe() throws IOException, InterruptedException {
79                if (isPříkazDostupný(PŘÍKAZ_PYGMENTIZE)) {
80                        Runtime r = Runtime.getRuntime();
81                        Process p = r.exec(new String[]{PŘÍKAZ_PYGMENTIZE, "-S", "default", "-f", "html"});
82                        return načtiProud(p.getInputStream());
83                } else {
84                        return null;
85                }
86        }
87}
88
Note: See TracBrowser for help on using the browser.