Module:entityTemplate
From Rhizome Artbase
Documentation for this module may be created at Module:entityTemplate/doc
local p = {}
function p.link(label, target)
local target_page = mw.title.new(target)
if target_page.exists then
return string.format('[[%s|%s]]', target, mw.text.nowiki(label))
else
return mw.text.nowiki(label)
end
end
p.HTML = {
no_data = '<span class="text-muted font-italic">no data</span>',
none = '<span class="text-muted font-italic">none</span>',
unknown = '<span class="text-muted font-italic">unknown</span>',
instance_of_links = '<div class="artbase-instance-of-links pb-2">$links</div>',
instance_of_link = '<a class="artbase-instance-of-link badge badge-pill badge-light" href="$target">$label</a>',
instance_of_pill = '<a class="artbase-instance-of-link badge badge-pill badge-light" title="$descr">$label</a>',
edit_link = '<a class="artbase-edit-link artbase-edit badge badge-pill badge-secondary" href="$target">edit</a>',
data_line = '<div class="artbase-data-line artbase-property-$pid"><label>$key:</label> <span class="artbase-value">$value</span></div>',
active_period = '<div class="artbase-data-line artbase-property-active-period"><label>Active period:</label> <span class="artbase-value"><span class="artbase-property-P26">$inception</span> — <span class="artbase-property-P83">$conclusion</span></div>',
carousel = [==[
<div id="$carousel_id" class="carousel slide" data-ride="carousel" data-interval="5000">
<div class="position-relative artbase-carousel">
<div class="carousel-inner d-flex position-absolute w-100 h-100 no-gutters" style="top:0; left:0;">$carousel_images</div>
$carousel_controls</div>
</div>]==],
carousel_image = [==[<figure class="artbase-figure carousel-item$active"><div><img src="$image_url?1980"
srcset="$image_url?width=640 640w,
$image_url?width=800 800w,
$image_url?width=1024 1024w,
$image_url?width=1280 1280w,
$image_url?width=1600 1600w,
$image_url?width=1980 1980w,
$image_url?width=2560 2560w,
$image_url?width=4000 4000w"
sizes="(max-width:768px) 100vw,
(max-width:1200px) 66vw,
75vw"></div></figure>]==],
carousel_image_gif = [==[<figure class="artbase-figure carousel-item$active"><div><img src="$image_url"></div></figure>]==],
carousel_controls = [==[<a class="carousel-control-prev" href="#$carousel_id" role="button" data-slide="prev"><img src="/w/resources/assets/artbase/icons/carousel-control-left.svg" class="carousel-control-prev-icon" aria-hidden="true"><span class="sr-only">Previous</span></a><a class="carousel-control-next" href="#$carousel_id" role="button" data-slide="next"><img src="/w/resources/assets/artbase/icons/carousel-control-left.svg" class="carousel-control-next-icon" aria-hidden="true"><span class="sr-only">Next</span></a>]==],
-- Artwork Page
artwork_0 = [==[
<div class="artbase-entity artbase-entity-$qid">
$instance_of
<div class="artbase-credits lead">$credits</div>
<div class="row mt-6 mb-4">
<div class="col-lg-9 col-md-8">$carousel</div>
<div class="col-lg-3 col-md-4 mt-4 align-middle align-self-center">$variant_links</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6">
<div class="accordion" id="AccordionDescription">
<div class="card border-bottom mb-2 rounded">
<div class="card-header h2" id="AccordionDescriptionHeader" data-toggle="collapse" data-target="#AccordionDescriptionBody" aria-expanded="false" aria-controls="AccordionDescriptionBody"><div class="fas fa-angle-down"></div>Description</div>
<div id="AccordionDescriptionBody" class="collapse" aria-labelledby="AccordionDescriptionHeader" data-parent="#AccordionDescription">
<div class="artbase-description card-body pt-2 pb-0">
$description$legacy_tags
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6">
<div class="accordion" id="AccordionMetadata">
<div class="card border-bottom mb-2 rounded">
<div class="card-header h2" id="AccordionMetadataHeader" data-toggle="collapse" data-target="#AccordionMetadataBody" aria-expanded="true" aria-controls="AccordionMetadataBody"><div class="fas fa-angle-down"></div>Metadata</div>
<div id="AccordionMetadataBody" class="collapse" aria-labelledby="AccordionMetadataHeader" data-parent="#AccordionMetadata">
<div class="card-body">$metadata</div>
</div>
</div>
</div>
</div>
</div>
</div>
]==],
metadata_descriptive = [==[
<div class="mb-4 artbase-metadata-descriptive">
<div class="h3">Descriptive Data</div>
$data_lines
</div>
]==],
variant_history = [==[
<div class="mb-4 mt-4 artbase-variant-history">
<div class="h3">Variant History</div>
$variants
</div>
]==],
variant_1_headline = [==[
<div class="h3 pb-2 pt-3 border-top">Variant History</div>
]==],
artwork_1 = [==[
<div class="artbase-artwork-1 col-sm-6 col-lg-4 col-xl-3 col-xxl-2"><a href="$artwork_page_url">
<div><img src="$image_url" loading="lazy">
<div class="row p-3">
<div class="artbase-artwork-title col-9">$label</div>
<div class="artbase-inception col-3">$inception</div>
</div>
<div class="row pl-3 pr-3 pb-3">
<div class="col-12">$artist</div>
</div>
</div></a>
</div>
]==],
----------------------------------------------------------------------------
variant_1 = [==[
<div class="accordion artbase-instance-of-$instance_of artbase-variant-1" id="AccordionVariant$variant_id">
<div class="card border-bottom mb-2 rounded">
<div class="card-header h4" id="AccordionVariantHeader$variant_id" data-toggle="collapse" data-target="#AccordionVariantBody$variant_id" aria-expanded="false" aria-controls="AccordionVariantBody$variant_id"><div class="fas fa-angle-down"></div>$type_of_artifact</div>
<div id="AccordionVariantBody$variant_id" class="collapse" aria-labelledby="AccordionVariantHeader$variant_id" data-parent="#AccordionVariant$variant_id">
<div class="card-body pt-2">
$data_lines
</div>
</div>
</div>
</div>
]==],
variant_2 = [==[<a class="artbase-instance-of-$instance_of artbase-variant-2" href="$access_url"><span class="btn btn-lg btn-block btn-light btn-link border text-left">$type_of_artifact</span></a>]==],
metadata_administrative = [==[
<div class="artbase-metadata-administrative">
<div class="h3">Administrative Data</div>
$data_lines
</div>
]==],
----------------------------------------------------------------------------
person_0 = [==[
<div class="artbase-entity artbase-entity-$qid">
$instance_of
<div class="artbase-person-data pt-2 pb-4 lead">
$data_lines
</div>
$artworks
</div>
]==],
person_0_artworks = [==[
<div class="h2">Artworks</div>
<div class="row">
$artworks
</div>
]==],
----------------------------------------------------------------------------
collective_0 = [==[
<div class="artbase-entity artbase-entity-$qid">
$instance_of
<div class="artbase-collective-data pt-2 pb-4 lead">
$data_lines
</div>
<div class="h2">Artworks</div>
<div class="row">
$artworks
</div>
</div>
]==],
collective_1 = [==[<span class="artbase-collective">$collective_link$members</span>]==],
----------------------------------------------------------------------------
description_1 = [==[
<div class="mb-4 pt-2 artbase-description-1">
$instance_of_links
<div class="mb-2">
$document
</div>
<div class="small mx-4 text-black-50">
$data_lines
</div>
</div>
]==],
summary_1 = [==[
<div class="mb-4 pt-2 artbase-summary-1">
$instance_of_links
<div class="mb-2 lead">
$document
</div>
<div class="small mx-4 text-black-50">
$data_lines
</div>
</div>
]==],
legacy_tags = [==[
<div class=" pb-2 pt-4 artbase-legacy-tags">
<div class="h3">Legacy descriptive tags</div>
$tags
</div>
]==],
legacy_tags_1 = [==[
<div class="mb-4">
<div class="mb-2">$tags</div>
<div class="small mx-4 text-black-50">
<div>Attribution: $attributed_to</div>
</div>
</div>
]==]
}
function p.render(template_name, context)
local template = p.HTML[template_name]
-- order context by key length, with longest first,
-- so that the primitive substitution works regardless
-- of key names
local keys = {}
for k,v in pairs(context) do
table.insert(keys, k)
end
table.sort(keys, function(a,b) return #a>#b end)
for i,k in ipairs(keys) do
local replacement_str = tostring(context[k])
-- make sure that % is escaped by doubling it
replacement_str = replacement_str:gsub('%%', '%%%%')
template = template:gsub('%$'..k, replacement_str)
end
return template
end
return p