মডিউল:রসায়ন ২

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে
মডিউল নথি[তৈরি করুন]
local getArgs = require('Module:Arguments').getArgs
local p = {} -- module's table

-- Elements with wiki links
local am = {
	H = "হাইড্রোজেন",
	He = "হিলিয়াম",
	Li = "লিথিয়াম",
	Be = "বেরিলিয়াম",
	B = "বোরন",
	C = "কার্বন",
	N = "নাইট্রোজেন",
	O = "অক্সিজেন",
	F = "ফ্লোরিন",
	Ne = "নিয়ন",
	Na = "  সোডিয়াম ",
	Mg = "ম্যাগনেসিয়াম",
	Al = "অ্যালুমিনিয়াম",
	Si = "সিলিকন",
	P = "ফসফরাস",
	S = "সালফার",
	Cl = "ক্লোরিন",
	Ar = "আর্গন",
	K = "পটাসিয়াম",
	Ca = "ক্যালসিয়াম",
	Sc = "স্ক্যান্ডিয়াম",
	Ti = "টাইটানিয়াম",
	V = "ভ্যানডিয়াম",
	Cr = "ক্রোমিয়াম",
	Mn = "ম্যাঙ্গানিজ",
	Fe = "লোহা",
	Co = "কোবল্ট",
	Ni = "নিকেল",
	Cu = "তামা",
	Zn = "জিঙ্ক",
	Ga = "গ্যালিয়াম",
	Ge = "জার্মানিয়াম",
	As = "আর্সেনিক",
	Se = "সেলেনিয়াম",
	Br = "ব্রোমিন",
	Kr = "ক্রিপ্টন",
	Rb = "রুবিডিয়াম",
	Sr = "স্ট্রনটিয়াম",
	Y = "Yttrium",
	Zr = "জিরকোনিয়াম",
	Nb = "নিওবিয়াম",
	Mo = "মলিবডেনাম",
	Tc = "টেকনেটিয়াম",
	Ru = "রুথেনিয়াম",
	Rh = "রোডিয়াম",
	Pd = "প্যালাডিয়াম",
	Ag = "সিলভার",
	Cd = "ক্যাডমিয়াম",
	In = "ইন্ডিয়াম",
	Sn = "টিন",
	Sb = "অ্যান্টিমনি",
	Te = "টেলুরিয়াম",
	I = "আয়োডিন",
	Xe = "জেনন",
	Cs = "সিসিয়াম",
	Ba = "বেরিয়াম",
	La = "ল্যান্থানাম",
	Ce = "সেরিয়াম",
	Pr = "প্রাসোডিয়ামিয়াম",
	Nd = "নিওডিয়ামিয়াম",
	Pm = "প্রোমিথিয়াম",
	Sm = "সামেরিয়াম",
	Eu = "ইউরোপিয়াম",
	Gd = "গ্যাডোলিনিয়াম",
	Tb = "টার্বিয়াম",
	Dy = "Dysprosium",
	Ho = "হোলমিয়াম",
	Er = "Erbium",
	Tm = "থুলিয়াম",
	Yb = "Ytterbium",
	Lu = "লুটেটিয়াম",
	Hf = "হাফনিয়াম",
	Ta = "ট্যান্টালাম",
	W = "টাংস্টেন",
	Re = "রেনিয়াম",
	Os = "ওসমিয়াম",
	Ir = "ইরিডিয়াম",
	Pt = "প্ল্যাটিনাম",
	Au = "সোনা",
	Hg = "বুধ (উপাদান)",
	Tl = "থ্যালিয়াম",
	Pb = "লিড",
	Bi = "বিসমাথ",
	Po = "পোলোনিয়াম",
	At = "অ্যাস্টাটাইন",
	Rn = "Radon",
	Fr = "ফ্রান্সিয়াম",
	Ra = "রেডিয়াম",
	Ac = "অ্যাক্টিনিয়াম",
	Th = "থোরিয়াম",
	Pa = "প্রোট্যাক্টিনিয়াম",
	U = "ইউরেনিয়াম",
	Np = "নেপচুনিয়াম",
	Pu = "প্লুটোনিয়াম",
	Am = "Americium",
	Cm = "কিউরিয়াম",
	Bk = "বার্কেলিয়াম",
	Cf = "ক্যালিফোর্নিয়াম",
	Es = "আইনস্টাইনিয়াম",
	Fm = "ফার্মিয়াম",
	Md = "মেন্ডেলভিয়াম",
	No = "নোবেলিয়াম",
	Lr = "লরেন্সিয়াম",
	Rf = "রাদারফোর্ডিয়াম",
	Db = "ডবনিয়াম",
	Sg = "সিবোরজিয়াম",
	Bh = "বোহরিয়াম",
	Hs = "হাসিয়াম",
	Mt = "মেইটনেরিয়াম",
	Ds = "ডার্মস্টাডটিয়াম",
	Rg = "রেন্টজেনিয়াম",
	Cp = "কোপার্নিশিয়াম",
	Nh = "নিহোনিয়াম",
	Fl = "ফ্লেরভিয়াম",
	Mc = "মস্কোভিয়াম",
	Lv = "লিভারমোরিয়াম",
	Ts = "টেনেসাইন",
	Og = "ওগেনেসন",
	
	-- গ্রুপ ইত্যাদি উপাদানের মত নাম
	Bn = 'বেনজিল গ্রুপ',
	Bz = 'বেনজয়েল গ্রুপ',
	D = 'ডিউটেরিয়াম',
	Et = 'ইথাইল গ্রুপ',
	Ln = 'ল্যান্থানাইড',
	Nu = 'নিউক্লিওফাইল',
	Ph = 'ফিনাইল গ্রুপ',
	R = 'প্রতিস্থাপক',
	T = 'ট্রিটিয়াম',
	Tf = 'ট্রাইফ্লুরোমিথাইলসালফোনাইল গ্রুপ',
	X = 'হ্যালোজেন',
}

-- Groups which are redirected from their normal target if wikilinked; never
-- autolinked.
local groups = {
	CH3 = 'মিথাইল গ্রুপ',
	CO3 = 'কার্বনেট',
	COOH = 'কারবক্সিল গ্রুপ',
	ClO = 'হাইপোক্লোরাইট',
	ClO2 = 'ক্লোরাইট',
	ClO3 = 'ক্লোরেট',
	ClO4 = 'পারক্লোরেট',
	H2O = 'ক্রিস্টালাইজেশনের জল',
	H3O = 'হাইড্রোনিয়াম',
	NH2 = 'আমিন গ্রুপ',
	NH4 = 'অ্যামোনিয়াম',
	NO3 = 'নাইট্রেট',
	PO3 = 'ফসফাইট',
	PO4 = 'ফসফেট',
	SH = 'থিওল গ্রুপ',
	SO3 = 'সালফাইট',
	SO4 = 'সালফেট',
	SeH = 'সেলেনল গ্রুপ'
}

local T_ELEM = 0         -- token types
local T_NUM = 1          -- number
local T_OPEN = 2         -- open '('
local T_CLOSE = 3        -- close ')'
local T_PM_CHARGE = 4    -- + or −
local T_WATER = 6        -- .xH2O x number
local T_CRYSTAL = 9      -- .x
local T_CHARGE = 8       -- charge (x+), (x-)
local T_SUF_CHARGE = 10  -- suffix and charge e.g. 2+ from H2+
local T_SUF_CHARGE2 = 12 -- suffix and (charge) e.g. 2(2+) from He2(2+)
local T_SPECIAL = 14     -- starting with \ e.g. \d for double bond (=)
local T_SPECIAL2 = 16    -- starting with \y{x} e.g. \i{12} for isotope with mass number 12
local T_ARROW_R = 17     -- match: ->
local T_ARROW_EQ = 18    -- match: <->
local T_UNDERSCORE = 19  -- _{ ... }
local T_CARET = 20       -- ^{ ... }
local T_LINKOPEN = 21    -- Opening of link, always like "[[target|" even if the source wasn't
local T_NOCHANGE = 30    -- Anything else like ☃

function su(up, down)
	if up == "" then
		return ('<sub class="template-chem2-sub">%s</sub>'):format(down)
	end
	if down == "" then
		return ('<sup class="template-chem2-sup">%s</sup>'):format(up)
	end
    return ('<span class="template-chem2-su"><span>%s</span><span>%s</span></span>'):format(up, down)
end

function DotIt()
    return '&middot;'
end

function item(f) -- (iterator) returns one token (type, value) at a time from the formula 'f'
    local i = 1

    return function ()
        local t, x = nil, nil

        if (i == 1) and f:match('^[0-9]', i) then 
            x = f:match('^[%d.]+', i); t = T_NOCHANGE; i = i + x:len();   -- matching coefficient (need a space first)

        elseif i <= f:len() then
            x = f:match('^%s+[%d.]+', i); t = T_NOCHANGE;  -- matching coefficient (need a space first)
            if not x then x = f:match('^%s[+]', i); t = T_NOCHANGE; end       -- matching + (H2O + H2O)
            if not x then x = f:match('^%&%#[%w%d]+%;', i); t = T_NOCHANGE; end       -- &#...;
            if not x then x = f:match('^%<%-%>', i); t = T_ARROW_EQ; end       -- matching <->
            if not x then x = f:match('^%-%>', i); t = T_ARROW_R; end       -- matching ->
            if not x then x = f:match('^%u%l*', i); t = T_ELEM; end        -- matching symbols like Aaaaa
            if not x then x = f:match('^%d+[+-]', i); t = T_SUF_CHARGE; end        -- matching x+, x-
            if not x then x = f:match('^%d+%(%d*[+-]%)', i); t = T_SUF_CHARGE2; end        -- matching x(y+/-), x(+/-)
            if not x then x = f:match('^%(%d*[+-]%)', i); t = T_CHARGE; end        -- matching (x+) (xx+), (x-) (xx-)
            if not x then x = f:match('^[%d.]+', i); t = T_NUM; end        -- matching number
            if not x and (f:match('^%[%[%[[^[]', i) or f:match('^%[[^[]', i)) then
            	i = i + 1; return T_OPEN, '&#91;' end -- escape [[[X or [X (relevant to auto-linking)
            if not x and f:sub(i, i + 1) == '[[' then
            	x = f:match('^%[%[([^]|]*)', i) -- link target
            	local len = x:len() + 3
            	x = '[[' .. (groups[x] or am[x] or x) .. '|'  -- override link target for common groups

            	if f:sub(len + i, len + i) == ']' then
            		-- We're going to read the link twice, once as target and once as
            		-- chemical markup, e.g. [[CH3]] => "[[CH3|", "CH3]]"
            		i = i + 2
            	else
            		i = i + len
            	end
            	return T_LINKOPEN, x
            end
            if not x then x = f:match('^[(|{|%[]', i); t = T_OPEN; end     -- matching ({[
            if not x then x = f:match('^[)|}|%]]', i); t = T_CLOSE; end    -- matching )}]
            if not x then x = f:match('^[+-]', i); t = T_PM_CHARGE; end    -- matching + or -
            if not x then x = f:match('^%*[%d.]*H2O', i); t = T_WATER; end -- Crystal water
            if not x then x = f:match('^%*[%d.]*', i); t = T_CRYSTAL; end  -- Crystal
            if not x then x = f:match('^[\\].{%d+}', i); t = T_SPECIAL2; end -- \y{x}
            if not x then x = f:match('^[\\].', i); t = T_SPECIAL; end -- \x
            if not x then x = f:match('^_{[^}]*}', i); t = T_UNDERSCORE; end -- _{...}
            if not x then x = f:match('^\^{[^}]*}', i); t = T_CARET; end   -- ^{...}
            if not x then x = f:match('^.', i); t = T_NOCHANGE; end  --the rest - one by one
            if x then i = i + x:len(); else i = i + 999; error("Invalid character in formula! : "..f) end
        end
        return t, x
    end
end

function p._chem(args)
    local f = args[1] or ''

    f = string.gsub(f, "–", "-")  -- replace – with - (hyphen not ndash)
    f = string.gsub(f, "−", "-")  -- replace – with - (hyphen not minus sign)

    local formula = ''
    local t, x

    local link = args['link'] or ""
    local auto = args['auto'] or ""
    local seen = {}
    local _debug = false

    if not (link == '') then formula = formula .. "[[" .. link .. "|"; end   -- wikilink start [[link|
 
    for t, x in item(f) do 
    	if _debug then
    		formula = ("%s\n* %d %s"):format(formula, t, x)
        elseif t == T_ELEM then
            if (auto == '') or (not am[x]) or seen[x] then formula = formula .. x
            else formula = ("%s[[%s|%s]]"):format(formula, am[x], x); seen[x] = true
            end
        elseif t == T_COEFFICIENT then formula = formula .. x
        elseif t == T_NUM   then formula = formula .. su("", x);
        elseif t == T_LINKOPEN  then formula = formula .. x;      -- [[Link|
        elseif t == T_OPEN  then formula = formula .. x;          -- ([{
        elseif t == T_CLOSE then formula = formula .. x;          -- )]}
        elseif t == T_PM_CHARGE    then formula = formula .. su(x:gsub("-", "−"), "");
        elseif t == T_SUF_CHARGE then 
            formula = formula .. su(x:match("[+-]"):gsub("-", "−"), x:match("%d+"), "");
        elseif t == T_SUF_CHARGE2 then 
            formula = formula .. su(x:match("%(%d*[+-]"):gsub("-", "−"):sub(2, -1), x:match("%d+"))
        elseif t == T_CHARGE then
        	formula = formula .. "<sup>"
        	if x:match("%d+") then formula = formula .. x:match("%d+"); end
        	formula = formula .. x:match("[%+-]"):gsub("-", "−") .. "</sup>";
        	-- Cannot concatenat a nil value from x:match("%d+");
        elseif t == T_CRYSTAL then formula = formula .. DotIt() .. string.gsub( x, "*", '', 1 );
        elseif t == T_SPECIAL then
            parameter = x:sub(2, 2) -- x fra \x  
            if     parameter == "s" then formula = formula .. "−"   -- single bond
            elseif parameter == "d" then formula = formula .. "="   -- double bond
            elseif parameter == "t" then formula = formula .. "≡"   -- tripple bond
            elseif parameter == "q" then formula = formula .. "≣"   -- Quadruple bond
            elseif parameter == "h" then formula = formula .. "η"   -- η, hapticity
            elseif parameter == "*" then formula = formula .. "*"   -- *, normal *
            elseif parameter == "-" then formula = formula .. "-"   -- -
            elseif parameter == "\\" then formula = formula .. "\\"   -- \
            elseif parameter == "\'" then formula = formula .. "&#39;"   -- html-code for '
            end
        elseif t == T_SPECIAL2 then  -- \y{x}
            parameter = x:sub(2, 2) -- y fra \y{x} 
            if parameter  == "h" then --[[Hapticity]]
                if (auto == '') then formula = formula .. "η<sup>" .. x:match('%d+') .. "</sup>-"
                else
                    formula = formula .. "[[Hapticity|η<sup>" .. x:match('%d+') .. "</sup>]]-"
                end
            elseif parameter == "m" then formula = formula .. "μ<sub>" .. x:match('%d+') .. "</sub>-" -- mu ([[bridging ligand]])
            end
        elseif t == T_WATER then 
            if x:match("^%*[%d.]") then 
                formula = formula .. DotIt() .. x:match("%f[%.%d]%d*%.?%d*%f[^%.%d%]]") .. "H<sub>2</sub>O";
            else
                formula = formula .. DotIt() .. "H<sub>2</sub>O";
            end  
        elseif t == T_UNDERSCORE  then formula = formula .. su("", x:sub(3,-2)) -- x contains _{string}
        elseif t == T_CARET then formula = formula .. su(x:sub(3,-2), "") -- x contains ^{string}
        elseif t == T_ARROW_R then formula = formula .. " → "
        elseif t == T_ARROW_EQ then formula = formula .. " ⇌ "
        elseif t == T_NOCHANGE  then formula = formula .. x;  -- The rest - everything which isn't captured by the regular expresions.
     
        else error('unreachable - ???') end -- in fact, unreachable
    end

    if not (link == nil or link == '') then formula = formula .. "]]"; end   -- wikilink closing ]]
    formula = mw.getCurrentFrame():preprocess('<templatestyles src="মডিউল:রসায়ন ২/শৈলী.css"/>') ..
        '<span class="chemf nowrap">' .. formula .. '</span>'
    if args[2] or args[3] or args[4] then
        formula = formula .. require('Module:If preview')._warning{
'&#123;&#123;chem2&#125;&#125; was called with multiple positional arguments. It should have just one, e.g. &#123;&#123;chem2&#124;H2O&#125;&#125;.'
        }
    end
    return formula
end

function p.chem(frame)
    local args = getArgs(frame)
    return p._chem(args)
end

-- PRIVATE function to generate documentation.
function p._autodoc(frame)
	local TableTools = require('Module:TableTools') -- we don't want to load this on articles for no reason
	local result = {
		'===Elements and element-style symbols===\nThese may be automatically linked or used as if they were redirects.\n',
		'{| class="wikitable"\n! Symbol !! Link target\n'
	}
	for symbol, target in TableTools.sortedPairs(am) do
		result[#result + 1] = ('|-\n| %s || [[%s]]\n'):format(symbol, target)
	end
	result[#result + 1] = '|}\n===Groups===\nThese must be linked manually; they work as if they were redirects.\n'
	result[#result + 1] = '{| class="wikitable"\n! Symbol !! Link target\n'
	for symbol, target in TableTools.sortedPairs(groups) do
		result[#result + 1] = ('|-\n| %s || [[%s]]\n'):format(symbol, target)
	end
	result[#result + 1] = '|}'
	return table.concat(result)
end

return p