Created
June 29, 2025 21:14
-
-
Save Zash/e1e7d3207e1c8b693580dfa22d3fe7be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm site.sqlite3 | |
hg files -I '**.md' -X '4[01][0134].md' | awk.lua \ | |
'BEGIN-->print[[CREATE VIRTUAL TABLE "site" USING fts5 ("filename" unindexed, "metadata", "content");]]' \ | |
"-->local meta = {}; | |
local yq = string.format('. * load(%q)', (_:gsub('%.md$','.yaml'))) | |
local cmd = string.format('yq -f extract -oj %q %s', yq, _) | |
local ph = io.popen(cmd) | |
if ph then meta = ph:read('*a') ph:close() meta = require'cjson.safe'.decode(meta) end | |
if not meta then local f = io.open(_); if f then local line = f:read(); f:close() | |
if line and line:match '^% ' then meta = { title = line:sub(3) } end end end | |
if meta then meta = require'cjson.safe'.encode(meta) end | |
if meta then meta = meta:gsub(\"'\", \"''\") else meta = '{}' end | |
return string.format([[insert into \"site\" (\"filename\", \"metadata\", \"content\") values ('%s', '%s', readfile('%s'));]], _,meta, _)" | | |
sqlite3 site.sqlite3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local db = assert(require"DBI".Connect("SQLite3", "site.sqlite3")); | |
local query = db:prepare[[ | |
SELECT | |
"filename", | |
"metadata"->>'file' AS "path", | |
"metadata"->>'title' AS "title", | |
"metadata"->>'pagetitle' AS "pagetitle", | |
"metadata"->>'abstract' AS "abstract" | |
FROM "site" | |
WHERE | |
"content" MATCH ? | |
ORDER BY rank | |
LIMIT 10; | |
]] | |
local template = [[ | |
- [%s](https://prosody.im/%s) | |
%s | |
]] | |
assert(query:execute((...))); | |
for row in query:rows() do | |
print(string.format(template, row[3] or row[4] or row[1]:match("([^/]+)%.md$"), row[2] or row[1]:gsub("%.md$",""), row[5] or "")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment