about summary refs log tree commit diff
path: root/tools/cheddar/src/main.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-25T01·02+0100
committertazjin <mail@tazj.in>2020-07-25T01·42+0000
commit6936ee40afa5b333c3396e1be48f3a4aab72cf07 (patch)
tree21701d696d688ebb58edf0c9138df6dbca782f05 /tools/cheddar/src/main.rs
parenta12b0efc9e0f2d60ed3f56a9bc385e0aa56b1903 (diff)
feat(tools/cheddar): Add endpoint for Markdown rendering r/1464
Similar to the source code highlighting endpoint, but for Markdown.
This is to be used by the bug tracker, as well as Sourcegraph in the
future.

Change-Id: I4bea5c46ba969ba9965b61409e1c19c2edf1246c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1424
Tested-by: BuildkiteCI
Reviewed-by: glittershark <grfn@gws.fyi>
Diffstat (limited to 'tools/cheddar/src/main.rs')
-rw-r--r--tools/cheddar/src/main.rs82
1 files changed, 56 insertions, 26 deletions
diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs
index a4b9a93ed3cd..cde056834063 100644
--- a/tools/cheddar/src/main.rs
+++ b/tools/cheddar/src/main.rs
@@ -3,7 +3,7 @@ use comrak::arena_tree::Node;
 use comrak::nodes::{Ast, AstNode, NodeCodeBlock, NodeHtmlBlock, NodeValue};
 use comrak::{format_html, parse_document, Arena, ComrakOptions};
 use lazy_static::lazy_static;
-use rouille::try_or_400;
+use rouille::{router, try_or_400};
 use rouille::Response;
 use serde::Deserialize;
 use std::cell::RefCell;
@@ -283,10 +283,9 @@ fn format_code<R: BufRead, W: Write>(
     writeln!(writer, "</pre>").expect("write should not fail");
 }
 
-// Starts a Sourcegraph-compatible syntax highlighting server. This
+// Server endpoint for rendering the syntax of source code. This
 // replaces the 'syntect_server' component of Sourcegraph.
-fn highlighting_server(listen: &str) {
-    println!("Starting syntax highlighting server on '{}'", listen);
+fn code_endpoint(request: &rouille::Request) -> rouille::Response {
     #[derive(Deserialize)]
     struct SourcegraphQuery {
         filepath: String,
@@ -294,32 +293,63 @@ fn highlighting_server(listen: &str) {
         code: String,
     }
 
-    // Sourcegraph only uses a single endpoint, so we don't attempt to
-    // deal with routing here for now.
-    rouille::start_server(listen, move |request| {
-        let query: SourcegraphQuery = try_or_400!(rouille::input::json_input(request));
-        println!("Handling highlighting request for '{}'", query.filepath);
+    let query: SourcegraphQuery = try_or_400!(rouille::input::json_input(request));
+    let mut buf: Vec<u8> = Vec::new();
+
+    // We don't use syntect with the sourcegraph themes bundled
+    // currently, so let's fall back to something that is kind of
+    // similar (tm).
+    let theme = &THEMES.themes[match query.theme.as_str() {
+        "Sourcegraph (light)" => "Solarized (light)",
+        _ => "Solarized (dark)",
+    }];
+
+    format_code(
+        theme,
+        &mut query.code.as_bytes(),
+        &mut buf,
+        &query.filepath,
+    );
+
+    Response::json(&json!({
+        "is_plaintext": false,
+        "data": String::from_utf8_lossy(&buf)
+    }))
+}
+
+// Server endpoint for rendering a Markdown file.
+fn markdown_endpoint(request: &rouille::Request) -> rouille::Response {
+    let mut texts: HashMap<String, String> =
+        try_or_400!(rouille::input::json_input(request));
+
+    for text in texts.values_mut() {
         let mut buf: Vec<u8> = Vec::new();
+        format_markdown(&mut text.as_bytes(), &mut buf);
+        *text = String::from_utf8_lossy(&buf).to_string();
+    }
 
-        // We don't use syntect with the sourcegraph themes bundled
-        // currently, so let's fall back to something that is kind of
-        // similar (tm).
-        let theme = &THEMES.themes[match query.theme.as_str() {
-            "Sourcegraph (light)" => "Solarized (light)",
-            _ => "Solarized (dark)",
-        }];
+    Response::json(&texts)
+}
 
-        format_code(
-            theme,
-            &mut query.code.as_bytes(),
-            &mut buf,
-            &query.filepath,
-        );
+fn highlighting_server(listen: &str) {
+    println!("Starting syntax highlighting server on '{}'", listen);
 
-        Response::json(&json!({
-            "is_plaintext": false,
-            "data": String::from_utf8_lossy(&buf)
-        }))
+    rouille::start_server(listen, move |request| {
+        router!(request,
+                // Markdown rendering route
+                (POST) (/markdown) => {
+                    markdown_endpoint(request)
+                },
+
+                // Code rendering route
+                (POST) (/) => {
+                    code_endpoint(request)
+                },
+
+                _ => {
+                    rouille::Response::empty_404()
+                },
+        )
     });
 }