Khác biệt giữa bản sửa đổi của “Mô đun:Thử”

Nội dung được xóa Nội dung được thêm vào
nKhông có tóm lược sửa đổi
Không có tóm lược sửa đổi
Dòng 1:
local getPortalImage = require('Module:Portal').image
---Mô đun này hỗ trợ {{URL}}.
--
-- Xem các trường hợp kiểm thử đơn vị tại [[Module:URL/tests]].
 
local p = {}
 
local function trimgetArgNums(sprefix, args)
-- Returns a table containing the numbers of the arguments that exist for the specified prefix. For example, if the
return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
-- prefix was 'data', and 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local nums = {}
for k, v in pairs(args) do
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
 
local function safeUrimakeHorizontalRule(s)
local success, urirow = pcall(functionmw.html.create('tr')
row
return mw.uri.new(s)
end :tag('td')
:attr('colspan', '2')
if success then
:tag('hr', {selfClosing = true})
return uri
return tostring(row)
end
 
local function makeItem(image, text)
local root = mw.html.create('li')
root
:css('float', 'left')
:css('margin-left', '0.3em')
:css('height', '3.6em')
:tag('span')
:css('display', 'inline-block')
:css('margin-right', '0.3em')
:css('width', '30px')
:css('line-height', '3.6em')
:css('text-align', 'center')
:wikitext(image)
:done()
:tag('span')
:css('display', 'inline-block')
:css('width', '11em')
:css('vertical-align', 'middle')
:wikitext(text)
return tostring(root)
end
 
local function makeRow(items, heading, subheading, options)
if #items < 1 then return end
local swapHeadingSize = type(options) == 'table' and options.swapHeadingSize or false
local row = mw.html.create('tr')
row
:tag('td')
:css('width', '175px')
:tag('span')
:css('font-size', swapHeadingSize and '90%' or '125%')
:wikitext(heading)
:done()
:tag('br', {selfClosing = true})
:done()
:tag('span')
:css('font-size', swapHeadingSize and '125%' or '90%')
:wikitext(subheading)
local list = row:tag('td'):css('text-align', 'left'):tag('ul')
for i, item in ipairs(items) do
local image = item[1]
local text = item[2]
list
:wikitext(makeItem(image, text))
end
return tostring(row)
end
 
local function makeNumberedRow(prefix, args, heading, subheading, getItemValsFunc, options)
function p._url(url, text)
if args[prefix] then
url = trim(url or '')
args[prefix .. '1'] = args[prefix]
text = trim(text or '')
if url == '' then
if text == '' then
return mw.getCurrentFrame():expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''văn bản hiển thị tùy chọn''" } }
else
return text
end
end
local argNums = getArgNums(prefix, args)
local items = {}
-- If the URL contains any unencoded spaces, encode them, because MediaWiki will otherwise interpret a space as the end of the URL.
for i, argNum in ipairs(argNums) do
url = mw.ustring.gsub(url, '%s', function(s) return mw.uri.encode(s, 'PATH') end)
local image, text = getItemValsFunc(args[prefix .. tostring(argNum)])
table.insert(items, {image, text})
-- If there is an empty query string or fragment id, remove it as it will cause mw.uri.new to throw an error
url = mw.ustring.gsub(url, '#$', '')
url = mw.ustring.gsub(url, '%?$', '')
-- If it's an HTTP[S] URL without the double slash, fix it.
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')
local uri = safeUri(url)
-- Handle URLs from Wikidata of the format http&#58;//
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?)&#58;//', 'http%1://')
-- Handle URL's without a protocol and URL's that are protocol-relative,
-- e.g. www.example.com/foo or www.example.com:8080/foo, and //www.example.com/foo
if uri and (not uri.protocol or (uri.protocol and not uri.host)) and url:sub(1, 2) ~= '//' then
url = 'http://' .. url
uri = safeUri(url)
end
return makeRow(items, heading, subheading, options)
end
if text == '' then
 
if uri then
function p._main(args)
if uri.path == '/' then uri.path = '' end
local rows = {}
 
local port = ''
-- Get the book row text.
if uri.port then port = ':' .. uri.port end
local bookHeading = "'''[[Wikipedia:Sách|Sách]]'''"
local bookSubheading = 'Xem hoặc đặt mua những bộ sưu tập các bài viết'
text = mw.ustring.lower(uri.host or '') .. port .. (uri.relativePath or '')
local function getBookItemVals(book)
else -- URL is badly-formed, so just display whatever was passed in
local image = '[[File:Office-book.svg|30px|alt=|link=]]'
text = url
local text = mw.ustring.format("'''''[[Sách:%s|%s]]'''''", book, book)
return image, text
end
local bookRow = makeNumberedRow('book', args, bookHeading, bookSubheading, getBookItemVals)
table.insert(rows, bookRow)
 
-- Get the portal row text
local portalHeading = "'''[[Wikipedia:Chủ đề|Chủ đề]]'''"
local portalSubheading = 'Truy cập chủ đề liên quan'
local function getPortalItemVals(portal)
local image = mw.ustring.format('[[File:%s|30x30px]]', getPortalImage{portal})
local text = mw.ustring.format("'''''[[Chủ đề:%s|Chủ đề %s]]'''''", portal, portal)
return image, text
end
local portalRow = makeNumberedRow('portal', args, portalHeading, portalSubheading, getPortalItemVals)
table.insert(rows, portalRow)
 
-- Get the sister projects row text.
local sisters = {
{arg = 'commons', image = 'Commons-logo.svg', prefix = 'commons', display = 'Tư liệu đa phương tiện', from = 'Commons'},
{arg = 'species', image = 'Wikispecies-logo.svg', prefix = 'wikispecies', display = 'Danh mục các loài', from = 'Wikispecies'},
{arg = 'voy', image = 'Wikivoyage-Logo-v3-icon.svg', prefix = 'voy', display = 'Cẩm nang du lịch', from = 'Wikivoyage'},
{arg = 'n', image = 'Wikinews-logo.svg', prefix = 'wikinews', display = 'Tin tức', from = 'Wikinews'},
{arg = 'wikt', image = 'Wiktionary-logo-v2.svg', prefix = 'wiktionary', postfix = 'tiếng Việt', display = 'Định nghĩa', from = 'Wiktionary'},
{arg = 'b', image = 'Wikibooks-logo.svg', prefix = 'wikibooks', display = 'Sách giáo khoa', from = 'Wikibooks'},
{arg = 'q', image = 'Wikiquote-logo.svg', prefix = 'wikiquote', display = 'Trích dẫn', from = 'Wikiquote'},
{arg = 's', image = 'Wikisource-logo.svg', prefix = 'wikisource', display = 'Văn bản gốc', from = 'Wikisource'},
{arg = 'v', image = 'Wikiversity-logo.svg', prefix = 'wikiversity', display = 'Học liệu', from = 'Wikiversity'},
{arg = 'd', image = 'Wikidata-logo.svg', prefix = 'wikidata', display = 'Dữ liệu', from = 'Wikidata'},
{arg = 'spoken', image = 'Sound-icon.svg', prefix = 'spoken wikipedia', display = 'Nghe nội dung trang này', from = 'Wikipedia Nói'},
}
local sisterItems = {}
for i, t in ipairs(sisters) do
if args[t.arg] then
-- Get the image value.
local image = mw.ustring.format('[[File:%s|30x30px|alt=|link=]]', t.image)
-- Get the text value.
local prefix = t.prefix
local search = args[t.arg .. '-search'] or mw.title.getCurrentTitle().text
local postfix = t.postfix
postfix = postfix and ('#' .. postfix) or ''
local display = t.display
local from = t.from
local text = mw.ustring.format(
'[[%s:Special:Search/%s%s|%s]]<br />trên %s',
prefix, search, postfix, display, from
)
if t.arg == 'spoken' then
text = mw.ustring.format('%s trên %s<br />[[File:%s]]',
display, from, args[t.arg]
)
end
-- Add the values to the items table.
table.insert(sisterItems, {image, text})
end
end
local sisterHeading = "Tìm hiểu thêm tại các"
-- Add <wbr> before _/.-# sequences
local sisterSubheading = "'''[[Wikipedia:Dự án liên quan|Dự án liên quan]]'''"
text = mw.ustring.gsub(text,"(/+)","<wbr/>%1") -- This entry MUST be the first. "<wbr/>" has a "/" in it, you know.
local sisterRow = makeRow(sisterItems, sisterHeading, sisterSubheading, {swapHeadingSize = true})
text = mw.ustring.gsub(text,"(%.+)","<wbr/>%1")
table.insert(rows, sisterRow)
-- text = mw.ustring.gsub(text,"(%-+)","<wbr/>%1") -- DISABLED for now
 
text = mw.ustring.gsub(text,"(%#+)","<wbr/>%1")
-- Make the table.
text = mw.ustring.gsub(text,"(_+)","<wbr/>%1")
local root = mw.html.create('table')
return mw.ustring.format('<span class="url">[%s %s]</span>', url, text)
root
:attr('role', 'presentation')
:addClass('noprint')
:addClass('navbox')
:addClass('metadata')
:addClass('plainlist')
:css('background-color', '#f9f9f9')
:css('border', '1px solid #aaa')
:css('clear', 'both')
:css('margin-bottom', '0.5em')
:css('margin-top', '0.5em')
:wikitext(table.concat(rows, makeHorizontalRule()))
 
return tostring(root)
end
 
function p.urlmain(frame)
-- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist. Otherwise
local templateArgs = frame.args
-- assume args are being passed directly in from the debug console or from another Lua module.
local url = templateArgs[1] or ''
local text = templateArgs[2] or ''origArgs
if frame == mw.getCurrentFrame() then
return p._url(url, text)
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Remove blank arguments.
local args = {}
for k, v in pairs(origArgs) do
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end