Module:Cite Talmud: Difference between revisions
Appearance
Content deleted Content added
Troubleshoot empty space bug |
Switch to using mw.log for debugging |
||
Line 1: | Line 1: | ||
function inspect(str) |
function inspect(str) |
||
str = tostring(str) |
|||
local r = tostring(#str) .. ':"' |
|||
for i = 1, #str do |
for i = 1, #str do |
||
local c = string.byte(str:sub(i,i)) |
local c = string.byte(str:sub(i,i)) |
||
Line 9: | Line 10: | ||
end |
end |
||
end |
end |
||
r = r .. '"' |
|||
return r |
return r |
||
end |
end |
||
Line 16: | Line 18: | ||
if string.find(string.gsub(val, ' ', ''), '^%s*$') then |
if string.find(string.gsub(val, ' ', ''), '^%s*$') then |
||
template_args[key] = nil |
template_args[key] = nil |
||
mw.log("Replacing argument " .. inspect(key) .. " which was " .. inspect(val)) |
|||
⚫ | |||
mw.log("Keeping argument " .. inspect(key) .. " which is " .. inspect(val)) |
|||
end |
end |
||
end |
end |
||
Line 27: | Line 32: | ||
local template_args = frame:getParent().args |
local template_args = frame:getParent().args |
||
-- local invoke_args = frame.args -- parameters from {{#invoke:}} |
-- local invoke_args = frame.args -- parameters from {{#invoke:}} |
||
mw.log(inspect(template_args[1])) |
|||
blanks_to_nil(template_args) |
blanks_to_nil(template_args) |
||
mw.log(inspect(template_args[1])) |
|||
jb = template_args[1] or 'b' |
jb = template_args[1] or 'b' |
||
if not talmud.jb_key[jb] then |
if not talmud.jb_key[jb] then |
||
str = frame:expandTemplate{ title = 'error', args = { 'First argument must be either b for Babylonian Talmud or y for Jerusalem Talmud. (Given |
str = frame:expandTemplate{ title = 'error', args = { 'First argument must be either b for Babylonian Talmud or y for Jerusalem Talmud. (Given ' .. inspect(jb) .. ')' } } |
||
str = str .. '1--' .. inspect(string.gsub(jb, ' ', '')) |
|||
if string.find(string.gsub(jb, ' ', ''), '^%s*$') then |
|||
str = str .. '2--true' |
|||
⚫ | |||
str = str .. '2--false' |
|||
end |
|||
end |
end |
||
tractate = template_args[2] |
tractate = template_args[2] |
Revision as of 10:02, 16 May 2019
Implements {{Cite Talmud}}
function inspect(str)
str = tostring(str)
local r = tostring(#str) .. ':"'
for i = 1, #str do
local c = string.byte(str:sub(i,i))
if c > 32 and c < 127 then
r = r .. str:sub(i,i)
else
r = r .. '&#' .. c .. ';'
end
end
r = r .. '"'
return r
end
function blanks_to_nil(template_args)
for key, val in pairs(template_args) do
if string.find(string.gsub(val, ' ', ''), '^%s*$') then
template_args[key] = nil
mw.log("Replacing argument " .. inspect(key) .. " which was " .. inspect(val))
else
mw.log("Keeping argument " .. inspect(key) .. " which is " .. inspect(val))
end
end
end
local talmud = {}
talmud.jb_key = {b = "Babylonian", y = "Jerusalem"}
function talmud.generate_citation(frame)
local template_args = frame:getParent().args
-- local invoke_args = frame.args -- parameters from {{#invoke:}}
mw.log(inspect(template_args[1]))
blanks_to_nil(template_args)
mw.log(inspect(template_args[1]))
jb = template_args[1] or 'b'
if not talmud.jb_key[jb] then
str = frame:expandTemplate{ title = 'error', args = { 'First argument must be either b for Babylonian Talmud or y for Jerusalem Talmud. (Given ' .. inspect(jb) .. ')' } }
end
tractate = template_args[2]
chapter = template_args[3] -- Chapter name or number (optional)
daf = template_args[4] -- These are page or folio numbers as described at Talmud#Slavuta Talmud 1795 and Vilna Talmud 1835. Ranges are accepted, eg. 2b-4a
url = template_args['url']
if not url then
url = "https://www.sefaria.org/"
if(jb == 'y') then url = url .. 'Jerusalem_Talmud_' end
url = url .. string.gsub(tractate, ' ', '_') .. '.' .. string.gsub(daf or '2a', ' ', '_')
end
-- str is only set if there has been an error.
if not str then
str = '[[Talmud]], <abbr title="' .. talmud.jb_key[jb] .. '">' .. jb .. '.</abbr> ['
str = str .. url .. ' ' .. tractate .. ' ' .. ( daf or '' ) .. ']'
end
return str
end
return talmud