diff --git a/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownParser.java b/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownParser.java index bbadd977d8..e38c4ea9a1 100644 --- a/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownParser.java +++ b/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownParser.java @@ -19,17 +19,21 @@ package org.apache.zeppelin.markdown; import org.pegdown.Extensions; import org.pegdown.PegDownProcessor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.pegdown.plugins.PegDownPlugins; /** Markdown Parser using pegdown processor. */ public class PegdownParser implements MarkdownParser { private PegDownProcessor processor; + public static final long PARSING_TIMEOUT_AS_MILLIS = 5000; + public static final int OPTIONS = Extensions.ALL_WITH_OPTIONALS - Extensions.ANCHORLINKS; + public PegdownParser() { - int pegdownOptions = Extensions.ALL_WITH_OPTIONALS - Extensions.ANCHORLINKS; - int parsingTimeoutAsMillis = 5000; - processor = new PegDownProcessor(pegdownOptions, parsingTimeoutAsMillis); + PegDownPlugins plugins = new PegDownPlugins.Builder() + .withPlugin(PegdownYumlPlugin.class) + .withPlugin(PegdownWebSequencelPlugin.class) + .build(); + processor = new PegDownProcessor(OPTIONS, PARSING_TIMEOUT_AS_MILLIS, plugins); } @Override diff --git a/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownWebSequencelPlugin.java b/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownWebSequencelPlugin.java new file mode 100644 index 0000000000..48a1720b8b --- /dev/null +++ b/markdown/src/main/java/org/apache/zeppelin/markdown/PegdownWebSequencelPlugin.java @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zeppelin.markdown; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.parboiled.BaseParser; +import org.parboiled.Rule; +import org.parboiled.support.StringBuilderVar; +import org.pegdown.Parser; +import org.pegdown.ast.ExpImageNode; +import org.pegdown.ast.TextNode; +import org.pegdown.plugins.BlockPluginParser; +import org.pegdown.plugins.PegDownPlugins; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class PegdownWebSequencelPlugin extends Parser implements BlockPluginParser { + + public PegdownWebSequencelPlugin() { + super(PegdownParser.OPTIONS, + PegdownParser.PARSING_TIMEOUT_AS_MILLIS, + DefaultParseRunnerProvider); + } + + public PegdownWebSequencelPlugin(Integer options, + Long maxParsingTimeInMillis, + ParseRunnerProvider parseRunnerProvider, + PegDownPlugins plugins) { + super(options, maxParsingTimeInMillis, parseRunnerProvider, plugins); + } + + public static final String TAG = "%%%"; + + Rule StartMarker() { + return Sequence(Spn1(), TAG, Sp(), "sequence", Sp()); + } + + String EndMarker() { + return TAG; + } + + Rule Body() { + return OneOrMore(TestNot(TAG), BaseParser.ANY); + } + + Rule BlockRule() { + StringBuilderVar style = new StringBuilderVar(); + StringBuilderVar body = new StringBuilderVar(); + + return NodeSequence( + StartMarker(), + String("style="), + Sequence(OneOrMore(Letter()), style.append(match()), Spn1()), + Sequence(Body(), body.append(match())), + EndMarker(), + push( + new ExpImageNode("title", + createWebsequenceUrl(style.getString(), body.getString()), + new TextNode("")) + ) + ); + } + + public static String createWebsequenceUrl(String style, + String content) { + + style = StringUtils.defaultString(style, "default"); + + OutputStreamWriter writer = null; + BufferedReader reader = null; + + try { + String query = new StringBuilder() + .append("style=") + .append(style) + .append("&message=") + .append(URLEncoder.encode(content, "UTF-8")) + .append("&apiVersion=1") + .toString(); + + URL url = new URL("http://www.websequencediagrams.com"); + URLConnection conn = url.openConnection(); + conn.setDoOutput(true); + writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); + writer.write(query); + writer.flush(); + + StringBuilder response = new StringBuilder(); + reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + + writer.close(); + reader.close(); + + String json = response.toString(); + + int start = json.indexOf("?png="); + int end = json.indexOf("\"", start); + + if (start != -1 && end != -1) { + return "http://www.websequencediagrams.com/" + json.substring(start, end); + } + } catch (IOException e) { + throw new RuntimeException("Failed to get proper response from websequencediagrams.com"); + } finally { + IOUtils.closeQuietly(writer); + IOUtils.closeQuietly(reader); + } + + return ""; + } + + @Override + public Rule[] blockPluginRules() { + return new Rule[]{BlockRule()}; + } +} diff --git a/markdown/src/test/java/org/apache/zeppelin/markdown/PegdownParserTest.java b/markdown/src/test/java/org/apache/zeppelin/markdown/PegdownParserTest.java index 3b60155077..432e54a484 100644 --- a/markdown/src/test/java/org/apache/zeppelin/markdown/PegdownParserTest.java +++ b/markdown/src/test/java/org/apache/zeppelin/markdown/PegdownParserTest.java @@ -22,8 +22,13 @@ import static org.junit.Assert.assertEquals; import java.util.Properties; import org.apache.zeppelin.interpreter.InterpreterResult; + import static org.apache.zeppelin.markdown.PegdownParser.wrapWithMarkdownClassDiv; +import static org.junit.Assert.assertThat; + +import org.hamcrest.CoreMatchers; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -75,39 +80,39 @@ public class PegdownParserTest { public void testStrikethrough() { InterpreterResult result = md.interpret("This is ~~deleted~~ text", null); assertEquals( - wrapWithMarkdownClassDiv("

This is deleted text

"), result.message()); + wrapWithMarkdownClassDiv("

This is deleted text

"), result.message()); } @Test public void testItalics() { InterpreterResult result = md.interpret("This is *italics* text", null); assertEquals( - wrapWithMarkdownClassDiv("

This is italics text

"), result.message()); + wrapWithMarkdownClassDiv("

This is italics text

"), result.message()); } @Test public void testStrongEmphasis() { InterpreterResult result = md.interpret("This is **strong emphasis** text", null); assertEquals( - wrapWithMarkdownClassDiv("

This is strong emphasis text

"), - result.message()); + wrapWithMarkdownClassDiv("

This is strong emphasis text

"), + result.message()); } @Test public void testOrderedList() { String input = - new StringBuilder() - .append("1. First ordered list item\n") - .append("2. Another item") - .toString(); + new StringBuilder() + .append("1. First ordered list item\n") + .append("2. Another item") + .toString(); String expected = - new StringBuilder() - .append("
    \n") - .append("
  1. First ordered list item
  2. \n") - .append("
  3. Another item
  4. \n") - .append("
") - .toString(); + new StringBuilder() + .append("
    \n") + .append("
  1. First ordered list item
  2. \n") + .append("
  3. Another item
  4. \n") + .append("
") + .toString(); InterpreterResult result = md.interpret(input, null); assertEquals(wrapWithMarkdownClassDiv(expected), result.message()); @@ -116,20 +121,20 @@ public class PegdownParserTest { @Test public void testUnorderedList() { String input = - new StringBuilder() - .append("* Unordered list can use asterisks\n") - .append("- Or minuses\n") - .append("+ Or pluses") - .toString(); + new StringBuilder() + .append("* Unordered list can use asterisks\n") + .append("- Or minuses\n") + .append("+ Or pluses") + .toString(); String expected = - new StringBuilder() - .append("") - .toString(); + new StringBuilder() + .append("") + .toString(); InterpreterResult result = md.interpret(input, null); assertEquals(wrapWithMarkdownClassDiv(expected), result.message()); @@ -138,49 +143,49 @@ public class PegdownParserTest { @Test public void testLinks() { String input = - new StringBuilder() - .append("[I'm an inline-style link](https://www.google.com)\n") - .append("\n") - .append( - "[I'm an inline-style link with title](https://www.google.com \"Google's Homepage\")\n") - .append("\n") - .append("[I'm a reference-style link][Arbitrary case-insensitive reference text]\n") - .append("\n") - .append("[I'm a relative reference to a repository file](../blob/master/LICENSE)\n") - .append("\n") - .append("[You can use numbers for reference-style link definitions][1]\n") - .append("\n") - .append("Or leave it empty and use the [link text itself].\n") - .append("\n") - .append("URLs and URLs in angle brackets will automatically get turned into links. \n") - .append("http://www.example.com or and sometimes \n") - .append("example.com (but not on Github, for example).\n") - .append("\n") - .append("Some text to show that the reference links can follow later.\n") - .append("\n") - .append("[arbitrary case-insensitive reference text]: https://www.mozilla.org\n") - .append("[1]: http://slashdot.org\n") - .append("[link text itself]: http://www.reddit.com") - .toString(); + new StringBuilder() + .append("[I'm an inline-style link](https://www.google.com)\n") + .append("\n") + .append( + "[I'm an inline-style link with title](https://www.google.com \"Google's Homepage\")\n") + .append("\n") + .append("[I'm a reference-style link][Arbitrary case-insensitive reference text]\n") + .append("\n") + .append("[I'm a relative reference to a repository file](../blob/master/LICENSE)\n") + .append("\n") + .append("[You can use numbers for reference-style link definitions][1]\n") + .append("\n") + .append("Or leave it empty and use the [link text itself].\n") + .append("\n") + .append("URLs and URLs in angle brackets will automatically get turned into links. \n") + .append("http://www.example.com or and sometimes \n") + .append("example.com (but not on Github, for example).\n") + .append("\n") + .append("Some text to show that the reference links can follow later.\n") + .append("\n") + .append("[arbitrary case-insensitive reference text]: https://www.mozilla.org\n") + .append("[1]: http://slashdot.org\n") + .append("[link text itself]: http://www.reddit.com") + .toString(); String expected = - new StringBuilder() - .append( - "

I’m an inline-style link

\n") - .append( - "

I’m an inline-style link with title

\n") - .append( - "

I’m a reference-style link

\n") - .append( - "

I’m a relative reference to a repository file

\n") - .append( - "

You can use numbers for reference-style link definitions

\n") - .append( - "

Or leave it empty and use the link text itself.

\n") - .append( - "

URLs and URLs in angle brackets will automatically get turned into links.
http://www.example.com or http://www.example.com and sometimes
example.com (but not on Github, for example).

\n") - .append("

Some text to show that the reference links can follow later.

") - .toString(); + new StringBuilder() + .append( + "

I’m an inline-style link

\n") + .append( + "

I’m an inline-style link with title

\n") + .append( + "

I’m a reference-style link

\n") + .append( + "

I’m a relative reference to a repository file

\n") + .append( + "

You can use numbers for reference-style link definitions

\n") + .append( + "

Or leave it empty and use the link text itself.

\n") + .append( + "

URLs and URLs in angle brackets will automatically get turned into links.
http://www.example.com or http://www.example.com and sometimes
example.com (but not on Github, for example).

\n") + .append("

Some text to show that the reference links can follow later.

") + .toString(); InterpreterResult result = md.interpret(input, null); assertEquals(wrapWithMarkdownClassDiv(expected), result.message()); @@ -190,71 +195,71 @@ public class PegdownParserTest { public void testInlineCode() { InterpreterResult result = md.interpret("Inline `code` has `back-ticks around` it.", null); assertEquals( - wrapWithMarkdownClassDiv( - "

Inline code has back-ticks around it.

"), - result.message()); + wrapWithMarkdownClassDiv( + "

Inline code has back-ticks around it.

"), + result.message()); } @Test public void testBlockQuotes() { InterpreterResult r1 = - md.interpret( - "> Blockquotes are very handy in email to emulate reply text.\n" - + "> This line is part of the same quote.", - null); + md.interpret( + "> Blockquotes are very handy in email to emulate reply text.\n" + + "> This line is part of the same quote.", + null); assertEquals( - wrapWithMarkdownClassDiv( - "
\n" - + "

Blockquotes are very handy in email to emulate reply text.
This line is part of the same quote.

\n" - + "
"), - r1.message()); + wrapWithMarkdownClassDiv( + "
\n" + + "

Blockquotes are very handy in email to emulate reply text.
This line is part of the same quote.

\n" + + "
"), + r1.message()); InterpreterResult r2 = - md.interpret( - "> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **MarkdownInterpreter** into a blockquote. ", - null); + md.interpret( + "> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **MarkdownInterpreter** into a blockquote. ", + null); assertEquals( - wrapWithMarkdownClassDiv( - "
\n" - + "

This is a very long line that will still be quoted properly when it wraps. Oh boy let’s keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can put MarkdownInterpreter into a blockquote.

\n" - + "
"), - r2.message()); + wrapWithMarkdownClassDiv( + "
\n" + + "

This is a very long line that will still be quoted properly when it wraps. Oh boy let’s keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can put MarkdownInterpreter into a blockquote.

\n" + + "
"), + r2.message()); } @Test public void testSimpleTable() { String input = - new StringBuilder() - .append("MarkdownInterpreter | Less | Pretty\n") - .append("--- | --- | ---\n") - .append("*Still* | `renders` | **nicely**\n") - .append("1 | 2 | 3") - .toString(); + new StringBuilder() + .append("MarkdownInterpreter | Less | Pretty\n") + .append("--- | --- | ---\n") + .append("*Still* | `renders` | **nicely**\n") + .append("1 | 2 | 3") + .toString(); String expected = - new StringBuilder() - .append("\n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append("
MarkdownInterpreter Less Pretty
Still renders nicely
1 2 3
") - .toString(); + new StringBuilder() + .append("\n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append("
MarkdownInterpreter Less Pretty
Still renders nicely
1 2 3
") + .toString(); InterpreterResult result = md.interpret(input, null); assertEquals(wrapWithMarkdownClassDiv(expected), result.message()); @@ -262,41 +267,71 @@ public class PegdownParserTest { @Test public void testAlignedTable() { - String input = - new StringBuilder() - .append("| First Header | Second Header | Third Header |\n") - .append("| :----------- | :-----------: | -------------------: |\n") - .append("| First row | Data | Very long data entry |\n") - .append("| Second row | **Cell** | *Cell* |") - .toString(); + new StringBuilder() + .append("| First Header | Second Header | Third Header |\n") + .append("| :----------- | :-----------: | -------------------: |\n") + .append("| First row | Data | Very long data entry |\n") + .append("| Second row | **Cell** | *Cell* |") + .toString(); String expected = - new StringBuilder() - .append("\n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append(" \n") - .append("
First Header Second Header Third Header
First row Data Very long data entry
Second row Cell Cell
") - .toString(); + new StringBuilder() + .append("\n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append(" \n") + .append("
First Header Second Header Third Header
First row Data Very long data entry
Second row Cell Cell
") + .toString(); InterpreterResult result = md.interpret(input, null); assertEquals(wrapWithMarkdownClassDiv(expected), result.message()); } + + @Test + public void testWebsequencePlugin() { + String input = + new StringBuilder() + .append("\n \n %%% sequence style=modern-blue\n") + .append("title Authentication Sequence\n") + .append("Alice->Bob: Authentication Request\n") + .append("note right of Bob: Bob thinks about it\n") + .append("Bob->Alice: Authentication Response\n") + .append(" %%% ") + .toString(); + + InterpreterResult result = md.interpret(input, null); + assertEquals("", result.message()); + assertThat(result.message(), CoreMatchers.containsString("-orders>[Order]\n") + .append("[Order]++-0..>[LineItem]\n") + .append("[Order]-[note:Aggregate root.]\n") + .append(" %%% ") + .toString(); + + InterpreterResult result = md.interpret(input, null); + assertThat(result.message(), CoreMatchers.containsString("