From 22bae1da396d465ec6b8099b6fcd9feaf357e30e Mon Sep 17 00:00:00 2001 From: Wenzhuo Liu Date: Sat, 10 Jun 2023 15:26:54 +0800 Subject: [PATCH] chore: upgrade to typst 0.5.0 (#40) --- Cargo.toml | 6 +++--- addons/vscode/CHANGELOG.md | 6 +++++- addons/vscode/README.md | 6 +++++- addons/vscode/package.json | 2 +- src/main.rs | 26 +++++++++++++++++++++++--- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 20133116..f131e547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "typst-ws" -version = "0.4.0" +version = "0.5.0" authors = ["The Typst Project Developers"] edition = "2021" @@ -13,8 +13,8 @@ bench = false doc = false [dependencies] -typst = { git = "https://github.com/typst/typst.git", tag = "v0.4.0"} -typst-library = { git = "https://github.com/typst/typst.git", tag = "v0.4.0" } +typst = { git = "https://github.com/typst/typst.git", tag = "v0.5.0"} +typst-library = { git = "https://github.com/typst/typst.git", tag = "v0.5.0" } chrono = { version = "0.4", default-features = false, features = [ "clock", "std", diff --git a/addons/vscode/CHANGELOG.md b/addons/vscode/CHANGELOG.md index 0fe96712..3ec39889 100644 --- a/addons/vscode/CHANGELOG.md +++ b/addons/vscode/CHANGELOG.md @@ -57,4 +57,8 @@ Add preview button ## 0.4.1 - Makes the WebSocket connection retry itself when it is closed, with a delay of 1 second. +- Makes the WebSocket connection retry itself when it is closed, with a delay of 1 second. + +## v0.5.0 + +- Upgrade to typst v0.5.0 diff --git a/addons/vscode/README.md b/addons/vscode/README.md index bed3740d..dc2a28b4 100644 --- a/addons/vscode/README.md +++ b/addons/vscode/README.md @@ -75,4 +75,8 @@ Add preview button ### 0.4.1 - Makes the WebSocket connection retry itself when it is closed, with a delay of 1 second. +- Makes the WebSocket connection retry itself when it is closed, with a delay of 1 second. + +### v0.5.0 + +- Upgrade to typst v0.5.0 diff --git a/addons/vscode/package.json b/addons/vscode/package.json index 4cb93ba1..bbd3ef22 100644 --- a/addons/vscode/package.json +++ b/addons/vscode/package.json @@ -8,7 +8,7 @@ "type": "git", "url": "https://github.com/Enter-tainer/typst-preview-vscode" }, - "version": "0.4.1", + "version": "0.5.0", "engines": { "vscode": "^1.77.0" }, diff --git a/src/main.rs b/src/main.rs index 04261638..9d784860 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod args; +use chrono::Datelike; use clap::Parser; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::term::{self, termcolor}; @@ -14,7 +15,7 @@ use once_cell::unsync::OnceCell; use same_file::Handle; use serde::{Deserialize, Serialize}; use siphasher::sip128::{Hasher128, SipHasher}; -use std::cell::{RefCell, RefMut}; +use std::cell::{RefCell, RefMut, Cell}; use std::collections::HashMap; use std::fs::{self, File}; use std::hash::Hash; @@ -32,7 +33,7 @@ use tokio::sync::{Mutex, RwLock}; use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::WebSocketStream; use typst::diag::{FileError, FileResult, SourceError, StrResult}; -use typst::eval::Library; +use typst::eval::{Library, Datetime}; use typst::font::{Font, FontBook, FontInfo, FontVariant}; use typst::geom::RgbaColor; use typst::syntax::{Source, SourceId}; @@ -577,6 +578,7 @@ struct SystemWorld { hashes: RefCell>>, paths: RefCell>, sources: FrozenVec>, + today: Cell>, main: SourceId, } @@ -614,6 +616,7 @@ impl SystemWorld { hashes: RefCell::default(), paths: RefCell::default(), sources: FrozenVec::new(), + today: Cell::new(None), main: SourceId::detached(), } } @@ -644,7 +647,7 @@ impl World for SystemWorld { } fn source(&self, id: SourceId) -> &Source { - &self.sources[id.into_u16() as usize] + &self.sources[id.as_u16() as usize] } fn book(&self) -> &Prehashed { @@ -667,6 +670,23 @@ impl World for SystemWorld { .get_or_init(|| read(path).map(Buffer::from)) .clone() } + + fn today(&self, offset: Option) -> Option { + if self.today.get().is_none() { + let datetime = match offset { + None => chrono::Local::now().naive_local(), + Some(o) => (chrono::Utc::now() + chrono::Duration::hours(o)).naive_utc(), + }; + + self.today.set(Some(Datetime::from_ymd( + datetime.year(), + datetime.month().try_into().ok()?, + datetime.day().try_into().ok()?, + )?)) + } + + self.today.get() + } } impl SystemWorld {