1616//! ```rust
1717//! use hashql_core::{
1818//! heap::Heap,
19- //! pretty::{Format, Formatter, RenderOptions},
19+ //! pretty::{Formatter, RenderOptions},
2020//! symbol::sym,
2121//! };
2222//!
@@ -44,19 +44,21 @@ mod write;
4444use core:: fmt:: { self , Display } ;
4545use std:: io;
4646
47- use self :: write:: { AnsiWriter , PlainWriter , RenderError } ;
47+ use self :: write:: { AnsiWriter , HtmlFragmentWriter , PlainWriter , RenderError , XmlEscapingWriter } ;
4848pub use self :: {
4949 formatter:: { Doc , Formatter , FormatterOptions } ,
5050 semantic:: Semantic ,
5151} ;
5252
5353/// Output format for rendered documents.
5454#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
55- pub enum Format {
55+ pub enum RenderFormat {
5656 /// Plain text without ANSI escape codes.
5757 Plain ,
5858 /// ANSI-colored output for terminals.
5959 Ansi ,
60+ /// Output as a series of HTML text spans.
61+ HtmlFragment ,
6062}
6163
6264/// Configuration for rendering documents to output.
@@ -65,7 +67,7 @@ pub enum Format {
6567#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
6668pub struct RenderOptions {
6769 /// Whether to use ANSI colors or plain text.
68- pub format : Format ,
70+ pub format : RenderFormat ,
6971 /// Maximum line width before wrapping (default: 80).
7072 pub max_width : usize ,
7173}
@@ -76,7 +78,7 @@ impl RenderOptions {
7678 /// Enables terminal colors and syntax highlighting in the rendered output.
7779 #[ must_use]
7880 pub const fn with_ansi ( mut self ) -> Self {
79- self . format = Format :: Ansi ;
81+ self . format = RenderFormat :: Ansi ;
8082 self
8183 }
8284
@@ -86,7 +88,7 @@ impl RenderOptions {
8688 /// non-terminal output.
8789 #[ must_use]
8890 pub const fn with_plain ( mut self ) -> Self {
89- self . format = Format :: Plain ;
91+ self . format = RenderFormat :: Plain ;
9092 self
9193 }
9294
@@ -104,7 +106,7 @@ impl RenderOptions {
104106impl Default for RenderOptions {
105107 fn default ( ) -> Self {
106108 Self {
107- format : Format :: Ansi ,
109+ format : RenderFormat :: Ansi ,
108110 max_width : 80 ,
109111 }
110112 }
@@ -121,27 +123,35 @@ pub fn render_into<W>(doc: &Doc<'_>, options: RenderOptions, write: &mut W) -> i
121123where
122124 W : io:: Write ,
123125{
126+ let mut writer = PlainWriter :: new_io ( write) ;
127+
124128 match options. format {
125- Format :: Plain => doc
126- . render_raw ( options. max_width , & mut PlainWriter :: new_io ( write ) )
127- . map_err ( RenderError :: into_io ) ,
128- Format :: Ansi => doc
129- . render_raw ( options . max_width , & mut AnsiWriter :: new_io ( write ) )
130- . map_err ( RenderError :: into_io ) ,
129+ RenderFormat :: Plain => doc. render_raw ( options . max_width , & mut writer ) ,
130+ RenderFormat :: Ansi => doc . render_raw ( options. max_width , & mut AnsiWriter :: new ( writer ) ) ,
131+ RenderFormat :: HtmlFragment => doc . render_raw (
132+ options . max_width ,
133+ & mut XmlEscapingWriter :: new ( HtmlFragmentWriter :: new ( writer ) ) ,
134+ ) ,
131135 }
136+ . map_err ( RenderError :: into_io)
132137}
133138
134139/// Get a displayable representation.
135140///
136141/// Returns a [`Display`] implementor for using in formatting contexts.
137142#[ must_use]
138143pub fn render ( doc : Doc < ' _ > , options : RenderOptions ) -> impl Display {
139- fmt:: from_fn ( move |fmt| match options. format {
140- Format :: Plain => doc
141- . render_raw ( options. max_width , & mut PlainWriter :: new_fmt ( fmt) )
142- . map_err ( RenderError :: into_fmt) ,
143- Format :: Ansi => doc
144- . render_raw ( options. max_width , & mut AnsiWriter :: new_fmt ( fmt) )
145- . map_err ( RenderError :: into_fmt) ,
144+ fmt:: from_fn ( move |fmt| {
145+ let mut writer = PlainWriter :: new_fmt ( fmt) ;
146+
147+ match options. format {
148+ RenderFormat :: Plain => doc. render_raw ( options. max_width , & mut writer) ,
149+ RenderFormat :: Ansi => doc. render_raw ( options. max_width , & mut AnsiWriter :: new ( writer) ) ,
150+ RenderFormat :: HtmlFragment => doc. render_raw (
151+ options. max_width ,
152+ & mut XmlEscapingWriter :: new ( HtmlFragmentWriter :: new ( writer) ) ,
153+ ) ,
154+ }
155+ . map_err ( RenderError :: into_fmt)
146156 } )
147157}
0 commit comments