Explorar el Código

update to use short hash

Getty Ritter hace 6 meses
padre
commit
5b7dcb608e
Se han modificado 5 ficheros con 76 adiciones y 29 borrados
  1. 21 1
      BUILD.bazel
  2. 13 5
      WORKSPACE
  3. 39 21
      main.py
  4. 2 1
      requirements.txt
  5. 1 1
      templates/quote.mustache

+ 21 - 1
BUILD.bazel

@@ -5,6 +5,18 @@ py_binary(
     name = "main",
     srcs = ["main.py"],
     data = glob(["templates/*.mustache", "static/*"]),
+    deps = [
+        requirement("Markdown"),
+        requirement("pystache"),
+        requirement("PyYAML"),
+        requirement("base58"),
+    ]
+)
+
+py_binary(
+    name = "experimental",
+    srcs = ["experimental.py"],
+    data = glob(["templates/*.mustache", "static/*"]),
     deps = [
         requirement("Markdown"),
         requirement("pystache"),
@@ -14,7 +26,7 @@ py_binary(
 
 filegroup(
     name = "data",
-    srcs=glob(["quips/*", "quotes/*", "works/**/*", "works.json"], exclude=["*~"]),
+    srcs=glob(["quips/*", "quotes/*", "works/**/*", "works.json"], exclude=["**/*~"]),
 )
 
 genrule(
@@ -24,3 +36,11 @@ genrule(
     outs = ["output"],
     cmd = "$(location :main) $(location output) $(locations :data)",
 )
+
+genrule(
+    name = "experiment",
+    srcs = [":data"] + glob(["templates/*.mustache", "static/*"]),
+    tools = [":experimental"],
+    outs = ["exp"],
+    cmd = "$(location :experimental) $(location exp) $(locations :data)",
+)

+ 13 - 5
WORKSPACE

@@ -2,14 +2,22 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 
 http_archive(
     name = "rules_python",
-    sha256 = "9fcf91dbcc31fde6d1edb15f117246d912c33c36f44cf681976bd886538deba6",
-    strip_prefix = "rules_python-0.8.0",
-    url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.8.0.tar.gz",
+    sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
+    strip_prefix = "rules_python-0.26.0",
+    url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.26.0.tar.gz",
 )
 
-load("@rules_python//python:pip.bzl", "pip_install")
+load("@rules_python//python:repositories.bzl", "py_repositories")
 
-pip_install(
+py_repositories()
+
+load("@rules_python//python:pip.bzl", "pip_parse")
+
+pip_parse(
    name = "deps",
    requirements = "//:requirements.txt",
 )
+
+load("@deps//:requirements.bzl", "install_deps")
+
+install_deps()

+ 39 - 21
main.py

@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
 
 from dataclasses import dataclass
+import base58
 import datetime
 import os
 import markdown
@@ -11,6 +12,9 @@ import tempfile
 from typing import Optional
 import yaml
 
+def short_hash(s: str) -> str:
+    return base58.b58encode(bytes.fromhex(s.replace('-', '')))[:16].decode('utf-8')
+
 class Datum:
     @classmethod
     def from_yaml(cls, data):
@@ -28,11 +32,17 @@ class Quote(Datum):
     content: str
     author: str
 
+    def short_hash(self) -> str:
+        return "qt_" + short_hash(self.id)
+
 @dataclass
 class Quip(Datum):
     id: str
     content: str
 
+    def short_hash(self) -> str:
+        return "qp_" + short_hash(self.id)
+
 @dataclass
 class Work:
     slug: str
@@ -105,17 +115,21 @@ def main():
         q = Quip.from_file(Path.data('quips', uuid))
         q.content = markdown.markdown(q.content)
         quips.append(q)
+        h = q.short_hash()
+        html = Template.main({
+            'title': f"Quip",
+            'contents': Template.quote({'quotelist': [q]}),
+            'copy': no_copy,
+            'opengraph': {
+                'title': f'quip:{h}',
+                'url': f'/quip/{h}/',
+                'description': q.content,
+            },
+        })
         with Path.write('quips', uuid, 'index.html') as f:
-            f.write(Template.main({
-                'title': f"Quip",
-                'contents': Template.quote({'quotelist': [q]}),
-                'copy': no_copy,
-                'opengraph': {
-                    'title': f'quip:{uuid}',
-                    'url': f'/quip/{uuid}/',
-                    'description': q.content,
-                },
-            }))
+            f.write(html)
+        with Path.write('quips', h, 'index.html') as f:
+            f.write(html)
 
     # sort 'em and make the combined page
     quips.sort(key=lambda q: q.id)
@@ -133,18 +147,22 @@ def main():
         q.content = markdown.markdown(q.content)
         q.author = markdown.markdown(q.author)
         quotes.append(q)
+        contents = Template.quote({'quotelist': [q]})
+        short_hash = q.short_hash()
+        html = Template.main({
+            'title': f"Quote",
+            'contents': contents,
+            'copy': no_copy,
+            'opengraph': {
+                'title': f'quote:{short_hash}',
+                'url': f'/quote/{short_hash}/',
+                'description': f'{q.content}\n—{q.author}',
+            },
+        })
         with Path.write('quotes', uuid, 'index.html') as f:
-            contents = Template.quote({'quotelist': [q]})
-            f.write(Template.main({
-                'title': f"Quote",
-                'contents': contents,
-                'copy': no_copy,
-                'opengraph': {
-                    'title': f'quote:{uuid}',
-                    'url': f'/quote/{uuid}/',
-                    'description': f'{q.content}\n---{q.author}',
-                },
-            }))
+            f.write(html)
+        with Path.write('quotes', short_hash, 'index.html') as f:
+            f.write(html)
 
     # sort 'em and make their combined page
     quotes.sort(key=lambda q: q.id)

+ 2 - 1
requirements.txt

@@ -1,3 +1,4 @@
 Markdown==3.3.6
 pystache==0.6.0
-PyYAML==6.0
+PyYAML==6.0.1
+base58==2.1.1

+ 1 - 1
templates/quote.mustache

@@ -7,7 +7,7 @@
         —{{{author}}}
       </div>
     {{/author}}
-    <div class="quotelink"><a href="{{id}}">#{{id}}</a></div>
+    <div class="quotelink"><a href="{{short_hash}}">#{{short_hash}}</a></div>
     </div>
   {{/quotelist}}
 </div>