Documentation for this module may be created at Module:Sandbox/NguoiDungKhongDinhDanh/Foobar/doc

--[[
  __  __             _         _              _     _____  ____        _      _ 
 |  \/  |  ___    __| | _   _ | |  ___  _    / \   |  ___|/ ___|__  __| |__  | |
 | |\/| | / _ \  / _` || | | || | / _ \(_)  / _ \  | |_   \___ \\ \/ /| '_ \ | |
 | |  | || (_) || (_| || |_| || ||  __/ _  / ___ \ |  _|   ___) |>  < | | | || |
 |_|  |_| \___/  \__,_| \__,_||_| \___|(_)/_/   \_\|_|    |____//_/\_\|_| |_||_|
 -------------------------------------------------------------------------------
--]]

local p = {}

--[[
			 __     __            _         _      _            
			 \ \   / /__ _  _ __ (_)  __ _ | |__  | |  ___  ___ 
			  \ \ / // _` || '__|| | / _` || '_ \ | | / _ \/ __|
			   \ V /| (_| || |   | || (_| || |_) || ||  __/\__ \
			    \_/  \__,_||_|   |_| \__,_||_.__/ |_| \___||___/
			 ===================================================
--]]

p.d = {}
p.d.k = {
	'lcase', 'ucase', 'length', 'string',
	'int', 'float', 'bool',
	'norm', 'ccnorm', 'ccnorm_contains_any', 'ccnorm_contains_all',
	'specialratio', 'rmspecials',
	'rmdoubles', 'rmwhitespace',
	'count', 'rcount', 'get_matches',
	'ip_in_range',
	'contains_any', 'contains_all', 'equals_to_any',
	'substr', 'strlen', 'strpos', 'str_replace', 'rescape',
	'set', 'set_var',
	
	'like', 'matches', 'in', 'contains',
	'rlike', 'irlike', 'regex',
	'if', 'then', 'else', 'end'
}
p.d.v = {
	'action', 'timestamp', 'wiki_name', 'wiki_language',
	
	'user_editcount', 'user_name', 'user_emailconfirm', 'user_age', 'user_blocked', 'user_groups', 'user_rights',
	
	'page_id', 'page_namespace', 'page_age', 'page_title', 'page_prefixedtitle',
	'page_restrictions_edit', 'page_restrictions_move', 'page_restrictions_upload', 'page_restrictions_create',
	'page_recent_contributors', 'page_first_contributor',
	
	'summary', 'old_wikitext', 'new_wikitext', 'new_size', 'old_size', 'added_lines', 'removed_lines',
	'old_content_model', 'new_content_model',
	'edit_diff', 'edit_diff_pst', 'edit_delta', 'added_lines_pst',
	'new_pst', 'new_html', 'new_text',
	
	'all_links', 'old_links', 'added_links', 'removed_links',
	
	'file_sha1', 'file_size', 'file_width', 'file_height','file_bits_per_channel', 'file_mime', 'file_mediatype',
	
	'moved_to_id', 'moved_to_title', 'moved_to_prefixedtitle', 'moved_to_namespace', 'moved_to_age',
	'moved_to_restrictions_edit', 'moved_to_restrictions_move', 'moved_to_restrictions_upload', 'moved_to_restrictions_create',
	'moved_to_recent_contributors', 'moved_to_first_contributor',
	
	'moved_from_namespace', 'moved_from_title', 'moved_from_prefixedtitle', 'moved_from_id', 'moved_from_age',
	'moved_from_restrictions_edit', 'moved_from_restrictions_move', 'moved_from_restrictions_upload', 'moved_from_restrictions_create',
	'moved_from_recent_contributors', 'moved_from_first_contributor',
	
	'board_id', 'board_namespace', 'board_title', 'board_prefixedtitle',
	
	'accountname', 'global_user_groups',
	'translate_source_text', 'translate_target_language',
	
	'tor_exit_node', 'user_mobile', 'user_app',
	
	'page_views', 'moved_from_views', 'moved_to_views',
	
	'sfs_blocked'
}
p.d.s = {
	[['[^']-']],
	[["[^"]-"]]
}
p.d.o = {
	'+', '-', '*', '**', '/', '%',
	'=', '<', '>', '(', ')',
	'|', '&', '^', '!'
}
p.d.b = {
	'true',
	'false',
	'null'
}
p.d.d = {
	'article_articleid', 'article_namespace', 'article_text', 'article_prefixedtext',
	'article_restrictions_edit', 'article_restrictions_move', 'article_restrictions_upload', 'article_restrictions_create',
	'article_recent_contributors', 'article_first_contributor',
	'moved_to_articleid', 'moved_to_text', 'moved_to_prefixedtext', 'moved_from_text', 'moved_from_prefixedtext', 'moved_from_articleid',
	'board_articleid', 'board_text', 'board_prefixedtext',
	'article_views'
}
p.d.n = {
	1, 2, 3, 4, 5, 6, 7, 8, 9, 0
}
p.d.i = {
	'minor_edit',
	'old_html', 'old_text'
}

p.a = {}
for k, v in pairs(p.d) do
	table.insert(p.a, k)
end

p.s = {}
p.s.s = {}
p.s.t = {}

function p.main(frame)
	local content = frame.args[1] or frame.args['c'] or frame.args['content']
	local sp = p.esc(mw.text.split(content, '\n'))
	local newcontent = {}
	
	for j = 1, 3 do
		for l = 1, #sp do
			if j == 1 then
				p.ins(newcontent, sp[l])
			elseif j == 2 then
				for s = 1, #p.d.s do
					if string.match(sp[l], s) then
						newcontent[l] = sp[l]:gsub(string.match(sp[l], s), c('%0', 's'))
					end
				end
			else
				for f = 1, #p.a do
					for i = 1, #p.d[f] do
						if string.match(sp[l], p.d[f][i]) then
							newcontent[l] = sp[l]:gsub(string.match(sp[l], (p.a[f] ~= 'n' and '' or '[+-]?') .. p.d[f][i]), p.c('%0', x))
						end
					end
				end
			end
		end
	end
	
	return mw.text.listToText(newcontent, '<br>', '<br>')
end

function p.c(c, t)
	local span = mw.html.create('span')
			:attr('class', t)
			:wikitext(c)
	return tostring(span)
end

function p.ins(a, e)
	local i = #a
	a[i+1] = e
	return a
end

function p.esc(t)
	local r = {}
	for i = 1, #t do
		r[i] = mw.text.nowiki(r[i])
	end
	return r
end

-- For attribution: //stackoverflow.com/a/832414
function p.det(s)
	local t = p.split(s)
	local r = {}
	local f = {}
	local b = {false, false}
	
	for i = 1, #t do
		if t[i] == [[']] and not b[2] then
			table.insert(f, {})
			f[#f].s = i
		elseif t[i] == [["]] and not b[1] then
			table.insert(f, {})
			f[#f].s = i
		elseif t[i] == [[']] and b[1] then
			
		elseif t[i] == [["]] and b[2] then
		
		end
	end
	
	if b[1] or b[2] then
		f[#f] = nil
	end
	
	return 0
end

function p.str(s, t)
	local f
end

function p.uns(s)
	
end

function p.split(s)
	local r = {}
	for i in mw.ustring.gmatch(s, '.') do
		table.insert(r, i)
	end
	return r
end

return p