Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart Quotation Marks in Formattable Strings. #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/file-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,20 @@ publisher:
Title and sentence case folding will always be deactivated if your item has set
the `language` key to something other than English.

Double quotation marks in a formattable string will be we transformed to
fitting opening and closing quotation marks according to the chosen locale.
Quotes will always be appropriately nested:

```yaml
title: Reflections on "Harlem" and Other Poems
```

If you use quotation marks as YAML delimiters, simply escape the quotation marks in the title:

```yaml
title: "Reflections on \"Harlem\" and Other Poems"
```

You can also include mathematical markup evaluated by [Typst](https://typst.app) by
wrapping it in dollars.

Expand Down
5 changes: 5 additions & 0 deletions src/csl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,11 @@ impl<'a, T: EntryLike> Context<'a, T> {
self.writing.buf.push_verbatim(&chunk.value);
self.writing.pull_punctuation = false;
}
ChunkKind::Quote => {
self.push_quotes();
self.push_str(&chunk.value);
self.pop_quotes();
}
ChunkKind::Math => {
self.writing.buf.prevent_trimming();
self.writing.save_to_block();
Expand Down
18 changes: 16 additions & 2 deletions src/types/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl ChunkedString {
let config = c.case();
for chunk in &self.0 {
match chunk.kind {
ChunkKind::Normal => c.reconfigure(config),
ChunkKind::Normal | ChunkKind::Quote => c.reconfigure(config),
ChunkKind::Verbatim | ChunkKind::Math => c.reconfigure(Case::NoTransform),
};

Expand Down Expand Up @@ -416,6 +416,13 @@ impl FromStr for ChunkedString {
'$' => {
kind = ChunkKind::Math;
}
'"' if kind == ChunkKind::Quote => {
kind =
if depth > 0 { ChunkKind::Verbatim } else { ChunkKind::Normal };
}
'"' => {
kind = ChunkKind::Quote;
}
_ => chunks.push_char(c, kind),
}
}
Expand Down Expand Up @@ -573,6 +580,11 @@ impl StringChunk {
write_escaped(self, buf)?;
buf.write_char('$')?;
}
ChunkKind::Quote => {
buf.write_char('"')?;
write_escaped(self, buf)?;
buf.write_char('"')?;
}
}

Ok(())
Expand All @@ -590,6 +602,8 @@ pub enum ChunkKind {
/// The contained markup is expected to be evaluated using
/// [Typst](https://typst.app/).
Math,
/// Quotation marks to be formatted according to the locale.
Quote,
}

/// The kind of a string chunk for use with the case folder.
Expand All @@ -609,7 +623,7 @@ impl TryFrom<ChunkKind> for FoldableKind {
match value {
ChunkKind::Normal => Ok(Self::Normal),
ChunkKind::Verbatim => Ok(Self::Verbatim),
ChunkKind::Math => Err(()),
ChunkKind::Math | ChunkKind::Quote => Err(()),
}
}
}
Expand Down