chiark / gitweb /
subst: Implement format 2 and convert all libraries
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 10:35:05 +0000 (11:35 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 14:19:55 +0000 (15:19 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
library/cards-oxymoron.toml
library/edited.toml
library/wikimedia.toml
src/shapelib.rs

index fafcd4196bb3c1dc9e06737c89fbad1141db7106..68d1179af6df46dae102122767974016f60fd194 100644 (file)
@@ -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"
index 68035202b84d257f001af5fd7b0c2fe1b2778208..cfd6eefb29ef32df8e0cf91a90cc76cc76ab16c9 100644 (file)
@@ -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]
index 1f99ed21c07c3cff217960b8e13b1a7a40e11000..43c4288ba3f323e7c39c7d02c4585f4f13348ee6 100644 (file)
@@ -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]
index 3d3ce64642351ab91a0a5a170559b7fc5eb4ab48..bd608f7a2ed23a86da039ef3d5a5abaec2fcb76c 100644 (file)
@@ -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<S: Into<Cow<'s, str>>>(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)]