add HtmlDefaultTheme to pretty_print

This commit is contained in:
Mark 2024-06-26 21:17:58 +02:00
parent 50ebe5fb54
commit 14c8e7717d
2 changed files with 33 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mers_lib" name = "mers_lib"
version = "0.8.22" version = "0.8.23"
edition = "2021" edition = "2021"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
description = "library to use the mers language in other projects" description = "library to use the mers language in other projects"
@ -9,7 +9,7 @@ readme = "README.md"
repository = "https://github.com/Dummi26/mers" repository = "https://github.com/Dummi26/mers"
[features] [features]
default = ["parse"] default = ["parse", "pretty-print", "ecolor-html"]
# for parsing and running mers code (for most situations: just enable parse) # for parsing and running mers code (for most situations: just enable parse)
parse = ["run"] parse = ["run"]

View File

@ -1,4 +1,4 @@
use std::{io::Write, process::exit, sync::Arc}; use std::{io::Write, sync::Arc};
use crate::{ use crate::{
errors::CheckError, errors::CheckError,
@ -10,7 +10,7 @@ use crate::{
pub fn pretty_print(src: Source) { pub fn pretty_print(src: Source) {
if let Err(e) = pretty_print_to(src, &mut std::io::stdout(), DefaultTheme) { if let Err(e) = pretty_print_to(src, &mut std::io::stdout(), DefaultTheme) {
eprintln!("{e:?}"); 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("<span style=\"color:");
t.push_str(color);
t.push_str(";\">");
self.nocolor(text, t);
t.push_str("</span>");
} 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::Comment: Color = Color::BrightBlack;
// const FColor::Variable: Color = Color::Green; // const FColor::Variable: Color = Color::Green;
// const FColor::Variable_Ref: Color = Color::Green; // const FColor::Variable_Ref: Color = Color::Green;