FANDOM


local p = {}
local getArgs = require('Dev:Arguments').getArgs
 
 
function p.empty(s)
    local output = false
 
    if type(s) == "nil" 
        then output = true
        elseif type(s) == "string" 
            then
                if s == "" 
                    then output = true
                end
            elseif type(s) == "table" 
                then
                    if next(s) == nil 
                        then output = true
                    end
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
--checks if page exists or not
function p.exists(frame)
    local args = getArgs(frame)
    local page = args[1]
 
    if not p.empty(page) and mw.title.new(page).exists
        then return true
        else return false
    end
end
 
 
------------------------------------------------------------------------------------------
-- checks if 'text' is part of the 'list' or not
function p.in_list(list, text)
    local output = false
 
    if not p.empty(list) and not p.empty(text)
        then 
            for i, v in ipairs( list ) do
                if v == text
                    then 
                        output = true 
                        break
                end
            end
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
-- adds all elements from table2 into table1
function p.join_tables(table1, table2)
    local output = {}
 
    if not p.empty(table1)
        then 
            if type(table1) == "table"  
                then output = table1
                else table.insert(output, table1)
            end
    end
 
    if not p.empty(table2) and type(table2) == "table"
        then
            for i = 1, #table2 do
                table.insert(output, table2[i])
            end
    end
 
    return output
end
 
 
 
------------------------------------------------------------------------------------------
function p.wuxiaworld(frame)
    return 'http://www.wuxiaworld.com/novel/emperors-domination/emperor-chapter-'
end
 
 
------------------------------------------------------------------------------------------
function p.plural_noun(frame)
	local args = getArgs(frame)
	local text = args[1]
	local suffix1 = ''
	local suffix2 = ''
	local first_letter
	local output = ''
 
	if not p.empty(text)
		then
		    first_letter = string.sub(text,1,1)
			suffix1 = string.sub(text,-1)
			suffix2 = string.sub(text,-2)
			text = string.lower(text)
			if text == 'child'
			    then output = 'children'
			    elseif text == 'goose'
			        then output = 'geese'
			    elseif text == 'man'
			        then output = 'men'
			    elseif text == 'woman'
			        then output = 'women'
			    elseif text == 'tooth'
			        then output = 'teeth'
			    elseif text == 'foot'
			        then output = 'feet'
			    elseif text == 'mouse'
			        then output = 'mice'
			    elseif text == 'person'
			        then output = 'people'
			    elseif p.in_list( {'deer', 'sheep', 'series', 'species'}, text )
			        then output = text
			    elseif suffix2 == 'is'
			        then output = string.sub(text, 1, #text-2)..'es'
			    elseif p.in_list( {'s', 'x', 'z'}, suffix1 ) or p.in_list( {'ch', 'sh', 'ss'}, suffix2 )
				    then output = text..'es'
				elseif (suffix1 == 'f' or suffix2 == 'fe') and not p.in_list( {'roof', 'belief', 'chef', 'chief'}, text )
				    then 
				        if suffix1 == 'f'
				            then output = string.sub(text, 1, #text-1)..'ves'
				            else output = string.sub(text, 1, #text-2)..'ves'
				        end
				elseif suffix1 == 'y' and string.match(string.sub(text, #text-1, #text-1), '[bcdfghjklmnpqrstvwxyz]')
				    then output = string.sub(text, 1, #text-1)..'ies'
				elseif suffix1 == 'o' and not p.in_list( {'photo', 'piano', 'halo'}, text )
				    then output = text..'es'
				elseif suffix2 == 'us'
				    then output = string.sub(text, 1, #text-2)..'i'
				elseif suffix2 == 'on'
				    then output = string.sub(text, 1, #text-2)..'a'
				else output = text..'s'
			end
 
			output = first_letter..string.sub(output, 2)
	end
 
	return output
end
 
 
--************************************************************************************************
--**********		LINKS AND CATEGORIES		**********--
--************************************************************************************************
--------------------------------------------------------------------------------------------------
--returns true if text is a wikilink
function p.is_link(link)
    if not p.empty(link) and string.find(link, "[[",1,true) ~= nil and string.find(link, "]]",1,true) ~= nil
        then return true
        else return false
    end
end
 
 
--------------------------------------------------------------------------------------------------
--Check if 'link' is a wikilink. If yes, then check if it has | inside. If yes, then return 'part' part of it. If 'link' is not a wikilink, returns 'link'.
function p.break_link(frame)
    local args = getArgs(frame)
    local link = args[1] or ''
    local part = args[2]
    local i
    local j
    local output = link
 
    if not p.empty(part)
        then part = tonumber(part)
        else part = 1
    end
 
    i = string.find(link, "]]",1,true)
    if i ~= nil
        then
            j = string.find(link, "|", 1, true)
            if j == nil 
                then output = string.sub(link, 3, i-1) 
                elseif j < i
                    then
                        if part == 2 
                            then output = string.sub(link, j+1, i-1)
                            else output = string.sub(link, 3, j-1)
                        end
                    else output = string.sub(link, 3, i-1) 
            end
    end
 
    return output
end
 
 
--------------------------------------------------------------------------------------------------
-- creates wikilink
function p.link(link, text, bold)
    local tag = mw.html.create('span'):css('font-weight', 'bold')
    local output = ''
 
    if not p.empty(link)
        then 
            if not p.empty(text)
                then text = '|'..text
                else text = '|'..link
            end
            output = '[['..link..text..']]'
            if not p.empty(bold)
                then output = tostring( tag:wikitext(output) )
            end
    end
 
    return output
end
 
--------------------------------------------------------------------------------------------------
-- creates wikilink to category
function p.category_link(category, text)
    local output = ''
 
    if not p.empty(category)
        then 
            if not p.empty(text)
                then text = '|'..text
                else text = '|'..category
            end
            output = '[[:Category:'..category..text..']]'
    end
 
    return output
end
 
 
--------------------------------------------------------------------------------------------------
function p.add_category(category, sortname)
    local output = ''
 
    if not p.empty(category)
        then 
            if not p.empty(sortname)
                then sortname = '|'..sortname
                else sortname = ''
            end
            output = '[[Category:'..category..sortname..']]'
    end
 
    return output
end
 
 
--------------------------------------------------------------------------------------------------
-- transform 'categories' into list of categories
function p.add_categories(categories, sortname)
    local i
    local output = {}
 
    if not p.empty(categories)
        then
            for i = 1,#categories do
                if not p.empty(categories[i])
                    then table.insert( output, p.add_category(categories[i], sortname) )
                end
            end
    end
 
    return table.concat(output)
end
 
 
--------------------------------------------------------------------------------------------------
-- counts pages in 'category'
function p.pages_in_category(category, t)
    --t can be one of '*', 'all', 'pages', 'subcats', or 'files'
    --'pages' by default
    if p.empty(t)
        then t ='pages'
    end
 
    category = string.gsub(category,'&#39;',"'")
 
    return mw.site.stats.pagesInCategory(category, t)
end
 
 
--************************************************************************************************
--**********		DESIGN		**********--
--************************************************************************************************
-- main color scheme
function p.get_main_color(number)
	local output = ''
 
	if number == 1
		then output = '#fdb632'
		elseif number == 2
			then output = '#fec356'
		elseif number == 3
			then output = '#fcd07e'
		elseif number == 4
			then output = '#fcdea4'
		elseif number == 5
			then output = '#fcebca'
		elseif number == 6
			then output = '#fcf4e3'
	end
 
	return output
end
 
 
------------------------------------------------------------------------------------------
function p.green(frame)
    local args = getArgs (frame)
    local text = args[1]
    local output = ''
 
    if not p.empty(text)
        then 
            output = mw.html.create( 'span' )
                :css( 'color', 'darkgreen' )
                :css( 'font-weight', 'bold' )
                :css( 'font-style', 'italic' )
                :css( 'font-size', 'smaller' )
                :wikitext('('..text..')')
                :done()
            output = tostring(output)
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
function p.show_hide(frame)
    local args = getArgs (frame)
    local title = args['title'] or ' '
    local body = args['body'] or args['text'] or ''
    local collapsed = args['collapsed']
    local clear = args['clear'] or 'both'
    local border = args['border'] or p.get_main_color(1)
    local extrastyle = args['extrastyle'] or ''
    local extrastyle2 = args['extrastyle2'] or ''
    local extrastyle3 = args['extrastyle3'] or ''
    local width = args['width'] or '100%'
    local background = args['background'] or p.get_main_color(5)
    local titlestyle = args['titlestyle'] or ''
 
    if p.empty(collapsed) or collapsed == 'true' or collapsed == true
        then collapsed = ' mw-collapsed'
        else collapsed = ''
    end
 
    local tag_td = mw.html.create( 'td' )
        :css( 'margin', '0.5em' )
        :css( 'font-size', '12px' )
        :wikitext(body)
        :done()
    if not p.empty(extrastyle3)
        then tag_td:cssText (extrastyle3):done()
    end
    tag_td = '<tr>'..tostring(tag_td)..'</tr>'
 
    local tag_div = mw.html.create( 'div' )
        :css( 'font-weight', 'bold' )
        :wikitext(title)
        :done()
    if not p.empty(titlestyle)
        then tag_div:cssText (titlestyle):done()
    end
    tag_div = tostring(tag_div)
 
    local tag_th = mw.html.create( 'th' )
        :attr('colspan', '1')
        :css( 'background-color', background )
        :css( 'font-size', '12px' )
        :wikitext(tag_div)
        :done()
    if not p.empty(extrastyle2)
        then tag_th:cssText (extrastyle2):done()
    end
    tag_th = '<tr>'..tostring(tag_th)..'</tr>'
 
    local tag_table = mw.html.create( 'table' )
        :addClass('mw-collapsible'..collapsed)
        :css( 'clear', clear )
        :css( 'border', '1px solid '..border )
        :css( 'font-size', '12px' )
        :css( 'width', width )
        :wikitext(tag_th..tag_td)
        :done()
    if not p.empty(extrastyle)
        then tag_table:cssText (extrastyle):done()
    end
 
    return tostring(tag_table) 
end
 
 
------------------------------------------------------------------------------------------
-- creates header
function p.header(text, level, align)
	local output = ''
	level = level or '2'
	align = align or 'left'
 
	if not p.empty(text)
		then output = '\n'..tostring( mw.html.create( 'h'..level ):css('text-align', align):wikitext( text ) )
	end
 
	return output
end
 
 
------------------------------------------------------------------------------------------
function p.span(text)
    local italic = mw.html.create( 'span' ):css( 'font-style', 'italic' )
    local bold = mw.html.create( 'span' ):css( 'font-weight', 'bold' )
    local both = mw.html.create( 'span' ):css( 'font-weight', 'bold' ):css( 'font-style', 'italic' )
    local output = { italic = '', bold = '', both = '' }
 
    if not p.empty(text)
        then
            output = {
                italic  = tostring(italic:wikitext(text):done()),
                bold    = tostring(bold:wikitext(text):done()),
                both    = tostring(both:wikitext(text):done()),
            }
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
function p.tooltip(frame)
    local args = getArgs (frame)
    local text = args[1]
    local tip = args[2]
	local output = ''
 
	if not p.empty(text)
		then
			tip = tip or ''
			output = mw.html.create('span')
				:css('border-bottom', '1px dotted')
				:css('cursor', 'help')
				:attr('title', tip)
				:wikitext(text)
				:done()
			output = tostring(output)
	end
 
	return output
end
 
 
------------------------------------------------------------------------------------------
function p.translation(frame)
    local args = getArgs (frame)
    local literal = args[1] or args['literal']
    local original = args[2] or args['original']
    local output = ''
 
    if not p.empty(literal)
        then
            if not p.empty(original)
                then 
                    output = '('..original..': '..p.span(literal).italic..')'
                    output = p.tooltip( {output, 'Literal Meaning'} )
                else
                    output = mw.html.create('span')
                                :css('font-size', '85%')
                                :wikitext( p.tooltip( {literal, 'Literal Meaning'} ) )
				                :done()
				    output = tostring(output)
			end
    end
 
    return output
end
 
 
--------------------------------------------------------------------------------------------------
-- changes color of link
function p.link_color(frame)
    local args = getArgs(frame)
    local link = args[1]
    local color = args[2]
    local i
    local part1 = ''
    local part2 = ''
    local after_link = ''
    local output = ''
 
    if not p.empty(link) 
        then 
            if p.is_link(link)
                then 
                    i = string.find(link, ']]', 1, true)
                    if i ~= #link
                        then after_link = string.sub(link, i+2, #link)
                    end
                    part1 = p.break_link({link, 1})
                    part2 = p.break_link({link, 2})
                    part2 = tostring( mw.html.create( 'span' ):attr( 'class', color):wikitext(part2):done() )
                    output = p.link(part1, part2)..after_link
                else 
                    output = tostring( mw.html.create( 'span' ):attr( 'class', color):wikitext(link):done() )
        end
    end
 
    return output
end
 
 
--------------------------------------------------------------------------------------------------
--dead or destroyed
function p.dead_or_destroyed(frame)
    local link = getArgs(frame)[1]
    local output = ''
 
    if not p.empty(link)
        then output = p.span( p.link_color({link, 'tag_Death'}) ).italic
    end
 
    return output
end
 
 
--------------------------------------------------------------------------------------------------
function p.arrow(text, arrow)
    local output = ''
 
    if not p.empty(text)
        then 
            output = p.category_link(text, arrow)
            output = tostring( mw.html.create('span'):css('font-size', '36px'):wikitext(output):done() )
    end
 
    return output
end
 
function p.right_arrow(text)
    return p.arrow(text, '→')
end
 
function p.left_arrow(text)
    return p.arrow(text, '←')
end
 
 
--************************************************************************************************
--**********		CONTENT and PAGE TYPE		**********--
--************************************************************************************************
------------------------------------------------------------------------------------------
-- returns content of the page
function p.get_content(pagename)
    local output = ''
 
    if not p.empty(pagename)
        then
        	output = p.break_link({pagename, 1})
        	output = mw.title.new(output):getContent() or ''
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
-- returns namespace of the page
function p.get_namespace(pagename)
    local title = mw.title.new(pagename)
    local output = ''
 
    if not p.empty(pagename)
        then
            if title.namespace == 0
                then 
                    if title.exists
                        then return 'Main'
                    end
                else return title.nsText
            end
            if title.exists 
                then return 'Main'
                elseif p.exists('File:'..pagename)
                    then return "File"
                elseif p.exists('Category:'..pagename)
                    then return 'Category'
                else return ''
            end
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
-- returns type of page based on the name of its main template (works only for templates in 'Infobox' namespace.
function p.get_page_type(pagename)
    local namespace = p.get_namespace(pagename)
    local content = ''
    local s_match = '{{Infobox (.-)\n-|'
    local s_match2 = '{{Infobox_(.-)\n-|'
    local output = namespace
 
    if namespace == 'Main'
        then
            content = p.get_content(pagename)
            if not p.empty(content)
                then
                    s_match = string.match(content, s_match)
                    if not p.empty(s_match)
                        then output = s_match
                        else output = string.match(content, s_match2) or 'Unknown'
                    end
            end
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
--Check if "page" is redirect or not
function p.is_redirect(page)
    local output = false
 
    if not p.empty(page)
        then 
            page = p.break_link({page, 1})
            output = mw.title.new(page).isRedirect
    end
 
    return output
end
 
 
------------------------------------------------------------------------------------------
--return target of redirect
function p.get_redirect_target(page)
    local content = ''
    local output = ''
 
    if not p.empty(page) 
        then
            page = p.break_link({page, 1})
            if p.is_redirect(page)
                then
                    content = p.get_content(page)
                    output = mw.ustring.gsub(content, "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]", "%1") or
                mw.ustring.gsub(content,"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]", "%1")
            end
    end
 
    return output
end
 
 
--************************************************************************************************
--**********		NUMBERS			**********--
--************************************************************************************************
------------------------------------------------------------------------------------------
function p.number_to_text(number)
    local list = {
        ['0']  = 'Zero',
        ['1']  = 'One',
        ['2']  = 'Two',
        ['3']  = 'Three',
        ['4']  = 'Four',
        ['5']  = 'Five',
        ['6']  = 'Six',
        ['7']  = 'Seven',
        ['8']  = 'Eight',
        ['9']  = 'Nine',
        ['10'] = 'Ten',
        ['11'] = 'Eleven',
        ['12'] = 'Twelve',
        ['13'] = 'Thirteen',
        ['14'] = 'Fourteen',
        ['15'] = 'Fifteen',
        ['16'] = 'Sixteen',
        ['17'] = 'Seventeen',
        ['18'] = 'Eighteen',
        ['19'] = 'Nineteen',
        ['20'] = 'Twenty',
    }
 
    return list[tostring(number)] or ''
end
 
 
------------------------------------------------------------------------------------------
--transform 'number' into ordinal number, for example '1' into '1st'
function p.ordinal_number(number)
    local output = ''
 
    number = tonumber(number)
    if number ~= nil
        then
            if number % 10 == 1 and number ~= 11 -- in (1,21,31,41,51,61,71)
                then output = number..'st'
                elseif number % 10 == 2 and number ~= 12 -- in (2,22,32,42,52,62,72)
                    then output = number..'nd'
                    elseif number % 10 == 3 and number ~= 13 --in (3,23,33,43,53,63,73)
                        then output = number..'rd'
                        else output = number..'th'
            end
    end
 
    return output
end
 
 
return p
Community content is available under CC-BY-SA unless otherwise noted.