This module is for formatting and linking chemical names. For formatting chemical formulas, see template chem2 (and module chem2).
| This module is rated as pre-alpha. It is incomplete and may or may not be in active development. Do not use it in article namespace pages. A module remains in pre-alpha until its developer, or another editor who adopts it if it is abandoned for some time, considers the basic structure complete. |
This module is for formatting and linking chemical names. For formatting chemical formulas, see template chem2 (and module chem2).
Only some chemical names need formatting, mostly in biochemistry and organic chemistry.
Formatting does not include adjustments of letter case, because letter case affects the meaning of chemical names.
{{#invoke:Sandbox/teaktl17|main|text to format}}
D-, L-, DL-, R, S, r, s, meso-, sn-
E, Z, cis, trans
--[[
Sandbox of User:teaktl17
]]--
require ('strict')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local prefs = {
-- stereo specifiers
['cis'] = true,
['trans'] = true,
['syn'] = true,
['anti'] = true,
['endo'] = true,
['exo'] = true,
['meso'] = true,
['sn'] = true,
-- arene substitution patters
['ortho'] = true,
['o'] = true,
['meta'] = true,
['m'] = true,
['para'] = true,
['p'] = true,
-- traditional: sec-, s-, tert-, t-
['sec'] = true,
['s'] = true,
['tert'] = true,
['t'] = true,
-- historical: n- for normal, i- for iso
['n'] = true,
['i'] = true
}
function p.format(in_str)
local out = in_str or ''
local i -- position in 'out' string to analyze
local function insert2(s, e, on, a_off)
local off = a_off or on
out = out:sub(1, s - 1) .. on .. out:sub(s, e) .. off .. out:sub(e + 1)
i = e + 1 + #on + #off
end
i = 1
repeat
local s, e -- start and end position in 'out' string
local found -- pattern is matched
local function find3(pat, lit)
s, e = out:find(pat, i, lit)
found = not not (s and e)
return found
end
-- small for D-, L-, DL-, etc.
if find3('^[DL]+%-') then
e = e - 1
end
if not found then
if find3('%-[DL]+%-') then
s = s + 1
e = e - 1
end
end
if found then
insert2(s, e, '<small>', '</small>')
else
-- italic for
-- Stereo specifiers: R/S, E/Z, etc.
local function find4(pair)
local before = '%' .. pair:sub(1,1)
local after = '%' .. pair:sub(2,2)
local pat = before .. '%d*%l?[RSrsEZ]' .. after
if find3(pat) then
s = e - 1
e = e - 1
insert2(s, e, "''")
end
end
find4('()')
if not found then
find4('(,')
if found then
repeat
find4(',,')
until not found
find4(',)')
end
end
if not found then
mw.log("prefs", i, out:sub(i))
local function test_pr()
local str = out:sub(s, e)
if prefs[str] then
mw.log(i, out:sub(i))
insert2(s, e, "''")
mw.log(i, out:sub(i))
else
i = e
end
end
if i == 1 and find3("^%l+[-,]") then
e = e - 1
test_pr()
end
if not found and find3("%A%l+[-,]") then
s = s + 1
e = e - 1
test_pr()
end
end
end
until not found
return out
end
function p._main(args)
local in_str = args[1] or ''
return p.format(in_str)
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
return p
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.