From 6936ee40afa5b333c3396e1be48f3a4aab72cf07 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 25 Jul 2020 02:02:45 +0100 Subject: feat(tools/cheddar): Add endpoint for Markdown rendering 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 --- tools/cheddar/src/main.rs | 82 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 26 deletions(-) (limited to 'tools') diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs index a4b9a93ed3..cde0568340 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( writeln!(writer, "").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 = 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 = + try_or_400!(rouille::input::json_input(request)); + + for text in texts.values_mut() { let mut buf: Vec = 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() + }, + ) }); } -- cgit 1.4.1