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

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