summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYip Coekjan <69834864+Coekjan@users.noreply.github.com>2024-05-25 07:14:56 +0800
committerGitHub <noreply@github.com>2024-05-24 23:14:56 +0000
commit374b82f8cad0d36d36c549a0295b8d14b093c3f9 (patch)
tree247ebf196c0883936266e246a2a4fa932c9884ce
parentea4c64a7997556871934e20be7415cba8ec275a5 (diff)
Set default value of `raw.theme` to `auto`, and allow setting `raw.theme` to `auto` (#4186)
-rw-r--r--crates/typst/src/text/raw.rs19
-rw-r--r--tests/ref/raw-theme-set-to-auto.pngbin0 -> 3221 bytes
-rw-r--r--tests/suite/text/raw.typ15
3 files changed, 28 insertions, 6 deletions
diff --git a/crates/typst/src/text/raw.rs b/crates/typst/src/text/raw.rs
index 1aa69fbf..0493c080 100644
--- a/crates/typst/src/text/raw.rs
+++ b/crates/typst/src/text/raw.rs
@@ -233,10 +233,10 @@ pub struct RawElem {
/// ````
#[parse(
let (theme_path, theme_data) = parse_theme(engine, args)?;
- theme_path.map(Some)
+ theme_path
)]
#[borrowed]
- pub theme: Option<EcoString>,
+ pub theme: Smart<EcoString>,
/// The raw file buffer of syntax theme file.
#[internal]
@@ -321,7 +321,7 @@ impl Packed<RawElem> {
.unwrap()
});
- let theme = theme.as_deref().unwrap_or(&RAW_THEME);
+ let theme = theme.as_ref().map(std::ops::Deref::deref).unwrap_or(&RAW_THEME);
let foreground = theme.settings.foreground.unwrap_or(synt::Color::BLACK);
let mut seq = vec![];
@@ -784,12 +784,19 @@ fn load_theme(path: &str, bytes: &Bytes) -> StrResult<Arc<synt::Theme>> {
fn parse_theme(
engine: &mut Engine,
args: &mut Args,
-) -> SourceResult<(Option<EcoString>, Option<Bytes>)> {
- let Some(Spanned { v: path, span }) = args.named::<Spanned<EcoString>>("theme")?
+) -> SourceResult<(Option<Smart<EcoString>>, Option<Bytes>)> {
+ let Some(Spanned { v: path, span }) =
+ args.named::<Spanned<Smart<EcoString>>>("theme")?
else {
+ // Argument `theme` not found.
return Ok((None, None));
};
+ let Smart::Custom(path) = path else {
+ // Argument `theme` is `auto`.
+ return Ok((Some(Smart::Auto), None));
+ };
+
// Load theme file.
let id = span.resolve_path(&path).at(span)?;
let data = engine.world.file(id).at(span)?;
@@ -797,7 +804,7 @@ fn parse_theme(
// Check that parsing works.
let _ = load_theme(&path, &data).at(span)?;
- Ok((Some(path), Some(data)))
+ Ok((Some(Smart::Custom(path)), Some(data)))
}
/// The syntect syntax definitions.
diff --git a/tests/ref/raw-theme-set-to-auto.png b/tests/ref/raw-theme-set-to-auto.png
new file mode 100644
index 00000000..3d2c8857
--- /dev/null
+++ b/tests/ref/raw-theme-set-to-auto.png
Binary files differ
diff --git a/tests/suite/text/raw.typ b/tests/suite/text/raw.typ
index 6a3ea6bd..8ced276c 100644
--- a/tests/suite/text/raw.typ
+++ b/tests/suite/text/raw.typ
@@ -633,6 +633,21 @@ fn main() {
`code`
```
+--- raw-theme-set-to-auto ---
+```typ
+#let hi = "Hello World"
+```
+
+#set raw(theme: "/assets/themes/halcyon.tmTheme")
+```typ
+#let hi = "Hello World"
+```
+
+#set raw(theme: auto)
+```typ
+#let hi = "Hello World"
+```
+
--- raw-unclosed ---
// Test unterminated raw text.
//