tikz-gallery-generator

Custum build of stapix for tikz.pablopie.xyz

Commit
222aa8a1861087fc996a0ad53b5ccb79abeaa89c
Parent
9845df6caeeee15038a3084fa849cfeee2ec845a
Author
Pablo <pablo-escobar@riseup.net>
Date

Added an option for figures distributed under proprietary licenses

Had to remodel the LicenseType type

Also renamed the old CreativeCommonsVersion type

Diffstat

3 files changed, 130 insertions, 145 deletions

Status File Name N° Changes Insertions Deletions
Modified examples/config.yml 3 1 2
Modified src/gallery_entry.rs 250 113 137
Modified src/main.rs 22 16 6
diff --git a/examples/config.yml b/examples/config.yml
@@ -107,8 +107,7 @@
   alt: The Poincaré disc model of the hyperbolic plane
 
 - path: ./images/j-function.svg
-  # TODO: This is actually proprietary :)
-  license: PD
+  license: proprietary
   author: Eugene Jahnke & Fritz Emde
   source: https://archive.org/details/tablesoffunction00jahn/mode/2up
   alt: Relief representation of the j-invariant elliptic modular
diff --git a/src/gallery_entry.rs b/src/gallery_entry.rs
@@ -5,6 +5,7 @@ use serde::{
 use std::{fmt::{self, Display}, path::PathBuf};
 
 const LICENSES: &[&str] = &[
+    "proprietary",
     "PD",
     "CC0",
     "CC-BY-1",
@@ -43,7 +44,7 @@ pub struct GalleryEntry {
     pub file_format: FileFormat,
     pub alt: String,
     pub caption: Option<String>,
-    pub license: Option<LicenseType>,
+    pub license: LicenseType,
     pub source: Option<String>,
     pub author: String,
     pub author_url: Option<String>,
@@ -58,199 +59,210 @@ pub enum FileFormat {
 }
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub struct LicenseType(CreativeCommons);
+pub enum LicenseType {
+    PublicDomain,
+    Cc(CreativeCommons),
+    Proprietary,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct CreativeCommons(CcInternal);
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
-enum CreativeCommons {
+enum CcInternal {
     /// Creative Commons (without attribution)
     Cc0,
     /// Creative Commons Attributions (derivatives allowed)
     CcBy {
-        version: CreativeCommonsVersion,
+        version: CcVersion,
         non_commercial: bool,
         share_alike: bool,
     },
     // The ND (non-derivatives) option excludes the SA (share alike) option
     /// Creative Commons Attributions Non-Derivatives
     CcByNd {
-        version: CreativeCommonsVersion,
+        version: CcVersion,
         non_commercial: bool,
     },
 }
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
-enum CreativeCommonsVersion {
+enum CcVersion {
     One,
     Two,
     Three,
     Four,
 }
 
-impl LicenseType {
+impl CreativeCommons {
     pub const fn url(&self) -> &'static str {
         match self.0 {
             // CC0
-            CreativeCommons::Cc0 => {
+            CcInternal::Cc0 => {
                 "https://creativecommons.org/publicdomain/zero/1.0/"
             },
             // CC-BY-1
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::One,
+            CcInternal::CcBy {
+                version: CcVersion::One,
                 non_commercial: false,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by/1.0/",
             // CC-BY-2
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Two,
+            CcInternal::CcBy {
+                version: CcVersion::Two,
                 non_commercial: false,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by/2.0/",
             // CC-BY-3
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Three,
+            CcInternal::CcBy {
+                version: CcVersion::Three,
                 non_commercial: false,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by/3.0/",
             // CC-BY-4
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Four,
+            CcInternal::CcBy {
+                version: CcVersion::Four,
                 non_commercial: false,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by/4.0/",
             // CC-BY-SA-1
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::One,
+            CcInternal::CcBy {
+                version: CcVersion::One,
                 non_commercial: false,
                 share_alike: true,
             } => "http://creativecommons.org/licenses/by-sa/1.0/",
             // CC-BY-SA-2
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Two,
+            CcInternal::CcBy {
+                version: CcVersion::Two,
                 non_commercial: false,
                 share_alike: true,
             } => "http://creativecommons.org/licenses/by-sa/2.0/",
             // CC-BY-SA-3
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Three,
+            CcInternal::CcBy {
+                version: CcVersion::Three,
                 non_commercial: false,
                 share_alike: true,
             } => "http://creativecommons.org/licenses/by-sa/3.0/",
             // CC-BY-SA-4
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Four,
+            CcInternal::CcBy {
+                version: CcVersion::Four,
                 non_commercial: false,
                 share_alike: true,
             } => "http://creativecommons.org/licenses/by-sa/4.0/",
             // CC-BY-NC-1
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::One,
+            CcInternal::CcBy {
+                version: CcVersion::One,
                 non_commercial: true,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by-nc/1.0/",
             // CC-BY-NC-2
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Two,
+            CcInternal::CcBy {
+                version: CcVersion::Two,
                 non_commercial: true,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by-nc/2.0/",
             // CC-BY-NC-3
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Three,
+            CcInternal::CcBy {
+                version: CcVersion::Three,
                 non_commercial: true,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by-nc/3.0/",
             // CC-BY-NC-4
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Four,
+            CcInternal::CcBy {
+                version: CcVersion::Four,
                 non_commercial: true,
                 share_alike: false,
             } => "http://creativecommons.org/licenses/by-nc/4.0/",
             // CC-BY-NC-SA-1
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::One,
+            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
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Two,
+            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
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Three,
+            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
-            CreativeCommons::CcBy {
-                version: CreativeCommonsVersion::Four,
+            CcInternal::CcBy {
+                version: CcVersion::Four,
                 non_commercial: true,
                 share_alike: true,
             } => "http://creativecommons.org/licenses/by-nc-sa/4.0/",
             // CC-BY-ND-1
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::One,
+            CcInternal::CcByNd {
+                version: CcVersion::One,
                 non_commercial: false,
             } => "http://creativecommons.org/licenses/by-nd/1.0/",
             // CC-BY-ND-2
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::Two,
+            CcInternal::CcByNd {
+                version: CcVersion::Two,
                 non_commercial: false,
             } => "http://creativecommons.org/licenses/by-nd/2.0/",
             // CC-BY-ND-3
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::Three,
+            CcInternal::CcByNd {
+                version: CcVersion::Three,
                 non_commercial: false,
             } => "http://creativecommons.org/licenses/by-nd/3.0/",
             // CC-BY-ND-4
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::Four,
+            CcInternal::CcByNd {
+                version: CcVersion::Four,
                 non_commercial: false,
             } => "http://creativecommons.org/licenses/by-nd/4.0/",
             // CC-BY-NC-ND-1
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::One,
+            CcInternal::CcByNd {
+                version: CcVersion::One,
                 non_commercial: true,
             } => "http://creativecommons.org/licenses/by-nc-nd/1.0/",
             // CC-BY-NC-ND-2
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::Two,
+            CcInternal::CcByNd {
+                version: CcVersion::Two,
                 non_commercial: true,
             } => "http://creativecommons.org/licenses/by-nc-nd/2.0/",
             // CC-BY-NC-ND-3
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::Three,
+            CcInternal::CcByNd {
+                version: CcVersion::Three,
                 non_commercial: true,
             } => "http://creativecommons.org/licenses/by-nc-nd/3.0/",
             // CC-BY-NC-ND-4
-            CreativeCommons::CcByNd {
-                version: CreativeCommonsVersion::Four,
+            CcInternal::CcByNd {
+                version: CcVersion::Four,
                 non_commercial: true,
             } => "http://creativecommons.org/licenses/by-nc-nd/4.0/",
         }
     }
+}
 
-    pub fn parse(s: &str) -> Result<Option<Self>, ()> {
+impl LicenseType {
+    pub fn parse(s: &str) -> Result<Self, ()> {
         if !LICENSES.contains(&s) {
             return Err(());
         }
 
-        if s == "PD" {
-            return Ok(None);
+        if s == "proprietary" {
+            return Ok(Self::Proprietary);
+        } else if s == "PD" {
+            return Ok(Self::PublicDomain);
         } else if s == "CC0" {
-            return Ok(Some(Self(CreativeCommons::Cc0)));
+            return Ok(Self::Cc(CreativeCommons(CcInternal::Cc0)));
         }
 
         assert!(s.len() >= 3,
                 "if s is in LICENSES it should contain at least 3 chars");
 
         let version = match &s[s.len() - 1..] {
-            "1" => CreativeCommonsVersion::One,
-            "2" => CreativeCommonsVersion::Two,
-            "3" => CreativeCommonsVersion::Three,
-            "4" => CreativeCommonsVersion::Four,
+            "1" => CcVersion::One,
+            "2" => CcVersion::Two,
+            "3" => CcVersion::Three,
+            "4" => CcVersion::Four,
             _   => {
                 unreachable!("if s is in LICENSES we should be able to parse the license version")
             },
@@ -258,80 +270,44 @@ impl LicenseType {
 
         match &s[..s.len() - 1] {
             "CC-BY-" => {
-                Ok(
-                    Some(
-                        Self(
-                            CreativeCommons::CcBy {
-                                version,
-                                non_commercial: false,
-                                share_alike: false,
-                            }
-                        )
-                    )
-                )
+                Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
+                    version,
+                    non_commercial: false,
+                    share_alike: false,
+                })))
             },
             "CC-BY-NC-" => {
-                Ok(
-                    Some(
-                        Self(
-                            CreativeCommons::CcBy {
-                                version,
-                                non_commercial: true,
-                                share_alike: false,
-                            }
-                        )
-                    )
-                )
+                Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
+                    version,
+                    non_commercial: true,
+                    share_alike: false,
+                })))
             },
             "CC-BY-SA-" => {
-                Ok(
-                    Some(
-                        Self(
-                            CreativeCommons::CcBy {
-                                version,
-                                non_commercial: false,
-                                share_alike: true,
-                            }
-                        )
-                    )
-                )
+                Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
+                    version,
+                    non_commercial: false,
+                    share_alike: true,
+                })))
             },
             "CC-BY-NC-SA-" => {
-                Ok(
-                    Some(
-                        Self(
-                            CreativeCommons::CcBy {
-                                version,
-                                non_commercial: true,
-                                share_alike: true,
-                            }
-                        )
-                    )
-                )
+                Ok(Self::Cc(CreativeCommons(CcInternal::CcBy {
+                    version,
+                    non_commercial: true,
+                    share_alike: true,
+                })))
             },
             "CC-BY-ND-" => {
-                Ok(
-                    Some(
-                        Self(
-                            CreativeCommons::CcByNd {
-                                version,
-                                non_commercial: false,
-                            }
-                        )
-                    )
-                )
+                Ok(Self::Cc(CreativeCommons(CcInternal::CcByNd {
+                    version,
+                    non_commercial: false,
+                })))
             },
             "CC-BY-NC-ND-" => {
-                Ok(
-                    Some(
-                        Self(
-                            CreativeCommons::CcByNd {
-                                version,
-                                non_commercial: true,
-                            }
-                        )
-                    )
-                )
+                Ok(Self::Cc(CreativeCommons(CcInternal::CcByNd {
+                    version,
+                    non_commercial: true,
+                })))
             },
             _ => {
                 unreachable!("if s is in LICENSES we should be able to parse the license-type")
@@ -408,11 +384,11 @@ impl<'de> Deserialize<'de> for GalleryEntry {
     }
 }
 
-impl Display for LicenseType {
+impl Display for CreativeCommons {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
         match self.0 {
-            CreativeCommons::Cc0 => write!(f, "CC0"),
-            CreativeCommons::CcBy { version, non_commercial, share_alike } => {
+            CcInternal::Cc0 => write!(f, "CC0"),
+            CcInternal::CcBy { version, non_commercial, share_alike } => {
                 write!(f, "CC-BY")?;
                 if non_commercial {
                     write!(f, "-NC")?;
@@ -422,7 +398,7 @@ impl Display for LicenseType {
                 }
                 write!(f, " {version}")
             },
-            CreativeCommons::CcByNd { version, non_commercial } => {
+            CcInternal::CcByNd { version, non_commercial } => {
                 write!(f, "CC-BY")?;
                 if non_commercial {
                     write!(f, "-NC")?;
@@ -433,13 +409,13 @@ impl Display for LicenseType {
     }
 }
 
-impl Display for CreativeCommonsVersion {
+impl Display for CcVersion {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
         match self {
-            CreativeCommonsVersion::One   => write!(f, "1.0"),
-            CreativeCommonsVersion::Two   => write!(f, "2.0"),
-            CreativeCommonsVersion::Three => write!(f, "3.0"),
-            CreativeCommonsVersion::Four  => write!(f, "4.0"),
+            CcVersion::One   => write!(f, "1.0"),
+            CcVersion::Two   => write!(f, "2.0"),
+            CcVersion::Three => write!(f, "3.0"),
+            CcVersion::Four  => write!(f, "4.0"),
         }
     }
 }
diff --git a/src/main.rs b/src/main.rs
@@ -11,7 +11,7 @@ use std::{
     sync::mpsc,
     os::unix,
 };
-use gallery_entry::{GalleryEntry, FileFormat};
+use gallery_entry::{GalleryEntry, FileFormat, LicenseType};
 use threadpool::ThreadPool;
 
 mod gallery_entry;
@@ -396,11 +396,21 @@ fn render_pic_page(pic: &GalleryEntry) -> io::Result<()> {
         writeln!(f, "{}", Escaped(&pic.author))?;
     }
     writeln!(f, "<br>")?;
-    if let Some(license) = &pic.license {
-        writeln!(f, "licensed under <a role=\"license\" href=\"{url}\">{license}</a>",
-                 url = license.url())?;
-    } else {
-        writeln!(f, "this is public domain")?;
+    match &pic.license {
+        LicenseType::Cc(license) => {
+            writeln!(
+                f,
+                "licensed under <a role=\"license\" href=\"{url}\">{license}</a>",
+                url = license.url()
+            )?;
+        }
+        LicenseType::PublicDomain => writeln!(f, "this is public domain")?,
+        LicenseType::Proprietary => {
+            writeln!(
+                f,
+                "this is distributed under a proprietary license"
+            )?;
+        }
     }
     writeln!(f, "</footer>")?;