Moduuli:Hiekkalaatikko

Wikiopistosta

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Hiekkalaatikko/ohje

 local p = {}

function p.aritmetiikka()
	local a = 5
	local b = 3
	local result
    result = ';Aritmetiikka\n'

    result = result .. ':a on ' .. a .. '\n'
    result = result .. ':b on ' .. b .. '\n'
    result = result .. ':a + b on ' .. a + b .. '\n'
    result = result .. ':a - b on ' .. a - b .. '\n'
    result = result .. ':a * b on ' .. a * b .. '\n'
    result = result .. ':a / b on ' .. a / b .. '\n'
    result = result .. ':a % b on ' .. a % b .. '\n'
    result = result .. ':a ^ b on ' .. a ^ b .. '\n'
    result = result .. ':-a on ' .. -a .. '\n'
	return result
end


function p.relaatiot()
    local a = 5
    local b = 3
    local result
 
    result = ';Relaatiot\n'
    result = result .. ':a on ' .. a .. '\n'
    result = result .. ':b on ' .. b .. '\n'
    result = result .. ':a == b on ' .. tostring(a == b) .. '\n'
    result = result .. ':a ~= b on ' .. tostring(a ~= b) .. '\n'
    result = result .. ':a < b on ' .. tostring(a < b) .. '\n'
    result = result .. ':a > b on ' .. tostring(a > b) .. '\n'
    result = result .. ':a <= b on ' .. tostring(a <= b) .. '\n'
    result = result .. ':a >= b on ' .. tostring(a >= b) .. '\n'
 
    return result
end
 		
function p.loogiset()
    local a = 5
    local b = 3
    local result
 
    result = ';Loogiset\n'
    result = result .. ':a on ' .. a .. '\n'
    result = result .. ':b on ' .. b .. '\n'
    result = result .. ':a < b and b < a on ' .. tostring(a < b and b < a) .. '\n'
    result = result .. ':a < b or b < a on ' .. tostring(a < b or b < a) .. '\n'
    result = result .. ':a < b on ' .. tostring(a < b) .. '\n'
    result = result .. ':not (a < b) on ' .. tostring(not (a < b)) .. '\n'
 
    return result
end


function p.length()
    local string = 'Tämä on merkkijono'
    local result
 
    result = ';Pituus\n'
    result = result .. ':Merkkijonon "' .. string .. '" pituus on ' .. #string
 
    return result
end

function p.ehdot()
    local tunti
    local result
 
    tunti = tonumber(os.date('%H'))
    if tunti < 9 then
        result = 'Hyvää huomenta!'
    elseif tunti < 15 then
        result = 'Hyvää iltapäivää!'
    else
        result = 'Hyvää iltaa!'
    end
 
    return result
end


function p.forloop()
    local i
    local result
 
    result = ';for\n'
    for i = 2, 10, 2 do
        result = result .. ":i = " .. i .. '\n'
    end
 
    return result
end
 
function p.whileloop()
    local i
    local result
 
    result = ';while\n'
    i = 2
    while i <= 10 do 
        result = result .. ":i = " .. i .. '\n'
        i = i + 2
    end
 
    return result
end
 
function p.repeatloop()
    local i
    local result
 
    result = ';repeat\n'
    i = 2
    repeat 
        result = result .. ":i = " .. i .. '\n'
        i = i + 2
    until i > 10
 
    return result
end


local function toCelsius(f)
    return (f - 32) * 5 / 9
end

local function toFahrenheit(c)
    return c * 9 / 5 + 32
end

function p.functions()
    local temperature
    local result
	
    result = ';Fahrenheit-asteluvut Celsius-asteikolle\n'
    for temperature = 0, 100, 10 do
        result = result .. ':' .. temperature .. ' °F on ' .. string.format('%.1f', toCelsius(temperature)) .. ' °C\n'
    end
 
    result = result .. ';Celsius-asteluvut Fahrenheit-asteikolle\n'
    for temperature = 0, 100, 10 do
        result = result .. ':' .. temperature .. ' °C on ' .. string.format('%.1f', toFahrenheit(temperature)) .. ' °F\n'
    end
    return result
end


local function tableToString(t)
    local key
    local value
    local result
 
    result = ''
 
    for key, value in pairs(t) do
        if (tonumber(key) ~= nil) then
            result = result .. ':table[' .. key .. '] on ' .. value .. '\n' 
        else
            result = result .. ':table[\'' .. key .. '\'] on ' .. value .. '\n' 
        end
    end
 
    return result
end
 
function p.sequence()
    local numbers = {10, 20, 30}
    local result
 
    result = ';jono\n'
    result = result .. tableToString(numbers)
 
    return result
end
 
function p.dictionary()
    local languages = {
        ['de'] = 'saksa',
        ['en'] = 'englanti', 
        ['es'] = 'espanja', 
        ['fi'] = 'suomi',
        ['fr'] = 'ranska',
        ['it'] = 'italia',
        ['ja'] = 'japani',
        ['ko'] = 'korea',
        ['ru'] = 'venäjä',
        ['zh'] = 'kiina'
    }
    local result
 
    result = ';sanakirja\n'
    result = result .. tableToString(languages)
 
    return result
end

 
local function reciprocal1(value)
    return 1 / value
end
 
function p.test1(frame)
    local value = frame.args[1]
    return reciprocal1(value)
end
 
local function reciprocal2(value)
    if value == nil then
        error('value must exist')
    end
    if tonumber(value) == nil then
        error('value must be a number')
    end
    if tonumber(value) == 0 then
        error('value must not be 0')
    end
    return 1 / value
end
 
function p.test2(frame)
    local value = frame.args[1]
    return reciprocal2(value)
end
 
local function reciprocal3(value)
    assert(value, 'value must exist')
    assert(tonumber(value), 'value must be a number')
    assert(tonumber(value) ~= 0, 'value must not be zero')
    return 1 / value
end
 
function p.test3(frame)
    local value = frame.args[1]
    return reciprocal3(value)
end
 
function p.test4(frame)
    local value = frame.args[1]
    if pcall(function () result = reciprocal3(value) end) then 
        return result
    else
        return 'Error: Value must exist, must be numeric, and not zero.'
    end
end
 
function p.args(frame)
    return ';args\n' .. tableToString(frame.args)
end

function p.callParserFunction(frame)
    return ';callParserFunction\n:' .. frame:callParserFunction('#time', 'Y-m-d H:i:s') .. '\n'
end

function p.expandTemplate(frame)
    return ';expandTemplate\n:' .. frame:expandTemplate({title = 'Malline:Hiekkalaatikko', args = {'arg1', 'arg2'}}) .. '\n'
end

function p.extensionTag(frame)
    return ';extensionTag\n:' .. frame:extensionTag('nowiki', '[[text]]', {}) .. '\n'
end

function p.getParent(frame)
	frame = frame:getParent()
    return ';getParent\n:' .. frame:getTitle() .. '\n'
end

function p.getTitle(frame)
    return ';getTitle\n:' .. frame:getTitle() .. '\n'
end

function p.canTalk(frame)
    local title = mw.title.getCurrentTitle()
    return ';canTalk\n:' .. tostring(title.canTalk) .. '\n'
end
 
function p.baseText(frame)
    local title = mw.title.getCurrentTitle()
    return ';baseText\n:' .. tostring(title.baseText) .. '\n'
end
 
function p.exists(frame)
    local title = mw.title.getCurrentTitle()
    return ';exists\n:' .. tostring(title.exists) .. '\n'
end
 
function p.fileExists(frame)
    local title = mw.title.getCurrentTitle()
    return ';fileExists\n:' .. tostring(title.fileExists) .. '\n'
end
 
function p.fragment(frame)
    local title = mw.title.getCurrentTitle()
    return ';fragment\n:' .. title.fragment .. '\n'
end
 
function p.fullText(frame)
    local title = mw.title.getCurrentTitle()
    return ';fullText\n:' .. title.fullText .. '\n'
end
 
function p.getContent(frame)
    local text = mw.text.trim(frame.args[1])
    local namespace = mw.text.trim(frame.args[2])
    local title = mw.title.new(text, namespace)
    return ';getContent\n<blockquote>' .. title:getContent() .. '</blockquote>\n'
end
 
function p.id(frame)
    local title = mw.title.getCurrentTitle();
    return ';id\n:' .. title.id .. '\n'
end
 
function p.inNamespace(frame)
    local title = mw.title.getCurrentTitle();
    return ';inNamespace\n:' .. tostring(title:inNamespace(0)) .. '\n'
end
 
function p.inNamespaces(frame)
    local title = mw.title.getCurrentTitle();
    return ';inNamespaces\n:' .. tostring(title:inNamespaces(0)) .. '\n'
end
 
function p.interwiki(frame)
    local title = mw.title.getCurrentTitle();
    return ';interwiki\n:' .. title.interwiki .. '\n'
end
 
function p.isContentPage(frame)
    local title = mw.title.getCurrentTitle();
    return ';isContentPage\n:' .. tostring(title.isContentPage) .. '\n'
end
 
function p.isExternal(frame)
    local title = mw.title.getCurrentTitle();
    return ';isExternal\n:' .. tostring(title.isExternal) .. '\n'
end
 
function p.isLocal(frame)
    local title = mw.title.getCurrentTitle();
    return ';isLocal\n:' .. tostring(title.isLocal) .. '\n'
end
 
function p.isRedirect(frame)
    local title = mw.title.getCurrentTitle();
    return ';isRedirect\n:' .. tostring(title.isRedirect) .. '\n'
end
 
function p.isSpecialPage(frame)
    local title = mw.title.getCurrentTitle();
    return ';isSpecialPage\n:' .. tostring(title.isSpecialPage) .. '\n'
end
 
function p.isSubpage(frame)
    local title = mw.title.getCurrentTitle();
    return ';isSubpage\n:' .. tostring(title.isSubpage) .. '\n'
end
 
function p.isTalkPage(frame)
    local title = mw.title.getCurrentTitle();
    return ';isTalkPage\n:' .. tostring(title.isTalkPage) .. '\n'
end
 
function p.isSubpageOf(frame)
    local title = mw.title.getCurrentTitle();
    local text = mw.text.trim(frame.args[1])
    local namespace = mw.text.trim(frame.args[2])
    local title2 = mw.title.new(text, namespace)
    return ';isSubpageOf\n:' .. tostring(title:isSubpageOf(title2)) .. '\n'
end
 
function p.new(frame)
    local text = mw.text.trim(frame.args[1])
    local namespace = mw.text.trim(frame.args[2])
    local title = mw.title.new(text, namespace)
    return ';new\n:' .. title.id .. '\n'
end
 
function p.nsText(frame)
    local title = mw.title.getCurrentTitle();
    return ';nsText\n:' .. title.nsText .. '\n'
end
 
function p.prefixedText(frame)
    local title = mw.title.getCurrentTitle()
    return ';prefixedText\n:' .. title.prefixedText .. '\n'
end
 
function p.rootText(frame)
    local title = mw.title.getCurrentTitle()
    return ';rootText\n:' .. title.rootText .. '\n'
end
 
function p.subjectNsText(frame)
    local title = mw.title.getCurrentTitle()
    return ';subjectNsText\n:' .. title.subjectNsText .. '\n'
end
 
function p.subpageText(frame)
    local title = mw.title.getCurrentTitle()
    return ';subpageText\n:' .. title.subpageText .. '\n'
end
 
function p.text(frame)
    local title = mw.title.getCurrentTitle()
    return ';text\n:' .. title.text .. '\n'
end


function p.lopputeksti(frame)
	return ';sub\n:string.sub(frame.args[1]) on' .. string.sub(frame.args[1], frame.args[2], string.len(frame.args[1])) .. '\n'
end

function p.korvaa(frame)
	local teksti = frame.args['teksti']
	teksti = string.gsub(teksti, '%[%[.*AAA', '' )
	teksti = string.gsub(teksti, '%]%]', '' )
	return teksti
end


 local function poista_linkit(t)
	local result = t
	
    -- poistetaan sisäiset linkitykset
    if string.find(result,'%[%[.-|') ~= nil then
    	result = string.gsub(result, '%[%[.-|', '' )
    	result = string.gsub(result, '%]%]', '' )
    elseif string.find(result, '%[%[.-%]%]') then
    	result = string.gsub(result, '%[%[', '' )
    	result = string.gsub(result, '%]%]', '' )
	end
	
	--poistetaan ulkoiset linkitykset
	if string.find(result, '%[http.-%s' ) ~= nil then
		result = string.gsub(result, '%[http.-%s', '' )
		result = string.gsub(result, '%]', '' )
	end
	return result
 end
 
 local function hakemistolinkki(kohde, otsake, otsaketaso)
	local linkin_alkuosa, linkin_loppuosa
 	local linkki

--	local i
	
--[[	for i = 1, (otsaketaso-1) do
		linkki = linkki .. '#'
	end
]]

	if(otsake ~= nil) then 
		linkin_alkuosa = kohde..'#'..mw.text.trim(otsake)
		linkin_loppuosa = mw.text.trim(otsake)
		--alkup.
		--linkki = linkki..' [['..kohde..'#'..mw.text.trim(otsake)..'|'..mw.text.trim(otsake)..']]'
	else 
		linkki = ''
		return linkki
	end

	linkin_alkuosa = string.gsub(linkin_alkuosa, '{', '%%7B' ) 
	linkin_alkuosa = string.gsub(linkin_alkuosa, '}', '%%7D' ) 
	linkin_alkuosa = string.gsub(linkin_alkuosa, '%[', '%%5B' ) 
	linkin_alkuosa = string.gsub(linkin_alkuosa, '%]', '%%5D' ) 
--	linkin_loppuosa = string.gsub(linkin_loppuosa, '{', '<nowiki>{</nowiki>' )
	
	linkki = '[['..linkin_alkuosa..'|'..linkin_loppuosa..']]'
	return linkki
end

 
 	
function p.find()
    return ';find\n:string.find(\.\.\.) is ' .. string.find('Olipa kerran == \n==teksti', '==.\n') .. '\n'
end
 
 function p.otsikot(frame)
 	
    assert(frame.args['sivu'], 'Teksti 1')
    assert(frame.args['nimiavaruus'], 'Teksti 2')

    local text = mw.text.trim(frame.args['sivu'])
    local namespace = mw.text.trim(frame.args['nimiavaruus'])
    local title = mw.title.new(text, namespace)
	local aineisto = title:getContent()
	local alku
	local loppu
	local i
	local taso
	local aiempi_taso
	local otsikko
	local korjaus = 0
	local runko
	local result = ''
	local hakemisto = '<ol>'
	
	--result = result .. '\nT: ' .. text .. '\nfullText:' .. title.fullText .. '\nRoot: ' .. title.rootText 
	--result = result .. '\nBase: ' .. title.baseText
	

	
	alku = string.find(aineisto, '\n==')
	loppu = string.find(aineisto, '\n', alku+1)

	if alku == nil or loppu == nil then
		return 'Otsikoita ei löytynyt.'
	end

	if (string.find(string.sub(aineisto, alku, loppu),'=%s\n') ) then
    	korjaus = 1
	else
    	korjaus = 0
	end
	

	aiempi_taso = 2
	
	
	while alku ~= nil and loppu ~= nil do
		
		i = 1
		    while string.sub(aineisto,alku+i,alku+i) == '=' do
        		i = i + 1
        	end
        taso = i - 1
        
        if taso > aiempi_taso then
			for i = 1, (taso - aiempi_taso) do 
        		hakemisto = hakemisto .. '<ol>'
        	end
        elseif taso < aiempi_taso then
        	for i = 1, (aiempi_taso - taso) do
        		hakemisto = hakemisto .. '</ol>'
        	end
        end
        
        aiempi_taso = taso

        otsikko = string.sub(aineisto, alku, loppu)
        
        if (string.find(otsikko,'=%s\n') ) then
        	korjaus = 1
        else
        	korjaus = 0
        end
        
        runko = string.sub(otsikko,taso+2,-(taso+korjaus+2))
		result = result .. otsikko .. '\nTaso: ' .. taso .. '\nRunko: ' .. runko
		result = result .. '\nRaaka: ' .. poista_linkit(runko)
		result = result .. '\nmw.text.nowiki: ' .. mw.text.nowiki(runko)
    	result = result .. '\nlinkki: '  .. hakemistolinkki(title.fullText, poista_linkit(runko), taso) .. '\n'
    	
        hakemisto = hakemisto..'<li>'
    	hakemisto = hakemisto .. hakemistolinkki(title.fullText, poista_linkit(runko),taso) .. '</li>'
    	


		aineisto = string.sub(aineisto, loppu, #aineisto)
		alku = string.find(aineisto, '\n==')
		if (alku ~= nil) then 
			loppu = string.find(aineisto, '\n', alku+1)
		end

	end

	return hakemisto
end


local function hlink( merkkijono )
    local mjono = merkkijono
    local ide = string.sub( mjono, 1, -2 )
    local result
	result = 'https://www.elonet.fi/fi/henkilo/'..tonumber(ide,36) 

	if string.sub(mjono, -1) ~= '/' then
    	result = result .. string.sub(mjono, -1)
    end
    
    return result
end

function laske_S(s1, s2)
    return select(2, s1:gsub(s2, ""))
end
 
function p.testi_jep(frame)

    local s = frame.args[1]
    local result = s
    local a = '%+'
	 
	 s  = laske_S(s, 'sos'..a)
	 
	 if s == 0 then
	 	s = 'Nolla'
	 elseif s > 2 then
	 	s ='Tulos oli ' .. s
	 end

	result = string.gsub(result, "'?''(.-)'''?", '%1')

	
	if string.find(s,'https?://.?.?.?.?elonet.fi/title/ek[%a%d]-/[%a%d%#][%a%d%#]-[%s|<%]]') ~= nil then
		s = 'aAa'
	end
	
	if string.find(s,'https?://.?.?.?.?elonet.fi/title/ek[%a%d]-[/|\n%s%]<]') ~= nil then
		s = 'aAa'
	end
	
	if string.find(s,'https?://.?.?.?.?elonet.fi/name/he[%a%d]-/[%a%d%#][%a%d%#]-[%s|<%]]') ~= nil then
		s = string.gsub(s, '(https?://.?.?.?.?elonet.fi/name/he[%a%d]-/)[%a%d%#][%a%d%#]-([%s|<%]])', 
			'%1'..'%2')
	end

	if string.find(s,'https?://.?.?.?.?elonet.fi/name/he[%a%d]-[/|\n%s%]<]') ~= nil then
		s = string.gsub(s, 'https?://.?.?.?.?elonet.fi/name/he([%a%d]-[/|\n%s%]<])', 
		hlink)
	end

    return result
end




return p