2022-04-22 20:44:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2023-06-20 13:00:23 +00:00
|
|
|
if [[ ! -d .venv ]]; then
|
|
|
|
python3 -m venv .venv
|
2023-06-20 13:03:16 +00:00
|
|
|
.venv/bin/python3 -m pip install sqlite-utils markdown-to-sqlite markdown
|
2023-06-20 13:00:23 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
. .venv/bin/activate
|
|
|
|
|
2022-04-22 20:44:09 +00:00
|
|
|
DATABASE_PATH="${DATABASE_PATH:-blog.db}"
|
|
|
|
sqlite3 -batch -bail "$DATABASE_PATH" "
|
|
|
|
CREATE TABLE IF NOT EXISTS [posts] (
|
|
|
|
[_id] TEXT PRIMARY KEY,
|
|
|
|
[_path] TEXT,
|
|
|
|
[text] TEXT,
|
|
|
|
[html] TEXT,
|
|
|
|
[permalink] TEXT,
|
|
|
|
[title] TEXT,
|
|
|
|
[published_date] TEXT,
|
|
|
|
[layout] TEXT,
|
2024-01-06 17:14:50 +00:00
|
|
|
[tags] TEXT,
|
2022-04-22 20:44:09 +00:00
|
|
|
[data] TEXT,
|
|
|
|
[excerpt] TEXT
|
|
|
|
);
|
|
|
|
PRAGMA journal_mode = WAL;
|
|
|
|
PRAGMA foreign_keys = ON;
|
|
|
|
" >/dev/null
|
|
|
|
|
|
|
|
markdown-to-sqlite "$DATABASE_PATH" posts _posts/*
|
2022-04-22 21:41:41 +00:00
|
|
|
|
|
|
|
sqlite-utils convert "$DATABASE_PATH" posts text '
|
|
|
|
from markdown import Markdown
|
|
|
|
import io
|
|
|
|
|
2022-04-22 21:49:06 +00:00
|
|
|
def convert(text):
|
|
|
|
global Markdown
|
2022-04-22 21:41:41 +00:00
|
|
|
|
|
|
|
def unmark_element(element, stream=None):
|
|
|
|
global io
|
|
|
|
if stream is None:
|
|
|
|
stream = io.StringIO()
|
|
|
|
if element.text:
|
|
|
|
stream.write(element.text)
|
|
|
|
for sub in element:
|
|
|
|
unmark_element(sub, stream)
|
|
|
|
if element.tail:
|
|
|
|
stream.write(element.tail)
|
|
|
|
return stream.getvalue()
|
|
|
|
|
|
|
|
Markdown.output_formats["plain"] = unmark_element
|
2022-04-22 21:49:06 +00:00
|
|
|
|
2022-04-22 21:41:41 +00:00
|
|
|
__md = Markdown(output_format="plain")
|
|
|
|
__md.stripTopLevelTags = False
|
|
|
|
return __md.convert(text)
|
|
|
|
'
|
|
|
|
|
2022-04-22 20:44:09 +00:00
|
|
|
sqlite-utils enable-fts --fts5 "$DATABASE_PATH" posts title text 2>/dev/null || true
|
|
|
|
sqlite-utils rebuild-fts "$DATABASE_PATH" posts
|