-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmknotes.lua
More file actions
207 lines (186 loc) · 4.67 KB
/
mknotes.lua
File metadata and controls
207 lines (186 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
-- mknotes.lua
-- Run with: nvim -l mknotes.lua [options] NOTE_COUNT
vim.opt.rtp:append("~/Plugins/obsidian.nvim/")
local uv = vim.loop
local api = require("obsidian.api")
local Note = require("obsidian.note")
-- defaults
local opts = {
dry_run = false,
file_name = "note",
dir_name = ".",
min_paragraphs = 1,
max_paragraphs = 5,
min_words = 25,
max_words = 150,
seed = nil,
frontmatter = false,
wrap = nil,
}
local LOREM = vim.split(
[[
Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum
]],
"%s+"
)
-- utils
local function usage()
print([[
mknotes - generate Lorem Ipsum Obsidian notes
Options:
-n, --dry-run
-f, --file-name NAME
-d, --dir-name DIR
-p, --paragraphs MAX[,MIN]
-w, --words MAX[,MIN]
-s, --seed SEED
--frontmatter
--wrap WIDTH
]])
os.exit(1)
end
local function parse_range(s, default_min, default_max)
local min, max = default_min, default_max
if s:match("^%d+$") then
max = tonumber(s)
elseif s:match("^(%d+),(%d+)$") then
local a, b = s:match("^(%d+),(%d+)$")
min, max = math.min(tonumber(a), tonumber(b)), math.max(tonumber(a), tonumber(b))
else
usage()
end
return min, max
end
local function wrap_text(text, width)
if not width then
return text
end
local out, line, col = {}, {}, 0
for word in text:gmatch("%S+") do
if col + #word + 1 > width then
table.insert(out, table.concat(line, " "))
line, col = {}, 0
end
table.insert(line, word)
col = col + #word + 1
end
if #line > 0 then
table.insert(out, table.concat(line, " "))
end
return table.concat(out, "\n")
end
local function gen_paragraph(words, links)
local t = {}
for _ = 1, words do
local w = LOREM[math.random(#LOREM)]
if links[w] then
local note = Note.new(w, {}, {}, links[w])
local link = api.format_link(note)
table.insert(t, link)
else
table.insert(t, w)
end
end
t[1] = t[1]:sub(1, 1):upper() .. t[1]:sub(2):lower()
return table.concat(t, " ") .. "."
end
local function gen_frontmatter(id)
return string.format(
[[---
id: "%s"
tags: [lorem, generated]
---
]],
id
)
end
local function write_note(path, title, paras, links)
local buf = {}
if opts.frontmatter then
table.insert(buf, gen_frontmatter(title))
end
for _ = 1, paras do
local wc = math.random(opts.min_words, opts.max_words)
local p = gen_paragraph(wc, links)
table.insert(buf, wrap_text(p, opts.wrap) .. "\n")
end
local content = table.concat(buf, "\n")
if opts.dry_run then
io.write(content)
else
local f = assert(io.open(path, "w"))
f:write(content)
f:close()
print("Created " .. path .. " (" .. paras .. " paragraphs)")
end
end
-- arg parsing
local argv = vim.v.argv
local note_count
local i = 4
while i <= #argv do
local a = argv[i]
if a == "-n" or a == "--dry-run" then
opts.dry_run = true
elseif a == "-f" or a == "--file-name" then
i = i + 1
opts.file_name = argv[i]
elseif a == "-d" or a == "--dir-name" then
i = i + 1
opts.dir_name = argv[i]
elseif a == "-p" or a == "--paragraphs" then
i = i + 1
opts.min_paragraphs, opts.max_paragraphs = parse_range(argv[i], 1, 5)
elseif a == "-w" or a == "--words" then
i = i + 1
opts.min_words, opts.max_words = parse_range(argv[i], 25, 150)
elseif a == "-s" or a == "--seed" then
i = i + 1
opts.seed = tonumber(argv[i])
elseif a == "--frontmatter" then
opts.frontmatter = true
elseif a == "--wrap" then
i = i + 1
opts.wrap = tonumber(argv[i])
elseif a == "-h" or a == "--help" then
usage()
else
note_count = tonumber(a)
end
i = i + 1
end
if not note_count then
usage()
end
if opts.seed then
math.randomseed(opts.seed)
end
-- ensure dir exists
if not opts.dry_run and note_count > 0 then
uv.fs_mkdir(opts.dir_name, 493) -- 0755, ignore errors
end
require("obsidian").setup({
legacy_commands = false,
workspaces = {
{
name = "lorem vault",
path = opts.dir_name,
},
},
})
-- main
if note_count == 0 then
local paras = math.random(opts.min_paragraphs, opts.max_paragraphs)
write_note(opts.file_name .. ".md", opts.file_name:gsub("-", " "), paras)
else
local id2path = {}
for _ = 1, note_count do
local id = LOREM[math.random(#LOREM)]
local path = vim.fs.normalize(vim.fs.joinpath(opts.dir_name, id .. ".md"))
id2path[id] = path
end
for id, path in pairs(id2path) do
local paras = math.random(opts.min_paragraphs, opts.max_paragraphs)
write_note(path, id, paras, id2path)
end
end