Module:Book: Difference between revisions

From Eli's Software Encyclopedia
mNo edit summary
mNo edit summary
Line 31: Line 31:
end
end


local function addLabelValue(row, label, value)
local function addLabelValue(label, value)
   if not value or value == '' then return end  
   if not value or value == '' then return end  
   row:tag('td'):wikitext(label):done()
   local tr = mw.html.create('tr')
      :tag('td'):wikitext(value):done()
tr:tag('td'):wikitext(label)
   :done()
    tr:tag('td'):wikitext(value)
   return tr
end
end


Line 85: Line 86:
-- Build infobox
-- Build infobox
local html = mw.html.create()
local html = mw.html.create()
local infobox = html:tag('table'):addClass(infoboxClass):css('font-size', '90%'):attr('cellpadding', '0'):attr('cellspacing', '0')
local infobox = html:tag('table'):addClass(infoboxClass)
local dataRow = infobox:tag('tr').create()
:css('font-size', '90%')
:attr('cellpadding', '0')
:attr('cellspacing', '0')
local dataRow = infobox:tag('tr')
dataRow:tag('td'):attr('colspan', '2')
dataRow:tag('td'):attr('colspan', '2')
:tag('div'):addClass('summary text-center font-italic'):css('font-size', '110%')
:tag('div'):addClass('summary text-center font-italic'):css('font-size', '110%')
Line 102: Line 106:
end
end
addLabelValue(dataRow, 'Title', booktitle)
local function appendIfExists(label, value)
    local tr = addLabelValue(label, value)
    if tr then infobox:node(tr) end
end
appendIfExists('Title', booktitle)
if authors ~= '' then
if authors ~= '' then
authorRow(buildAuthorOutput(authors))
infobox:node(authorRow(buildAuthorOutput(authors)))
end
end
addLabelValue(dataRow, 'Publisher', publisher)
appendIfExists('Publisher', publisher)
addLabelValue(dataRow, 'Release Date', releaseDate)
appendIfExists('Release Date', releaseDate)
addLabelValue(dataRow, 'Genre', genre)
appendIfExists('Genre', genre)
addLabelValue(dataRow, 'ISBN', isbn)
appendIfExists('ISBN', isbn)
addLabelValue(dataRow, 'LCC', lcc)
appendIfExists('LCC', lcc)
addLabelValue(dataRow, 'Format', bookformat)
appendIfExists('Format', bookformat)
addLabelValue(dataRow, 'Country', country)
appendIfExists('Country', country)
addLabelValue(dataRow, 'Language', language)
appendIfExists('Language', language)
return html
return html

Revision as of 13:30, August 19, 2025

Documentation for this module may be created at Module:Book/doc

local p = {}

local function splitAndTrim(raw)
  local t = {}
  for part in raw:gmatch("[^,]+") do
    part = part:match("^%s*(.-)%s*$")
    if part ~= '' then
      table.insert(t, part)
    end
  end
  return t
end

local function buildAuthorOutput(rawAuthors)
	local authors = splitAndTrim(rawAuthors)
	local authorLabel = (#authors > 1) and 'Authors' or 'Author'
	local linkParts = {}
	for _, name in ipairs(authors) do
    	table.insert(linkParts, string.format('[[%s]]', name))
	end
	local authorLinks = table.concat(linkParts, ', ')
	return authorLabel, authorLinks
end

local function authorRow(authorLabel, authorLinks)
  local tr = mw.html.create('tr')
    tr:tag('td'):wikitext(authorLabel):done()
    tr:tag('td'):wikitext(authorLinks):done()
	:done()
  return tr
end

local function addLabelValue(label, value)
  if not value or value == '' then return end 
  local tr = mw.html.create('tr')
	tr:tag('td'):wikitext(label)
    tr:tag('td'):wikitext(value)
  return tr
end

function p.semanticStore(frame)

	local titleObj    = mw.title.getCurrentTitle()
	local args        = frame:getParent().args or {}
	-- Get infobox helper params
	local collapsible = args['collapsible'] or 'no'
	local state       = args['state'] or 'autocollapse'
	local showImage   = args['show image'] or 'no'
	local title       = titleObj.text 
	-- Get semantic properties
	local image       = args['image'] or ''
	local imageCaption= args['caption'] or ''
	local booktitle   = args['booktitle'] or title
	local authors     = args['author'] or ''
	local publisher	  = args['publisher'] or ''
	local releaseDate = args['released'] or args['release date']  or ''
	local genre		  = args['genre']  or ''
	local isbn        = args['ISBN']  or ''
	local lcc         = args['LCC']  or ''
	local bookformat  = args['format']  or ''
	local country     = args['country']  or ''
	local language	  = args['language']  or ''
	-- Set properties
    local data = {
    	'Has Title=' .. booktitle,
    	'Author=' .. authors,
    	'+sep=,',
    	'Publisher=' .. publisher,
    	'Release Date=' .. releaseDate,
    	'Genre=' .. genre,
    	'ISBN=' .. isbn,
    	'LCC=' .. lcc,
    	'Book Format=' .. bookformat,
    	'Country=' .. country,
    	'Language=' .. language
	}
	mw.smw.set( data )
	-- Infobox variables
	local infoboxClass = 'chameleon-infobox smwtable-clean d-table w-100 w-md-30 d-md-flex float-none float-md-right border shadow-sm ml-md-4 mb-4 text-left'
	local pad = ''
	if collapsible == 'yes' then
		infoboxClass = infoboxClass .. ' collapsible ' .. state
		pad = frame:preprocess('{{pad|5em}}')
	end
	local infoboxImage = frame:preprocess('{{PAGENAME:' .. image .. '}}')
	-- Build infobox
	local html = mw.html.create()
	local infobox = html:tag('table'):addClass(infoboxClass)
		:css('font-size', '90%')
		:attr('cellpadding', '0')
		:attr('cellspacing', '0')
	local dataRow = infobox:tag('tr')
		dataRow:tag('td'):attr('colspan', '2')
			:tag('div'):addClass('summary text-center font-italic'):css('font-size', '110%')
				:wikitext(pad .. title):done()
		:done()
	if image ~= '' and showImage == 'yes' then
		dataRow:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
			:wikitext(string.format('[[File:%s|class=img-fluid|%s]]', infoboxImage, imageCaption))
			:done()
		if imageCaption ~= '' then
			dataRow:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
				:wikitext(imageCaption)
				:done()
		end
	end
	
	local function appendIfExists(label, value)
    	local tr = addLabelValue(label, value)
    	if tr then infobox:node(tr) end
	end
	
	appendIfExists('Title', booktitle)
	
	if authors ~= '' then
		infobox:node(authorRow(buildAuthorOutput(authors)))
	end
	
	appendIfExists('Publisher', publisher)
	appendIfExists('Release Date', releaseDate)
	appendIfExists('Genre', genre)
	appendIfExists('ISBN', isbn)
	appendIfExists('LCC', lcc)
	appendIfExists('Format', bookformat)
	appendIfExists('Country', country)
	appendIfExists('Language', language)
	
	return html
end

return p