tikz-gallery-generator

Custum build of stapix for tikz.pablopie.xyz

Commit
a46cb25171de5d9b555da10f203f49a56b2a2fbb
Parent
64301793f75db43077a0cd9ee9b23683a103acc0
Author
Pablo <pablo-escobar@riseup.net>
Date

Added support for CC-2.1 and CC-2.5 licenses

Also had to implement a wrapper for printing the URL of licenses to avoid the combinatorial explosion of case analysis

Diffstat

4 files changed, 120 insertions, 169 deletions

Status File Name N° Changes Insertions Deletions
Modified Cargo.lock 2 1 1
Modified README.md 15 9 6
Modified examples/config.yml 3 1 2
Modified src/gallery_entry.rs 269 109 160
diff --git a/Cargo.lock b/Cargo.lock
@@ -615,7 +615,7 @@ dependencies = [
 
 [[package]]
 name = "tikz_gallery_generator"
-version = "0.2.2"
+version = "0.1.0"
 dependencies = [
  "crossterm",
  "image",
diff --git a/README.md b/README.md
@@ -55,13 +55,16 @@ Each entry in the list should contain the following fields:
 * **`caption` (optional):** A description of the picture
 * **`author`:** The name of the author of the picture
 * **`author-url` (optional):** A URL to a webpage by/on the picture's author
-* **`license`:** The license type of the picture. Should be one of `PD` (public
-  domain), `CC0`, `CC-BY-1`, `CC-BY-2`, `CC-BY-3`, `CC-BY-4`, `CC-BY-SA-1`,
-  `CC-BY-SA-2`, `CC-BY-SA-3`, `CC-BY-SA-4`, `CC-BY-NC-1`, `CC-BY-NC-2`,
+* **`license`:** The license type of the picture. Should be one of
+  `proprietary` (some proprietary license), `PD` (public domain), `CC0`,
+  `CC-BY-1`, `CC-BY-2`, `CC-BY-2.1`, `CC-BY-2.5`, `CC-BY-3`, `CC-BY-4`,
+  `CC-BY-SA-1`, `CC-BY-SA-2`, `CC-BY-SA-2.1`, `CC-BY-SA-2.5`, `CC-BY-SA-3`,
+  `CC-BY-SA-4`, `CC-BY-NC-1`, `CC-BY-NC-2`, `CC-BY-NC-2.1`, `CC-BY-NC-2.5`,
   `CC-BY-NC-3`, `CC-BY-NC-4`, `CC-BY-NC-SA-1`, `CC-BY-NC-SA-2`,
-  `CC-BY-NC-SA-3`, `CC-BY-NC-SA-4`, `CC-BY-ND-1`, `CC-BY-ND-2`, `CC-BY-ND-3`,
-  `CC-BY-ND-4`, `CC-BY-NC-ND-1`, `CC-BY-NC-ND-2`, `CC-BY-NC-ND-3` or
-  `CC-BY-NC-ND-4`
+  `CC-BY-NC-SA-2.1`, `CC-BY-NC-SA-2.5`, `CC-BY-NC-SA-3`, `CC-BY-NC-SA-4`,
+  `CC-BY-ND-1`, `CC-BY-ND-2`, `CC-BY-ND-2.1`, `CC-BY-ND-2.5`, `CC-BY-ND-3`,
+  `CC-BY-ND-4`, `CC-BY-NC-ND-1`, `CC-BY-NC-ND-2`, `CC-BY-NC-ND-2.1`,
+  `CC-BY-NC-ND-2.5`, `CC-BY-NC-ND-3` or `CC-BY-NC-ND-4`
 
 For best accessibility, the `alt` field should contain a concise visual
 description of the picture in question (including subjects, colors and scenery)
diff --git a/examples/config.yml b/examples/config.yml
@@ -6,8 +6,7 @@
   alt: The 26-nodes diagram
 
 - path: ./images/caleb-yau.png
-  # TODO: This is actually license under CC BY SA 2.5 for some reason
-  license: CC-BY-SA-2
+  license: CC-BY-SA-2.5
   author: Lunch
   source: https://commons.wikimedia.org/wiki/File:Calabi-Yau-alternate.png
   alt: A visual representation of the Calebi-Yau manifold
diff --git a/src/gallery_entry.rs b/src/gallery_entry.rs
@@ -10,26 +10,38 @@ const LICENSES: &[&str] = &[
     "CC0",
     "CC-BY-1",
     "CC-BY-2",
+    "CC-BY-2.1",
+    "CC-BY-2.5",
     "CC-BY-3",
     "CC-BY-4",
     "CC-BY-SA-1",
     "CC-BY-SA-2",
+    "CC-BY-SA-2.1",
+    "CC-BY-SA-2.5",
     "CC-BY-SA-3",
     "CC-BY-SA-4",
     "CC-BY-NC-1",
     "CC-BY-NC-2",
+    "CC-BY-NC-2.1",
+    "CC-BY-NC-2.5",
     "CC-BY-NC-3",
     "CC-BY-NC-4",
     "CC-BY-NC-SA-1",
     "CC-BY-NC-SA-2",
+    "CC-BY-NC-SA-2.1",
+    "CC-BY-NC-SA-2.5",
     "CC-BY-NC-SA-3",
     "CC-BY-NC-SA-4",
     "CC-BY-ND-1",
     "CC-BY-ND-2",
+    "CC-BY-ND-2.1",
+    "CC-BY-ND-2.5",
     "CC-BY-ND-3",
     "CC-BY-ND-4",
     "CC-BY-NC-ND-1",
     "CC-BY-NC-ND-2",
+    "CC-BY-NC-ND-2.1",
+    "CC-BY-NC-ND-2.5",
     "CC-BY-NC-ND-3",
     "CC-BY-NC-ND-4",
 ];
@@ -68,6 +80,10 @@ pub enum LicenseType {
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
 pub struct CreativeCommons(CcInternal);
 
+#[derive(Debug, Clone, Copy)]
+/// Wrapper for printing the url of a Creative Commons license
+pub struct LicenseUrl(CcInternal);
+
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
 enum CcInternal {
     /// Creative Commons (without attribution)
@@ -90,154 +106,15 @@ enum CcInternal {
 enum CcVersion {
     One,
     Two,
+    TwoOne,
+    TwoFive,
     Three,
     Four,
 }
 
 impl CreativeCommons {
-    pub const fn url(&self) -> &'static str {
-        match self.0 {
-            // CC0
-            CcInternal::Cc0 => {
-                "https://creativecommons.org/publicdomain/zero/1.0/"
-            },
-            // CC-BY-1
-            CcInternal::CcBy {
-                version: CcVersion::One,
-                non_commercial: false,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by/1.0/",
-            // CC-BY-2
-            CcInternal::CcBy {
-                version: CcVersion::Two,
-                non_commercial: false,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by/2.0/",
-            // CC-BY-3
-            CcInternal::CcBy {
-                version: CcVersion::Three,
-                non_commercial: false,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by/3.0/",
-            // CC-BY-4
-            CcInternal::CcBy {
-                version: CcVersion::Four,
-                non_commercial: false,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by/4.0/",
-            // CC-BY-SA-1
-            CcInternal::CcBy {
-                version: CcVersion::One,
-                non_commercial: false,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-sa/1.0/",
-            // CC-BY-SA-2
-            CcInternal::CcBy {
-                version: CcVersion::Two,
-                non_commercial: false,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-sa/2.0/",
-            // CC-BY-SA-3
-            CcInternal::CcBy {
-                version: CcVersion::Three,
-                non_commercial: false,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-sa/3.0/",
-            // CC-BY-SA-4
-            CcInternal::CcBy {
-                version: CcVersion::Four,
-                non_commercial: false,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-sa/4.0/",
-            // CC-BY-NC-1
-            CcInternal::CcBy {
-                version: CcVersion::One,
-                non_commercial: true,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by-nc/1.0/",
-            // CC-BY-NC-2
-            CcInternal::CcBy {
-                version: CcVersion::Two,
-                non_commercial: true,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by-nc/2.0/",
-            // CC-BY-NC-3
-            CcInternal::CcBy {
-                version: CcVersion::Three,
-                non_commercial: true,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by-nc/3.0/",
-            // CC-BY-NC-4
-            CcInternal::CcBy {
-                version: CcVersion::Four,
-                non_commercial: true,
-                share_alike: false,
-            } => "http://creativecommons.org/licenses/by-nc/4.0/",
-            // CC-BY-NC-SA-1
-            CcInternal::CcBy {
-                version: CcVersion::One,
-                non_commercial: true,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-nc-sa/1.0/",
-            // CC-BY-NC-SA-2
-            CcInternal::CcBy {
-                version: CcVersion::Two,
-                non_commercial: true,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-nc-sa/2.0/",
-            // CC-BY-NC-SA-3
-            CcInternal::CcBy {
-                version: CcVersion::Three,
-                non_commercial: true,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-nc-sa/3.0/",
-            // CC-BY-NC-SA-4
-            CcInternal::CcBy {
-                version: CcVersion::Four,
-                non_commercial: true,
-                share_alike: true,
-            } => "http://creativecommons.org/licenses/by-nc-sa/4.0/",
-            // CC-BY-ND-1
-            CcInternal::CcByNd {
-                version: CcVersion::One,
-                non_commercial: false,
-            } => "http://creativecommons.org/licenses/by-nd/1.0/",
-            // CC-BY-ND-2
-            CcInternal::CcByNd {
-                version: CcVersion::Two,
-                non_commercial: false,
-            } => "http://creativecommons.org/licenses/by-nd/2.0/",
-            // CC-BY-ND-3
-            CcInternal::CcByNd {
-                version: CcVersion::Three,
-                non_commercial: false,
-            } => "http://creativecommons.org/licenses/by-nd/3.0/",
-            // CC-BY-ND-4
-            CcInternal::CcByNd {
-                version: CcVersion::Four,
-                non_commercial: false,
-            } => "http://creativecommons.org/licenses/by-nd/4.0/",
-            // CC-BY-NC-ND-1
-            CcInternal::CcByNd {
-                version: CcVersion::One,
-                non_commercial: true,
-            } => "http://creativecommons.org/licenses/by-nc-nd/1.0/",
-            // CC-BY-NC-ND-2
-            CcInternal::CcByNd {
-                version: CcVersion::Two,
-                non_commercial: true,
-            } => "http://creativecommons.org/licenses/by-nc-nd/2.0/",
-            // CC-BY-NC-ND-3
-            CcInternal::CcByNd {
-                version: CcVersion::Three,
-                non_commercial: true,
-            } => "http://creativecommons.org/licenses/by-nc-nd/3.0/",
-            // CC-BY-NC-ND-4
-            CcInternal::CcByNd {
-                version: CcVersion::Four,
-                non_commercial: true,
-            } => "http://creativecommons.org/licenses/by-nc-nd/4.0/",
-        }
+    pub const fn url(&self) -> LicenseUrl {
+        LicenseUrl(self.0)
     }
 }
 
@@ -258,52 +135,56 @@ impl LicenseType {
         assert!(s.len() >= 3,
                 "if s is in LICENSES it should contain at least 3 chars");
 
-        let version = match &s[s.len() - 1..] {
-            "1" => CcVersion::One,
-            "2" => CcVersion::Two,
-            "3" => CcVersion::Three,
-            "4" => CcVersion::Four,
-            _   => {
+        let (license, version) = s.rsplit_once("-").ok_or(())?;
+
+        let version = match version {
+            "1"   => CcVersion::One,
+            "2"   => CcVersion::Two,
+            "2.1" => CcVersion::TwoOne,
+            "2.5" => CcVersion::TwoFive,
+            "3"   => CcVersion::Three,
+            "4"   => CcVersion::Four,
+            _     => {
                 unreachable!("if s is in LICENSES we should be able to parse the license version")
             },
         };
 
-        match &s[..s.len() - 1] {
-            "CC-BY-" => {
+        match license {
+            "CC-BY" => {
                 Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
                     version,
                     non_commercial: false,
                     share_alike: false,
                 })))
             },
-            "CC-BY-NC-" => {
+            "CC-BY-NC" => {
                 Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
                     version,
                     non_commercial: true,
                     share_alike: false,
                 })))
             },
-            "CC-BY-SA-" => {
+            "CC-BY-SA" => {
                 Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
                     version,
                     non_commercial: false,
                     share_alike: true,
                 })))
             },
-            "CC-BY-NC-SA-" => {
+            "CC-BY-NC-SA" => {
                 Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
                     version,
                     non_commercial: true,
                     share_alike: true,
                 })))
             },
-            "CC-BY-ND-" => {
+            "CC-BY-ND" => {
                 Ok(Self::Cc(CreativeCommons(CcInternal::CcByNd {
                     version,
                     non_commercial: false,
                 })))
             },
-            "CC-BY-NC-ND-" => {
+            "CC-BY-NC-ND" => {
                 Ok(Self::Cc(CreativeCommons(CcInternal::CcByNd {
                     version,
                     non_commercial: true,
@@ -412,10 +293,78 @@ impl Display for CreativeCommons {
 impl Display for CcVersion {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
         match self {
-            CcVersion::One   => write!(f, "1.0"),
-            CcVersion::Two   => write!(f, "2.0"),
-            CcVersion::Three => write!(f, "3.0"),
-            CcVersion::Four  => write!(f, "4.0"),
+            CcVersion::One     => write!(f, "1.0"),
+            CcVersion::Two     => write!(f, "2.0"),
+            CcVersion::TwoOne  => write!(f, "2.1"),
+            CcVersion::TwoFive => write!(f, "2.5"),
+            CcVersion::Three   => write!(f, "3.0"),
+            CcVersion::Four    => write!(f, "4.0"),
+        }
+    }
+}
+
+impl Display for LicenseUrl {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
+        match self.0 {
+            // CC0
+            CcInternal::Cc0 => {
+                write!(f, "https://creativecommons.org/publicdomain/zero/1.0/")
+            },
+            // CC-BY-x
+            CcInternal::CcBy {
+                version,
+                non_commercial: false,
+                share_alike: false,
+            } => {
+                write!(f, "http://creativecommons.org/licenses/by/{version}/")
+            }
+            // CC-BY-SA-x
+            CcInternal::CcBy {
+                version,
+                non_commercial: false,
+                share_alike: true,
+            } => {
+                write!(
+                    f,
+                    "http://creativecommons.org/licenses/by-sa/{version}/"
+                )
+            }
+            // CC-BY-NC-x
+            CcInternal::CcBy {
+                version,
+                non_commercial: true,
+                share_alike: false,
+            } => {
+                write!(
+                    f,
+                    "http://creativecommons.org/licenses/by-nc/{version}/"
+                )
+            }
+            // CC-BY-NC-SA-x
+            CcInternal::CcBy {
+                version,
+                non_commercial: true,
+                share_alike: true,
+            } => {
+                write!(
+                    f,
+                    "http://creativecommons.org/licenses/by-nc-sa/{version}/"
+                )
+            }
+            // CC-BY-ND-x
+            CcInternal::CcByNd { version, non_commercial: false, } => {
+                write!(
+                    f,
+                    "http://creativecommons.org/licenses/by-nd/{version}/"
+                )
+            }
+            // CC-BY-NC-ND-x
+            CcInternal::CcByNd { version, non_commercial: true, } => {
+                write!(
+                    f,
+                    "http://creativecommons.org/licenses/by-nc-nd/{version}/"
+                )
+            }
         }
     }
 }