diff --git a/README.md b/README.md index 36a4170..581fe2a 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,6 @@ Change appearance of questions and answers by hacking the files `qquestions.css` Here is the source code for a minimal example: [example.qmd](example.qmd). +## Quirks + +In book projects, questions are numbered accross chapters. The way it is done now (v0.2.0) gives funny results in the previewer, but works well when rendering the whole project at once. \ No newline at end of file diff --git a/_extensions/qquestion/_extension.yml b/_extensions/qquestion/_extension.yml index b650e31..e7a8538 100644 --- a/_extensions/qquestion/_extension.yml +++ b/_extensions/qquestion/_extension.yml @@ -1,6 +1,6 @@ title: qquestion author: Ute Hahn -version: 0.1.0 +version: 0.2.0 quarto-required: ">=1.4.0" contributes: filters: diff --git a/_extensions/qquestion/qquestion.lua b/_extensions/qquestion/qquestion.lua index decfb47..916b4f4 100644 --- a/_extensions/qquestion/qquestion.lua +++ b/_extensions/qquestion/qquestion.lua @@ -1,28 +1,67 @@ -- nice rename function learned from shafayetShafee :-) -- local str = pandoc.utils.stringify --- local pout = quarto.log.output +local pout = quarto.log.output --- important quasi global variables +-- initiate rendering information and global question number +local utelz = require("./utels") +local rinfo = {} -local ishtml = quarto.doc.is_format("html") -local ispdf = quarto.doc.is_format("pdf") -local qnum = 0 +-- initialize qnum, store and retrieve + +local function readqnum(renderinfo) + local file = io.open(renderinfo.qnumstore,"r") + local qnum = 0 + if file then + local qnumjson = file:read "*a" + file:close() + if qnumjson + then qnum = quarto.json.decode(qnumjson) + end + end + -- pout("got qnum as "..qnum) + -- pout(rinfo) + return(qnum) +end; + +local function store_qnum(renderinfo) + local qnjson = quarto.json.encode(renderinfo.qnum) + local file = io.open(renderinfo.qnumstore,"w") + if file ~= nil then + file:write(qnjson) + file:close() + end +end + +function init_qnum(renderinfo) + if renderinfo.ishtmlbook then + if renderinfo.isfirst then + renderinfo.qnum = 0 + else + renderinfo.qnum = readqnum(renderinfo) + end + else + renderinfo.qnum = 0 + end +end + +-- functions for rendering local function qstart() - qnum = qnum+1 - if ishtml then - return pandoc.RawInline("html",'🤔[Q'..qnum..']') - elseif ispdf then - return pandoc.RawInline("tex",'\\qquestion{'..qnum.."}{") + rinfo.qnum = rinfo.qnum+1 + -- pout("qstart "..rinfo.qnum) + if rinfo.ishtml then + return pandoc.RawInline("html",'🤔[Q'..rinfo.qnum..']') + elseif rinfo.ispdf then + return pandoc.RawInline("tex",'\\qquestion{'..rinfo.qnum.."}{") else return pandoc.Str("") end end local function qend() - if ishtml then + if rinfo.ishtml then return pandoc.RawInline("html",'') - elseif ispdf then + elseif rinfo.ispdf then return pandoc.RawInline("tex",'}') else return pandoc.Str("") @@ -30,10 +69,10 @@ local function qend() end local function qans() - if ishtml then - return pandoc.RawInline("html",'') - elseif ispdf then - return pandoc.RawInline("tex",'}\\qanswer{'..qnum.."}{") + if rinfo.ishtml then + return pandoc.RawInline("html",'') + elseif rinfo. ispdf then + return pandoc.RawInline("tex",'}\\qanswer{'..rinfo.qnum.."}{") else return pandoc.Str("") end @@ -42,7 +81,7 @@ end -- find {?? bla ??} -Inlines = function(el) +function Inlines_parse(el) for i,ele in pairs(el) do if ele.t == "Str" then if ele.text == "{??" then @@ -59,20 +98,41 @@ Inlines = function(el) end --- TODO: book, save number between processed files? - -Pandoc = function(doc) - if ishtml +function Pandoc_doit(doc) + if rinfo.ishtml then quarto.doc.add_html_dependency({ name = 'qqstyle', stylesheets = {'qquestion.css'} }) - elseif ispdf + elseif rinfo.ispdf then quarto.doc.use_latex_package("tcolorbox") quarto.doc.include_file('before-body','qquestion.tex') quarto.doc.add_format_resource("Emo_think.png") end + + -- pout("yeah, still have to store qnum "..rinfo.qnum) + store_qnum(rinfo) return(doc) -end \ No newline at end of file +end + + +return{ +{ -- first get rendering information + Meta = function(meta) + rinfo = utelz.Meta_getinfo(meta) + rinfo.qnumstore = "_qnumstore.json" + init_qnum(rinfo) + -- pout("render info ") + -- pout("==== book render info ==") + -- pout(meta.book.render) + -- pout("==== book chapter info ==") + -- pout(meta.book.chapters) + end +}, +{ + Inlines = Inlines_parse, + Pandoc = Pandoc_doit +} +} diff --git a/_extensions/qquestion/utels.lua b/_extensions/qquestion/utels.lua new file mode 100644 index 0000000..ef6be35 --- /dev/null +++ b/_extensions/qquestion/utels.lua @@ -0,0 +1,75 @@ + +local str = pandoc.utils.stringify +local pout = quarto.log.output + +-- rendering information + +local function chapterinfo(book, fname) + local first = "" + local last = "" + local chapno = nil + local info = {} + --if book.render then + for _, v in pairs(book.render) do + if str(v.type) == "chapter" then + last = pandoc.path.split_extension(str(v.file)) + if first == "" then first = last end + if last == fname then chapno = v.number end + end + end + info.islast = (fname == last) + info.isfirst = (fname == first) + info.lastchapter = last + info.chapno = chapno + -- pout("chapter inf:", info) + return(info) + end + + +local function Meta_getinfo(meta) + local processedfile = pandoc.path.split_extension(PANDOC_STATE.output_file) + local ispdf, ishtml, isbook, ishtmlbook = false, false, false, false + local rinfo={} + + -- pout("here we go") + rinfo.ispdf = quarto.doc.is_format("pdf") + rinfo.ishtml = quarto.doc.is_format("html") + rinfo.isbook = meta.book ~=nil + rinfo.ishtmlbook = rinfo.isbook and rinfo.ishtml + + rinfo.processedfile = processedfile + rinfo.output_file = PANDOC_STATE.output_file + -- pout(" now in "..processedfile.." later becomes ".. str(fbx.output_file)) + + rinfo.isfirst = not rinfo.ishtmlbook + rinfo.islast = not rinfo.ishtmlbook + if rinfo.isbook then + local chinfo = chapterinfo(meta.book, processedfile) + rinfo.isfirst = chinfo.isfirst + rinfo.islast = chinfo.islast + if meta.chapno then + rinfo.chapno = str(meta.chapno) + else + if chinfo.chapno ~= nil then + rinfo.chapno = str(chinfo.chapno) + else + rinfo.chapno = "" + rinfo.unnumbered = true + end + end + else -- not a book. + rinfo.chapno = "" + rinfo.unnumbered = true + end + --pout(rinfo) + return(rinfo) + end + + +--[[ +--]] +return { + Meta_getinfo = Meta_getinfo +} +--[[ +--]] \ No newline at end of file