「モジュール:Wd」の版間の差分

en:Module:Wd 2017年5月9日 (火) 20:12(UTC)より
template>K-iczn
(en:Module:Wd 2017年5月7日 (日) 16:15(UTC)より)
template>K-iczn
(en:Module:Wd 2017年5月9日 (火) 20:12(UTC)より)
1行目: 1行目:
-- Original module located at [[:en:Module:Wd]].
local p = {}
local p = {}
local arg = ...
local i18n
function loadSubmodules(frame)
-- internationalization
if not i18n then
if frame then
i18n = require(frame:getTitle().."/i18n")
else
i18n = require(arg.."/i18n")
end
end
end


local aliasesP = {
local aliasesP = {
coord          = "P625",
coord          = "P625",
---------------
author          = "P50",
author          = "P50",
publisher      = "P123",
publisher      = "P123",
126行目: 141行目:
return stt
return stt
end
function applyStringParams(str, ...)
for i, v in ipairs(arg) do
str = mw.ustring.gsub(str, "$"..i, v)
end
return str
end
end


function unknownDataTypeError(dataType)
function unknownDataTypeError(dataType)
return "" .. dataType .. "は不明もしくは未対応のデータタイプです。"
return applyStringParams(i18n['errors']['unknown-data-type'], dataType)
end
end


function missingRequiredParameterError()
function missingRequiredParameterError()
return "必要なパラメータが定義されていません。最低限1つ必要です。"
return i18n['errors']['missing-required-parameter']
end
end


function extraRequiredParameterError(param)
function extraRequiredParameterError(param)
return "パラメータ「" .. param .. "」は任意として定義する必要があります。"
return applyStringParams(i18n['errors']['extra-required-parameter'], param)
end
end


330行目: 353行目:


function getOrdinalSuffix(num)
function getOrdinalSuffix(num)
if tostring(num):sub(-2,-2) == '1' then
return i18n.getOrdinalSuffix(num)
return "th"  -- 10th, 11th, 12th, 13th, ... 19th
end
end
 
function addDecimalMarks(num)
num = tostring(num):sub(-1)
return i18n.addDecimalMarks(num)
if num == '1' then
return ""
elseif num == '2' then
return ""
elseif num == '3' then
return ""
else
return ""
end
end
end


function addDecimalMarks(n)
-- used for cleaner output when subst:ituting this module
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
function replaceHTMLSpaces(str)
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
return mw.ustring.gsub(str, " ", " ")
end
end


604行目: 617行目:
local yFactor = 1
local yFactor = 1
local sign = 1
local sign = 1
local prefix = ""
local suffix = ""
local suffix = ""
local mayAddCalendar = false
local mayAddCalendar = false
639行目: 653行目:
if not raw then
if not raw then
if precision == 6 then
if precision == 6 then
suffix = "千年"
suffix = i18n['datetime']['suffixes']['millennium']
else
else
suffix = "世紀"
suffix = i18n['datetime']['suffixes']['century']
end
end
656行目: 670行目:
if not raw then
if not raw then
suffix = "s"
prefix = i18n['datetime']['prefixes']['decade-period']
suffix = i18n['datetime']['suffixes']['decade-period']
end
end
end
end
722行目: 737行目:
if not raw then
if not raw then
if precision == 3 then
if precision == 3 then
suffix = "百万年"
suffix = i18n['datetime']['suffixes']['million-years']
elseif precision == 0 then
elseif precision == 0 then
suffix = "億年"
suffix = i18n['datetime']['suffixes']['billion-years']
else
else
yRound = yRound * yFactor
yRound = yRound * yFactor
if yRound == 1 then
if yRound == 1 then
suffix = "年"
suffix = i18n['datetime']['suffixes']['year']
else
else
suffix = "年"
suffix = i18n['datetime']['suffixes']['years']
end
end
end
end
748行目: 763行目:
if not raw then
if not raw then
if link then
if link then
calendar = " ([[ユリウス暦]])"
calendar = " ([["..i18n['datetime']['julian-calendar'].."|"..i18n['datetime']['julian'].."]])"
else
else
calendar = " (ユリウス暦)"
calendar = " ("..i18n['datetime']['julian']..")"
end
end
else
else
calendar = "/ユリウス暦"
calendar = "/"..i18n['datetime']['julian']
end
end
end
end
762行目: 777行目:
if sign < 0 then
if sign < 0 then
ce = "BCE"
ce = i18n['datetime']['BCE']
elseif precision <= 5 then
elseif precision <= 5 then
ce = "CE"
ce = i18n['datetime']['CE']
end
end
if ce then
if ce then
if link then
if link then
ce = "[[:en:Common Era|" .. ce .. "]]"
ce = "[[" .. i18n['datetime']['common-era'] .. "|" .. ce .. "]]"
end
end
suffix = suffix .. " " .. ce
suffix = suffix .. " " .. ce
790行目: 805行目:
end
end
value = value .. suffix .. calendar
value = prefix .. value .. suffix .. calendar
else
else
value = tostring(yRound * sign)
value = tostring(yRound * sign)
811行目: 826行目:
local precision, numDigits, strFormat, value, globe
local precision, numDigits, strFormat, value, globe
local latValue, latitude, latDegrees, latMinutes, latSeconds
local latValue, latitude, latDegrees, latMinutes, latSeconds
local latDirection = "北緯"
local latDirection = i18n['coord']['latitude-north']
local lonValue, longitude, lonDegrees, lonMinutes, lonSeconds
local lonValue, longitude, lonDegrees, lonMinutes, lonSeconds
local lonDirection = "東経"
local lonDirection = i18n['coord']['longitude-east']
local degSymbol = "度"
local degSymbol = i18n['coord']['degrees']
local minSymbol = "分"
local minSymbol = i18n['coord']['minutes']
local secSymbol = ''
local secSymbol = i18n['coord']['seconds']
local partsGlue = ", "
local separator = i18n['coord']['separator']
if raw then
if raw then
824行目: 839行目:
minSymbol = "/"
minSymbol = "/"
secSymbol = "/"
secSymbol = "/"
partsGlue = "/"
separator = "/"
end
end
831行目: 846行目:
if latitude < 0 then
if latitude < 0 then
latDirection = "南緯"
latDirection = i18n['coord']['latitude-south']
latitude = math.abs(latitude)
latitude = math.abs(latitude)
end
end
if longitude < 0 then
if longitude < 0 then
lonDirection = "西経"
lonDirection = i18n['coord']['longitude-west']
longitude = math.abs(longitude)
longitude = math.abs(longitude)
end
end
887行目: 902行目:
lonValue = lonValue .. lonDirection
lonValue = lonValue .. lonDirection
value = latValue .. partsGlue .. lonValue
value = latValue .. separator .. lonValue
if link then
if link then
893行目: 908行目:
if globe then
if globe then
globe = mw.wikibase.label(globe):lower()
globe = mw.wikibase.getEntity(globe):getLabel("en"):lower()
else
else
globe = "earth"
globe = "earth"
945行目: 960行目:
return " "  -- single space represents 'somevalue'
return " "  -- single space represents 'somevalue'
else
else
return "unknown"
return i18n['values']['unknown']
end
end
elseif snak.snaktype == 'novalue' then
elseif snak.snaktype == 'novalue' then
951行目: 966行目:
return ""  -- empty string represents 'novalue'
return ""  -- empty string represents 'novalue'
else
else
return "none"
return i18n['values']['none']
end
end
else
else
1,351行目: 1,366行目:
-- logic determined based on https://www.wikidata.org/wiki/Help:Sources
-- logic determined based on https://www.wikidata.org/wiki/Help:Sources
function State:getReference(statement)
function State:getReference(statement)
local snakValue, lang, property
local snakValue, lang, property, url, title
local value = ""
local value = ""
local ref = {}
local ref = {}
1,365行目: 1,380行目:
end
end
-- don't include "imported from" that has been added by a bot
if snaks[aliasesP.importedFrom] then
if snaks[aliasesP.importedFrom] then
snaks[aliasesP.importedFrom] = nil
snaks[aliasesP.importedFrom] = nil
end
end
if snaks[aliasesP.referenceURL] and snaks[aliasesP.title] then
-- use the general template for citing web references if both URL and title are present
params["url"] = self.conf:getValue(snaks[aliasesP.referenceURL])
if snaks[aliasesP.referenceURL] and snaks[aliasesP.title] and i18n['cite']['cite-web'] and i18n['cite']['cite-web'] ~= "" then
params["title"] = self.conf:getValue(snaks[aliasesP.title], false, false, false, true)  -- anyLang = true
params[i18n['cite']['url']]   = self.conf:getValue(snaks[aliasesP.referenceURL])
params[i18n['cite']['title']] = self.conf:getValue(snaks[aliasesP.title], false, false, false, true)  -- anyLang = true
if snaks[aliasesP.publicationDate] then params["date"]        = self.conf:getValue(snaks[aliasesP.publicationDate])                  end
if snaks[aliasesP.publicationDate] then params[i18n['cite']['date']]        = self.conf:getValue(snaks[aliasesP.publicationDate])                  end
if snaks[aliasesP.retrieved]      then params["access-date"]  = self.conf:getValue(snaks[aliasesP.retrieved])                        end
if snaks[aliasesP.retrieved]      then params[i18n['cite']['access-date']]  = self.conf:getValue(snaks[aliasesP.retrieved])                        end
if snaks[aliasesP.archiveURL]      then params["archive-url"]  = self.conf:getValue(snaks[aliasesP.archiveURL])                      end
if snaks[aliasesP.archiveURL]      then params[i18n['cite']['archive-url']]  = self.conf:getValue(snaks[aliasesP.archiveURL])                      end
if snaks[aliasesP.archiveDate]    then params["archive-date"] = self.conf:getValue(snaks[aliasesP.archiveDate])                      end
if snaks[aliasesP.archiveDate]    then params[i18n['cite']['archive-date']] = self.conf:getValue(snaks[aliasesP.archiveDate])                      end
if snaks[aliasesP.author]          then params["author"]      = self.conf:getValue(snaks[aliasesP.author])                          end
if snaks[aliasesP.author]          then params[i18n['cite']['author']]      = self.conf:getValue(snaks[aliasesP.author])                          end
if snaks[aliasesP.publisher]      then params["publisher"]    = self.conf:getValue(snaks[aliasesP.publisher])                        end
if snaks[aliasesP.publisher]      then params[i18n['cite']['publisher']]    = self.conf:getValue(snaks[aliasesP.publisher])                        end
if snaks[aliasesP.quote]          then params["quote"]        = self.conf:getValue(snaks[aliasesP.quote], false, false, false, true) end  -- anyLang = true
if snaks[aliasesP.quote]          then params[i18n['cite']['quote']]        = self.conf:getValue(snaks[aliasesP.quote], false, false, false, true) end  -- anyLang = true
if snaks[aliasesP.language] then
if snaks[aliasesP.language] then
1,385行目: 1,402行目:
if self.conf.langName ~= snakValue then
if self.conf.langName ~= snakValue then
params["language"] = snakValue
params[i18n['cite']['language']] = snakValue
end
end
end
end
1,394行目: 1,411行目:
end
end
value = "{{cite_web2" .. value .. "}}"
value = "{{" .. i18n['cite']['cite-web'] .. value .. "}}"
else
else
value = mw.getCurrentFrame():expandTemplate{title="cite_web", args=params}
value = mw.getCurrentFrame():expandTemplate{title=i18n['cite']['cite-web'], args=params}
end
end
else
else
-- if no general template for citing web references was defined but URL and title are present, add these together
if snaks[aliasesP.referenceURL] and snaks[aliasesP.title] then
url  = self.conf:getValue(snaks[aliasesP.referenceURL])
title = self.conf:getValue(snaks[aliasesP.title], false, false, false, true)
leadParams[#leadParams + 1] = "[" .. url .. " " .. title .. "]"
-- set to nil so that they won't be added a second time
snaks[aliasesP.referenceURL] = nil
snaks[aliasesP.title] = nil
end
for i, v in pairs(snaks) do
for i, v in pairs(snaks) do
property = getLabel(i)
property = getLabel(i)
1,539行目: 1,568行目:


function p.property(frame)
function p.property(frame)
loadSubmodules(frame)
return p._property(copyTable(frame.args))
return p._property(copyTable(frame.args))
end
end


function p._property(args)
function p._property(args)
loadSubmodules()
return execCommand(args, "property")
return execCommand(args, "property")
end
end


function p.properties(frame)
function p.properties(frame)
loadSubmodules(frame)
return p._properties(copyTable(frame.args))
return p._properties(copyTable(frame.args))
end
end


function p._properties(args)
function p._properties(args)
loadSubmodules()
return execCommand(args, "properties")
return execCommand(args, "properties")
end
end


function p.qualifier(frame)
function p.qualifier(frame)
loadSubmodules(frame)
return p._qualifier(copyTable(frame.args))
return p._qualifier(copyTable(frame.args))
end
end


function p._qualifier(args)
function p._qualifier(args)
loadSubmodules()
return execCommand(args, "qualifier")
return execCommand(args, "qualifier")
end
end


function p.qualifiers(frame)
function p.qualifiers(frame)
loadSubmodules(frame)
return p._qualifiers(copyTable(frame.args))
return p._qualifiers(copyTable(frame.args))
end
end


function p._qualifiers(args)
function p._qualifiers(args)
loadSubmodules()
return execCommand(args, "qualifiers")
return execCommand(args, "qualifiers")
end
end


function p.reference(frame)
function p.reference(frame)
loadSubmodules(frame)
return p._reference(copyTable(frame.args))
return p._reference(copyTable(frame.args))
end
end


function p._reference(args)
function p._reference(args)
loadSubmodules()
return execCommand(args, "reference")
return execCommand(args, "reference")
end
end


function p.references(frame)
function p.references(frame)
loadSubmodules(frame)
return p._references(copyTable(frame.args))
return p._references(copyTable(frame.args))
end
end


function p._references(args)
function p._references(args)
loadSubmodules()
return execCommand(args, "references")
return execCommand(args, "references")
end
end
1,764行目: 1,805行目:


function p.label(frame)
function p.label(frame)
loadSubmodules(frame)
return p._label(copyTable(frame.args))
return p._label(copyTable(frame.args))
end
end
1,770行目: 1,812行目:
_ = _ or Config.new()
_ = _ or Config.new()
_.curState = State.new(_)
_.curState = State.new(_)
loadSubmodules()
local ID = nil
local ID = nil
1,875行目: 1,919行目:


function p.title(frame)
function p.title(frame)
loadSubmodules(frame)
return p._title(copyTable(frame.args))
return p._title(copyTable(frame.args))
end
end
1,881行目: 1,926行目:
_ = _ or Config.new()
_ = _ or Config.new()
_.pageTitle = true
_.pageTitle = true
-- loadSubmodules() will already be called by _label()
return p._label(args, _)
return p._label(args, _)
end
end
1,887行目: 1,933行目:
function p.main(frame)
function p.main(frame)
local f, args, i, v
local f, args, i, v
loadSubmodules(frame)
-- get the parent frame to take the arguments that were passed to the wrapper template
-- get the parent frame to take the arguments that were passed to the wrapper template
frame = frame:getParent() or frame
frame = frame:getParent() or frame
f = mw.text.trim(frame.args[1] or "")
if not frame.args[1] then
error(i18n["errors"]["no-function-specified"])
end
f = mw.text.trim(frame.args[1])
if f == "main" then
if f == "main" then
error('ファンクション"main"は2度も呼び出せません。')
error(i18n["errors"]["main-called-twice"])
end
end
assert(p["_"..f], 'ファンクション"' .. f .. '"が存在しません。')
assert(p["_"..f], applyStringParams(i18n['errors']['no-such-function'], f))
-- copy arguments from immutable to mutable table
-- copy arguments from immutable to mutable table
匿名利用者