mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 05:24:24 -05:00
refactor: handle obsidian uri & templating in lua
This commit is contained in:
@@ -5,8 +5,10 @@ local default_config = {
|
||||
excalidraw = {
|
||||
assetsDir = 'assets',
|
||||
filename = 'drawing-%Y-%m-%d-%H-%M-%S',
|
||||
fileExtension = '.excalidraw.md',
|
||||
fileExtensionInserted = '.excalidraw.svg',
|
||||
obsidianOpenConfig = nil,
|
||||
uriOpenCommand = 'xdg-open',
|
||||
templatePath = nil,
|
||||
},
|
||||
snippets = {
|
||||
enable = true,
|
||||
@@ -22,8 +24,10 @@ local default_config = {
|
||||
|
||||
function M.merge_config(args)
|
||||
M.config = vim.tbl_deep_extend('force', default_config, args or {})
|
||||
M.config.excalidraw.obsidianOpenConfig = M.config.excalidraw.obsidianOpenConfig or
|
||||
M.config.typstarRoot .. '/res/obsidian_open_config_example.json'
|
||||
M.config.excalidraw.templatePath = M.config.excalidraw.templatePath or
|
||||
{
|
||||
['%.excalidraw%.md$'] = M.config.typstarRoot .. '/res/excalidraw_template.excalidraw.md',
|
||||
}
|
||||
end
|
||||
|
||||
M.merge_config(nil)
|
||||
|
||||
@@ -2,32 +2,42 @@ local M = {}
|
||||
local config = require('typstar.config')
|
||||
local utils = require('typstar.utils')
|
||||
|
||||
|
||||
local cfg = config.config.excalidraw
|
||||
local affix = [[
|
||||
#figure(
|
||||
image("%s"),
|
||||
image("%s"),
|
||||
)
|
||||
]]
|
||||
|
||||
local function launch_obsidian_open(path)
|
||||
local function launch_obsidian(path)
|
||||
print(string.format('Opening %s in Excalidraw', path))
|
||||
utils.run_shell_command('python3 ' ..
|
||||
config.config.typstarRoot .. '/python/obsidian_open.py ' ..
|
||||
path .. ' --config ' .. cfg.obsidianOpenConfig)
|
||||
utils.run_shell_command(string.format('%s "obsidian://open?path=%s"', cfg.uriOpenCommand, utils.urlencode(path)))
|
||||
end
|
||||
|
||||
|
||||
function M.insert_drawing()
|
||||
local assets_dir = vim.fn.expand('%:p:h') .. '/' .. cfg.assetsDir
|
||||
local filename = os.date(cfg.filename)
|
||||
local path = assets_dir .. '/' .. filename .. cfg.fileExtension
|
||||
local path_inserted = cfg.assetsDir .. '/' .. filename .. cfg.fileExtensionInserted
|
||||
|
||||
if vim.fn.isdirectory(assets_dir) == 0 then
|
||||
vim.fn.mkdir(assets_dir, 'p')
|
||||
end
|
||||
local filename = os.date(cfg.filename)
|
||||
local path = assets_dir .. '/' .. filename .. '.excalidraw.md'
|
||||
local path_inserted = cfg.assetsDir .. '/' .. filename .. cfg.fileExtensionInserted
|
||||
local found_match = false
|
||||
for pattern, template_path in pairs(cfg.templatePath) do
|
||||
if string.match(path, pattern) then
|
||||
found_match = true
|
||||
utils.run_shell_command(string.format('cat %s > %s', template_path, path)) -- don't copy file metadata
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found_match then
|
||||
print('No matching template found for the path: ' .. path)
|
||||
return
|
||||
end
|
||||
|
||||
utils.insert_text_block(string.format(affix, path_inserted))
|
||||
launch_obsidian_open(path)
|
||||
launch_obsidian(path)
|
||||
end
|
||||
|
||||
function M.open_drawing()
|
||||
@@ -35,7 +45,7 @@ function M.open_drawing()
|
||||
local path = vim.fn.expand('%:p:h') ..
|
||||
'/' .. string.match(line, '"(.*)' .. string.gsub(cfg.fileExtensionInserted, '%.', '%%%.')) ..
|
||||
'.excalidraw.md'
|
||||
launch_obsidian_open(path)
|
||||
launch_obsidian(path)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -30,6 +30,20 @@ function M.run_shell_command(cmd)
|
||||
vim.fn.jobstart(cmd)
|
||||
end
|
||||
|
||||
function M.char_to_hex(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end
|
||||
|
||||
function M.urlencode(url)
|
||||
if url == nil then
|
||||
return ''
|
||||
end
|
||||
url = string.gsub(url, '\n', '\r\n')
|
||||
url = string.gsub(url, '([^%w _%%%-%.~])', M.char_to_hex)
|
||||
url = string.gsub(url, ' ', '%%20')
|
||||
return url
|
||||
end
|
||||
|
||||
function M.get_treesitter_root(bufnr)
|
||||
return ts.get_parser(bufnr):parse()[1]:root()
|
||||
end
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import urllib.parse
|
||||
|
||||
argument_parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
prog="Obsidian File Opener",
|
||||
description="Open and create files in Obsidian with template support.",
|
||||
epilog="""
|
||||
\n\n
|
||||
Template config file format (json):
|
||||
|
||||
{
|
||||
"filename_regex": "template_path"
|
||||
}
|
||||
|
||||
example:
|
||||
{
|
||||
"\\\\.md$": "~/Templates/default.md"
|
||||
"\\\\.excalidraw\\\\.md$": "~/Templates/excalidraw.md"
|
||||
}
|
||||
""",
|
||||
)
|
||||
argument_parser.add_argument("file", help="file to open/create")
|
||||
argument_parser.add_argument(
|
||||
"-c", "--config", help="path to json template config file", required=True
|
||||
)
|
||||
args = argument_parser.parse_args()
|
||||
path = os.path.abspath(args.file)
|
||||
url_params = {
|
||||
"path": path,
|
||||
}
|
||||
|
||||
if not os.path.exists(path):
|
||||
filename = os.path.basename(path)
|
||||
with open(args.config) as f:
|
||||
config = json.loads(f.read())
|
||||
template_content = ""
|
||||
|
||||
for regex, template in config.items():
|
||||
if re.search(regex, filename):
|
||||
print(f"Template regex is matching: {regex}")
|
||||
with open(os.path.expanduser(template), encoding="utf-8") as f:
|
||||
template_content = f.read()
|
||||
break
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(template_content)
|
||||
|
||||
encoded = urllib.parse.urlencode(url_params, quote_via=urllib.parse.quote)
|
||||
os.system(f"xdg-open obsidian://open?{encoded}")
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"\\.excalidraw\\.md$": "~/typstar/res/excalidraw_template.excalidraw.md"
|
||||
}
|
||||
Reference in New Issue
Block a user