root/šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Wiki.java @ 133:8628ef19f353

Revision 133:8628ef19f353, 5.4 KB (checked in by František Kučera <franta-hg@…>, 10 years ago)

wiki: konstanta utf-8

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, 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 */
18package cz.frantovo.xmlWebGenerator.makra;
19
20import java.io.IOException;
21import java.io.PrintStream;
22import static cz.frantovo.xmlWebGenerator.NástrojeCLI.*;
23import java.io.BufferedReader;
24import java.io.InputStreamReader;
25import java.io.OutputStreamWriter;
26import java.net.URL;
27import java.net.URLConnection;
28import java.net.URLEncoder;
29import java.nio.charset.StandardCharsets;
30
31/**
32 * Wiki syntaxe
33 *
34 * @author František Kučera (frantovo.cz)
35 */
36public class Wiki {
37
38        public enum SYNTAXE {
39
40                markdown,
41                texy
42        }
43        private static final String PŘÍKAZ_MARKDOWN = "markdown";
44        /**
45         * Zde číhá tento PHP skript:
46         * https://hg.frantovo.cz/nekurak.net/file/tip/php/texy/http/index.php
47         */
48        private static final String URL_TEXY = "http://nekurak.net/texy/http/";
49
50        /**
51         * Převede text ve wiki syntaxi do XHTML.
52         *
53         * @param wiki vstupní text v dané wiki syntaxi
54         * @param syntaxe null nebo volitelně syntaxe (markdown, texy)
55         * @return naformátované XHTML
56         * TODO:
57         * - vracet místo textu instanci com.icl.saxon.om.NodeInfo
58         * http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
59         * - nebo kontrolovat validitu vygenerovaného kódu (v současnosti se spoléháme na bezchybnost
60         * markdownu případně texy)
61         *
62         */
63        public static String formátujWiki(String wiki, String syntaxe) throws IOException {
64                if (syntaxe == null || SYNTAXE.valueOf(syntaxe) == SYNTAXE.markdown) {
65                        return formátujMarkdown(wiki);
66                } else if (SYNTAXE.valueOf(syntaxe) == SYNTAXE.texy) {
67                        return formátujTexy(wiki);
68                } else {
69                        throw new IllegalArgumentException("Syntaxe není podporovaná: " + syntaxe);
70                }
71        }
72
73        private static String formátujMarkdown(String wiki) throws IOException {
74                if (isPříkazDostupný(PŘÍKAZ_MARKDOWN)) {
75                        Runtime r = Runtime.getRuntime();
76                        Process p = r.exec(new String[]{PŘÍKAZ_MARKDOWN});
77
78                        /**
79                         * TODO: oříznout mezery na začátcích řádků, pokud je jich všude stejně?
80                         * (odsazení v XML)
81                         */
82                        PrintStream vstupProcesu = new PrintStream(p.getOutputStream());
83                        vstupProcesu.print(wiki);
84                        vstupProcesu.close();
85
86                        String chyby = načtiProud(p.getErrorStream());
87                        String xhtml = načtiProud(p.getInputStream());
88
89                        if (chyby.length() == 0) {
90                                return xhtml;
91                        } else {
92                                System.err.print("Při zpracování wiki syntaxe došlo k chybě: " + chyby);
93                                return null;
94                        }
95                } else {
96                        System.err.println("Příkaz " + PŘÍKAZ_MARKDOWN + " není na vašem systému dostupný → nelze formátovat texty ve wiki syntaxi.");
97                        System.err.println("Můžete ho nainstalovat pomocí:");
98                        System.err.println("\t$ aptitude install markdown         # (Debian/Ubuntu)");
99                        System.err.println("\t$ yum install perl-Text-Markdown    # (Fedora/RedHat)");
100                        return null;
101                }
102        }
103
104        /**
105         * Texy! syntaxe je experimentální a oficiálně nepodporovaná.
106         *
107         * TODO: až bude balíček texy pro GNU/Linuxové distribuce:
108         * http://forum.texy.info/cs/873-balicek-pro-linuxove-distribuce
109         * řešit stejně jako Markdown.
110         */
111        private static String formátujTexy(String wiki) throws IOException {
112                System.out.println("Pozor: Texy! wiki syntaxe je experimentální a oficiálně nepodporovaná.");
113                System.out.println("Pozor: používáte na vlastní nebezpečí!");
114                System.out.println("Pozor: text k interpretování bude odeslán na vzdálené URL: " + URL_TEXY);
115                System.out.println("Pokračovat? [a/N]");
116                int pokračovat = System.in.read();
117
118                if (pokračovat == 'a') {
119                        OutputStreamWriter požadavek = null;
120                        BufferedReader odpověď = null;                 
121                        try {
122                                URL url = new URL(URL_TEXY);
123                                URLConnection spojeni = url.openConnection();
124                                spojeni.setDoOutput(true);
125
126                                /** Odešleme data */
127                                požadavek = new OutputStreamWriter(spojeni.getOutputStream());
128                                požadavek.write(URLEncoder.encode(wiki, StandardCharsets.UTF_8.name()));
129                                požadavek.flush();
130
131                                /** Přijmeme odpověď */
132                                odpověď = new BufferedReader(new InputStreamReader(spojeni.getInputStream(), StandardCharsets.UTF_8.name()));
133                                StringBuilder vysledek = new StringBuilder();
134                                String radka;
135                                while ((radka = odpověď.readLine()) != null) {
136                                        vysledek.append(radka);
137                                        vysledek.append("\n");
138                                }
139
140                                return vysledek.toString();
141                        } catch (Exception e) {
142                                throw new RuntimeException("Chyba při zpracovávání Texy! syntaxe: " + wiki, e);
143                        } finally {
144                                try {
145                                        požadavek.close();
146                                } catch (IOException e) {
147                                        e.printStackTrace(System.err);
148                                }
149                                try {
150                                        odpověď.close();
151                                } catch (IOException e) {
152                                        e.printStackTrace(System.err);
153                                }
154                        }
155                } else {
156                        String hláška = "Texy! wiki syntaxe nebyla interpretována. Zdrojový text nebyl nikam odeslán.";
157                        System.out.println(hláška);
158                        return "<!-- " + hláška + " -->";
159                }
160        }
161}
Note: See TracBrowser for help on using the browser.