|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.zeppelin.markdown; |
| 19 | + |
| 20 | +import org.apache.commons.io.IOUtils; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.parboiled.BaseParser; |
| 23 | +import org.parboiled.Rule; |
| 24 | +import org.parboiled.support.StringBuilderVar; |
| 25 | +import org.pegdown.Parser; |
| 26 | +import org.pegdown.ast.ExpImageNode; |
| 27 | +import org.pegdown.ast.TextNode; |
| 28 | +import org.pegdown.plugins.BlockPluginParser; |
| 29 | +import org.pegdown.plugins.PegDownPlugins; |
| 30 | + |
| 31 | +import java.io.BufferedReader; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStreamReader; |
| 34 | +import java.io.OutputStreamWriter; |
| 35 | +import java.net.URL; |
| 36 | +import java.net.URLConnection; |
| 37 | +import java.net.URLEncoder; |
| 38 | +import java.nio.charset.StandardCharsets; |
| 39 | + |
| 40 | +public class PegdownWebSequencelPlugin extends Parser implements BlockPluginParser { |
| 41 | + |
| 42 | + public PegdownWebSequencelPlugin() { |
| 43 | + super(PegdownParser.OPTIONS, |
| 44 | + PegdownParser.PARSING_TIMEOUT_AS_MILLIS, |
| 45 | + DefaultParseRunnerProvider); |
| 46 | + } |
| 47 | + |
| 48 | + public PegdownWebSequencelPlugin(Integer options, |
| 49 | + Long maxParsingTimeInMillis, |
| 50 | + ParseRunnerProvider parseRunnerProvider, |
| 51 | + PegDownPlugins plugins) { |
| 52 | + super(options, maxParsingTimeInMillis, parseRunnerProvider, plugins); |
| 53 | + } |
| 54 | + |
| 55 | + public static final String TAG = "%%%"; |
| 56 | + |
| 57 | + Rule StartMarker() { |
| 58 | + return Sequence(Spn1(), TAG, Sp(), "sequence", Sp()); |
| 59 | + } |
| 60 | + |
| 61 | + String EndMarker() { |
| 62 | + return TAG; |
| 63 | + } |
| 64 | + |
| 65 | + Rule Body() { |
| 66 | + return OneOrMore(TestNot(TAG), BaseParser.ANY); |
| 67 | + } |
| 68 | + |
| 69 | + Rule BlockRule() { |
| 70 | + StringBuilderVar style = new StringBuilderVar(); |
| 71 | + StringBuilderVar body = new StringBuilderVar(); |
| 72 | + |
| 73 | + return NodeSequence( |
| 74 | + StartMarker(), |
| 75 | + String("style="), |
| 76 | + Sequence(OneOrMore(Letter()), style.append(match()), Spn1()), |
| 77 | + Sequence(Body(), body.append(match())), |
| 78 | + EndMarker(), |
| 79 | + push( |
| 80 | + new ExpImageNode("title", |
| 81 | + createWebsequenceUrl(style.getString(), body.getString()), |
| 82 | + new TextNode("")) |
| 83 | + ) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public static String createWebsequenceUrl(String style, |
| 88 | + String content) { |
| 89 | + |
| 90 | + style = StringUtils.defaultString(style, "default"); |
| 91 | + |
| 92 | + OutputStreamWriter writer = null; |
| 93 | + BufferedReader reader = null; |
| 94 | + |
| 95 | + try { |
| 96 | + String query = new StringBuilder() |
| 97 | + .append("style=") |
| 98 | + .append(style) |
| 99 | + .append("&message=") |
| 100 | + .append(URLEncoder.encode(content, "UTF-8")) |
| 101 | + .append("&apiVersion=1") |
| 102 | + .toString(); |
| 103 | + |
| 104 | + URL url = new URL("http://www.websequencediagrams.com"); |
| 105 | + URLConnection conn = url.openConnection(); |
| 106 | + conn.setDoOutput(true); |
| 107 | + writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); |
| 108 | + writer.write(query); |
| 109 | + writer.flush(); |
| 110 | + |
| 111 | + StringBuilder response = new StringBuilder(); |
| 112 | + reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); |
| 113 | + String line; |
| 114 | + while ((line = reader.readLine()) != null) { |
| 115 | + response.append(line); |
| 116 | + } |
| 117 | + |
| 118 | + writer.close(); |
| 119 | + reader.close(); |
| 120 | + |
| 121 | + String json = response.toString(); |
| 122 | + |
| 123 | + int start = json.indexOf("?png="); |
| 124 | + int end = json.indexOf("\"", start); |
| 125 | + |
| 126 | + if (start != -1 && end != -1) { |
| 127 | + return "http://www.websequencediagrams.com/" + json.substring(start, end); |
| 128 | + } |
| 129 | + } catch (IOException e) { |
| 130 | + throw new RuntimeException("Failed to get proper response from websequencediagrams.com"); |
| 131 | + } finally { |
| 132 | + IOUtils.closeQuietly(writer); |
| 133 | + IOUtils.closeQuietly(reader); |
| 134 | + } |
| 135 | + |
| 136 | + return ""; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Rule[] blockPluginRules() { |
| 141 | + return new Rule[]{BlockRule()}; |
| 142 | + } |
| 143 | +} |
0 commit comments