Module:Sandbox/teaktl17
Appearance
| 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.
Usage
[edit]{{#invoke:Sandbox/teaktl17|main|text to format}}
Examples and tests
[edit]Stereocenters
[edit]D-, L-, DL-, R, S, r, s, meso-, sn-
- L-seryl-L-alanine
- Glycyl-L-alanine
- DL-methionine
- (S)-alanine
- meso-tartaric acid
- sn-Glycerol 3-phosphate
Cis–trans isomerism
[edit]E, Z, cis, trans
- (2Z)-But-2-enedioic acid, also known as (Z)-butenedioic acid, cis-butenedioic acid, or maleic acid
- (2E)-But-2-enedioic acid, also known as (E)-butenedioic acid, trans-butenedioic acid, or fumaric acid
- (2E,4E)-Hexa-2,4-dienoic acid, also known as (E,E)-sorbic acid or trans,trans-sorbic acid
- (5Z,8Z,11Z,14Z)-Icosa-5,8,11,14-tetraenoic acid, also know as arachidonic acid
Arene substitution pattern
[edit]- ortho-Xylene, also known as o-xylene, 1,2-xylene, or 1,2-dimethylbenzene
- meta-Xylene, also known as m-xylene, 1,3-xylene, or 1,3-dimethylbenzene
- para-Xylene, also known as p-xylene, 1,4-xylene, or 1,4-dimethylbenzene
Skeletal isomers
[edit]- sec-Butylbenzene, also known as s-butylbenzene or (butan-2-yl)benzene
- tert-Butylbenzene, also known as t-butylbenzene
- n-butane, now just butane
- i-butane, also known as isobutane
Other formatting
[edit]- endo-borneol
- exo-Norborneol
--[[
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