| 1 |
//! Renders the preview SVG for the README. |
| 2 |
//! |
| 3 |
//! To update the preview, execute the following command from the top level of |
| 4 |
//! the repository: |
| 5 |
//! |
| 6 |
//! ```sh |
| 7 |
//! cargo run --example readme_preview svg > codespan-reporting/assets/readme_preview.svg |
| 8 |
//! ``` |
| 9 |
|
| 10 |
use codespan_reporting::diagnostic::{Diagnostic, Label}; |
| 11 |
use codespan_reporting::files::SimpleFile; |
| 12 |
use codespan_reporting::term::termcolor::{ |
| 13 |
Color, ColorChoice, ColorSpec, StandardStream, WriteColor, |
| 14 |
}; |
| 15 |
use codespan_reporting::term::{self}; |
| 16 |
use std::io::{self, Write}; |
| 17 |
|
| 18 |
#[derive(Debug)] |
| 19 |
pub enum Opts { |
| 20 |
/// Render SVG output |
| 21 |
Svg, |
| 22 |
/// Render Stderr output |
| 23 |
Stderr { |
| 24 |
/// Configure coloring of output |
| 25 |
color: ColorChoice, |
| 26 |
}, |
| 27 |
} |
| 28 |
|
| 29 |
fn parse_args() -> Result<Opts, pico_args::Error> { |
| 30 |
let mut pargs = pico_args::Arguments::from_env(); |
| 31 |
match pargs.subcommand()? { |
| 32 |
Some(value) => match value.as_str() { |
| 33 |
"svg" => Ok(Opts::Svg), |
| 34 |
"stderr" => { |
| 35 |
let color = pargs |
| 36 |
.opt_value_from_str("--color")? |
| 37 |
.unwrap_or(ColorChoice::Auto); |
| 38 |
Ok(Opts::Stderr { color }) |
| 39 |
} |
| 40 |
_ => Err(pico_args::Error::Utf8ArgumentParsingFailed { |
| 41 |
value, |
| 42 |
cause: "not a valid subcommand".to_owned(), |
| 43 |
}), |
| 44 |
}, |
| 45 |
None => Err(pico_args::Error::MissingArgument), |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
fn main() -> anyhow::Result<()> { |
| 50 |
let file = SimpleFile::new( |
| 51 |
"FizzBuzz.fun", |
| 52 |
unindent::unindent( |
| 53 |
r#" |
| 54 |
module FizzBuzz where |
| 55 |
|
| 56 |
fizz₁ : Nat → String |
| 57 |
fizz₁ num = case (mod num 5) (mod num 3) of |
| 58 |
0 0 => "FizzBuzz" |
| 59 |
0 _ => "Fizz" |
| 60 |
_ 0 => "Buzz" |
| 61 |
_ _ => num |
| 62 |
|
| 63 |
fizz₂ : Nat → String |
| 64 |
fizz₂ num = |
| 65 |
case (mod num 5) (mod num 3) of |
| 66 |
0 0 => "FizzBuzz" |
| 67 |
0 _ => "Fizz" |
| 68 |
_ 0 => "Buzz" |
| 69 |
_ _ => num |
| 70 |
"#, |
| 71 |
), |
| 72 |
); |
| 73 |
|
| 74 |
let diagnostics = [Diagnostic::error() |
| 75 |
.with_message("`case` clauses have incompatible types") |
| 76 |
.with_code("E0308") |
| 77 |
.with_labels(vec![ |
| 78 |
Label::primary((), 328..331).with_message("expected `String`, found `Nat`"), |
| 79 |
Label::secondary((), 211..331).with_message("`case` clauses have incompatible types"), |
| 80 |
Label::secondary((), 258..268).with_message("this is found to be of type `String`"), |
| 81 |
Label::secondary((), 284..290).with_message("this is found to be of type `String`"), |
| 82 |
Label::secondary((), 306..312).with_message("this is found to be of type `String`"), |
| 83 |
Label::secondary((), 186..192).with_message("expected type `String` found here"), |
| 84 |
]) |
| 85 |
.with_notes(vec![unindent::unindent( |
| 86 |
" |
| 87 |
expected type `String` |
| 88 |
found type `Nat` |
| 89 |
", |
| 90 |
)])]; |
| 91 |
|
| 92 |
match parse_args()? { |
| 93 |
Opts::Svg => { |
| 94 |
let mut buffer = Vec::new(); |
| 95 |
let mut writer = HtmlEscapeWriter::new(SvgWriter::new(&mut buffer)); |
| 96 |
let config = codespan_reporting::term::Config { |
| 97 |
#[cfg(feature = "termcolor")] |
| 98 |
styles: codespan_reporting::term::Styles::with_blue(Color::Blue), |
| 99 |
..codespan_reporting::term::Config::default() |
| 100 |
}; |
| 101 |
|
| 102 |
for diagnostic in &diagnostics { |
| 103 |
term::emit(&mut writer, &config, &file, diagnostic)?; |
| 104 |
} |
| 105 |
|
| 106 |
let num_lines = buffer.iter().filter(|byte| **byte == b'\n').count() + 1; |
| 107 |
|
| 108 |
let padding = 10; |
| 109 |
let font_size = 12; |
| 110 |
let line_spacing = 3; |
| 111 |
let width = 882; |
| 112 |
let height = padding + num_lines * (font_size + line_spacing) + padding; |
| 113 |
|
| 114 |
let stdout = std::io::stdout(); |
| 115 |
let writer = &mut stdout.lock(); |
| 116 |
|
| 117 |
write!( |
| 118 |
writer, |
| 119 |
r#"<svg viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg"> |
| 120 |
<style> |
| 121 |
/* https://github.com/aaron-williamson/base16-alacritty/blob/master/colors/base16-tomorrow-night-256.yml */ |
| 122 |
pre {{ |
| 123 |
background: #1d1f21; |
| 124 |
margin: 0; |
| 125 |
padding: {padding}px; |
| 126 |
border-radius: 6px; |
| 127 |
color: #ffffff; |
| 128 |
font: {font_size}px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace; |
| 129 |
}} |
| 130 |
|
| 131 |
pre .bold {{ font-weight: bold; }} |
| 132 |
|
| 133 |
pre .fg.black {{ color: #1d1f21; }} |
| 134 |
pre .fg.red {{ color: #cc6666; }} |
| 135 |
pre .fg.green {{ color: #b5bd68; }} |
| 136 |
pre .fg.yellow {{ color: #f0c674; }} |
| 137 |
pre .fg.blue {{ color: #81a2be; }} |
| 138 |
pre .fg.magenta {{ color: #b294bb; }} |
| 139 |
pre .fg.cyan {{ color: #8abeb7; }} |
| 140 |
pre .fg.white {{ color: #c5c8c6; }} |
| 141 |
|
| 142 |
pre .fg.black.bright {{ color: #969896; }} |
| 143 |
pre .fg.red.bright {{ color: #cc6666; }} |
| 144 |
pre .fg.green.bright {{ color: #b5bd68; }} |
| 145 |
pre .fg.yellow.bright {{ color: #f0c674; }} |
| 146 |
pre .fg.blue.bright {{ color: #81a2be; }} |
| 147 |
pre .fg.magenta.bright {{ color: #b294bb; }} |
| 148 |
pre .fg.cyan.bright {{ color: #8abeb7; }} |
| 149 |
pre .fg.white.bright {{ color: #ffffff; }} |
| 150 |
|
| 151 |
pre .bg.black {{ background-color: #1d1f21; }} |
| 152 |
pre .bg.red {{ background-color: #cc6666; }} |
| 153 |
pre .bg.green {{ background-color: #b5bd68; }} |
| 154 |
pre .bg.yellow {{ background-color: #f0c674; }} |
| 155 |
pre .bg.blue {{ background-color: #81a2be; }} |
| 156 |
pre .bg.magenta {{ background-color: #b294bb; }} |
| 157 |
pre .bg.cyan {{ background-color: #8abeb7; }} |
| 158 |
pre .bg.white {{ background-color: #c5c8c6; }} |
| 159 |
|
| 160 |
pre .bg.black.bright {{ background-color: #969896; }} |
| 161 |
pre .bg.red.bright {{ background-color: #cc6666; }} |
| 162 |
pre .bg.green.bright {{ background-color: #b5bd68; }} |
| 163 |
pre .bg.yellow.bright {{ background-color: #f0c674; }} |
| 164 |
pre .bg.blue.bright {{ background-color: #81a2be; }} |
| 165 |
pre .bg.magenta.bright {{ background-color: #b294bb; }} |
| 166 |
pre .bg.cyan.bright {{ background-color: #8abeb7; }} |
| 167 |
pre .bg.white.bright {{ background-color: #ffffff; }} |
| 168 |
</style> |
| 169 |
|
| 170 |
<foreignObject x="0" y="0" width="{width}" height="{height}"> |
| 171 |
<div xmlns="http://www.w3.org/1999/xhtml"> |
| 172 |
<pre>"#, |
| 173 |
padding = padding, |
| 174 |
font_size = font_size, |
| 175 |
width = width, |
| 176 |
height = height, |
| 177 |
)?; |
| 178 |
|
| 179 |
writer.write_all(&buffer)?; |
| 180 |
|
| 181 |
write!( |
| 182 |
writer, |
| 183 |
"</pre> |
| 184 |
</div> |
| 185 |
</foreignObject> |
| 186 |
</svg> |
| 187 |
" |
| 188 |
)?; |
| 189 |
} |
| 190 |
Opts::Stderr { color } => { |
| 191 |
let writer = StandardStream::stderr(color); |
| 192 |
let config = codespan_reporting::term::Config::default(); |
| 193 |
for diagnostic in &diagnostics { |
| 194 |
term::emit(&mut writer.lock(), &config, &file, diagnostic)?; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
Ok(()) |
| 200 |
} |
| 201 |
|
| 202 |
/// Rudimentary HTML escaper which performs the following conversions: |
| 203 |
/// |
| 204 |
/// - `<` ⇒ `<` |
| 205 |
/// - `>` ⇒ `>` |
| 206 |
/// - `&` ⇒ `&` |
| 207 |
pub struct HtmlEscapeWriter<W> { |
| 208 |
upstream: W, |
| 209 |
} |
| 210 |
|
| 211 |
impl<W> HtmlEscapeWriter<W> { |
| 212 |
pub fn new(upstream: W) -> HtmlEscapeWriter<W> { |
| 213 |
HtmlEscapeWriter { upstream } |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl<W: Write> Write for HtmlEscapeWriter<W> { |
| 218 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 219 |
let mut last_term = 0usize; |
| 220 |
for (i, byte) in buf.iter().enumerate() { |
| 221 |
let escape = match byte { |
| 222 |
b'<' => &b"<"[..], |
| 223 |
b'>' => &b">"[..], |
| 224 |
b'&' => &b"&"[..], |
| 225 |
_ => continue, |
| 226 |
}; |
| 227 |
self.upstream.write_all(&buf[last_term..i])?; |
| 228 |
last_term = i + 1; |
| 229 |
self.upstream.write_all(escape)?; |
| 230 |
} |
| 231 |
self.upstream.write_all(&buf[last_term..])?; |
| 232 |
Ok(buf.len()) |
| 233 |
} |
| 234 |
|
| 235 |
fn flush(&mut self) -> io::Result<()> { |
| 236 |
self.upstream.flush() |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl<W: WriteColor> WriteColor for HtmlEscapeWriter<W> { |
| 241 |
fn supports_color(&self) -> bool { |
| 242 |
self.upstream.supports_color() |
| 243 |
} |
| 244 |
|
| 245 |
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> { |
| 246 |
self.upstream.set_color(spec) |
| 247 |
} |
| 248 |
|
| 249 |
fn reset(&mut self) -> io::Result<()> { |
| 250 |
self.upstream.reset() |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
pub struct SvgWriter<W> { |
| 255 |
upstream: W, |
| 256 |
color: ColorSpec, |
| 257 |
} |
| 258 |
|
| 259 |
impl<W> SvgWriter<W> { |
| 260 |
pub fn new(upstream: W) -> SvgWriter<W> { |
| 261 |
SvgWriter { |
| 262 |
upstream, |
| 263 |
color: ColorSpec::new(), |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
impl<W: Write> Write for SvgWriter<W> { |
| 269 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 270 |
self.upstream.write(buf) |
| 271 |
} |
| 272 |
|
| 273 |
fn flush(&mut self) -> io::Result<()> { |
| 274 |
self.upstream.flush() |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
impl<W: Write> WriteColor for SvgWriter<W> { |
| 279 |
fn supports_color(&self) -> bool { |
| 280 |
true |
| 281 |
} |
| 282 |
|
| 283 |
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> { |
| 284 |
#![allow(unused_assignments)] |
| 285 |
|
| 286 |
if self.color == *spec { |
| 287 |
return Ok(()); |
| 288 |
} else { |
| 289 |
if !self.color.is_none() { |
| 290 |
write!(self, "</span>")?; |
| 291 |
} |
| 292 |
self.color = spec.clone(); |
| 293 |
} |
| 294 |
|
| 295 |
if spec.is_none() { |
| 296 |
write!(self, "</span>")?; |
| 297 |
return Ok(()); |
| 298 |
} else { |
| 299 |
write!(self, "<span class=\"")?; |
| 300 |
} |
| 301 |
|
| 302 |
let mut first = true; |
| 303 |
|
| 304 |
fn write_first<W: Write>(first: bool, writer: &mut SvgWriter<W>) -> io::Result<bool> { |
| 305 |
if !first { |
| 306 |
write!(writer, " ")?; |
| 307 |
} |
| 308 |
|
| 309 |
Ok(false) |
| 310 |
} |
| 311 |
|
| 312 |
fn write_color<W: Write>(color: &Color, writer: &mut SvgWriter<W>) -> io::Result<()> { |
| 313 |
match color { |
| 314 |
Color::Black => write!(writer, "black"), |
| 315 |
Color::Blue => write!(writer, "blue"), |
| 316 |
Color::Green => write!(writer, "green"), |
| 317 |
Color::Red => write!(writer, "red"), |
| 318 |
Color::Cyan => write!(writer, "cyan"), |
| 319 |
Color::Magenta => write!(writer, "magenta"), |
| 320 |
Color::Yellow => write!(writer, "yellow"), |
| 321 |
Color::White => write!(writer, "white"), |
| 322 |
// TODO: other colors |
| 323 |
_ => Ok(()), |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if let Some(fg) = spec.fg() { |
| 328 |
first = write_first(first, self)?; |
| 329 |
write!(self, "fg ")?; |
| 330 |
write_color(fg, self)?; |
| 331 |
} |
| 332 |
|
| 333 |
if let Some(bg) = spec.bg() { |
| 334 |
first = write_first(first, self)?; |
| 335 |
write!(self, "bg ")?; |
| 336 |
write_color(bg, self)?; |
| 337 |
} |
| 338 |
|
| 339 |
if spec.bold() { |
| 340 |
first = write_first(first, self)?; |
| 341 |
write!(self, "bold")?; |
| 342 |
} |
| 343 |
|
| 344 |
if spec.underline() { |
| 345 |
first = write_first(first, self)?; |
| 346 |
write!(self, "underline")?; |
| 347 |
} |
| 348 |
|
| 349 |
if spec.intense() { |
| 350 |
first = write_first(first, self)?; |
| 351 |
write!(self, "bright")?; |
| 352 |
} |
| 353 |
|
| 354 |
write!(self, "\">")?; |
| 355 |
|
| 356 |
Ok(()) |
| 357 |
} |
| 358 |
|
| 359 |
fn reset(&mut self) -> io::Result<()> { |
| 360 |
let color = self.color.clone(); |
| 361 |
|
| 362 |
if color != ColorSpec::new() { |
| 363 |
write!(self, "</span>")?; |
| 364 |
self.color = ColorSpec::new(); |
| 365 |
} |
| 366 |
|
| 367 |
Ok(()) |
| 368 |
} |
| 369 |
} |