DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
ae90e2a
Branches
Tags
master
memoire
src
addcards.lua
Add "pwc" card type
dkl9
commited
ae90e2a
at 2024-1 10:37:53
addcards.lua
Blame
History
Raw
--[[ returns a not-necessarily-meaningful number iff `s` has mismatched curly-brackets ]] local function mismatched(s) return ("}" .. s):find("}[^{]+}") or (s .. "{"):find("{[^}]+{") end --[[ returns a Card with note = s, ct autodetected, ease = ease ]] local function parsenote(s, ease) local c = Card:new({ note = s, ct = "pwc", created = os.time() }) local segs = {} local n = s:find(";;") while n do local p = s:find(";;", n + 2) local ns = s:sub(n, p) table.insert(segs, ns) n = p end for i, ns in ipairs(segs) do if ns:match("{[^}]+}") then for j = 1, #segs do if i ~= j then table.insert(c.pr, Prompt:new({ card = c, ease = ease })) end end end end if #c.pr >= 1 then return c end c = Card:new({ note = s, ct = "bmc", created = os.time() }) for _re in s:gmatch("{[^}]+}") do table.insert(c.pr, Prompt:new({ card = c, ease = ease })) end if #c.pr >= 1 then return c end c = Card:new({ note = s, ct = "seq", created = os.time() }) for _re in s:gmatch(";;") do table.insert(c.pr, Prompt:new({ card = c, ease = ease })) end if #c.pr >= 1 then return c end end local function fn(config, deck) if isatty() then io.write("Reading prompts; press Ctrl-D to stop\n") end local readtext = io.read("a") local cardctr = 0 local prctr = 0 for notetext in readtext:gmatch("(..-)\n\n") do if mismatched(notetext) then io.write(string.format("Card %d (\"%s\") contains mismatched braces\n", #deck + 1, notetext)) end local newcard = parsenote(notetext, config:get("StartingEase")) if newcard then table.insert(deck, newcard) cardctr = cardctr + 1 prctr = prctr + #newcard.pr else io.write(string.format("Card \"%s\" contains no answers (check braces/double-semicolons)\n", notetext)) end end io.write(string.format( "Added %d cards (%d prompts)\n", cardctr, prctr )) if cardctr > 0 then return deck end end return { desc = "(a)dd card (from stdin)", nd = true, fn = fn }