|
| 1 | +use std::fs; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use codespan_reporting::diagnostic::{Diagnostic, Label}; |
| 5 | +use codespan_reporting::term::{self, termcolor}; |
| 6 | +use termcolor::{ColorChoice, StandardStream}; |
| 7 | +use typst::diag::{bail, SourceError, StrResult}; |
| 8 | +use typst::doc::Document; |
| 9 | +use typst::eval::eco_format; |
| 10 | +use typst::file::FileId; |
| 11 | +use typst::geom::Color; |
| 12 | +use typst::syntax::Source; |
| 13 | +use typst::World; |
| 14 | + |
| 15 | +use crate::args::{CompileCommand, DiagnosticFormat}; |
| 16 | +use crate::watch::Status; |
| 17 | +use crate::world::SystemWorld; |
| 18 | +use crate::{color_stream, set_failed}; |
| 19 | + |
| 20 | +type CodespanResult<T> = Result<T, CodespanError>; |
| 21 | +type CodespanError = codespan_reporting::files::Error; |
| 22 | + |
| 23 | +/// Execute a compilation command. |
| 24 | +pub fn compile(mut command: CompileCommand) -> StrResult<()> { |
| 25 | + let mut world = SystemWorld::new(&command)?; |
| 26 | + compile_once(&mut world, &mut command, false)?; |
| 27 | + Ok(()) |
| 28 | +} |
| 29 | + |
| 30 | +/// Compile a single time. |
| 31 | +/// |
| 32 | +/// Returns whether it compiled without errors. |
| 33 | +#[tracing::instrument(skip_all)] |
| 34 | +pub fn compile_once( |
| 35 | + world: &mut SystemWorld, |
| 36 | + command: &mut CompileCommand, |
| 37 | + watching: bool, |
| 38 | +) -> StrResult<()> { |
| 39 | + tracing::info!("Starting compilation"); |
| 40 | + |
| 41 | + let start = std::time::Instant::now(); |
| 42 | + if watching { |
| 43 | + Status::Compiling.print(command).unwrap(); |
| 44 | + } |
| 45 | + |
| 46 | + // Reset everything and ensure that the main file is still present. |
| 47 | + world.reset(); |
| 48 | + world.source(world.main()).map_err(|err| err.to_string())?; |
| 49 | + |
| 50 | + let result = typst::compile(world); |
| 51 | + let duration = start.elapsed(); |
| 52 | + |
| 53 | + match result { |
| 54 | + // Export the PDF / PNG. |
| 55 | + Ok(document) => { |
| 56 | + export(&document, command)?; |
| 57 | + |
| 58 | + tracing::info!("Compilation succeeded in {duration:?}"); |
| 59 | + if watching { |
| 60 | + Status::Success(duration).print(command).unwrap(); |
| 61 | + } |
| 62 | + |
| 63 | + if let Some(open) = command.open.take() { |
| 64 | + open_file(open.as_deref(), &command.output())?; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Print diagnostics. |
| 69 | + Err(errors) => { |
| 70 | + set_failed(); |
| 71 | + tracing::info!("Compilation failed"); |
| 72 | + |
| 73 | + if watching { |
| 74 | + Status::Error.print(command).unwrap(); |
| 75 | + } |
| 76 | + |
| 77 | + print_diagnostics(world, *errors, command.diagnostic_format) |
| 78 | + .map_err(|_| "failed to print diagnostics")?; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Ok(()) |
| 83 | +} |
| 84 | + |
| 85 | +/// Export into the target format. |
| 86 | +fn export(document: &Document, command: &CompileCommand) -> StrResult<()> { |
| 87 | + match command.output().extension() { |
| 88 | + Some(ext) if ext.eq_ignore_ascii_case("png") => export_png(document, command), |
| 89 | + _ => export_pdf(document, command), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// Export to a PDF. |
| 94 | +fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> { |
| 95 | + let output = command.output(); |
| 96 | + let buffer = typst::export::pdf(document); |
| 97 | + fs::write(output, buffer).map_err(|_| "failed to write PDF file")?; |
| 98 | + Ok(()) |
| 99 | +} |
| 100 | + |
| 101 | +/// Export to one or multiple PNGs. |
| 102 | +fn export_png(document: &Document, command: &CompileCommand) -> StrResult<()> { |
| 103 | + // Determine whether we have a `{n}` numbering. |
| 104 | + let output = command.output(); |
| 105 | + let string = output.to_str().unwrap_or_default(); |
| 106 | + let numbered = string.contains("{n}"); |
| 107 | + if !numbered && document.pages.len() > 1 { |
| 108 | + bail!("cannot export multiple PNGs without `{{n}}` in output path"); |
| 109 | + } |
| 110 | + |
| 111 | + // Find a number width that accommodates all pages. For instance, the |
| 112 | + // first page should be numbered "001" if there are between 100 and |
| 113 | + // 999 pages. |
| 114 | + let width = 1 + document.pages.len().checked_ilog10().unwrap_or(0) as usize; |
| 115 | + let mut storage; |
| 116 | + |
| 117 | + for (i, frame) in document.pages.iter().enumerate() { |
| 118 | + let pixmap = typst::export::render(frame, command.ppi / 72.0, Color::WHITE); |
| 119 | + let path = if numbered { |
| 120 | + storage = string.replace("{n}", &format!("{:0width$}", i + 1)); |
| 121 | + Path::new(&storage) |
| 122 | + } else { |
| 123 | + output.as_path() |
| 124 | + }; |
| 125 | + pixmap.save_png(path).map_err(|_| "failed to write PNG file")?; |
| 126 | + } |
| 127 | + |
| 128 | + Ok(()) |
| 129 | +} |
| 130 | + |
| 131 | +/// Opens the given file using: |
| 132 | +/// - The default file viewer if `open` is `None`. |
| 133 | +/// - The given viewer provided by `open` if it is `Some`. |
| 134 | +fn open_file(open: Option<&str>, path: &Path) -> StrResult<()> { |
| 135 | + if let Some(app) = open { |
| 136 | + open::with_in_background(path, app); |
| 137 | + } else { |
| 138 | + open::that_in_background(path); |
| 139 | + } |
| 140 | + |
| 141 | + Ok(()) |
| 142 | +} |
| 143 | + |
| 144 | +/// Print diagnostic messages to the terminal. |
| 145 | +fn print_diagnostics( |
| 146 | + world: &SystemWorld, |
| 147 | + errors: Vec<SourceError>, |
| 148 | + diagnostic_format: DiagnosticFormat, |
| 149 | +) -> Result<(), codespan_reporting::files::Error> { |
| 150 | + let mut w = match diagnostic_format { |
| 151 | + DiagnosticFormat::Human => color_stream(), |
| 152 | + DiagnosticFormat::Short => StandardStream::stderr(ColorChoice::Never), |
| 153 | + }; |
| 154 | + |
| 155 | + let mut config = term::Config { tab_width: 2, ..Default::default() }; |
| 156 | + if diagnostic_format == DiagnosticFormat::Short { |
| 157 | + config.display_style = term::DisplayStyle::Short; |
| 158 | + } |
| 159 | + |
| 160 | + for error in errors { |
| 161 | + // The main diagnostic. |
| 162 | + let diag = Diagnostic::error() |
| 163 | + .with_message(error.message) |
| 164 | + .with_notes( |
| 165 | + error |
| 166 | + .hints |
| 167 | + .iter() |
| 168 | + .map(|e| (eco_format!("hint: {e}")).into()) |
| 169 | + .collect(), |
| 170 | + ) |
| 171 | + .with_labels(vec![Label::primary(error.span.id(), error.span.range(world))]); |
| 172 | + |
| 173 | + term::emit(&mut w, &config, world, &diag)?; |
| 174 | + |
| 175 | + // Stacktrace-like helper diagnostics. |
| 176 | + for point in error.trace { |
| 177 | + let message = point.v.to_string(); |
| 178 | + let help = Diagnostic::help().with_message(message).with_labels(vec![ |
| 179 | + Label::primary(point.span.id(), point.span.range(world)), |
| 180 | + ]); |
| 181 | + |
| 182 | + term::emit(&mut w, &config, world, &help)?; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + Ok(()) |
| 187 | +} |
| 188 | + |
| 189 | +impl<'a> codespan_reporting::files::Files<'a> for SystemWorld { |
| 190 | + type FileId = FileId; |
| 191 | + type Name = FileId; |
| 192 | + type Source = Source; |
| 193 | + |
| 194 | + fn name(&'a self, id: FileId) -> CodespanResult<Self::Name> { |
| 195 | + Ok(id) |
| 196 | + } |
| 197 | + |
| 198 | + fn source(&'a self, id: FileId) -> CodespanResult<Self::Source> { |
| 199 | + Ok(self.lookup(id)) |
| 200 | + } |
| 201 | + |
| 202 | + fn line_index(&'a self, id: FileId, given: usize) -> CodespanResult<usize> { |
| 203 | + let source = self.lookup(id); |
| 204 | + source |
| 205 | + .byte_to_line(given) |
| 206 | + .ok_or_else(|| CodespanError::IndexTooLarge { |
| 207 | + given, |
| 208 | + max: source.len_bytes(), |
| 209 | + }) |
| 210 | + } |
| 211 | + |
| 212 | + fn line_range( |
| 213 | + &'a self, |
| 214 | + id: FileId, |
| 215 | + given: usize, |
| 216 | + ) -> CodespanResult<std::ops::Range<usize>> { |
| 217 | + let source = self.lookup(id); |
| 218 | + source |
| 219 | + .line_to_range(given) |
| 220 | + .ok_or_else(|| CodespanError::LineTooLarge { given, max: source.len_lines() }) |
| 221 | + } |
| 222 | + |
| 223 | + fn column_number( |
| 224 | + &'a self, |
| 225 | + id: FileId, |
| 226 | + _: usize, |
| 227 | + given: usize, |
| 228 | + ) -> CodespanResult<usize> { |
| 229 | + let source = self.lookup(id); |
| 230 | + source.byte_to_column(given).ok_or_else(|| { |
| 231 | + let max = source.len_bytes(); |
| 232 | + if given <= max { |
| 233 | + CodespanError::InvalidCharBoundary { given } |
| 234 | + } else { |
| 235 | + CodespanError::IndexTooLarge { given, max } |
| 236 | + } |
| 237 | + }) |
| 238 | + } |
| 239 | +} |
0 commit comments