DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
76690c1
Branches
Tags
master
memoire
src
addcards.lua
Fix prompt-counting bug in src/addcards.lua
dkl9
commited
76690c1
at 2023-336 12:48:47
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 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 local newcard = Card:new({ note = notetext, ct = "bmc", created = os.time() }) for _re in notetext:gmatch("{[^}]+}") do table.insert(newcard.pr, Prompt:new({ card = newcard, ease = config:get("StartingEase") })) end if mismatched(notetext) then io.write(string.format("Card %d (\"%s\") contains mismatched braces\n", #deck + 1, notetext)) end if #newcard.pr >= 1 then table.insert(deck, newcard) cardctr = cardctr + 1 prctr = prctr + #newcard.pr else newcard = Card:new({ note = notetext, ct = "seq", created = os.time() }) for _re in notetext:gmatch(";;") do table.insert(newcard.pr, Prompt:new({ card = newcard, ease = config:get("StartingEase") })) end if #newcard.pr >= 1 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 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 }