Add Meta Variables in Markdown
authorGeorgios Atheridis <georgios@atheridis.org>
Fri, 31 Mar 2023 19:33:25 +0000 (20:33 +0100)
committerGeorgios Atheridis <georgios@atheridis.org>
Fri, 31 Mar 2023 19:33:25 +0000 (20:33 +0100)
Markdown files can now define variables in the beginning of the file,
these can be used in place of defining them in data.toml

13 files changed:
data.toml
pages/final.md
pages/posts/example.md
pages/posts/other.md
pages/posts/python-code.html/_value.md
pages/posts/test.md
sigma
static/main.css
templates/base.html
templates/footer.html
templates/post.html
templates/scripts/build_posts.py
templates/scripts/build_table.py

index 8ae54199adff28b9d7d56bc122c87df5398bf3fe..d46ad062a955200069d5c73cbd96e521611b375f 100644 (file)
--- a/data.toml
+++ b/data.toml
@@ -12,26 +12,26 @@ _DATE_FORMAT = "%Y-%m-%d"
 _extend = "base.html"
 _footer = "{% file 'footer.html' %}"
 _title = "Atheridis' Page"
+_web_title = "Atheridis' Page"
 _owner = "Georgios Atheridis"
 _email = "georgios@atheridis.org"
 
 [index]
 _name = "Home"
 _title = "My Blog"
-_nav = 0
+_nav = 100
 
 [final]
-_nav = 5
+_name = "Final"
+_nav = 70
 
 [textfile]
-_nav = 20
+_name = "Text"
+_nav = 80
 
 [about]
-_nav = 10
-
-[page_dir.index]
-_name = "Page Dir"
-_nav = 30
+_name = "About"
+_nav = 90
 
 [posts]
 _extend = "post.html"
@@ -43,30 +43,7 @@ _extend = "base.html"
 _name = "Tags"
 _nav = 40
 
-
-[posts.example]
-_owner = "John Doe"
-_email = "john_doe@example.com"
-_date = "2023-02-04"
-_title = "An example post"
-_description = "This is a description for the example post"
-_tags = ["example", "post", "sigma"]
-
-[posts.test]
-_date = "2023-02-02"
-_title = "Test"
-_description = "This is a test"
-_tags = ["test", "post"]
-
-[posts.other]
-_title = "Another Post"
-_description = "The description of another post"
-_tags = ["other", "post", "sigma"]
 [posts.other._table]
 name = ["Georgios Atheridis", "John Doe", "Jane Doe"]
 email = ["georgios@atheridis.org", "johndoe@example.com", "janedoe@example.com"]
 "phone number" = ["1234567890", "0777777777"]
-
-[posts.python-code]
-_title = "Python Code"
-_tags = ["post", "python", "example"]
index facbd28ee265450251139d9ce70e3f07c44cf4c3..4d801275ca8dd3fa777456d80f9be7b42d0f4ccc 100644 (file)
@@ -1 +1,3 @@
 # Is this a markdown file?
+
+$x$
index 5a0ba5484febca1edcd6faad96e46948c3c71d98..08af75b7c41511cc4cc22417a0f176a32ccf15d9 100644 (file)
@@ -1,3 +1,13 @@
+Title: This is my title
+Owner: John Doe
+Email: john_doe@example.com
+Date: 2023-02-05
+Description: This is a description for the example post
+Tags: example
+      post
+      sigma
+
+
 This *is a header* for
 ===
 
index 86cef3c94df67f542971cbf0ecc4b5642d699753..62165621a75bcc90495b18947d391310de03fc8b 100644 (file)
@@ -1,7 +1,10 @@
-# {{ self._title }}
-
-{{ self._description }}
+title: Another Post
+description: The description of another post
+tags: other
+      post
+      sigma
 
+### {{ self._description }}
 This is another file
 this was posted on: {{ self._date }}
 
index 8968ecec80456a67377b71facc8714a5330fa5e0..0bb8dd4ed6227db76be5b3927e51c4418ca85009 100644 (file)
@@ -1,6 +1,11 @@
-# Here is some python code inside my page
+title: Python Code
+tags: post
+      python
+      example
 
-<h1> test </h1>
+### Here is some python code inside my page
+
+<h3> test </h3>
 ```python
 <h1> test </h1>
 {! self._code !}
index 74377fc1597e119eb861ac9481ecee589d660148..45f82ae2513a92dd2b7208a9d21eddd67d2f1402 100644 (file)
@@ -1,3 +1,8 @@
-# Testing
+title: Test
+description: This is a test
+tags: test
+      post
 
-This is a test
+# {{ self._description }}
+
+Some test article is being written here
diff --git a/sigma b/sigma
index ae4760a60199277790ad13ebce2188a6a8924eb4..076a777e0846923c39172a76b6a9a4a89885c2e9 100755 (executable)
--- a/sigma
+++ b/sigma
@@ -40,7 +40,12 @@ regex_execute = re.compile(r"{%\s?file (\"|')(.+)\1\s?%}")
 
 
 def md_to_html(md: str) -> str:
-    return markdown.markdown(md, extensions=["extra", "sane_lists"])
+    return markdown.markdown(md, extensions=["extra", "sane_lists", "meta"])
+
+def md_meta(md: str) -> dict:
+    md_meta = markdown.Markdown(extensions=["meta"])
+    md_meta.convert(md)
+    return md_meta.Meta
 
 
 def get_max_mtime(dir: str) -> float:
@@ -125,7 +130,25 @@ def get_value_from_file(
         return
 
     if not os.path.isdir(os.path.join(path, dir_name + dir_ext)):
-        return
+        if dir_ext != ".md":
+            return
+        with open(os.path.join(path, dir_name + dir_ext), "r") as file_in:
+            m = md_meta(file_in.read()).get(key[1:])
+            if not m:
+                return
+            if len(m) == 1:
+                return m[0]
+            return m
+
+    try:
+        with open(os.path.join(path, dir_name + dir_ext, "_value.md"), "r") as file_in:
+            m = md_meta(file_in.read()).get(key[1:])
+            if m:
+                if len(m) == 1:
+                    return m[0]
+                return m
+    except FileNotFoundError:
+        pass
 
     for file in os.listdir(os.path.join(path, dir_name + dir_ext)):
         file_name, file_ext = os.path.splitext(file)
@@ -148,6 +171,8 @@ def get_value(data: dict, namespace: tuple, key: str, interpret_ok=True):
             return value
     except FileNotFoundError:
         pass
+    except IndexError:
+        pass
     value = data.get(key)
     for namespace_item in namespace:
         data = data.get(namespace_item, data)
@@ -343,6 +368,7 @@ def main(args):
 
     create_page_index(data)
 
+
     builds = []
     for root, _, files in os.walk(data["_PAGE_ROOT"]):
         if root.startswith(("_", ".")):
index 58de5f98dced470a59db5242bbf9a4902be6fa07..2a0dc8455a07b2b91a765c34ffb67e9a100b58be 100644 (file)
@@ -1,15 +1,96 @@
+:root {
+       --background-colour-1: #F2FAFB;
+       --background-colour-2: #CBE5E9;
+       --background-colour-3: #CFEEDE;
+       --foreground-colour-1: #2D3536;
+       --foreground-colour-2: #408089;
+       --foreground-colour-3: #9BC7CD;
+       --primary-colour: #B2A6C4;
+       --secondary-colour: #A6C4B2;
+       --tertiary-colour: #C4B2A6;
+}
+
 body {
-    background: #DDDDDD;
-    color: #333333;
-    max-width: 900px;
-    margin: auto;
-    padding-top: 1rem;
+       background: radial-gradient(circle, var(--background-colour-3) 0%, var(--background-colour-2) 100%);
+       color: var(--foreground-colour-1);
+       max-width: 900px;
+       margin: auto;
+       font-family: sans-serif;
+       min-height: 100%;
+}
+a {
+       color: var(--foreground-colour-2);
+       text-decoration: none;
+       transition: 0.15s;
+}
+
+a:hover {
+       color: var(--foreground-colour-1);
+       text-decoration: none;
+}
+
+.content {
+       box-shadow: 0em 0.1em 0.3em var(--foreground-colour-1);
+       border-radius: 0.7em;
+       background: var(--background-colour-1);
+       margin: 2rem;
+       padding: 1.4rem;
+}
+
+header {
+       display: grid;
+       grid-template-areas: "p nav";
+}
+
+header > h1 {
+       margin: 0;
+       padding: 0;
+       display: flex;
+       align-items: center;
 }
+
+nav {
+       display: flex;
+       flex-direction: row-reverse;
+}
+
 nav a {
-    padding-left: 1rem;
-    padding-right: 1rem;
+       background: var(--foreground-colour-2);
+       color: var(--background-colour-1);
+       margin-left: 1rem;
+       padding: 0.7rem;
+       padding-top: 0.3rem;
+       padding-bottom: 0.3rem;
+       border-radius: 0.5em;
+       display: flex;
+       align-items: center;
 }
+
 nav a:hover {
-    background: #350;
-    color: #ddd;
+       background: var(--foreground-colour-3);
+       color: var(--foreground-color-1);
+}
+
+article {
+       margin: 1rem;
+}
+
+
+article table {
+       width: 100%;
+}
+
+article table thead th {
+       text-align: left;
+}
+
+article table tr:hover td {
+       background-color: var(--background-colour-2);
+}
+
+article table tr td:nth-child(2),
+article table tr td:nth-child(3),
+article table tr td:nth-child(3),
+article table tr td:nth-child(2) {
+       white-space: normal;
 }
index dc079ad906da19c3d5adaf919fc151fc178bdbf3..49c1187986c1df0715c9e929c512245395d7b535 100644 (file)
@@ -1,14 +1,19 @@
 <!DOCTYPE html>
 <html>
-    <head>
-        <title>{{ self._title }}</title>
-        <link rel="stylesheet" href="/static/main.css" />
-    </head>
-    <body>
-        {% file 'scripts/build_nav.py' %}
-        <hr />
-        {{ self._value }}
-        <hr />
-        {{ self._footer }}
-    </body>
+       <head>
+               <title>{{ self._title }}</title>
+               <link rel="stylesheet" href="/static/main.css" />
+       </head>
+       <body>
+               <header class="content">
+                       <h1><a href="/">{{ self._web_title }}</a></h1>
+                       {% file 'scripts/build_nav.py' %}
+               </header>
+               <div class="content">
+                       {{ self._value }}
+               </div>
+               <footer class="content">
+                       {{ self._footer }}
+               </footer>
+       </body>
 </html>
index 821754528bb29e83c4926eae9ebb135864367e31..c21d2ec28062b55e890736c3728a12903831987e 100644 (file)
@@ -1,3 +1 @@
-<footer>
-    This is my footer on page
-</footer>
+This is my footer on page
index 296e3af7053edc47e378f4e66cb012d95bfa0f45..33321b492c3a8eb01b63d28dd2a733b521341855 100644 (file)
@@ -1,7 +1,19 @@
 {% extend 'base.html' %}
-<h3>Here is my article</h3>
-<p>From: <a href="mailto:{{ self._email }}">{{ self._owner }}</a></p>
-<p>Date: {{ self._date }}</p>
+<div class="post-meta">
+       <h2>{{ self._title }}</h2>
+       <table>
+               <tbody>
+                       <tr>
+                               <td>From:</td>
+                               <td><a href="mailto:{{ self._email }}">{{ self._owner }}</a></td>
+                       </tr>
+                       <tr>
+                               <td>Date:</td>
+                               <td>{{ self._date }}</td>
+                       </tr>
+               </tbody>
+       </table>
+</div>
 <article>
-    {{ self._value }}
+       {{ self._value }}
 </article>
index d4419185780f50b3dfb0edefa0d9466b18c54af1..cbff49fc8526b86c6fa27d2c72cf1d60e50eba19 100644 (file)
@@ -11,7 +11,13 @@ for post in posts:
     if post == "tags":
         continue
     post_timestamp.append(
-        (time.strptime(posts[post]["_date"], data["_DATE_FORMAT"]), post)
+        (
+            time.strptime(
+                get_value(data, ("posts", post), "_date"),
+                get_value(data, (), "_DATE_FORMAT"),
+            ),
+            post,
+        )
     )
 post_timestamp = sorted(post_timestamp, reverse=True)
 
index 30dc6f84711f3baebe7861e85add6c54e876c16b..ad6a071199651ad557cea76f66486f44d5600620 100644 (file)
@@ -1,4 +1,4 @@
-_value = "<table><thead><tr>%(head)s</tr></thead><tbody>%(body)s</tbody></table>"
+_value = '<table class="tables"><thead><tr>%(head)s</tr></thead><tbody>%(body)s</tbody></table>'
 posts = ""
 
 table = get_value(data, namespace, "_table")