Module:Book: Difference between revisions

From Eli's Software Encyclopedia
mNo edit summary
mNo edit summary
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
local utils = require('Module:Utils')
local p = {}
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')
    :add{ mw.html.create('td'):wikitext(authorLabel) }
    :add{ mw.html.create('td'):wikitext(authorLinks) }
  return tr:plain()
end


function p.semanticStore(frame)
function p.semanticStore(frame)
Line 35: Line 7:
local titleObj    = mw.title.getCurrentTitle()
local titleObj    = mw.title.getCurrentTitle()
local args        = frame:getParent().args or {}
local args        = frame:getParent().args or {}
-- Get infobox helper params
-- Get infobox helper params
local collapsible = args['collapsible'] or 'no'
local collapsible = args['collapsible'] or 'no'
Line 40: Line 13:
local showImage  = args['show image'] or 'no'
local showImage  = args['show image'] or 'no'
local title      = titleObj.text  
local title      = titleObj.text  
-- Get semantic properties
-- Get semantic properties
local image      = args['image'] or ''
local image      = args['image'] or ''
Line 53: Line 27:
local country    = args['country']  or ''
local country    = args['country']  or ''
local language   = args['language']  or ''
local language   = args['language']  or ''
local imageFilename, imageAlt, imageCaption = '', '', ''
if image ~= '' then
local fn, alt = utils.parseFileLink(image)
if fn then
    imageFilename = fn
        imageAlt  = alt or imageCaption or ''
        else
        imageFilename = frame:preprocess('{{PAGENAME:' .. image .. '}}')
    end
end
-- Set properties
-- Set properties
     local data = {
     local data = {
Line 58: Line 44:
     'Author=' .. authors,
     'Author=' .. authors,
     '+sep=,',
     '+sep=,',
     'Publisher=' .. publisher,
     'Publisher name=' .. publisher,
     'Release Date=' .. releaseDate,
     'Released Date=' .. releaseDate,
     'Genre=' .. genre,
     'Genre=' .. genre,
     'ISBN=' .. isbn,
     'ISBN=' .. isbn,
Line 65: Line 51:
     'Book Format=' .. bookformat,
     'Book Format=' .. bookformat,
     'Country=' .. country,
     'Country=' .. country,
     'Language=' .. language
     'Language=' .. language,
    'Has Image=' .. imageFilename,
    'Has Image Caption=' .. imageCaption
}
}
mw.smw.set( data )
mw.smw.set( data )
-- Infobox variables
-- 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 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'
Line 75: Line 64:
pad = frame:preprocess('{{pad|5em}}')
pad = frame:preprocess('{{pad|5em}}')
end
end
local infoboxImage = frame:preprocess('{{PAGENAME:' .. image .. '}}')
-- 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 row = infobox:tag('tr')
    :css('font-size', '90%')
row:tag('td'):attr('colspan', '2')
:attr('cellpadding', '0')
:tag('div'):addClass('summary text-center font-italic'):css('font-size', '110%')
:attr('cellspacing', '0')
:wikitext(pad .. title)
-- Header row (title + optional image)
local headerTr = mw.html.create('tr')
headerTr:tag('td'):attr('colspan', '2')
      :tag('div'):addClass('summary text-center font-italic font-weight-bold')
:css('font-size', '110%')
:wikitext(pad .. title):done()
infobox:node(headerTr)
-- Image and caption
if image ~= '' and showImage == 'yes' then
if image ~= '' and showImage == 'yes' then
row:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
local imgTr = mw.html.create('tr')
:wikitext(string.format('[[File:%s|class=img-fluid|%s]]', infoboxImage, imageCaption))
    imgTr:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
:wikitext(string.format('[[File:%s|class=img-fluid|%s]]',
imageFilename, imageCaption
))
    infobox:node(imgTr)
 
if imageCaption ~= '' then
if imageCaption ~= '' then
row:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
local capTr = mw.html.create('tr')
capTr:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
:wikitext(imageCaption)
:wikitext(imageCaption)
end
infobox:node(capTr)
    end
end
end
row:tag('td'):wikitext('Title'):done()
:tag('td'):wikitext(booktitle)
-- Print data rows
 
local function appendIfExists(label, value, link)
    local tr = utils.addLabelValue(label, value, link)
    if tr then infobox:node(tr) end
end
appendIfExists('Title', booktitle)
if authors ~= '' then
if authors ~= '' then
authorRow(buildAuthorOutput(authors))
infobox:node(utils.authorRow(utils.buildAuthorOutput(authors, 'Author', 'Authors')))
end
end
if publisher ~= '' then
row:tag('td'):wikitext('Publisher'):done()
appendIfExists('Publisher', publisher)
:tag('td'):wikitext(publisher)
appendIfExists('Release Date', releaseDate)
end
appendIfExists('Genre', genre)
if releaseDate ~= '' then
appendIfExists('ISBN', isbn)
row:tag('td'):wikitext('Release Date'):done()
appendIfExists('LCC', lcc)
:tag('td'):wikitext(releaseDate)
appendIfExists('Format', bookformat)
end
appendIfExists('Country', country)
if genre ~= '' then
appendIfExists('Language', language)
row:tag('td'):wikitext('Genre'):done()
:tag('td'):wikitext(genre)
end
if isbn ~= '' then
row:tag('td'):wikitext('ISBN'):done()
:tag('td'):wikitext(isbn)
end
if lcc ~= '' then
row:tag('td'):wikitext('LCC'):done()
:tag('td'):wikitext(lcc)
end
if bookformat ~= '' then
row:tag('td'):wikitext('Format'):done()
:tag('td'):wikitext(bookformat)
end
if country ~= '' then
row:tag('td'):wikitext('Country'):done()
:tag('td'):wikitext(country)
end
if language ~= '' then
row:tag('td'):wikitext('Language'):done()
:tag('td'):wikitext(language)
end
 
return html
return html
end
end


return p
return p

Latest revision as of 20:52, December 10, 2025

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

local utils = require('Module:Utils')

local p = {}

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 ''
	
	local imageFilename, imageAlt, imageCaption = '', '', ''
	if image ~= '' then
		local fn, alt = utils.parseFileLink(image)
		if fn then
    		imageFilename = fn
        	imageAlt  = alt or imageCaption or ''
        else
        	imageFilename = frame:preprocess('{{PAGENAME:' .. image .. '}}')
    	end
	end
	
	-- Set properties
    local data = {
    	'Has Title=' .. booktitle,
    	'Author=' .. authors,
    	'+sep=,',
    	'Publisher name=' .. publisher,
    	'Released Date=' .. releaseDate,
    	'Genre=' .. genre,
    	'ISBN=' .. isbn,
    	'LCC=' .. lcc,
    	'Book Format=' .. bookformat,
    	'Country=' .. country,
    	'Language=' .. language,
    	'Has Image=' .. imageFilename,
    	'Has Image Caption=' .. imageCaption
	}
	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
	-- Build infobox
	local html   = mw.html.create()
	local infobox = html:tag('table'):addClass(infoboxClass)
    	:css('font-size', '90%')
		:attr('cellpadding', '0')
		:attr('cellspacing', '0')
		
	-- Header row (title + optional image)
	local headerTr = mw.html.create('tr')
	headerTr:tag('td'):attr('colspan', '2')
       	:tag('div'):addClass('summary text-center font-italic font-weight-bold')
			:css('font-size', '110%')
			:wikitext(pad .. title):done()
		infobox:node(headerTr)
		
	-- Image and caption
	if image ~= '' and showImage == 'yes' then
		local imgTr = mw.html.create('tr')
    	imgTr:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
			:wikitext(string.format('[[File:%s|class=img-fluid|%s]]',
				imageFilename, imageCaption
			))
    	infobox:node(imgTr)

		if imageCaption ~= '' then
			local capTr = mw.html.create('tr')
			capTr:tag('td'):attr('colspan', '2'):addClass('text-center px-0')
				:wikitext(imageCaption)
			infobox:node(capTr)
    	end
	end
	
	-- Print data rows
	local function appendIfExists(label, value, link)
    	local tr = utils.addLabelValue(label, value, link)
    	if tr then infobox:node(tr) end
	end
	
	appendIfExists('Title', booktitle)
	
	if authors ~= '' then
		infobox:node(utils.authorRow(utils.buildAuthorOutput(authors, 'Author', '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