置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:IconLink

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. -- 请在[[模块:IconLink/data]]添加站点数据
  2. local data = require('Module:IconLink/data')
  3. local sites, domains, idPatterns = data.sites, data.domains, data.idPatterns
  4. -- 短点方便些
  5. local match = string.match
  6. local format = string.format
  7. -- 从模块参数分析站点
  8. -- 返回:site表 | nil
  9. local function getSiteFromUrl(url)
  10. local domainParts = mw.text.split(mw.uri.new(url).host, '%.')
  11. local curPart = domains
  12. for i = #domainParts, 1, -1 do
  13. curPart = curPart[domainParts[i]]
  14. if not curPart then return nil end
  15. if type(curPart) == 'string' then return sites[curPart] end
  16. end
  17. return nil
  18. end
  19. -- 根据[[模块:IconLink/data]]的sites表,对指定site和id生成URL
  20. local function makeUrl(site, id)
  21. local urlType = type(site.url)
  22. if urlType == 'table' then
  23. for _, v in ipairs(site.url) do
  24. if (not v[1]) or match(id, v[1]) then
  25. return format(v[2], id)
  26. end
  27. end
  28. return false
  29. end
  30. if urlType == 'function' then return site.url(id) end
  31. if urlType == 'string' then return format(site.url, id) end
  32. error('模块:IconLink/data中,sites表' .. site.name .. '的url填写有误。')
  33. end
  34. -- 生成最终链接
  35. local function makeIconLink(site, url, title, size)
  36. -- 错误
  37. -- * 无url,无site: 未正确填写参数
  38. -- * 无url,有site: 未填写URL/ID或填写了不受支持的URL/ID
  39. -- * 有url,无site: 未匹配到支持的网站,不显示网站图标,但不报错
  40. if not url then
  41. if site then
  42. error('URL/ID填写有误')
  43. else
  44. error('分析参数失败')
  45. end
  46. end
  47. if site then
  48. if title then
  49. -- [[File:icon]] [url title]
  50. return format('[[File:%s|%s|link=|%s]] [%s %s]',
  51. site.icon.file,
  52. size or site.icon.size or 'x20px',
  53. site.name,
  54. url,
  55. title
  56. )
  57. end
  58. -- [[File:icon|url]]
  59. return format('[[File:%s|%s|link=%s|%s]]',
  60. site.icon.file,
  61. size or site.icon.size or 'x20px',
  62. url,
  63. site.name
  64. )
  65. end
  66. -- [url title]
  67. if title then return '['..url..' '..title..']' end
  68. -- [url]
  69. return '['..url..']'
  70. end
  71. local p = {}
  72. function p.main(args)
  73. local url = args.link
  74. local site = (args.site and sites[args.site:lower()]) or (url and getSiteFromUrl(url))
  75. local title = args.title
  76. for _, arg in ipairs(args) do
  77. arg = mw.text.trim(arg)
  78. if not url then -- 默认有url的时候就知晓site和id
  79. -- 若参数是url
  80. if match(arg, '^[Hh][Tt][Tt][Pp][Ss]?://') then
  81. url = arg
  82. -- 从url获取网站
  83. if not site then site = getSiteFromUrl(url) end
  84. -- 若参数是site或id
  85. elseif not site then -- 未知site,尝试从该参数获取site
  86. site = sites[mw.ustring.lower(arg)]
  87. if not site then
  88. for idPattern, siteById in pairs(idPatterns) do -- 遍历idPatterns
  89. if match(arg, idPattern) then
  90. site = siteById
  91. url = makeUrl(site, arg)
  92. break
  93. end
  94. end
  95. end
  96. if not site then break end -- 连站点都获取不到,还搞啥,润!
  97. -- site已知晓,该参数是id,从中获取url
  98. else
  99. url = makeUrl(site, arg)
  100. end
  101. elseif not title then -- url(site、id)已知晓,只缺title
  102. title = arg
  103. end
  104. end
  105. return makeIconLink(site, url, title, args.size)
  106. end
  107. function p.fromParent(frame)
  108. return p.main(frame:getParent().args)
  109. end
  110. function p.wrapper(frame)
  111. local parent = frame:getParent()
  112. parent.args.site = frame.args.site
  113. return p._main(parent.args)
  114. end
  115. return p