Module:Chem:修订间差异
外观
化学式 |
小 已保护“Module:Chem”:常用模板([编辑=仅允许管理员](无限期)[移动=仅允许管理员](无限期)) |
||
(未显示5个用户的8个中间版本) | |||
第2行: | 第2行: | ||
function p.chem(frame) | function p.chem(frame) | ||
local | local args=frame:getParent().args | ||
if | --local args=frame | ||
local chemstr="" | |||
for i, v in pairs(args) do | |||
if type(i)=="number" then | |||
chemstr=chemstr .. v | |||
else | |||
chemstr=chemstr .. i .. "=" .. v | |||
end | |||
end | |||
chemstr=mw.ustring.gsub(chemstr, "%s", "") | chemstr=mw.ustring.gsub(chemstr, "%s", "") | ||
第16行: | 第24行: | ||
if ch=="." or ch=="·" then | if ch=="." or ch=="·" then | ||
table.insert(res, "·") | table.insert(res, "·") | ||
norm=true | |||
elseif ch=="=" then | |||
table.insert(res, "=") | |||
norm=true | norm=true | ||
elseif ch=="^" then | elseif ch=="^" then | ||
第55行: | 第66行: | ||
end | end | ||
return mw.html.create(" | return mw.html.create("span"):wikitext(table.concat(res)) | ||
end | end | ||
return p | return p |
2023年6月17日 (六) 12:50的最新版本
此模块的文档可以在Module:Chem/doc创建
local p={}
function p.chem(frame)
local args=frame:getParent().args
--local args=frame
local chemstr=""
for i, v in pairs(args) do
if type(i)=="number" then
chemstr=chemstr .. v
else
chemstr=chemstr .. i .. "=" .. v
end
end
chemstr=mw.ustring.gsub(chemstr, "%s", "")
local res={}
local len=mw.ustring.len(chemstr)
local i=1
local norm=true
local sup=false
while i<=len do
local ch=mw.ustring.sub(chemstr, i, i)
if ch=="." or ch=="·" then
table.insert(res, "·")
norm=true
elseif ch=="=" then
table.insert(res, "=")
norm=true
elseif ch=="^" then
sup=true
norm=false
elseif ch=="_" then
sup=false
norm=false
elseif ch=="+" or ch=="-" then
if i~=len then
local nc=mw.ustring.sub(chemstr, i+1, i+1)
norm=true
if nc=="+" or nc=="-" then
norm=false
end
end
if not norm then
table.insert(res, "<sup>" .. ch .. "</sup>")
else
table.insert(res, ch)
end
norm=true
elseif mw.ustring.match(ch, "%d") then
if not norm then
if sup then
table.insert(res, "<sup>" .. ch .. "</sup>")
else
table.insert(res, "<sub>" .. ch .. "</sub>")
end
else
table.insert(res, ch)
end
else
table.insert(res, ch)
norm=false
sup=false
end
i=i+1
end
return mw.html.create("span"):wikitext(table.concat(res))
end
return p