Module:Infobox

Template-info.svg 模块文档  [创建] [刷新]
  1. local module = { }
  2. local inner_args = { ["wrapper"] = true, ["class"] = true, ["m-style"] = true, ["font-size"] = true, ["text-align"] = true, ["width"] = true, ["max-width"] = true, ["float"] = true, ["bg-color"] = true, ["m-bcolor"] = true, ["m-color"] = true,
  3. ["notitle"] = true, ["title-main"] = true, ["top-style"] = true, ["top-bcolor"] = true, ["标题背景色"] = true, ["標題背景色"] = true, ["top-color"] = true, ["标题文字色"] = true, ["標題文字色"] = true, ["Title"] = true,
  4. ["标题"] = true, ["標題"] = true, ["tabs"] = true, ["image-style"] = true, ["image"] = true, ["size"] = true, ["图片大小"] = true, ["圖片大小"] = true, ["caption"] = true, ["图片说明"] = true, ["圖片說明"] = true, ["图片信息"] = true,
  5. ["圖片信息"] = true, ["image-lostmessage"] = true, ["l-style"] = true, ["l-width"] = true, ["l-bcolor"] = true, ["左栏背景色"] = true, ["左欄背景色"] = true, ["l-color"] = true, ["左栏文字色"] = true, ["左欄文字色"] = true,
  6. ["r-style"] = true, ["t-style"] = true, ["t-bcolor"] = true, ["底栏背景色"] = true, ["底欄背景色"] = true, ["t-color"] = true, ["底栏文字色"] = true, ["底欄文字色"] = true, ["i-style"] = true }
  7. function module.Modern(frame)
  8. local frame_self = frame
  9. local parent = frame:getParent()
  10. local title = parent:getTitle()
  11. local _chk_arg_self = function(...)
  12. local ret = nil
  13. for _, arg_name in ipairs{...} do
  14. ret = frame_self.args[arg_name]
  15. if ret then
  16. ret = mw.text.trim(ret)
  17. if ret ~= "" then return ret end
  18. end
  19. end
  20. return nil
  21. end
  22. local _chk_arg, _chk_arg_caller = _chk_arg_self, _chk_arg_self
  23. if parent and ((title == "Template:InfoboxModern") or (frame.args["wrapper"]=="1" and mw.title.new(title).namespace == 10)) then
  24. frame = parent
  25. _chk_arg_caller = function(...)
  26. local ret = nil
  27. for _, arg_name in ipairs{...} do
  28. ret = parent.args[arg_name]
  29. if ret then
  30. ret = mw.text.trim(ret)
  31. if ret ~= "" then return ret end
  32. end
  33. end
  34. return nil
  35. end
  36. _chk_arg = function(...)
  37. return _chk_arg_caller(...) or _chk_arg_self(...)
  38. end
  39. end
  40. local html = mw.html.create()
  41. local box = html:tag("table")
  42. :addClass("infobox"):addClass(_chk_arg("class") or "")
  43. :attr("align", "right")
  44. local style_main = _chk_arg("m-style")
  45. if style_main then
  46. box:cssText(style_main)
  47. else
  48. box:css("font-size", _chk_arg("font-size") or "90%")
  49. :css("text-align", _chk_arg("text-align") or "center")
  50. :css("width", _chk_arg("width") or "280px")
  51. :css("max-width", _chk_arg("max-width") or "100%")
  52. :css("float", _chk_arg("float") or "right")
  53. :css("background-color", _chk_arg("bg-color", "m-bcolor") or "#FFF")
  54. :css("color", _chk_arg("m-color") or "#000")
  55. end
  56. if not _chk_arg("notitle") then
  57. local title_main = _chk_arg_caller("title-main")
  58. if title_main then
  59. box:wikitext(title_main)
  60. else
  61. local td_title = box:tag("tr"):addClass("infobox-title"):tag("td"):css("padding", "0"):attr("colspan", "2")
  62. local style_top = _chk_arg("top-style")
  63. if style_top then
  64. td_title:cssText(style_top)
  65. else
  66. td_title:css("background-color", _chk_arg("top-bcolor", "标题背景色", "標題背景色") or"#26ca9b")
  67. :css("color", _chk_arg("m-color", "top-color", "标题文字色", "標題文字色") or"#FFF")
  68. :css("font-size", "100%")
  69. :css("padding", "1em")
  70. end
  71. td_title:wikitext(_chk_arg("Title", "title", "标题", "標題") or mw.title.getCurrentTitle().fullText)
  72. end
  73. end
  74. local arg_tabs = _chk_arg_caller("tabs")
  75. local arg_image = _chk_arg_caller("image")
  76. local arg_image_lostmessage = _chk_arg("image-lostmessage")
  77. if arg_tabs or arg_image or arg_image_lostmessage then
  78. local td_top = box:tag("tr"):tag("td"):attr("colspan", "2")
  79. if arg_tabs then
  80. td_top:wikitext(arg_tabs)
  81. else
  82. local style_image = _chk_arg("image-style")
  83. if style_image then
  84. td_top:cssText(style_image)
  85. end
  86. if arg_image then
  87. local size = _chk_arg("size", "图片大小", "圖片大小")
  88. if (not size) or (tonumber(mw.ustring.sub(size, 1, -3)) < 280) then
  89. size = "280px"
  90. end
  91. td_top:wikitext("[[Image:" .. arg_image .. "|" .. size .. "|class=infobox-image]]")
  92. local arg_caption = _chk_arg("caption", "图片说明", "圖片說明", "图片信息", "圖片信息")
  93. if arg_caption then
  94. td_top:tag("br"):done()
  95. :wikitext(arg_caption)
  96. end
  97. else
  98. td_top:wikitext(arg_image_lostmessage)
  99. end
  100. end
  101. end
  102. local num_names, item_type, num_insert = 1, 0, nil
  103. local arg_queue, arg_names = { }, { }
  104. for _, value in ipairs(frame_self.args) do
  105. local arg_trim = mw.text.trim(value)
  106. if num_insert == nil and arg_trim == "<customs>" then
  107. num_insert = #arg_queue
  108. else
  109. local arg_split = mw.text.split(arg_trim, ",", true)
  110. if mw.ustring.sub(arg_split[1], 1, 1) == "-" then
  111. arg_split[1] = mw.ustring.sub(arg_split[1], 2)
  112. item_type = 1
  113. else
  114. item_type = 0
  115. end
  116. arg_names[arg_split[1]] = arg_split[1]
  117. table.insert(arg_queue, { ["name"] = arg_split[1], ["type"] = item_type } )
  118. for index = 2, #arg_split do
  119. local alias = arg_split[index]
  120. arg_names[alias] = arg_split[1]
  121. table.insert(arg_queue, { ["name"] = alias, ["alias"] = num_names } )
  122. end
  123. num_names = num_names + #arg_split
  124. end
  125. end
  126. local arg_exclude = frame_self.args["exclude"]
  127. if arg_exclude then arg_exclude = mw.text.trim(arg_exclude) end
  128. if arg_exclude and arg_exclude ~= "" then
  129. local arg_split = mw.text.split(arg_exclude, ";", true)
  130. for _, value in ipairs(arg_split) do
  131. inner_args[value] = true
  132. end
  133. end
  134. -- Todo: CustomArgs by {{{order}}}
  135. local arg_order = frame_self.args["order"]
  136. if arg_order then arg_order = mw.text.trim(arg_order) end
  137. if arg_order and arg_order ~= "" then
  138. local arg_split = mw.text.split(arg_order, ";", true)
  139. for _, value in ipairs(arg_split) do
  140. if not (inner_args[value] or arg_names[value] or value == "") then
  141. if num_insert ~= nil then
  142. table.insert(arg_queue, num_insert, { ["name"] = value } )
  143. else
  144. table.insert(arg_queue, { ["name"] = value } )
  145. end
  146. arg_names[value] = value
  147. end
  148. end
  149. end
  150. local args_input = { }
  151. for key, value in pairs(frame.args) do
  152. value = mw.text.trim(value)
  153. if (not (tonumber(key) or inner_args[key])) and (value ~= "") then
  154. args_input[key] = value
  155. end
  156. end
  157. for key, value in pairs(args_input) do
  158. if not arg_names[key] then
  159. if num_insert ~= nil then
  160. table.insert(arg_queue, num_insert, { ["name"] = value } )
  161. else
  162. table.insert(arg_queue, { ["name"] = value } )
  163. end
  164. end
  165. end
  166. local style_left = _chk_arg("l-style")
  167. local width_left = _chk_arg("l-width")
  168. local bcolor_left = _chk_arg("l-bcolor", "左栏背景色", "左欄背景色")
  169. local color_left = _chk_arg("l-color", "左栏文字色", "左欄文字色")
  170. local style_right = _chk_arg("r-style")
  171. local style_rebar = _chk_arg("t-style")
  172. local bcolor_rebar = _chk_arg("t-bcolor", "底栏背景色", "底欄背景色")
  173. local color_rebar = _chk_arg("t-color", "底栏文字色", "底欄文字色")
  174. local style_text = _chk_arg("i-style")
  175. local InsertItem = function(key, value)
  176. local tr_item = box:tag("tr")
  177. local td_item = tr_item:tag("td")
  178. if style_left then
  179. td_item:cssText(style_left)
  180. else
  181. td_item:css("width", width_left or "80px")
  182. :css("background-color", bcolor_left or "#E0FFFF")
  183. :css("color", color_left or "#000")
  184. end
  185. td_item:wikitext(key)
  186. td_item = tr_item:tag("td")
  187. if style_right then
  188. td_item:cssText(style_right)
  189. end
  190. td_item:wikitext(value)
  191. end
  192. local InsertRebar = function(value)
  193. local td_item = box:tag("tr"):tag("td"):attr("colspan", "2")
  194. if style_rebar then
  195. td_item:cssText(style_rebar)
  196. else
  197. td_item:css("background-color", bcolor_rebar or "#62BCA9")
  198. :css("color", color_rebar or "#FFF")
  199. end
  200. td_item:wikitext(value)
  201. end
  202. local InsertText = function(value)
  203. local td_item = box:tag("tr"):tag("td"):attr("colspan", "2")
  204. if style_text then
  205. td_item:cssText(style_text)
  206. else
  207. td_item:css("text-align", "left")
  208. end
  209. td_item:wikitext(value)
  210. end
  211. -- mw.logObject(arg_queue)
  212. local temp_name, temp_type = nil, nil
  213. for _, arg_item in ipairs(arg_queue) do
  214. if args_input[arg_item.name] then
  215. if arg_item.alias then
  216. temp_name = arg_queue[arg_item.alias].name
  217. temp_type = arg_queue[arg_item.alias].type
  218. else
  219. temp_name = arg_item.name
  220. temp_type = arg_item.type
  221. end
  222. if temp_name then
  223. if temp_type == 1 then
  224. InsertRebar(temp_name)
  225. InsertText(args_input[arg_item.name])
  226. else
  227. InsertItem(temp_name, args_input[arg_item.name])
  228. end
  229. if arg_item.alias then
  230. arg_queue[arg_item.alias].name = nil
  231. end
  232. end
  233. args_input[arg_item.name] = nil
  234. arg_item.name = nil
  235. end
  236. end
  237. return html
  238. end
  239. return module
返回顶部
页面反馈