Created
October 3, 2021 00:32
-
-
Save sambatriste/16b2e9faad117092e0d712b2471a495f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.junit.Test; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import java.util.Arrays; | |
import java.util.stream.Collectors; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
public class TextileToMarkdownTest { | |
@Test | |
public void testUL() throws Exception { | |
String markdown = toMarkdown("* あああ\n** いいい"); | |
assertThat(markdown, is("- あああ\n - いいい")); | |
} | |
@Test | |
public void testH() throws Exception { | |
String markdown = toMarkdown("h1. あああ\n\nh2. いいい"); | |
assertThat(markdown, is("# あああ\n\n## いいい")); | |
} | |
static String toMarkdown(String textile) throws IOException, InterruptedException { | |
String[] cmd = {"docker", "run", "--rm", "--interactive", "pandoc/core", "--from", "textile", "--to", "markdown"}; // docker | |
//String[] cmd = {"pandoc", "--from", "textile", "--to", "markdown"}; // pandoc | |
Process p = new ProcessBuilder(cmd) | |
//.redirectErrorStream(true) // エラーがでたらこれで調べる | |
.start(); | |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), UTF_8))) { | |
try (Writer writer = new OutputStreamWriter(p.getOutputStream(), UTF_8)) { | |
writer.write(textile); | |
} | |
String markdown = reader.lines().collect(Collectors.joining("\n")); | |
int exitValue = p.waitFor(); | |
if (exitValue != 0) { | |
throw new IllegalStateException("cmd=[" + Arrays.asList(cmd) + "], " + "textile=[" + textile + "], " + | |
"markdown=[" + markdown + "], " + "exitValue=[" + exitValue + "]"); | |
} | |
return markdown; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment