This module is for making links to D&D books. It is usually invoked via the following templates:
- {{book}}, which creates a wikilink to a book
- {{cite book}}, which creates a <ref> citation to a book
Data format[]
Data for practically every first-party D&D sourcebook appears in Module:Book/data, which is held in a Lua table. Take great care when adding a new entry, as a syntax error may break citations across the entire wiki. Here's an example entry in the database:
["Queen of the Spiders"] = { year="1986", abbr="gdq17-1986", code="9179", series_code="GDQ1-7", isbn="0-88038-321-6" },
The fields are as follows:
- Key: The official name of the book.
- year: The year the book was first published. Don't put the whole date here; just the four-digit year.
- abbr: An internal code used in the <ref name=""> field. It can be anything, but it must be unique in the list, and must contain at least one non-numeric character. Usually, it's the book's initials followed by the year, e.g. dmg-2014.
- code: TSR or WotC's official four-digit or five-digit product code for the book. Sometimes found prefixed with "TSR" or "WTC", and/or suffixed with "0000", "7200", or "7400". The code isn't currently used by the book function, but should be included to help uniquely identify the book. You should verify this with a physical book; codes ending with "?" have not been found by online sources and not independently verified yet. The code can be "none" (if it has none) or "unknown" (if it is unknown whether it has one).
- series_code: (optional) An official TSR/WotC code generally used for adventure module series, e.g. "G3", "T1-4".
- isbn: (optional) ISBN for the book. Currently not used by the book function, but may be use in future.
Instead of these fields, an entry may have an alias_for entry like so:
["The World of Greyhawk Folio"] = { alias_for = "The World of Greyhawk Fantasy World Setting"},
- alias_for: This name is a synonym for another book. Any time this name is encountered, it is corrected to its alias_for entry. This is used for common misspellings, e.g. omitting "The" from the name, capitalization, "and" vs "&", "2" vs "II" vs "Two", etc.
Magazines don't appear in this list (see e.g. {{DragonMagazine}}), but magazine annuals do.
local p = {}
local bookData = mw.loadData( 'Module:Book/data' )
function p.Book( frame )
local title = frame.args["title"] or "default"
local page = frame.args["page"] or ""
local sub = frame.args["sub"] or ""
local isbn = frame.args["isbn"] or ""
local year = ""
local abbr = "unknown"
local series_code = ""
if bookData[title] then
if bookData[title]["alias_for"] then
title = bookData[title]["alias_for"] or ""
end
year = bookData[title]["year"] or ""
abbr = bookData[title]["abbr"] or "unknown"
series_code = bookData[title]["series_code"] or ""
end
if not (series_code == "") then
series_code = series_code .. " "
end
if not (abbr == "unknown") then
title = "[[" .. title .. "]]"
else
title = "''" .. title .. "''{{UnknownBook}}[[Category:Articles referencing unknown books]]"
end
if not (sub == "") then
sub = ", ''" .. sub .. "''"
end
if not (page == "") then
page = ", p." .. page
end
if not (isbn == "") then
isbn = ". ISBN " .. isbn
end
if not (year == "") then
year = " (" .. year .. ")"
end
return ( series_code .. title .. sub .. year .. page .. isbn)
end
function p.RefID ( frame )
local title = frame.args["title"] or "default"
local page = frame.args["page"] or ""
local sub = frame.args["sub"] or ""
local abbr = "unknown"
local out = ""
if bookData[title] then
if bookData[title]["alias_for"] then
title = bookData[title]["alias_for"] or ""
end
abbr = bookData[title]["abbr"] or "unknown"
end
if not (sub == "") then
sub = "-" .. string.gsub( string.lower(sub), "[^%l]", "" )
end
if not (page == "") then
page = "-" .. page
end
if not (abbr == "unknown") then
out = abbr .. sub .. page
else
out = title .. sub .. page
out = out:gsub('%W','')
end
return ( out )
end
function p.listAllBooks( frame )
-- create a list of all books
out = ""
sortedBookData = {}
for i,row in pairs(bookData) do
if row.abbr and row.year and row.code then
local entry = {i, row.year, row.code, row.series_code}
table.insert (sortedBookData, entry)
end
end
local function sorter(a,b)
return ( a[2]..a[1]) < ((b[2]..b[1]) )
end
table.sort(sortedBookData,sorter)
out = out .. "{|class=\"wikitable sortable\" \n! Book title !! Year !! TSR code\n|-\n"
for i,row in pairs(sortedBookData) do
out = out .. "| ".. (row[4] or "") .." [["..row[1].."]] "
-- alias handling
for ii,vv in pairs(bookData) do
if vv.alias_for and vv.alias_for == row[1] then
out = out .. "\n* alias: ''"..ii.."'' "
end
end
out = out .. "\n| "..row[2].." \n| " .. row[3] .. "\n"
out = out .. "|-\n"
end
out = out .. "|}"
return ( out )
end
return p