Skip to content

Commit

Permalink
quick fix for book projects
Browse files Browse the repository at this point in the history
save last question number in a file, and build on top of that. Gives strange results with preview.
  • Loading branch information
ute committed Feb 18, 2024
1 parent 90513f8 commit 67f9195
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 24 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion _extensions/qquestion/_extension.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: qquestion
author: Ute Hahn
version: 0.1.0
version: 0.2.0
quarto-required: ">=1.4.0"
contributes:
filters:
Expand Down
106 changes: 83 additions & 23 deletions _extensions/qquestion/qquestion.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,78 @@
-- 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",'🤔<sub>[Q'..qnum..']</sub><span class="qquestion" number='..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",'🤔<sub>[Q'..rinfo.qnum..']</sub><span class="qquestion" number='..rinfo.qnum..'>')
elseif rinfo.ispdf then
return pandoc.RawInline("tex",'\\qquestion{'..rinfo.qnum.."}{")
else
return pandoc.Str("<QStart>")
end
end

local function qend()
if ishtml then
if rinfo.ishtml then
return pandoc.RawInline("html",'</span>')
elseif ispdf then
elseif rinfo.ispdf then
return pandoc.RawInline("tex",'}')
else
return pandoc.Str("<QEnde>")
end
end

local function qans()
if ishtml then
return pandoc.RawInline("html",'</span><span class="qanswer" number='..qnum..'>')
elseif ispdf then
return pandoc.RawInline("tex",'}\\qanswer{'..qnum.."}{")
if rinfo.ishtml then
return pandoc.RawInline("html",'</span><span class="qanswer" number='..rinfo.qnum..'>')
elseif rinfo. ispdf then
return pandoc.RawInline("tex",'}\\qanswer{'..rinfo.qnum.."}{")
else
return pandoc.Str("<QEnde><QAnswer>")
end
Expand All @@ -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
Expand All @@ -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
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
}
}
75 changes: 75 additions & 0 deletions _extensions/qquestion/utels.lua
Original file line number Diff line number Diff line change
@@ -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
}
--[[
--]]

0 comments on commit 67f9195

Please sign in to comment.