init
3 files changed, 134 insertions(+), 0 deletions(-)

A => get_data.py
A => go.mod
A => previewer.go
A => get_data.py +32 -0
@@ 0,0 1,32 @@ 
+#!/bin/python
+import json
+import os
+
+import xettel.config
+import xettel.zxapian.reader as ZXR
+
+config_path = os.path.expanduser("~/.config/xettel/config.toml")
+xettel.config.set_config(config_path)
+path = xettel.config.xettel_cur_zk_data_dir
+reader: ZXR.ZXReader = ZXR.ZXReader(path)
+zk = reader.db_to_zk()
+zk.initialise_contents()
+
+
+def make_record(i):
+    zettel = zk[i]
+    return (zettel.get_uid(), {
+        "uid": zettel.get_uid_36(),
+        "id": zettel.get_uid(),
+        "tags": zettel.attributes["tags"] if "tags" in zettel.attributes else [],
+        "title": zettel.attributes["title"],
+        "abstract": zettel.attributes["abstract"] if "abstract" in
+        zettel.attributes.keys() else "lel",
+    })
+
+
+summaries = dict(map(make_record, zk))
+
+
+with open("summaries.json", "w") as f:
+    json.dump(summaries, f)

          
A => go.mod +3 -0
@@ 0,0 1,3 @@ 
+module previewer
+
+go 1.17

          
A => previewer.go +99 -0
@@ 0,0 1,99 @@ 
+package main
+
+import (
+	"encoding/json"
+	"flag"
+	"fmt"
+	"log"
+	"net/http"
+	"os"
+	"strconv"
+)
+
+type Zettel_Summary struct {
+	UID      string   `json:"uid"`
+	ID       int      `json:"id"`
+	Tags     []string `json:"tags"`
+	Title    string   `json:"title"`
+	Abstract string   `json:"abstract"`
+}
+
+type ZK_Summaries map[int]Zettel_Summary
+
+type UIDType int
+
+const (
+	UIDNumber UIDType = iota
+	UIDAlnum
+	UIDInvalid
+)
+
+func read_json_file(path string) ZK_Summaries {
+	file, err := os.ReadFile(path)
+	if err != nil {
+		fmt.Println(err)
+	}
+
+	var jsonfile ZK_Summaries
+
+	json.Unmarshal(file, &jsonfile)
+
+	return jsonfile
+}
+
+func uid_type(uid string) UIDType {
+	var uid_type UIDType = UIDNumber
+	for _, char := range uid {
+		if '0' <= char && char <= '9' {
+
+		} else if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') {
+			uid_type = UIDAlnum
+		} else {
+			return UIDInvalid
+		}
+
+	}
+	return uid_type
+}
+
+func canonicalise_uid(uid string) (int, error) {
+	switch uid_type(uid) {
+	case UIDNumber:
+		return strconv.Atoi(uid)
+	case UIDAlnum:
+		id, err := strconv.ParseInt(uid, 36, 0)
+		return int(id), err
+	default:
+		return -1, fmt.Errorf("Invalid UID: %s", uid)
+	}
+}
+func reply(summaries ZK_Summaries, uid string) (int, string) {
+	id, e := canonicalise_uid(uid)
+	if e != nil {
+		return http.StatusBadRequest, fmt.Sprintln(e)
+	} else if v, ok := summaries[id]; ok {
+		a, _ := json.Marshal(v)
+		return http.StatusAccepted, string(a)
+	} else {
+		return http.StatusNoContent, fmt.Sprintf("ID %v is valid, but not present.", id)
+	}
+}
+
+func main() {
+	var port int
+	var data string
+	flag.IntVar(&port, "port", 54256, "Port to listen to")
+	flag.StringVar(&data, "data", "summaries.json", "Path to the JSON file containing xettel's data")
+
+	flag.Parse()
+
+	var summaries ZK_Summaries = read_json_file(data)
+
+	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+		code, response := reply(summaries, r.FormValue("id"))
+		w.WriteHeader(code)
+		fmt.Fprintf(w, response)
+	})
+
+	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
+}