From fa66c28bd5cd1ed264cb7ad5f46d048310f0f37d Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Sat, 8 Feb 2025 22:07:10 +0800 Subject: [PATCH] Revert "Revert "revert html changes"" This reverts commit b42643399c70574a303ef16e068168e35c6c836e. --- Cargo.lock | 1 + crates/tinymist-project/src/compiler.rs | 2 +- crates/tinymist-task/Cargo.toml | 3 +- crates/tinymist-task/src/compute.rs | 63 +++++++++--------------- crates/tinymist-task/src/compute/pdf.rs | 3 +- crates/tinymist-task/src/compute/text.rs | 1 - crates/tinymist-world/src/compute.rs | 59 +++++++--------------- crates/tinymist-world/src/system.rs | 4 +- crates/tinymist/Cargo.toml | 1 + crates/tinymist/src/task/export2.rs | 51 +++++++++---------- 10 files changed, 69 insertions(+), 119 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d77710bbd..d71d68e49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4238,6 +4238,7 @@ dependencies = [ "typst-assets", "typst-pdf", "typst-render", + "typst-shim", "typst-svg", ] diff --git a/crates/tinymist-project/src/compiler.rs b/crates/tinymist-project/src/compiler.rs index fe58f03a1..71ff03c8c 100644 --- a/crates/tinymist-project/src/compiler.rs +++ b/crates/tinymist-project/src/compiler.rs @@ -90,7 +90,7 @@ impl CompiledArtifact { /// Runs the compiler and returns the compiled document. pub fn from_snapshot(mut snap: CompileSnapshot) -> CompiledArtifact { snap.world.set_is_compiling(true); - let res = ::typst::compile::(&snap.world); + let res = ::typst::compile(&snap.world); let warned = match res.output { Ok(doc) => Ok(Warned { output: Arc::new(doc), diff --git a/crates/tinymist-task/Cargo.toml b/crates/tinymist-task/Cargo.toml index 3aee84eb7..62b4aae20 100644 --- a/crates/tinymist-task/Cargo.toml +++ b/crates/tinymist-task/Cargo.toml @@ -35,10 +35,9 @@ toml.workspace = true typst.workspace = true typst-assets.workspace = true typst-pdf.workspace = true -typst-html.workspace = true +typst-shim.workspace = true typst-svg.workspace = true typst-render.workspace = true -typst-eval.workspace = true notify.workspace = true [features] diff --git a/crates/tinymist-task/src/compute.rs b/crates/tinymist-task/src/compute.rs index a4c68744f..bd28902ea 100644 --- a/crates/tinymist-task/src/compute.rs +++ b/crates/tinymist-task/src/compute.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use comemo::Track; use ecow::EcoString; use tinymist_std::error::prelude::*; -use tinymist_std::typst::{TypstHtmlDocument, TypstPagedDocument}; +use tinymist_std::typst::TypstPagedDocument; use tinymist_world::{ args::convert_source_date_epoch, CompileSnapshot, CompilerFeat, ExportComputation, WorldComputeGraph, @@ -14,14 +14,13 @@ use tinymist_world::{ use typst::diag::{SourceResult, StrResult}; use typst::foundations::{Bytes, Content, Datetime, IntoValue, LocatableSelector, Scope, Value}; use typst::layout::Abs; -use typst::routines::EvalMode; use typst::syntax::{ast, Span, SyntaxNode}; use typst::visualize::Color; use typst::World; -use typst_eval::eval_string; -use typst_pdf::{PdfOptions, Timestamp}; +use typst_pdf::PdfOptions; +use typst_shim::eval::EvalMode; -use crate::model::{ExportHtmlTask, ExportPdfTask, ExportPngTask, ExportSvgTask}; +use crate::model::{ExportPdfTask, ExportPngTask, ExportSvgTask}; use crate::primitives::TaskWhen; use crate::{ExportTransform, Pages, QueryTask}; @@ -41,10 +40,10 @@ pub struct HtmlFlag; pub struct ExportTimings; impl ExportTimings { - pub fn needs_run( + pub fn needs_run( snap: &CompileSnapshot, timing: Option, - docs: Option<&D>, + docs: Option<&TypstPagedDocument>, ) -> Option { let s = snap.signal; let when = timing.unwrap_or(TaskWhen::Never); @@ -57,7 +56,7 @@ impl ExportTimings { TaskWhen::OnType => Some(s.by_mem_events), TaskWhen::OnSave => Some(s.by_fs_events), TaskWhen::OnDocumentHasTitle if s.by_fs_events => { - docs.map(|doc| doc.info().title.is_some()) + docs.map(|doc| doc.info.title.is_some()) } TaskWhen::OnDocumentHasTitle => Some(false), } @@ -136,7 +135,7 @@ impl ExportComputation for PngExport { pixmap .encode_png() - .map(Bytes::new) + .map(Bytes::from) .context_ut("failed to encode PNG") } } @@ -149,21 +148,6 @@ impl ExportComputation for PngExport { // } // } -pub struct HtmlExport; - -impl ExportComputation for HtmlExport { - type Output = String; - type Config = ExportHtmlTask; - - fn run( - _graph: &Arc>, - doc: &Arc, - _config: &ExportHtmlTask, - ) -> Result { - Ok(typst_html::html(doc)?) - } -} - // impl WorldComputable for HtmlExport { // type Output = Option; @@ -177,13 +161,12 @@ pub struct DocumentQuery; impl DocumentQuery { // todo: query exporter /// Retrieve the matches for the selector. - pub fn retrieve( + pub fn retrieve( world: &dyn World, selector: &str, - document: &D, + document: &TypstPagedDocument, ) -> StrResult> { - let selector = eval_string( - &typst::ROUTINES, + let selector = typst_shim::eval::eval_string( world.track(), selector, Span::detached(), @@ -202,15 +185,15 @@ impl DocumentQuery { .map_err(|e| EcoString::from(format!("failed to cast: {}", e.message())))?; Ok(document - .introspector() + .introspector .query(&selector.0) .into_iter() .collect::>()) } - fn run_inner( + fn run_inner( g: &Arc>, - doc: &Arc, + doc: &Arc, config: &QueryTask, ) -> Result> { let selector = &config.selector; @@ -229,9 +212,9 @@ impl DocumentQuery { .collect()) } - pub fn get_as_value( + pub fn get_as_value( g: &Arc>, - doc: &Arc, + doc: &Arc, config: &QueryTask, ) -> Result { let mapped = Self::run_inner(g, doc, config)?; @@ -249,13 +232,13 @@ impl DocumentQuery { } } -impl ExportComputation for DocumentQuery { +impl ExportComputation for DocumentQuery { type Output = SourceResult; type Config = QueryTask; fn run( g: &Arc>, - doc: &Arc, + doc: &Arc, config: &QueryTask, ) -> Result> { let pretty = false; @@ -373,19 +356,17 @@ fn parse_color(fill: String) -> anyhow::Result { } } -/// Convert [`chrono::DateTime`] to [`Timestamp`] -fn convert_datetime(date_time: chrono::DateTime) -> Option { +/// Convert [`chrono::DateTime`] to [`Datetime`] +fn convert_datetime(date_time: chrono::DateTime) -> Option { use chrono::{Datelike, Timelike}; - let datetime = Datetime::from_ymd_hms( + Datetime::from_ymd_hms( date_time.year(), date_time.month().try_into().ok()?, date_time.day().try_into().ok()?, date_time.hour().try_into().ok()?, date_time.minute().try_into().ok()?, date_time.second().try_into().ok()?, - ); - - Some(Timestamp::new_utc(datetime.unwrap())) + ) } #[cfg(test)] diff --git a/crates/tinymist-task/src/compute/pdf.rs b/crates/tinymist-task/src/compute/pdf.rs index a19f7df74..cb65e1b0e 100644 --- a/crates/tinymist-task/src/compute/pdf.rs +++ b/crates/tinymist-task/src/compute/pdf.rs @@ -2,7 +2,6 @@ use super::*; pub use typst_pdf::pdf; pub use typst_pdf::PdfStandard as TypstPdfStandard; -pub use typst_pdf::Timestamp as TypstTimestamp; pub struct PdfExport; impl ExportComputation for PdfExport { @@ -24,7 +23,7 @@ impl ExportComputation for PdfExport { // todo: Some(pdf_uri.as_str()) - Ok(Bytes::new(typst_pdf::pdf( + Ok(Bytes::from(typst_pdf::pdf( doc, &PdfOptions { timestamp: convert_datetime(creation_timestamp), diff --git a/crates/tinymist-task/src/compute/text.rs b/crates/tinymist-task/src/compute/text.rs index ad117505b..eff566195 100644 --- a/crates/tinymist-task/src/compute/text.rs +++ b/crates/tinymist-task/src/compute/text.rs @@ -66,7 +66,6 @@ impl fmt::Display for FullTextDigest { } Ok(()) } - _ => Err(fmt::Error), } } } diff --git a/crates/tinymist-world/src/compute.rs b/crates/tinymist-world/src/compute.rs index e0b5e2941..3ce870a4c 100644 --- a/crates/tinymist-world/src/compute.rs +++ b/crates/tinymist-world/src/compute.rs @@ -3,11 +3,11 @@ use std::any::TypeId; use std::sync::{Arc, OnceLock}; +use ecow::EcoVec; use parking_lot::Mutex; use tinymist_std::error::prelude::*; -use tinymist_std::typst::{TypstHtmlDocument, TypstPagedDocument}; +use tinymist_std::typst::TypstPagedDocument; use typst::diag::{At, SourceResult, Warned}; -use typst::ecow::EcoVec; use typst::syntax::Span; use crate::snapshot::CompileSnapshot; @@ -147,6 +147,9 @@ impl WorldComputeGraph { } } +pub trait Document {} +impl Document for TypstPagedDocument {} + pub trait ExportDetection { type Config: Send + Sync + 'static; @@ -200,21 +203,19 @@ impl FlagTask { } pub type PagedCompilationTask = CompilationTask; -pub type HtmlCompilationTask = CompilationTask; pub struct CompilationTask(std::marker::PhantomData); -impl WorldComputable for CompilationTask -where - D: typst::Document + Send + Sync + 'static, -{ - type Output = Option>>>; +impl WorldComputable for CompilationTask { + type Output = Option>>>; fn compute(graph: &Arc>) -> Result { - let enabled = graph.must_get::>>()?.enabled; + let enabled = graph + .must_get::>>()? + .enabled; Ok(enabled.then(|| { - let compiled = typst::compile::(&graph.snap.world); + let compiled = typst::compile(&graph.snap.world); Warned { output: compiled.output.map(Arc::new), warnings: compiled.warnings, @@ -225,25 +226,18 @@ where pub struct OptionDocumentTask(std::marker::PhantomData); -impl WorldComputable for OptionDocumentTask -where - D: typst::Document + Send + Sync + 'static, -{ - type Output = Option>; +impl WorldComputable for OptionDocumentTask { + type Output = Option>; fn compute(graph: &Arc>) -> Result { - let doc = graph.compute::>()?; - let compiled = doc - .as_ref() - .as_ref() - .and_then(|warned| warned.output.clone().ok()); + let doc = graph.compute::>()?; + let doc = doc.as_ref().as_ref(); + let compiled = doc.and_then(|warned| warned.output.clone().ok()); Ok(compiled) } } -impl OptionDocumentTask where D: typst::Document + Send + Sync + 'static {} - struct CompilationDiagnostics { errors: Option>, warnings: Option>, @@ -262,7 +256,6 @@ impl CompilationDiagnostics { pub struct DiagnosticsTask { paged: CompilationDiagnostics, - html: CompilationDiagnostics, } impl WorldComputable for DiagnosticsTask { @@ -270,11 +263,9 @@ impl WorldComputable for DiagnosticsTask { fn compute(graph: &Arc>) -> Result { let paged = graph.compute::()?.clone(); - let html = graph.compute::()?.clone(); Ok(Self { - paged: CompilationDiagnostics::from_result(&paged), - html: CompilationDiagnostics::from_result(&html), + paged: CompilationDiagnostics::from_result(paged.as_ref()), }) } } @@ -285,8 +276,6 @@ impl DiagnosticsTask { .errors .iter() .chain(self.paged.warnings.iter()) - .chain(self.html.errors.iter()) - .chain(self.html.warnings.iter()) .flatten() } } @@ -353,8 +342,8 @@ impl WorldComputeGraph { } /// Compile once from scratch. - pub fn pure_compile(&self) -> Warned>> { - let res = ::typst::compile::(&self.snap.world); + pub fn pure_compile(&self) -> Warned>> { + let res = ::typst::compile(&self.snap.world); // compile document Warned { output: res.output.map(Arc::new), @@ -366,14 +355,4 @@ impl WorldComputeGraph { pub fn compile(&self) -> Warned>> { self.pure_compile() } - - /// Compile to html once from scratch. - pub fn compile_html(&self) -> Warned>> { - self.pure_compile() - } - - // With **the compilation state**, query the matches for the selector. - // fn query(&mut self, selector: String, document: &TypstDocument) -> - // SourceResult> { self.pure_query(world, selector, - // document) } } diff --git a/crates/tinymist-world/src/system.rs b/crates/tinymist-world/src/system.rs index 0ba61cc30..008a285cd 100644 --- a/crates/tinymist-world/src/system.rs +++ b/crates/tinymist-world/src/system.rs @@ -118,15 +118,13 @@ mod tests { #[test] fn test_args() { - use tinymist_std::typst::TypstPagedDocument; - let args = CompileOnceArgs::parse_from(["tinymist", "main.typ"]); let verse = args .resolve_system() .expect("failed to resolve system universe"); let world = verse.snapshot(); - let _res = typst::compile::(&world); + let _res = typst::compile(&world); } static FONT_COMPUTED: AtomicBool = AtomicBool::new(false); diff --git a/crates/tinymist/Cargo.toml b/crates/tinymist/Cargo.toml index 79ac38f31..0f96ce431 100644 --- a/crates/tinymist/Cargo.toml +++ b/crates/tinymist/Cargo.toml @@ -102,6 +102,7 @@ embed-fonts = ["tinymist-project/fonts"] # This requires modifying typst. no-content-hint = [ "tinymist-query/no-content-hint", + "tinymist-task/no-content-hint", "reflexo-typst/no-content-hint", "reflexo-vec2svg/no-content-hint", ] diff --git a/crates/tinymist/src/task/export2.rs b/crates/tinymist/src/task/export2.rs index abe35e9e2..7f79b73b1 100644 --- a/crates/tinymist/src/task/export2.rs +++ b/crates/tinymist/src/task/export2.rs @@ -2,8 +2,8 @@ use std::sync::Arc; -use reflexo_typst::{Bytes, CompilerFeat, EntryReader}; -use tinymist_project::{HtmlExport, LspCompilerFeat, PdfExport, PngExport, SvgExport, TaskWhen}; +use reflexo_typst::Bytes; +use tinymist_project::{EntryReader, LspCompilerFeat, PdfExport, PngExport, SvgExport, TaskWhen}; use tinymist_std::error::prelude::*; use tinymist_std::typst::{TypstDocument, TypstPagedDocument}; use tinymist_task::ExportTimings; @@ -13,8 +13,8 @@ use typst::diag::SourceResult; use crate::project::{ExportMarkdownTask, ExportTextTask, ProjectTask}; use crate::tool::text::FullTextDigest; use crate::world::base::{ - ConfigTask, DiagnosticsTask, ExportComputation, FlagTask, HtmlCompilationTask, - OptionDocumentTask, PagedCompilationTask, WorldComputable, WorldComputeGraph, + CompilerFeat, ConfigTask, DiagnosticsTask, ExportComputation, FlagTask, OptionDocumentTask, + PagedCompilationTask, WorldComputable, WorldComputeGraph, }; #[derive(Clone, Copy, Default)] @@ -24,7 +24,6 @@ impl ProjectCompilation { pub fn preconfig_timings(graph: &Arc>) -> Result { // todo: configure run_diagnostics! let paged_diag = Some(TaskWhen::OnType); - let html_diag = Some(TaskWhen::Never); let pdf: Option = graph .get::>::Config>>() @@ -38,10 +37,6 @@ impl ProjectCompilation { .get::>::Config>>() .transpose()? .map(|config| config.export.when); - let html: Option = graph - .get::>::Config>>() - .transpose()? - .map(|config| config.export.when); let md: Option = graph .get::>() .transpose()? @@ -55,12 +50,10 @@ impl ProjectCompilation { let check = |timing| ExportTimings::needs_run(&graph.snap, timing, doc).unwrap_or(true); let compile_paged = [paged_diag, pdf, svg, png, text, md].into_iter().any(check); - let compile_html = [html_diag, html].into_iter().any(check); let _ = graph.provide::>(Ok(FlagTask::flag(compile_paged))); - let _ = graph.provide::>(Ok(FlagTask::flag(compile_html))); - Ok(compile_paged || compile_html) + Ok(compile_paged) } } @@ -77,15 +70,12 @@ impl WorldComputable for ProjectCompilation { pub struct ProjectExport; impl ProjectExport { - fn export_bytes< - D: typst::Document + Send + Sync + 'static, - T: ExportComputation, - >( + fn export_bytes>( graph: &Arc>, when: Option, config: &T::Config, ) -> Result> { - let doc = graph.compute::>()?; + let doc = graph.compute::>()?; let doc = doc.as_ref(); let n = ExportTimings::needs_run(&graph.snap, when, doc.as_deref()).unwrap_or(true); if !n { @@ -96,15 +86,12 @@ impl ProjectExport { res.transpose() } - fn export_string< - D: typst::Document + Send + Sync + 'static, - T: ExportComputation, - >( + fn export_string>( graph: &Arc>, when: Option, config: &T::Config, ) -> Result> { - let doc = graph.compute::>()?; + let doc = graph.compute::>()?; let doc = doc.as_ref(); let n = ExportTimings::needs_run(&graph.snap, when, doc.as_deref()).unwrap_or(true); if !n { @@ -112,7 +99,11 @@ impl ProjectExport { } let doc = doc.as_ref(); - let res = doc.map(|doc| T::run(graph, doc, config).map(Bytes::from_string)); + let res = doc.map(|doc| { + T::run(graph, doc, config) + .map(String::into_bytes) + .map(Bytes::from) + }); res.transpose() } } @@ -133,10 +124,10 @@ impl WorldComputable for ProjectExport { use ProjectTask::*; match config.as_ref() { Preview(..) => todo!(), - ExportPdf(config) => Self::export_bytes::<_, PdfExport>(graph, when, config), - ExportPng(config) => Self::export_bytes::<_, PngExport>(graph, when, config), - ExportSvg(config) => Self::export_string::<_, SvgExport>(graph, when, config), - ExportHtml(config) => Self::export_string::<_, HtmlExport>(graph, when, config), + ExportPdf(config) => Self::export_bytes::(graph, when, config), + ExportPng(config) => Self::export_bytes::(graph, when, config), + ExportSvg(config) => Self::export_string::(graph, when, config), + ExportHtml(_config) => todo!(), ExportMd(_config) => { let doc = graph.compute::>()?; let doc = doc.as_ref(); @@ -146,9 +137,11 @@ impl WorldComputable for ProjectExport { return Ok(None); } - Ok(TypliteMdExport::run(graph)?.map(Bytes::from_string)) + Ok(TypliteMdExport::run(graph)? + .map(String::into_bytes) + .map(Bytes::from)) } - ExportText(config) => Self::export_string::<_, TextExport>(graph, when, config), + ExportText(config) => Self::export_string::(graph, when, config), Query(..) => todo!(), } };