@@ -8,6 +8,7 @@ use crate::display_list::line::DisplaySourceLine;
88use crate :: DisplayList ;
99use std:: cmp;
1010use std:: io:: Write ;
11+ use std:: iter:: repeat;
1112
1213pub struct Renderer { }
1314
@@ -25,12 +26,33 @@ fn digits(n: usize) -> usize {
2526 sum
2627}
2728
29+ enum MarkKind {
30+ Vertical ,
31+ Horizontal ,
32+ DownRight ,
33+ UpRight ,
34+ UpLeft ,
35+ }
36+
37+ impl MarkKind {
38+ pub fn get ( t : MarkKind ) ->char {
39+ match t{
40+ MarkKind :: Vertical =>'│' ,
41+ MarkKind :: Horizontal =>'─' ,
42+ MarkKind :: DownRight =>'┌' ,
43+ MarkKind :: UpRight =>'└' ,
44+ MarkKind :: UpLeft =>'┘' ,
45+ }
46+ }
47+ }
48+
2849impl Renderer {
2950pub fn new ( ) ->Self {
3051Renderer { }
3152}
3253
3354pub fn fmt ( & self , w : & mut impl Write , dl : & DisplayList ) -> std:: io:: Result < ( ) > {
55+ self . fmt_header ( w) ?;
3456let lineno_max = dl. body . iter ( ) . rev ( ) . find_map ( |line|{
3557if let DisplayLine :: Source {
3658lineno : Some ( lineno) ,
@@ -49,6 +71,7 @@ impl Renderer {
4971for linein & dl. body {
5072self . fmt_line ( w, line, lineno_max, inline_marks_width) ?;
5173}
74+ self . fmt_footer ( w) ?;
5275Ok ( ( ) )
5376}
5477
@@ -66,7 +89,7 @@ impl Renderer {
6689 inline_marks,
6790 line,
6891} =>{
69- let vertical_mark ='|' ;
92+ let vertical_mark =MarkKind :: get ( MarkKind :: Vertical ) ;
7093if let Some ( lineno) = lineno{
7194write ! (
7295 w,
@@ -90,9 +113,9 @@ impl Renderer {
90113"" ,
91114 width = inline_marks_width - inline_marks. len( )
92115) ?;
93- // for mark in inline_marks {
94- // self.fmt_display_mark(w, mark)?;
95- // }
116+ for markin inline_marks{
117+ self . fmt_display_mark ( w, mark) ?;
118+ }
96119self . fmt_source_line ( w, line) ?;
97120writeln ! ( w)
98121}
@@ -106,31 +129,33 @@ impl Renderer {
106129line : & DisplaySourceLine ,
107130) -> std:: io:: Result < ( ) > {
108131match line{
109- DisplaySourceLine :: Content { text} =>write ! ( w, " {}" , text) ,
132+ DisplaySourceLine :: Content { text} =>{
133+ write ! ( w, r#" <span class="source">{}</span>"# , text)
134+ }
110135DisplaySourceLine :: Annotation { annotation, range} =>{
111- // let(_, style) = self.get_annotation_type_style(&annotation.annotation_type) ;
112- //let styles = [StyleType::Emphasis, style] ;
113- // letindent =if range.start == 0 { 0 } else { range.start + 1 } ;
114- //write!(w, "{:>width$}", "", width = indent)?;
115- //if range.start == 0 {
116- //let horizontal_mark = MarkKind::get(MarkKind::Horizontal);
117- //S::fmt(
118- //w,
119- //format_args!(
120- //"{}{} {}" ,
121- //repeat(horizontal_mark).take(5).collect::<String>( ),
122- //MarkKind::get(MarkKind::UpLeft) ,
123- //annotation.label,
124- //),
125- //&styles,
126- //)
127- //} else {
128- //S::fmt(
129- //w,
130- //format_args!("{:->width$} {}", "", annotation.label, width = range.len() ),
131- //&styles,
132- //)
133- // }
136+ let indent = if range . start == 0 { 0 } else { range . start + 1 } ;
137+ write ! ( w , "{:>width$}" , "" , width = indent ) ? ;
138+ let horizontal_mark =MarkKind :: get ( MarkKind :: Horizontal ) ;
139+ if range . start == 0 {
140+ write ! (
141+ w ,
142+ "{}{} {}" ,
143+ repeat ( horizontal_mark )
144+ . take ( range . len ( ) )
145+ . collect :: < String > ( ) ,
146+ MarkKind :: get ( MarkKind :: UpLeft ) ,
147+ annotation . label ,
148+ ) ? ;
149+ } else {
150+ write ! (
151+ w ,
152+ "{} {}" ,
153+ repeat ( horizontal_mark )
154+ . take ( range . len ( ) )
155+ . collect :: < String > ( ) ,
156+ annotation . label
157+ ) ? ;
158+ }
134159Ok ( ( ) )
135160}
136161DisplaySourceLine :: Empty =>Ok ( ( ) ) ,
@@ -194,14 +219,26 @@ impl Renderer {
194219w : & mut impl std:: io:: Write ,
195220display_mark : & DisplayMark ,
196221) -> std:: io:: Result < ( ) > {
197- // let ch = match display_mark.mark_type {
198- // DisplayMarkType::AnnotationStart => MarkKind::get(MarkKind::DownRight),
199- // DisplayMarkType::AnnotationEnd => MarkKind::get(MarkKind::UpRight),
200- // DisplayMarkType::AnnotationThrough => MarkKind::get(MarkKind::Vertical),
201- // };
202- //S::fmt (w,ch, &[StyleType::Emphasis, style])
222+ let ch =match display_mark. mark_type {
223+ DisplayMarkType :: AnnotationStart =>MarkKind :: get ( MarkKind :: DownRight ) ,
224+ DisplayMarkType :: AnnotationEnd =>MarkKind :: get ( MarkKind :: UpRight ) ,
225+ DisplayMarkType :: AnnotationThrough =>MarkKind :: get ( MarkKind :: Vertical ) ,
226+ } ;
227+ write ! ( w, "{}" , ch ) ? ;
203228Ok ( ( ) )
204229}
230+
231+ fn fmt_header ( & self , w : & mut impl std:: io:: Write ) -> std:: io:: Result < ( ) > {
232+ writeln ! ( w, "<html><head><style>" ) ?;
233+ writeln ! ( w, r#".lineno {{ color: red; }}"# ) ?;
234+ writeln ! ( w, r#".line {{ color: blue; }}"# ) ?;
235+ writeln ! ( w, r#".source {{ color: gray; }}"# ) ?;
236+ write ! ( w, "</style></head><body><pre>" )
237+ }
238+
239+ fn fmt_footer ( & self , w : & mut impl std:: io:: Write ) -> std:: io:: Result < ( ) > {
240+ write ! ( w, "</pre></body></html>" )
241+ }
205242}
206243
207244impl RendererTrait for Renderer {