1
0
David Peter 2 жил өмнө
parent
commit
5d72c955f5

+ 24 - 0
numbat-wasm/src/html_formatter.rs

@@ -0,0 +1,24 @@
+use numbat::markup::{FormatType, FormattedString, Formatter};
+
+pub struct HtmlFormatter;
+
+impl Formatter for HtmlFormatter {
+    fn format_part(
+        &self,
+        FormattedString(_output_type, format_type, s): &FormattedString,
+    ) -> String {
+        match format_type {
+            FormatType::Whitespace => format!("{s}"),
+            FormatType::Dimmed => format!("{s}"),
+            FormatType::Text => format!("{s}"),
+            FormatType::String => format!("{s}"),
+            FormatType::Keyword => format!("<b>{s}</b>"),
+            FormatType::Value => format!("<span style=\"color:blue\">{s}</span>"),
+            FormatType::Unit => format!("<span style=\"color:green\">{s}</span>"),
+            FormatType::Identifier => format!("{s}"),
+            FormatType::TypeIdentifier => format!("{s}"),
+            FormatType::Operator => format!("{s}"),
+            FormatType::Decorator => format!("{s}"),
+        }
+    }
+}

+ 7 - 1
numbat-wasm/src/lib.rs

@@ -1,5 +1,9 @@
+mod html_formatter;
 mod utils;
 
+use html_formatter::HtmlFormatter;
+use numbat::markup::Formatter;
+use numbat::pretty_print::PrettyPrint;
 use numbat::resolver::{CodeSource, NullImporter};
 use numbat::{Context, InterpreterResult};
 
@@ -15,10 +19,12 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
 pub fn interpret(code: &str) -> String {
     utils::set_panic_hook();
 
+    let html_formatter = HtmlFormatter {};
+
     let mut numbat = Context::new(NullImporter {});
     match numbat.interpret(&code, CodeSource::Text) {
         Ok((_, result)) => match result {
-            InterpreterResult::Value(q) => format!("{}", q),
+            InterpreterResult::Value(q) => html_formatter.format(&q.pretty_print(), true),
             InterpreterResult::Continue => "Nothing to show".into(),
             InterpreterResult::Exit(_) => "Error!".into(),
         },