Skip to content

Commit

Permalink
Revert "Revert "revert html changes""
Browse files Browse the repository at this point in the history
This reverts commit b426433.
  • Loading branch information
Myriad-Dreamin committed Feb 8, 2025
1 parent b426433 commit fa66c28
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 119 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/tinymist-project/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<F: CompilerFeat> CompiledArtifact<F> {
/// Runs the compiler and returns the compiled document.
pub fn from_snapshot(mut snap: CompileSnapshot<F>) -> CompiledArtifact<F> {
snap.world.set_is_compiling(true);
let res = ::typst::compile::<tinymist_std::typst::TypstPagedDocument>(&snap.world);
let res = ::typst::compile(&snap.world);
let warned = match res.output {
Ok(doc) => Ok(Warned {
output: Arc::new(doc),
Expand Down
3 changes: 1 addition & 2 deletions crates/tinymist-task/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
63 changes: 22 additions & 41 deletions crates/tinymist-task/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ 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,
};
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};

Expand All @@ -41,10 +40,10 @@ pub struct HtmlFlag;
pub struct ExportTimings;

impl ExportTimings {
pub fn needs_run<F: CompilerFeat, D: typst::Document>(
pub fn needs_run<F: CompilerFeat>(
snap: &CompileSnapshot<F>,
timing: Option<TaskWhen>,
docs: Option<&D>,
docs: Option<&TypstPagedDocument>,
) -> Option<bool> {
let s = snap.signal;
let when = timing.unwrap_or(TaskWhen::Never);
Expand All @@ -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),
}
Expand Down Expand Up @@ -136,7 +135,7 @@ impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for PngExport {

pixmap
.encode_png()
.map(Bytes::new)
.map(Bytes::from)
.context_ut("failed to encode PNG")
}
}
Expand All @@ -149,21 +148,6 @@ impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for PngExport {
// }
// }

pub struct HtmlExport;

impl<F: CompilerFeat> ExportComputation<F, TypstHtmlDocument> for HtmlExport {
type Output = String;
type Config = ExportHtmlTask;

fn run(
_graph: &Arc<WorldComputeGraph<F>>,
doc: &Arc<TypstHtmlDocument>,
_config: &ExportHtmlTask,
) -> Result<String> {
Ok(typst_html::html(doc)?)
}
}

// impl<F: CompilerFeat> WorldComputable<F> for HtmlExport {
// type Output = Option<String>;

Expand All @@ -177,13 +161,12 @@ pub struct DocumentQuery;
impl DocumentQuery {
// todo: query exporter
/// Retrieve the matches for the selector.
pub fn retrieve<D: typst::Document>(
pub fn retrieve(
world: &dyn World,
selector: &str,
document: &D,
document: &TypstPagedDocument,
) -> StrResult<Vec<Content>> {
let selector = eval_string(
&typst::ROUTINES,
let selector = typst_shim::eval::eval_string(
world.track(),
selector,
Span::detached(),
Expand All @@ -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::<Vec<_>>())
}

fn run_inner<F: CompilerFeat, D: typst::Document>(
fn run_inner<F: CompilerFeat>(
g: &Arc<WorldComputeGraph<F>>,
doc: &Arc<D>,
doc: &Arc<TypstPagedDocument>,
config: &QueryTask,
) -> Result<Vec<Value>> {
let selector = &config.selector;
Expand All @@ -229,9 +212,9 @@ impl DocumentQuery {
.collect())
}

pub fn get_as_value<F: CompilerFeat, D: typst::Document>(
pub fn get_as_value<F: CompilerFeat>(
g: &Arc<WorldComputeGraph<F>>,
doc: &Arc<D>,
doc: &Arc<TypstPagedDocument>,
config: &QueryTask,
) -> Result<serde_json::Value> {
let mapped = Self::run_inner(g, doc, config)?;
Expand All @@ -249,13 +232,13 @@ impl DocumentQuery {
}
}

impl<F: CompilerFeat, D: typst::Document> ExportComputation<F, D> for DocumentQuery {
impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for DocumentQuery {
type Output = SourceResult<String>;
type Config = QueryTask;

fn run(
g: &Arc<WorldComputeGraph<F>>,
doc: &Arc<D>,
doc: &Arc<TypstPagedDocument>,
config: &QueryTask,
) -> Result<SourceResult<String>> {
let pretty = false;
Expand Down Expand Up @@ -373,19 +356,17 @@ fn parse_color(fill: String) -> anyhow::Result<Color> {
}
}

/// Convert [`chrono::DateTime`] to [`Timestamp`]
fn convert_datetime(date_time: chrono::DateTime<chrono::Utc>) -> Option<Timestamp> {
/// Convert [`chrono::DateTime`] to [`Datetime`]
fn convert_datetime(date_time: chrono::DateTime<chrono::Utc>) -> Option<Datetime> {
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)]
Expand Down
3 changes: 1 addition & 2 deletions crates/tinymist-task/src/compute/pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for PdfExport {
Expand All @@ -24,7 +23,7 @@ impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> 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),
Expand Down
1 change: 0 additions & 1 deletion crates/tinymist-task/src/compute/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl fmt::Display for FullTextDigest {
}
Ok(())
}
_ => Err(fmt::Error),
}
}
}
59 changes: 19 additions & 40 deletions crates/tinymist-world/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,6 +147,9 @@ impl<F: CompilerFeat> WorldComputeGraph<F> {
}
}

pub trait Document {}
impl Document for TypstPagedDocument {}

pub trait ExportDetection<F: CompilerFeat, D> {
type Config: Send + Sync + 'static;

Expand Down Expand Up @@ -200,21 +203,19 @@ impl<T> FlagTask<T> {
}

pub type PagedCompilationTask = CompilationTask<TypstPagedDocument>;
pub type HtmlCompilationTask = CompilationTask<TypstHtmlDocument>;

pub struct CompilationTask<D>(std::marker::PhantomData<D>);

impl<F: CompilerFeat, D> WorldComputable<F> for CompilationTask<D>
where
D: typst::Document + Send + Sync + 'static,
{
type Output = Option<Warned<SourceResult<Arc<D>>>>;
impl<F: CompilerFeat> WorldComputable<F> for CompilationTask<TypstPagedDocument> {
type Output = Option<Warned<SourceResult<Arc<TypstPagedDocument>>>>;

fn compute(graph: &Arc<WorldComputeGraph<F>>) -> Result<Self::Output> {
let enabled = graph.must_get::<FlagTask<CompilationTask<D>>>()?.enabled;
let enabled = graph
.must_get::<FlagTask<CompilationTask<TypstPagedDocument>>>()?
.enabled;

Ok(enabled.then(|| {
let compiled = typst::compile::<D>(&graph.snap.world);
let compiled = typst::compile(&graph.snap.world);
Warned {
output: compiled.output.map(Arc::new),
warnings: compiled.warnings,
Expand All @@ -225,25 +226,18 @@ where

pub struct OptionDocumentTask<D>(std::marker::PhantomData<D>);

impl<F: CompilerFeat, D> WorldComputable<F> for OptionDocumentTask<D>
where
D: typst::Document + Send + Sync + 'static,
{
type Output = Option<Arc<D>>;
impl<F: CompilerFeat> WorldComputable<F> for OptionDocumentTask<TypstPagedDocument> {
type Output = Option<Arc<TypstPagedDocument>>;

fn compute(graph: &Arc<WorldComputeGraph<F>>) -> Result<Self::Output> {
let doc = graph.compute::<CompilationTask<D>>()?;
let compiled = doc
.as_ref()
.as_ref()
.and_then(|warned| warned.output.clone().ok());
let doc = graph.compute::<CompilationTask<TypstPagedDocument>>()?;
let doc = doc.as_ref().as_ref();
let compiled = doc.and_then(|warned| warned.output.clone().ok());

Ok(compiled)
}
}

impl<D> OptionDocumentTask<D> where D: typst::Document + Send + Sync + 'static {}

struct CompilationDiagnostics {
errors: Option<EcoVec<typst::diag::SourceDiagnostic>>,
warnings: Option<EcoVec<typst::diag::SourceDiagnostic>>,
Expand All @@ -262,19 +256,16 @@ impl CompilationDiagnostics {

pub struct DiagnosticsTask {
paged: CompilationDiagnostics,
html: CompilationDiagnostics,
}

impl<F: CompilerFeat> WorldComputable<F> for DiagnosticsTask {
type Output = Self;

fn compute(graph: &Arc<WorldComputeGraph<F>>) -> Result<Self> {
let paged = graph.compute::<PagedCompilationTask>()?.clone();
let html = graph.compute::<HtmlCompilationTask>()?.clone();

Ok(Self {
paged: CompilationDiagnostics::from_result(&paged),
html: CompilationDiagnostics::from_result(&html),
paged: CompilationDiagnostics::from_result(paged.as_ref()),
})
}
}
Expand All @@ -285,8 +276,6 @@ impl DiagnosticsTask {
.errors
.iter()
.chain(self.paged.warnings.iter())
.chain(self.html.errors.iter())
.chain(self.html.warnings.iter())
.flatten()
}
}
Expand Down Expand Up @@ -353,8 +342,8 @@ impl<F: CompilerFeat> WorldComputeGraph<F> {
}

/// Compile once from scratch.
pub fn pure_compile<D: ::typst::Document>(&self) -> Warned<SourceResult<Arc<D>>> {
let res = ::typst::compile::<D>(&self.snap.world);
pub fn pure_compile(&self) -> Warned<SourceResult<Arc<TypstPagedDocument>>> {
let res = ::typst::compile(&self.snap.world);
// compile document
Warned {
output: res.output.map(Arc::new),
Expand All @@ -366,14 +355,4 @@ impl<F: CompilerFeat> WorldComputeGraph<F> {
pub fn compile(&self) -> Warned<SourceResult<Arc<TypstPagedDocument>>> {
self.pure_compile()
}

/// Compile to html once from scratch.
pub fn compile_html(&self) -> Warned<SourceResult<Arc<::typst::html::HtmlDocument>>> {
self.pure_compile()
}

// With **the compilation state**, query the matches for the selector.
// fn query(&mut self, selector: String, document: &TypstDocument) ->
// SourceResult<Vec<Content>> { self.pure_query(world, selector,
// document) }
}
4 changes: 1 addition & 3 deletions crates/tinymist-world/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TypstPagedDocument>(&world);
let _res = typst::compile(&world);
}

static FONT_COMPUTED: AtomicBool = AtomicBool::new(false);
Expand Down
1 change: 1 addition & 0 deletions crates/tinymist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
Loading

0 comments on commit fa66c28

Please sign in to comment.