Browse Source

notes-collect-metadata: better page path handling, commit link deletes

main
Brennen Bearnes 2 months ago
parent
commit
ccf1e53ca0
1 changed files with 19 additions and 11 deletions
  1. +19
    -11
      home/bin/notes-collect-metadata

+ 19
- 11
home/bin/notes-collect-metadata View File

@ -7,6 +7,7 @@ import sqlite3
from panflute import *
def resolve_target(target, page):
"""Resolve the target of a link to a canonical name."""
if re.search('https?://|file:|tel:|mailto:', target):
return target
@ -18,7 +19,7 @@ def resolve_target(target, page):
# Check for an absolute path (within the wiki):
if target.startswith('/'):
return target.replace('/', '', 1);
return target.replace('/', '', 1)
page_elements = page.split('/')
# Get rid of the page name:
@ -28,24 +29,25 @@ def resolve_target(target, page):
resolved_path = []
while len(path_elements) > 0:
el = path_elements.pop()
if el == '..' and len(path_elements) > 0:
element = path_elements.pop()
if element == '..' and len(path_elements) > 0:
# Discard a directory:
path_elements.pop()
else:
resolved_path.append(el)
resolved_path.append(element)
resolved_path.reverse()
return '/'.join(resolved_path)
def extract_values(elem, doc):
"""Extract links from given document, write to sqlite."""
if isinstance(elem, Link):
link_target = elem.url
# Skip in-page anchors, for now:
if link_target.startswith('#'):
return;
return
# Insert a row of data
c.execute(
@ -67,18 +69,21 @@ conn = sqlite3.connect('metadata.db')
c = conn.cursor()
for input_file in sys.argv[1:]:
# Trim leading dir and .wiki:
# XXX: This is such hacky garbage, jiminy:
pagename = input_file.replace('./vimwiki/', '', 1)
pagename = pagename.replace(vimwiki_dir + '/', '', 1)
# Trim leading directory and .wiki extension:
input_file_abspath = os.path.abspath(input_file)
pagename = input_file_abspath.replace(vimwiki_dir + os.sep, '', 1)
pagename = os.path.splitext(pagename)[0]
# XXX: do real logging
# print(pagename)
# _logscratch is for ephemeral renderings of logs for other pages - skip
# collecting any metadata for these pages:
if pagename.startswith('_logscratch'):
continue;
continue
with open(input_file) as page:
with open(input_file_abspath) as page:
doc = convert_text(
page.read(),
input_format='vimwiki',
@ -94,6 +99,9 @@ for input_file in sys.argv[1:]:
# Clear any links from this page in case something's been deleted:
c.execute("DELETE FROM links WHERE page = ?", (pagename,))
conn.commit()
doc.walk(extract_values)
conn.close()

Loading…
Cancel
Save