模块:作者 中华文库
模块文档[查看] [] [] []
{{作者 |name=孔子 |birthyear=-551 |deathyear=-479 |firstletter=K |wikipedia=孔子 |zh-classical-wikipedia=孔子 |wikiquote=孔子 |commons=Kŏng Zĭ |notes=[[史記/卷47|史記·孔子世家]] |image=Confucius 02.png(作者的图像。若未填写此项,默认从维基数据获得图像) |image_caption=(我们可以通过指定交替文本来改变它。) |country=(若非中國,請填入。澳門、香港、臺灣,分別自1557年、1842年、1895年起,亦同。) |times=(如果生卒年均不知道,但知道朝代填。X朝即可。) }}
- 各参数含义参见Template:Header。
- Template:Author重定向于此
使用Lua语言编写: |
本模块使用维基数据资源:
|
该模板曾经使用维基文本创建,后来因代码过于复杂,于2018年12月21日 (五) 改为Lua语言模块,原来的模板存档,见Template:作者/存档。
作者
参数 | 描述 | 类型 | 状态 | |
---|---|---|---|---|
姓名 | name Name | 若未添加,会将页面名作为作者姓名。 | 未知 | 推荐 |
拼音首字母 | firstletter FirstLetterLastName | 若未添加,从维基数据的姓氏英文标签的首字母获取此项。 | 未知 | 推荐 |
出生年份 | birthyear | 若未添加,从维基数据获取此项。 | 未知 | 可选 |
卒年 | deathyear | 若未添加,从维基数据获取此项。模块会假设最近100年内出生的人物仍然在世,也可以用deathyear=-明确声明在世人物。 | 未知 | 可选 |
朝代 | times | 无描述 | 未知 | 可选 |
简要说明 | notes MiscBio | 若未添加,从维基数据获取此项。 | 未知 | 可选 |
维基百科页面名 | wikipedia | 若未添加,从维基数据获取此项。 | 未知 | 可选 |
维基大典页面名 | zh-classical-wikipedia | 若未添加,从维基数据获取此项。 | 未知 | 弃用 |
维基语录页面名 | wikiquote | 若未添加,从维基数据获取此项。 | 未知 | 弃用 |
维基共享资源分类名 | commons | 若未添加,从维基数据获取此项。 | 未知 | 弃用 |
照片或画像 | image | 作者的图像。若未填写此项,默认从维基数据获得图像。 | 未知 | 可选 |
交替文 | image_caption | 无描述 | 未知 | 可选 |
国籍 | country | 无描述 | 未知 | 可选 |
sex | sex | 无描述 | 未知 | 可选 |
Dates | Dates | 无描述 | 未知 | 弃用 |
上述文档嵌入自Module:作者/doc。 ( | ) 编者可以在本模板的沙盒 ( | )和测试样例 ()页面进行实验。 本模块的子页面。 |
local p = {};
local timesTable = {
['秦'] = '秦朝',
['汉'] = '漢朝',
['漢'] = '漢朝',
['晋'] = '晉朝',
['晉'] = '晉朝',
['隋'] = '隋朝',
['唐'] = '唐朝',
['宋'] = '宋朝',
['金'] = '金朝',
['元'] = '元朝',
['明'] = '明朝',
['清'] = '清朝',
['阮'] = '阮朝'
}
local ChinaCountry = {['cn']=1,['china']=1,['China']=1,['中国']=1,['中國']=1}
local function addCategoryIf(text, condition, category)
local text2 = text
if condition and category ~= '' then
text2 = text2 .. '[[Category:' .. category .. ']]'
end
return text2
end
local function getStatementYear(entityid, propertyid)
local statement = mw.wikibase.getBestStatements(entityid, propertyid)
if statement[1] ~= nil then
if statement[1]['mainsnak']['snaktype'] == 'value' then
if statement[1]['mainsnak']['datavalue']['value']['precision']>=9 then
return tonumber(mw.ustring.match(statement[1]['mainsnak']['datavalue']['value']['time'], "^[\+\-]?%d+"))
else
return nil
end
else
return nil
end
else
return nil
end
end
local function getStatementString(entityid, propertyid)
local statement = mw.wikibase.getBestStatements(entityid, propertyid)
if statement[1] ~= nil then
if statement[1]['mainsnak']['snaktype'] == 'value' then
return mw.wikibase.getLabel(statement[1]['mainsnak']['datavalue']['value']["id"])
else
return nil
end
else
return nil
end
end
local function getFirstLetter(entityid, propertyid)
local statement = mw.wikibase.getBestStatements(entityid, propertyid)
if statement[1] ~= nil then
if statement[1]['mainsnak']['snaktype'] == 'value' then
local enlabel = mw.wikibase.getLabelByLang(statement[1]['mainsnak']['datavalue']['value']["id"], 'en')
if enlabel ~= nil then
return string.sub(enlabel,0,1)
else
return nil
end
else
return nil
end
else
return nil
end
end
local function yearToString(year)
if year == nil then
return '?'
elseif year > 0 then
return tostring(year) .. '年'
elseif year < 0 then
return '前' .. tostring(-year) .. '年'
else
return '?'
end
end
function p.main(frame)
local text = ''
local entityid = nil
if mw.wikibase ~= nil then
entityid = mw.wikibase.getEntityIdForCurrentPage()
end
local name = frame.args['name']
if name==nil or name=='' then
name=frame.args['Name']
end
if name==nil or name=='' then
name = mw.title.getCurrentTitle().text
end
local firstletter = frame.args['firstletter']
if firstletter==nil or firstletter=='' then
firstletter=frame.args['FirstLetterLastName']
end
local surname = frame.args['surname']
if entityid ~= nil then
surname = getStatementString(entityid, 'P734')
if firstletter == nil or firstletter=='' then
firstletter = getFirstLetter(entityid, 'P734')
end
end
-- if firstletter==nil or firstletter=='' then
-- text = text .. frame:expandTemplate{title='Template error', args={'未指定作者名称拼音首字母'}}
-- end
local times = frame.args['times']
local birthyear = frame.args['birthyear']
if (birthyear == nil or birthyear == '') and entityid ~= nil then
birthyear = getStatementYear(entityid, 'P569')
end
local deathyear = frame.args['deathyear']
if (deathyear == nil or deathyear == '') and entityid ~= nil then
deathyear = getStatementYear(entityid, 'P570')
end
local dates = frame.args['Dates']
local country = frame.args['country']
if country==nil or country=='' then
country = 'China'
end
local sex = frame.args['sex']
local notes = frame.args['notes']
if notes==nil or notes=='' then
notes = frame.args['MiscBio']
if notes==nil or notes=='' then
note, lang = mw.wikibase.getDescriptionWithLang(entityid)
if mw.ustring.sub(lang or '',1,2)=='zh' then
notes = note .. "{{Plain sister}}"
else
notes = "{{Plain sister}}"
end
end
end
local image = frame.args['image']
if (image == nil or image == '') then
image = frame.args['Image']
end
if (image == nil or image == '') and (entityid ~= nil) then
local imageStatement = mw.wikibase.getBestStatements(entityid, 'P18')
if imageStatement[1] ~= nil then
image = imageStatement[1]['mainsnak']['datavalue']['value']
end
end
local image_caption = frame.args['image_caption']
if image_caption == nil or image_caption == '' then
image_caption = name
end
if firstletter ~= nil then
text = text .. '<div align="right" style="font-size:12px">[[类型:作者/' .. firstletter .. '|作者-' .. firstletter .. ']]</div>'
end
text = text .. '<div style="width:100%; margin-bottom:1px; border:1px solid #D6C5C5; background:#FAF8F8;text-align:center" >';
text = text .. '<div style="font-weight:bold">' .. frame:preprocess(name) .. '</div>'
if (birthyear ~= nil and birthyear ~= '') or (deathyear ~= nil and deathyear ~= '') then
if birthyear ~= nil then
birthyear = tonumber(birthyear)
end
if deathyear ~= nil and deathyear ~= '-' then
deathyear = tonumber(deathyear)
end
text = text .. yearToString(birthyear)
text = text .. ' — '
if deathyear ~= '-' and (deathyear ~= nil or birthyear == nil or birthyear<os.date("*t").year-100) then
text = text .. yearToString(deathyear)
end
elseif dates ~= nil and dates ~= '' then
text = text .. frame:preprocess(dates)
end
if times ~= nil and times ~= '' then
text = text .. ' ' .. frame:preprocess(times)
end
text = text .. '</div>'
text = text .. '<div style="width:100%; margin-bottom:1px; font-size:small; border-bottom:1px solid #D6C5C5; background:#FEFFF0;" >'
if notes ~= nil then
text = text .. frame:preprocess(notes)
end
text = text .. frame:expandTemplate{title='works about author',args={}}
text = text .. '</div>'
if image ~= nil and image ~= '' then
text = text .. '[[File:' .. frame:preprocess(image) .. '|thumb|200px'
if image_caption ~= nil then
text = text .. '|' .. frame:expandTemplate{title='center',args={frame:preprocess(image_caption)}}
end
text = text .. ']]'
end
if name ~= nil and firstletter ~= nil then
text = addCategoryIf(text, name ~= nil and firstletter ~= nil, frame:preprocess(name) .. '|*')
end
if country ~= nil and country ~= '' then
text = addCategoryIf(text, ChinaCountry[country] == nil, frame:preprocess(country) .. '作者')
end
if firstletter ~= nil and firstletter ~= '' then
text = addCategoryIf(text, firstletter ~= nil and name ~= nil, '作者-' .. firstletter .. '|' .. frame:preprocess(name))
end
if sex ~= nil and sex ~= '' then
text = addCategoryIf(text, true, frame:preprocess(sex) .. '作者')
end
if birthyear ~= nil and birthyear ~='' then
text = addCategoryIf(text, birthyear >= 0, tostring(birthyear) .. '年出生')
text = addCategoryIf(text, birthyear < 0, '前' .. tostring(-birthyear) .. '年出生')
end
if deathyear ~= nil and deathyear ~='' and deathyear ~='-' then
text = addCategoryIf(text, deathyear >= 0, tostring(deathyear) .. '年逝世')
text = addCategoryIf(text, deathyear < 0, '前' .. tostring(-deathyear) .. '年逝世')
end
if times ~= nil and times ~= '' then
times = frame:preprocess(times)
if timesTable[times] ~= nil then
text = addCategoryIf(text, true, timesTable[times] .. '作者')
else
text = addCategoryIf(text, true, frame:preprocess(times) .. '作者')
end
end
if surname ~= nil and surname ~= '' then
text = addCategoryIf(text, true, surname .. '姓作者')
end
return text
end
return p;