Difference between revisions of "Module:Sandbox/Erutuon/random"

From blackwiki
Jump to navigation Jump to search
blackwiki>Erutuon
(convenient output class)
blackwiki>Erutuon
Line 4: Line 4:
 
Out_mt.__index = Out_mt
 
Out_mt.__index = Out_mt
  
function Out_mt:add(item)
+
function Out_mt:add(...)
self.i = self.i + 1
+
for i = 1, select('#', ...) do
self[self.i] = item
+
self.i = self.i + 1
 +
self[self.i] = select(i, ...)
 +
end
 
end
 
end
  
Line 21: Line 23:
 
function p.show(frame)
 
function p.show(frame)
 
local out = Out()
 
local out = Out()
out:add('hello')
+
 +
local language_data = require 'Module:Language/data'
 +
local Wiktionary = {
 +
name_to_code = require 'Module:Language/Wiktionary name to code',
 +
code_to_name = require 'Module:Language/Wiktionary code to name',
 +
}
 +
 +
for code, data in pairs(language_data.languages) do
 +
if not Wiktionary.code_to_name[code] then
 +
out:add(code)
 +
if data.name and Wiktionary.name_to_code[data.name] then
 +
out:add(' → ', Wiktionary.name_to_code[data.name])
 +
end
 +
out:add('\n* ')
 +
end
 +
end
 +
 
return out
 
return out
 
end
 
end
  
 
return p
 
return p

Revision as of 22:07, 26 May 2018

Test

  • Lua error in package.lua at line 80: module 'Module:Language/Wiktionary name to code' not found.



local p = {}

local Out_mt = {}
Out_mt.__index = Out_mt

function Out_mt:add(...)
	for i = 1, select('#', ...) do
		self.i = self.i + 1
		self[self.i] = select(i, ...)
	end
end

function Out_mt:__tostring()
	return table.concat(self)
end

function Out()
	local o = setmetatable({}, Out_mt)
	o.i = 0
	return o
end

function p.show(frame)
	local out = Out()
	
	local language_data = require 'Module:Language/data'
	local Wiktionary = {
		name_to_code = require 'Module:Language/Wiktionary name to code',
		code_to_name = require 'Module:Language/Wiktionary code to name',
	}
	
	for code, data in pairs(language_data.languages) do
		if not Wiktionary.code_to_name[code] then
			out:add(code)
			if data.name and Wiktionary.name_to_code[data.name] then
				out:add(' → ', Wiktionary.name_to_code[data.name])
			end
			out:add('\n* ')
		end
	end
	
	return out
end

return p