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

Module:组字

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local p={}
  2. p.parse=function(frame)
  3. local parseTable=mw.loadData("Module:组字/ParseTable")
  4. local function buildTree(str, index)
  5. -- 递归下降
  6. index=index or 1
  7. str=str or ""
  8. if mw.ustring.len(str) < index then return nil, index end
  9. local chr=mw.ustring.sub(str, index, index)
  10. local data={}
  11. data.head=chr
  12. if parseTable[chr] then
  13. data.body={}
  14. local nexti=index+1
  15. for i=1,parseTable[chr].length do
  16. data.body[i], nexti=buildTree(str, nexti)
  17. if not data.body[i] then break end
  18. end
  19. return data, nexti
  20. else
  21. return data, index+1
  22. end
  23. end
  24. local function setSize(tree, x, y, width, height)
  25. -- 计算尺寸
  26. if type(tree)~="table" then return end
  27. tree.x=x
  28. tree.y=y
  29. tree.width=width
  30. tree.height=height
  31. if not tree.body then return end
  32. local szt=parseTable[tree.head]
  33. if not szt then return end
  34. for i=1,szt.length do
  35. setSize(tree.body[i], szt.x[i]*width+x, szt.y[i]*height+y, szt.width[i]*width, szt.height[i]*height)
  36. end
  37. end
  38. local function plainize(tree)
  39. local arr={}
  40. if type(tree)~="table" then return end
  41. if tree.body then
  42. for i=1,#(tree.body) do
  43. local sarr=plainize(tree.body[i])
  44. for k,v in ipairs(sarr) do
  45. table.insert(arr, v)
  46. end
  47. end
  48. elseif not parseTable[tree.head] then
  49. table.insert(arr, {
  50. text=tree.head,
  51. width=tree.width,
  52. height=tree.height,
  53. left=tree.x,
  54. top=tree.y,
  55. })
  56. end
  57. return arr
  58. end
  59. local function make_text(text, unit, gw, gh)
  60. local ts=mw.html.create("span")
  61. if type(text)~="table" then return ts end
  62. ts:css("display", "inline-block")
  63. ts:css("position", "absolute")
  64. ts:css("top", tostring(text.top) .. unit)
  65. ts:css("left", tostring(text.left) .. unit)
  66. ts:css("width", tostring(gw) .. unit)
  67. ts:css("height", tostring(gh) .. unit)
  68. ts:css("transform", "scale(" .. tostring(text.width/gw) .. "," .. tostring(text.height/gh) .. ")" )
  69. ts:css("transform-origin", "0% 0%")
  70. ts:addClass("combination-text")
  71. ts:wikitext(text.text)
  72. return ts
  73. end
  74. local function make_frame(texts, width, height, unit)
  75. if type(texts)~="table" then return end
  76. local text=mw.html.create("span")
  77. text:css("display", "inline-block")
  78. --text:css("overflow", "hidden")
  79. text:css("position", "relative")
  80. text:css("width", tostring(width) .. unit)
  81. text:css("height", tostring(height) .. unit)
  82. --text:css("top", tostring(height*5/6) .. unit)
  83. --text:css("left", tostring(width/12) .. unit)
  84. text:css("font-size", tostring((height+width)/2) .. unit)
  85. --local place=mw.html.create("span")
  86. --place:css("display", "inline-block")
  87. --place:css("position", "absolute")
  88. --place:css("z-index", "-1")
  89. --text:node(place)
  90. for k,v in ipairs(texts) do
  91. text:node(make_text(v, unit, width, height))
  92. end
  93. return text
  94. end
  95. local function emit_html(tree, unit)
  96. local arr=plainize(tree)
  97. return tostring(make_frame(arr, tree.width, tree.height, unit))
  98. end
  99. local str=frame.args[1]
  100. if not str then return end
  101. local size=tonumber(frame.args[2]) or 24
  102. local unit=frame.args[3] or "px"
  103. local chrdata=buildTree(str, 1)
  104. setSize(chrdata, 0, 0, size, size)
  105. --do return mw.dumpObject(plainize(chrdata)) end
  106. return emit_html(chrdata, unit)
  107. end
  108. return p