From 4f2fdfddd68b2da56b3cb6b1549c23eb82b6bd7e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 15 May 2022 11:35:05 +0100 Subject: [PATCH] subst: Implement format 2 and convert all libraries Signed-off-by: Ian Jackson --- library/cards-oxymoron.toml | 16 ++++++------- library/edited.toml | 6 ++--- library/wikimedia.toml | 34 ++++++++++++++-------------- src/shapelib.rs | 45 ++++++++++++++++++++++++++++++------- 4 files changed, 65 insertions(+), 36 deletions(-) diff --git a/library/cards-oxymoron.toml b/library/cards-oxymoron.toml index fafcd419..68d1179a 100644 --- a/library/cards-oxymoron.toml +++ b/library/cards-oxymoron.toml @@ -16,8 +16,8 @@ outline = "Rect" size = [18.25, 24.25] item_suffix = "-c" -sort = "card-playing-c_s" -desc_template = "the _desc of clubs" +sort = "card-playing-c${s}" +desc_template = "the ${desc} of clubs" occulted.method = "ByBack" occulted.ilk = "card-back" @@ -46,17 +46,17 @@ target = "card-back" [group.diamonds] inherit = "clubs" item_suffix = "-d" -sort = "card-playing-d_s" -desc_template = "the _desc of diamonds" +sort = "card-playing-d${s}" +desc_template = "the ${desc} of diamonds" [group.hearts] inherit = "clubs" -sort = "card-playing-h_s" +sort = "card-playing-h${s}" item_suffix = "-h" -desc_template = "the _desc of hearts" +desc_template = "the ${desc} of hearts" [group.spades] inherit = "clubs" -sort = "card-playing-s_s" +sort = "card-playing-s${s}" item_suffix = "-s" -desc_template = "the _desc of spades" +desc_template = "the ${desc} of spades" diff --git a/library/edited.toml b/library/edited.toml index 68035202..cfd6eefb 100644 --- a/library/edited.toml +++ b/library/edited.toml @@ -31,7 +31,7 @@ outline.shape = "Circle" outline.size = [14] item_prefix = "die-image-" files = """ -d6-_c - a _colour die image +d6-_c - a ${colour} die image """ magic.item_prefix = "die-" @@ -39,9 +39,9 @@ magic.template = """ image = ! type = "Die" labels = 6 -desc = "a _colour die" +desc = "a ${colour} die" occult.label = "?" -occult.ilk = "d6-_colour" +occult.ilk = "d6-${colour}" """ [group.dice.colours.blue] diff --git a/library/wikimedia.toml b/library/wikimedia.toml index 1f99ed21..43c4288b 100644 --- a/library/wikimedia.toml +++ b/library/wikimedia.toml @@ -25,14 +25,14 @@ occulted.method = "ByColour" occulted.colour = "grey" files = """ -_c-P plt45 a _colour pawn -_c-B blt45 a _colour bishop -_c-R rlt45 a _colour rook -_c-K klt45 the _colour king -_c-Q qlt45 the _colour queen - -_c-elephant elt45 a _colour elephant piece -_c-mann Mlt45 a _colour Mann piece +_c-P plt45 a ${colour} pawn +_c-B blt45 a ${colour} bishop +_c-R rlt45 a ${colour} rook +_c-K klt45 the ${colour} king +_c-Q qlt45 the ${colour} queen + +_c-elephant elt45 a ${colour} elephant piece +_c-mann Mlt45 a ${colour} Mann piece """ [group.chess.colours.white] @@ -66,41 +66,41 @@ map = { "#ffffff" = "#888888" } inherit = "chess" scale = 0.3461 files = """ -_c-siege-engine tlt26 a _colour siege engine piece +_c-siege-engine tlt26 a ${colour} siege engine piece """ [group.chess-flip] inherit = "chess" flip = true files = """ -_c-N alt45 a _colour knight +_c-N alt45 a ${colour} knight -_c-giraffe Glt45 a _colour giraffe piece -_c-knight-king Cet45 a _colour knight king piece +_c-giraffe Glt45 a ${colour} giraffe piece +_c-knight-king Cet45 a ${colour} knight king piece # ^ wikimedia calls these Centaurs -_c-ship ship_slt45 a _colour ship piece -_c-zebra Zlt45 a _colour zebra piece +_c-ship ship_slt45 a ${colour} ship piece +_c-zebra Zlt45 a ${colour} zebra piece """ [group.chess-flip-26] inherit = "chess-26" flip = true files = """ -_c-flag Blt26 a _colour flag piece +_c-flag Blt26 a ${colour} flag piece """ [group.chess-flip-26-ship-adj] inherit = "chess-flip-26" flip = true files = """ -_c-cannon Clt26 a _colour cannon piece +_c-cannon Clt26 a ${colour} cannon piece """ [group.chess-anomalous-filenames] inherit = "chess" stem_prefix = "" files = """ -_c-commoner Commoner_Transparent a _colour commoner piece +_c-commoner Commoner_Transparent a ${colour} commoner piece """ [group.chess-onecolour] diff --git a/src/shapelib.rs b/src/shapelib.rs index 3d3ce646..bd608f7a 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -1032,17 +1032,22 @@ pub fn load_catalogue(libname: &str, src: &mut dyn LibrarySource) pub struct Substituting<'s> { s: Cow<'s, str>, mformat: materials_format::Version, + dollars: bool, } impl<'s> Substituting<'s> { pub fn new>>(mformat: materials_format::Version, s: S) -> Self { - Substituting { s: s.into(), mformat } + Substituting { s: s.into(), mformat, dollars: false } } #[throws(SubstError)] pub fn finish(self) -> String { - self.s.into() + if self.dollars { + subst_general_precisely(&self, "${$}", true, "$")?.0 + } else { + self + }.s.into() } #[throws(SubstError)] @@ -1056,9 +1061,10 @@ impl<'s> Substituting<'s> { } #[throws(SubstError)] -fn subst_general_mf1<'i>(input: &Substituting<'i>, - needle: &'static str, replacement: &str) - -> (Substituting<'i>, usize) { +fn subst_general_precisely<'i>(input: &Substituting<'i>, + needle: & str, dollars: bool, + replacement: &str) + -> (Substituting<'i>, usize) { let mut count = 0; let mut work = (*input.s).to_owned(); for m in input.s.rmatch_indices(needle) { @@ -1078,7 +1084,26 @@ fn subst_general_mf1<'i>(input: &Substituting<'i>, + replacement + rhs } - (Substituting{ s: work.into(), mformat: input.mformat }, count) + (Substituting{ + s: work.into(), + mformat: input.mformat, + dollars: input.dollars | dollars, + }, count) +} + +#[throws(SubstError)] +fn subst_general_mf2<'i>(input: &Substituting<'i>, + needle: &'static str, replacement: &str) + -> (Substituting<'i>, usize) { + let needle_buf; + let (needle, dollars) = if needle != "_c" { + let token = needle.strip_prefix('_').expect("needle has no '_'"); + needle_buf = format!("${{{}}}", token); + (&*needle_buf, true) + } else { + (needle, false) + }; + subst_general_precisely(input, needle, dollars, replacement)? } #[throws(SubstError)] @@ -1088,8 +1113,12 @@ fn subst_general_mf1<'i>(input: &Substituting<'i>, // Substituting and do them all at once. fn subst_general<'i>(input: &Substituting<'i>, needle: &'static str, replacement: &str) - -> (Substituting<'i>, usize) { - subst_general_mf1(input, needle, replacement)? + -> (Substituting<'i>, usize) { + if input.mformat == 1 { + subst_general_precisely(input, needle, false, replacement)? + } else { + subst_general_mf2(input, needle, replacement)? + } } #[throws(SubstError)] -- 2.30.2