Module:Recipe
From The Observatory
Documentation for this module may be created at Module:Recipe/doc
local p = {}
local function getRecipeDisplayTitle()
local fullPagename = mw.title.getCurrentTitle().text
local lastPart = mw.text.split(fullPagename, "/")
lastPart = lastPart[#lastPart]
-- Split by '.' and take the first part
local exploded = mw.text.split(lastPart, "%.")
local firstPart = exploded[1]
-- Calculate the length of the first part and add 1
local len = #firstPart + 2
-- Extract a substring starting from the calculated position
local displayTitle = string.sub(lastPart, len)
return displayTitle
end
function p.main(frame)
local template = frame:getParent().args
local rcategory, rcuisine, rdescription, rkeywords, rname, rnumber, rsource, sourcename, sourcesimple
local ryield, rnotes, rtime, rlayout, ringredients, rutensils
rcategory = template['Recipe category'] or ''
rcuisine = template['Recipe cuisine'] or ''
rdescription = template['Recipe description'] or ''
rkeywords = template['Recipe keyword'] or ''
local tkeywords = {}
if rkeywords ~= '' then
tkeywords = mw.text.split( rkeywords, ',' )
end
rname = template['Recipe name'] or ''
rnumber = template['Recipe number in source'] or ''
rsource = template['Recipe source']
sourcesimple = frame:preprocess('{{PAGENAME:' .. rsource .. '}}')
sourcename = 'Classics:' .. sourcesimple
sourceroot = frame:preprocess('{{PAGENAME:{{ROOTPAGENAME:' .. rsource .. '}}}}')
ryield = template['Recipe yield'] or ''
rnotes = template['Recipe notes'] or ''
rtime = template['Preparation time'] or ''
rdisplaytime = template['Display preparation time'] or 'No'
rlayout = template['Ingredient set layout'] or ''
ringredients = template['Recipe ingredients'] or ''
local tingredients = {}
if ringrediens ~= '' then
tingredients = mw.text.split( ringredients, '\n' )
end
rutensils = template['Recipe utensils'] or ''
local tutensils = {}
if rutensils ~= '' then
tutensils = mw.text.split( rutensils, '\n' )
end
local ingredientset = template['Ingredient set'] or ''
local recipeinstructions = template['Recipe instructions'] or ''
local usinginstructions = template['Using instructions'] or ''
local recipedata = {
'Recipe category=' .. rcategory,
'Recipe cuisine=' .. rcuisine,
'Recipe description=' .. rdescription,
'Recipe keyword=' .. table.concat(tkeywords, ','),
'+sep=,',
'Recipe source=' .. sourcename,
'Recipe name=' .. rname,
'Recipe number in source=' .. rnumber,
'Recipe yield=' .. ryield,
'Recipe notes=' .. rnotes,
'Preparation time=' .. rtime,
'Ingredient set layout=' .. rlayout,
'Ingredient=' .. table.concat(tingredients, ';'),
'+sep=;',
'Utensil=' .. table.concat(tutensils, ';'),
'+sep=;',
}
mw.smw.set( recipedata )
local category = '[[Category:Recipes]]'
frame:preprocess('{{DISPLAYTITLE:' .. getRecipeDisplayTitle() .. '}}')
local html = mw.html.create()
html:tag('div')
:addClass('d-none')
:wikitext(category)
:wikitext(ingredientset)
:wikitext(recipeinstructions)
:wikitext(usinginstructions)
local counters = {
'setNumber',
'ingNumber',
'stepno',
'stepno2'
}
for _, counter in ipairs(counters) do
frame:callParserFunction('#counter',
'type=numeral',
'set=0'
)
end
local parentsub = mw.smw.ask {
'[[Is part of::' .. sourcename .. ']]' ..
'[[Min::<' .. rnumber .. ']]' ..
'[[Max::>' .. rnumber .. ']]',
'mainlabel=-',
'?#-=parent',
'limit=1',
'named args=yes'
} or {}
local preparent, parent, preparenttext, parenttext
for _, pp in ipairs(parentsub) do
preparent = mw.text.split(pp.parent, '#')
parent = preparent[1]
preparenttext = mw.text.split(parent, '/')
parenttext = preparenttext[#preparenttext]
end
if not parent then
parent = ''
parenttext = ''
weight = 0
end
local secweight = mw.smw.ask {
'[[' .. parent .. ']]',
'mainlabel=-',
'?Has weight'
} or {}
local weight
for _, pp in ipairs(secweight) do
weight = pp['Has weight'] or 0
end
local subpage = mw.title.getCurrentTitle().subpageText
local lasttitlepart = subpage or ''
local checkname = string.format('%s. %s'
, rnumber
, rname
)
local previous = mw.smw.ask {
'[[Category:Parts]]' ..
'[[Is subpage of::' .. sourceroot .. ']]' ..
'[[Has weight::<=' .. weight .. ']]',
'mainlabel=-',
'?#-=page',
'sort=Has weight',
'order=desc',
'limit=1'
} or {}
local prev_page = tostring(previous.page)
if lasttitlepart == checkname then
html:tag('div')
:attr('id', 'inpagenav')
:addClass('d-flex flex-column flex-md-row justify-content-between rounded-0 interface small py-3 align-items-center text-center')
:tag('span')
:addClass('prev font-weight-bold')
:wikitext(string.format('[[%s|%s]]'
, parent
, parenttext
))
:done()
:tag('span')
:addClass('parent')
:wikitext(string.format('[[%s|%s]]', sourcename, sourcesimple))
:done()
end
if rdescription ~= '' then
html:tag('div')
:wikitext(rdescription)
end
if rlayout == 'Multiple sets' then
html:tag('dl')
:tag('dt')
:wikitext('Ingredients')
frame:callParserFunction( '#ask',
'[[-Has subobject::Recipe:' .. sourcename .. '/' .. checkname .. ']]',
'?Ingredient set header',
'?Ingredient',
'sort=Ingredient set number',
'order=asc',
'format=outline',
'outlineproperties=Ingredient set header',
'template=Ingredient',
'named args=yes',
'headers=hide',
'mainlabel=-'
)
elseif ringredients ~= '' and rlayout ~= 'Multiple sets' then
html:tag('dl')
:tag('dt')
:wikitext('Ingredients')
:done()
:done()
local listingredients = html:tag('ul')
for _, ing in ipairs(tingredients) do
listingredients:tag('li')
:wikitext(ing)
end
end
if rutensils ~= '' then
html:tag('dl')
:tag('dt')
:wikitext('Utensils')
:done()
:done()
local listutensils = html:tag('ul')
for _, utl in ipairs(tutensils) do
listutensils:tag('li')
:wikitext(utl)
end
end
local steps = mw.smw.ask {
'[[-Has subobject::Recipe:' .. sourcename .. '/' .. checkname .. ']]',
'?Howto step description',
'?Howto steps heading',
'?Howto step number auto',
'sort=Howto step number auto',
'mainlabel=-',
'named args=yes'
} or {}
if #steps > 0 then
for index, step in ipairs(steps) do
if step['Howto steps heading'] and step['Howto steps heading'] ~= '' then
html:tag('dl')
:tag('dt')
:wikitext(step['Howto steps heading'])
:done()
:done()
end
html:tag('div')
:addClass('d-flex pl-3 mb-1')
:css({
['gap'] = '.25rem'
})
:tag('div')
:wikitext(index .. '.')
:done()
:tag('div')
:wikitext(step['Howto step description'])
-- end
end
end
if rnotes ~= '' then
html:tag('dl')
:tag('dt')
:wikitext('Remarks')
:done()
:done()
html:tag('div')
:wikitext(rnotes)
end
if rdisplattime == 'Yes' and rtime ~= '' then
html:tag('dl')
:tag('dt')
:wikitext('Preparation time')
:done()
:done()
html:tag('ul')
:tag('li')
:wikitext(rtime)
end
local footer = mw.smw.ask {
'[[' .. sourcename .. ']]',
'mainlabel=-',
'?Credit line',
'?Is altered',
'?URL of the source#'
} or {}
if lasttitlepart == checkname then
local printfooter = html:tag('div')
:addClass('text-muted mt-4 border-top pt-3 small interface')
for _, item in ipairs(footer) do
local creditline = item['Credit line'] or ''
local isaltered = item['Is altered'] or false
local rurl = item['URL of the source'] or ''
if creditline ~= '' then
printfooter:tag('div')
:wikitext(creditline)
end
if isaltered == true then
local encodedSource = frame:preprocess('{{urlencode:' .. rurl .. '|WIKI}}')
local referSource = ''
if rurl ~= '' then
referSource = string.format('%s [%s %s].'
, 'To find the original source material as it was originally written and/or formatted, please '
, encodedSource
, ' click here'
)
end
printfooter:tag('div')
:attr('id', 'is_altered')
:addClass('text-muted mt-3 interface')
:wikitext('Please note that the original source ' ..
'material has been altered by a human editor or was ' ..
'human-edited with AI-assist to be easier to read ' ..
'and search for on the Observatory. ' .. referSource)
end
end
end
return tostring(html)
end
function p.test()
local parentsub = mw.smw.ask {
'[[Is part of::Classics:The Plain Sailing Cook Book]]' ..
'[[Min::<550]]' ..
'[[Max::>550]]',
'mainlabel=-',
'?#-=parent',
'limit=1',
'named args=yes'
} or {}
for _, pp in ipairs(parentsub) do
preparent = mw.text.split(pp.parent, '#')
parent = preparent[1]
end
return parent
end
return p