モジュール:サンドボックス/Nami-ja/sandbox2

モジュールの解説[表示] [編集] [履歴] [キャッシュを破棄]

使用法[編集]

{{#invoke:サンドボックス/Nami-ja|機能名}}
{{利用者:Nami-ja/Template:sandbox
| type    = type1
| url     = https://kotobank.jp/word/
| id      = Wikipedia
| title   = Wikipedia
| subtext = (知恵蔵2015)
| article = コトバンク
}}
{{利用者:Nami-ja/Template:sandbox
| type    = type2
| url     = https://kotobank.jp/word/
| id      = Wikipedia
| title   = @Wikipedia
| subtext = (知恵蔵2015)
| article = コトバンク
}}
{{利用者:Nami-ja/Template:sandbox
| type    = type3
| url     = https://kotobank.jp/word/
| id      = Wikipedia
| title   = Wikipedia
| subtext = 知恵蔵2015
| article = コトバンク
}}
{{利用者:Nami-ja/Template:sandbox
| type    = type4
| url     = https://kotobank.jp/word/
| id      = Wikipedia
| title   = Wikipedia(知恵蔵2015)
| subtext = 
| article = コトバンク
}}


local p = {}

local WRAPPER = '利用者:Nami-ja/Template:sandbox'

function p._error( message )
	local error_m = '<strong class="error">エラー:&nbsp;' ..message ..'</strong>'
	return error_m
end

function p._format( type, url, id, title, article, subtext )
	local label = ({
		type1 = article ..':[' ..url ..id ..' ' ..title ..']' ..subtext,
		type2 = '[' ..url ..id ..' ' ..title ..']' ..subtext ..' - ' ..article,
		type3 = subtext ..':『[' ..url ..id ..' ' ..title ..']』 - ' ..article,
		type4 = '[' ..url ..id ..' ' ..title ..']' ..subtext,
	})[type]
	if label then
		return label
	else
		return p._error('タイプが指定されていません。')
	end
end

function p.main( frame )
	local args = require('Module:Arguments').getArgs(frame, { wrappers = WRAPPER, removeBlanks = false })

--[=[
	local array_args = {
		'type', 'url', 'id', 'title', 'article', 'subtext',
	}
	local array = {}
	for key, val in pairs( array_args ) do
		array[array_args] = args[array_args] or ''
	end
]=]

	local type = args.type or ''
	if type == '' then
		type = 'type2'
	end

	local url = args.url or ''
	if url == '' then
		return p._error('URLが指定されていません。')
	end

	local id = args.id or ''
	if id == '' then
		return p._error('IDが指定されていません。')
	end

	local title = args.title or ''
	if title == '' then
		title = frame:preprocess('{{PAGENAME}}')
	end

	local article = args.article or ''
	if article == '' then
		if type ~= 'type4' then
			return p._error('ARTICLEが指定されていません。')
		end
	else
		article = '[[' ..article ..']]'
	end

	local subtext = args.subtext or ''

	return p._format( type, url, id, title, article, subtext )
end

return p