From 14c8e7717d286c60393f489abbf59dec99c4f91c Mon Sep 17 00:00:00 2001 From: Mark <> Date: Wed, 26 Jun 2024 21:17:58 +0200 Subject: [PATCH] add HtmlDefaultTheme to pretty_print --- mers_lib/Cargo.toml | 4 ++-- mers_lib/src/pretty_print.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/mers_lib/Cargo.toml b/mers_lib/Cargo.toml index da0c58d..78718d7 100755 --- a/mers_lib/Cargo.toml +++ b/mers_lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers_lib" -version = "0.8.22" +version = "0.8.23" edition = "2021" license = "MIT OR Apache-2.0" description = "library to use the mers language in other projects" @@ -9,7 +9,7 @@ readme = "README.md" repository = "https://github.com/Dummi26/mers" [features] -default = ["parse"] +default = ["parse", "pretty-print", "ecolor-html"] # for parsing and running mers code (for most situations: just enable parse) parse = ["run"] diff --git a/mers_lib/src/pretty_print.rs b/mers_lib/src/pretty_print.rs index 337176b..f2973cd 100644 --- a/mers_lib/src/pretty_print.rs +++ b/mers_lib/src/pretty_print.rs @@ -1,4 +1,4 @@ -use std::{io::Write, process::exit, sync::Arc}; +use std::{io::Write, sync::Arc}; use crate::{ errors::CheckError, @@ -10,7 +10,7 @@ use crate::{ pub fn pretty_print(src: Source) { if let Err(e) = pretty_print_to(src, &mut std::io::stdout(), DefaultTheme) { eprintln!("{e:?}"); - exit(28); + std::process::exit(28); } } @@ -95,6 +95,35 @@ impl ThemeGen for DefaultTheme { } } +#[cfg(feature = "ecolor-html")] +pub struct HtmlDefaultTheme; +#[cfg(feature = "ecolor-html")] +impl ThemeGen for HtmlDefaultTheme { + type C = FColor; + type T = String; + fn color(&self, text: &str, color: FColor, t: &mut String) { + if let Some(color) = map_color(color) { + let color = match color { + AbstractColor::Gray => "Gray", + AbstractColor::Green => "Green", + AbstractColor::Red => "Crimson", + AbstractColor::Blue => "RoyalBlue", + AbstractColor::Cyan => "DarkCyan", + }; + t.push_str(""); + self.nocolor(text, t); + t.push_str(""); + } else { + self.nocolor(text, t); + } + } + fn nocolor(&self, text: &str, t: &mut String) { + html_escape::encode_text_to_string(text, t); + } +} + // const FColor::Comment: Color = Color::BrightBlack; // const FColor::Variable: Color = Color::Green; // const FColor::Variable_Ref: Color = Color::Green;