Downloading and Converting Tiddly to JSON

Abstract

The code below is an example of using the Downloads and JSON3 Julia packages to connect to a sample TiddlyHost site, filter documents, select one, and convert to a JSON format.

using Downloads, JSON3, Dates

function fetch_tiddlywiki_store(url)
    html = String(take!(Downloads.download(url, IOBuffer())))
    m = match(r"<script class=\"tiddlywiki-tiddler-store\"[^>]*>(.*?)</script>"s, html)
    m === nothing && error("no tiddlywiki-tiddler-store script found in $url")
    JSON3.read(m.captures[1])
end

const _tiddler_cache = Dict{String,Any}()
get_tiddlers(url) = get!(() -> fetch_tiddlywiki_store(url), _tiddler_cache, url)

function filter_titles(tiddlers, show_system, filter_text)
    q = lowercase(filter_text)
    titles = [t.title for t in tiddlers]
    show_system || filter!(t -> !startswith(t, "\$:/"), titles)
    isempty(q) ? titles : filter(t -> occursin(q, lowercase(t)), titles)
end

function resolve_tiddler(tiddlers, title)
    i = isempty(title) ? nothing : findfirst(t -> t.title == title, tiddlers)
    i === nothing ? nothing : tiddlers[i]
end

tw_date(s) = isempty(s) ? "" : DateTime(s[1:14], dateformat"yyyymmddHHMMSS")

pretty_json(x) = sprint(io -> JSON3.pretty(io, x))

url_host(url) = (m = match(r"^\w+://([^/]+)", url); m === nothing ? url : m.captures[1])

function slugify(s)
    s = replace(lowercase(strip(s)), r"[^a-z0-9]+" => "-")
    strip(s, '-')
end
;
t = resolve_tiddler(tiddlers, selected_title)
if t === nothing
    "no tiddler selected"
else
    slate_table([(field="title", value=string(t.title)),
                 (field="tags", value=get(t, :tags, "")),
                 (field="modified", value=string(tw_date(get(t, :modified, "")))),
                 (field="type", value=get(t, :type, "")),
                 (field="text", value=get(t, :text, ""))])
end
fieldvalue
title1950's Large Farmhouse
tagshousing
modified2023-02-16T02:31:51
type
text
t = resolve_tiddler(tiddlers, selected_title)
t === nothing ? Text("") : Text(pretty_json(t))
{
    "created": "20230216020225285",
    "creator": "Telumire",
    "text": "",
    "tags": "housing",
    "title": "1950's Large Farmhouse",
    "modified": "20230216023151868",
    "modifier": "Telumire",
    "cost": "200",
    "description": "You are living with your family which has social and emotional pros and cons. "
}

Run this notebook live

Get the full interactive notebook (with the AI agent) on your machine. Needs Julia 1.10+. The launch script installs Kaimon + KaimonSlate and starts the notebook (its exact environment is reconstructed from the bundle).

One-lineryour platform (paste into a terminal):

Show the command for another OS ▾

Or download run.jl to inspect first, then julia run.jl. On Windows, grab run.bat + run.ps1 beside it and double-click run.bat. Just the env? download the bundle.