diff options
Diffstat (limited to 'web')
| -rw-r--r-- | web/mastosan/mastosan.go | 34 | ||||
| -rw-r--r-- | web/mastosan/src/main.rs | 30 | ||||
| -rwxr-xr-x[-rw-r--r--] | web/mastosan/testdata/mastosan.wasm | bin | 402296 -> 403989 bytes |
3 files changed, 52 insertions, 12 deletions
diff --git a/web/mastosan/mastosan.go b/web/mastosan/mastosan.go index 7e0d539..ea0db58 100644 --- a/web/mastosan/mastosan.go +++ b/web/mastosan/mastosan.go @@ -54,18 +54,12 @@ func init() { } } -// HTML2Slackdown converts a string full of HTML text to slack-flavored markdown. -// -// Internally this works by taking that HTML and piping it to a small Rust program -// using lol_html to parse the HTML and rejigger it into slack-flavored markdown. -// This has an added latency of about 0.2 seconds per invocation, but this is as -// fast as I can make it for now. -func HTML2Slackdown(ctx context.Context, text string) (string, error) { +func runWASM(ctx context.Context, text, mode string) (string, error) { fout := &bytes.Buffer{} fin := bytes.NewBufferString(text) name := strconv.Itoa(rand.Int()) - config := wazero.NewModuleConfig().WithStdout(fout).WithStdin(fin).WithArgs("mastosan").WithName(name) + config := wazero.NewModuleConfig().WithStdout(fout).WithStdin(fin).WithArgs("mastosan", mode).WithName(name) mod, err := r.InstantiateModule(ctx, code, config) if err != nil { @@ -75,3 +69,27 @@ func HTML2Slackdown(ctx context.Context, text string) (string, error) { return strings.TrimSpace(fout.String()), nil } + +// Slackdown converts a string full of HTML text to slack-flavored markdown. +// +// Internally this works by taking that HTML and piping it to a small Rust program +// using lol_html to parse the HTML and rejigger it into slack-flavored markdown. +func Slackdown(ctx context.Context, text string) (string, error) { + return runWASM(ctx, text, "slackdown") +} + +// Text converts a string with HTML content into an approximation of plain text. +// +// Internally this works by taking that HTML and piping it to a small Rust program +// using lol_html to parse the HTML and rejigger it into plain text. +func Text(ctx context.Context, text string) (string, error) { + return runWASM(ctx, text, "text") +} + +// Markdown converts a string with HTML content into an approximation of plain text. +// +// Internally this works by taking that HTML and piping it to a small Rust program +// using lol_html to parse the HTML and rejigger it into generic markdown. +func Markdown(ctx context.Context, text string) (string, error) { + return runWASM(ctx, text, "markdown") +} diff --git a/web/mastosan/src/main.rs b/web/mastosan/src/main.rs index a69e24a..80658b0 100644 --- a/web/mastosan/src/main.rs +++ b/web/mastosan/src/main.rs @@ -1,7 +1,18 @@ use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings}; +use std::env::args; use std::io::{self, prelude::*, stdin, stdout}; fn main() -> io::Result<()> { + let mode = match args().nth(1) { + None => { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "usage: mastosan [markdown|slackdown|text]", + )) + } + Some(mode) => mode, + }; + let mut output = Vec::new(); let mut rewriter = HtmlRewriter::new( Settings { @@ -22,10 +33,21 @@ fn main() -> io::Result<()> { }), element!("a[href]", |el| { let href = el.get_attribute("href").unwrap(); - el.prepend("|", ContentType::Html); - el.prepend(&href, ContentType::Html); - el.prepend("<", ContentType::Html); - el.append(">", ContentType::Html); + match mode.as_str() { + "slackdown" => { + el.prepend("|", ContentType::Html); + el.prepend(&href, ContentType::Html); + el.prepend("<", ContentType::Html); + el.append(">", ContentType::Html); + } + "markdown" => { + el.prepend("](", ContentType::Html); + el.prepend(&href, ContentType::Html); + el.prepend("[", ContentType::Html); + el.append(")", ContentType::Html); + } + _ => {} + } el.remove_and_keep_content(); Ok(()) diff --git a/web/mastosan/testdata/mastosan.wasm b/web/mastosan/testdata/mastosan.wasm Binary files differindex 218ee48..c03f403 100644..100755 --- a/web/mastosan/testdata/mastosan.wasm +++ b/web/mastosan/testdata/mastosan.wasm |
