mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
[ZEPPELIN-1387] Support table in markdown interpreter
This commit is contained in:
parent
d11221fb8a
commit
029f550a04
3 changed files with 102 additions and 14 deletions
|
|
@ -41,14 +41,14 @@
|
|||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.commonjava.googlecode.markdown4j</groupId>
|
||||
<artifactId>markdown4j</artifactId>
|
||||
<version>2.2-cj-1.0</version>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<groupId>org.pegdown</groupId>
|
||||
<artifactId>pegdown</artifactId>
|
||||
<version>1.6.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -29,7 +29,8 @@ import org.apache.zeppelin.interpreter.InterpreterUtils;
|
|||
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
|
||||
import org.apache.zeppelin.scheduler.Scheduler;
|
||||
import org.apache.zeppelin.scheduler.SchedulerFactory;
|
||||
import org.markdown4j.Markdown4jProcessor;
|
||||
import org.pegdown.Extensions;
|
||||
import org.pegdown.PegDownProcessor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -37,7 +38,7 @@ import org.slf4j.LoggerFactory;
|
|||
* Markdown interpreter for Zeppelin.
|
||||
*/
|
||||
public class Markdown extends Interpreter {
|
||||
private Markdown4jProcessor md;
|
||||
private PegDownProcessor md;
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(Markdown.class);
|
||||
|
||||
public Markdown(Properties property) {
|
||||
|
|
@ -46,7 +47,10 @@ public class Markdown extends Interpreter {
|
|||
|
||||
@Override
|
||||
public void open() {
|
||||
md = new Markdown4jProcessor();
|
||||
md = new PegDownProcessor(
|
||||
Extensions.ALL_WITH_OPTIONALS,
|
||||
5000 /** max parsing timeout as millis */
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -56,8 +60,9 @@ public class Markdown extends Interpreter {
|
|||
public InterpreterResult interpret(String st, InterpreterContext interpreterContext) {
|
||||
String html;
|
||||
try {
|
||||
html = md.process(st);
|
||||
} catch (IOException | java.lang.RuntimeException e) {
|
||||
html = md.markdownToHtml(st);
|
||||
if (null == html) throw new RuntimeException("Cannot parse markdown");
|
||||
} catch (java.lang.RuntimeException e) {
|
||||
LOGGER.error("Exception in Markdown while interpret ", e);
|
||||
return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,20 +29,103 @@ import org.junit.Test;
|
|||
|
||||
public class MarkdownTest {
|
||||
|
||||
Markdown md;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
md = new Markdown(new Properties());
|
||||
md.open();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
md.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Markdown md = new Markdown(new Properties());
|
||||
md.open();
|
||||
public void testStrikethrough() {
|
||||
InterpreterResult result = md.interpret("This is ~~deleted~~ text", null);
|
||||
assertEquals("<p>This is <s>deleted</s> text</p>\n", result.message());
|
||||
assertEquals("<p>This is <del>deleted</del> text</p>", result.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testItalics() {
|
||||
InterpreterResult result = md.interpret("This is *italics* text", null);
|
||||
assertEquals("<p>This is <em>italics</em> text</p>", result.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrongEmphasis() {
|
||||
InterpreterResult result = md.interpret("This is **strong emphasis** text", null);
|
||||
assertEquals("<p>This is <strong>strong emphasis</strong> text</p>", result.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleTable() {
|
||||
InterpreterResult result = md.interpret(
|
||||
"Markdown | Less | Pretty\n" +
|
||||
"--- | --- | ---\n" +
|
||||
"*Still* | `renders` | **nicely**\n" +
|
||||
"1 | 2 | 3", null);
|
||||
|
||||
assertEquals(
|
||||
"<table>\n" +
|
||||
" <thead>\n" +
|
||||
" <tr>\n" +
|
||||
" <th>Markdown </th>\n" +
|
||||
" <th>Less </th>\n" +
|
||||
" <th>Pretty</th>\n" +
|
||||
" </tr>\n" +
|
||||
" </thead>\n" +
|
||||
" <tbody>\n" +
|
||||
" <tr>\n" +
|
||||
" <td><em>Still</em> </td>\n" +
|
||||
" <td><code>renders</code> </td>\n" +
|
||||
" <td><strong>nicely</strong></td>\n" +
|
||||
" </tr>\n" +
|
||||
" <tr>\n" +
|
||||
" <td>1 </td>\n" +
|
||||
" <td>2 </td>\n" +
|
||||
" <td>3</td>\n" +
|
||||
" </tr>\n" +
|
||||
" </tbody>\n" +
|
||||
"</table>",
|
||||
result.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlignedTable() {
|
||||
InterpreterResult result = md.interpret(
|
||||
"| First Header | Second Header | Third Header |\n" +
|
||||
"| :----------- | :-----------: | -------------------: |\n" +
|
||||
"| First row | Data | Very long data entry |\n" +
|
||||
"| Second row | **Cell** | *Cell* |",
|
||||
null
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
"<table>\n" +
|
||||
" <thead>\n" +
|
||||
" <tr>\n" +
|
||||
" <th align=\"left\">First Header </th>\n" +
|
||||
" <th align=\"center\">Second Header </th>\n" +
|
||||
" <th align=\"right\">Third Header </th>\n" +
|
||||
" </tr>\n" +
|
||||
" </thead>\n" +
|
||||
" <tbody>\n" +
|
||||
" <tr>\n" +
|
||||
" <td align=\"left\">First row </td>\n" +
|
||||
" <td align=\"center\">Data </td>\n" +
|
||||
" <td align=\"right\">Very long data entry </td>\n" +
|
||||
" </tr>\n" +
|
||||
" <tr>\n" +
|
||||
" <td align=\"left\">Second row </td>\n" +
|
||||
" <td align=\"center\"><strong>Cell</strong> </td>\n" +
|
||||
" <td align=\"right\"><em>Cell</em> </td>\n" +
|
||||
" </tr>\n" +
|
||||
" </tbody>\n" +
|
||||
"</table>",
|
||||
result.message()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue