Module:User:Jberkel/sandbox

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Jberkel, for their own experimentation. Items in this module may be added and removed at Jberkel's discretion; do not rely on this module's stability.


local export = {}

local function get_all_matches(str, pattern)
	local matches = require 'Module:array'()
	for match in str:gmatch(pattern) do
		matches:insert(match)
	end
	return matches
end

function export.generate_table(data, code_in_page_name)
	local output = require 'Module:array'()
	local language_cache = require 'Module:languages/cache'
	local tag_link = require 'Module:script tag link'.tag_link
	
	if code_in_page_name == 'all' or code_in_page_name and not language_cache[code_in_page_name] then
		code_in_page_name = nil
	end
	
	output:insert(require 'Module:TemplateStyles' 'Template:User:Erutuon/missing entries.css')
	
	output:insert([[
{| class="wikitable sortable redlink-list"
|-
! data-sort-type="number" | Count
]]

.. (not code_in_page_name and [[
! data-sort-type="text" | Language<br>code
! data-sort-type="text" | Language name
]] or [[]])

.. [[
! Entry
! class="unsortable" | Sources]])
	
	for line in data:gmatch '[^\n]+' do
		output:insert '|-\n'
		local language, link_page, entries = line:match '^(.-)\t(.-)\t(.+)$'
		
		if not language then
			error('The line ' .. line .. ' did not match the pattern.')
		end
		
		entries = get_all_matches(entries, '[^\t]+')
		
		output:insert('| ' .. #entries)
		
		if not code_in_page_name then
			output:insert('| ' .. language)
			output:insert('| [[:Category:'
				.. language_cache[language]:getCategoryName()
				.. '|' .. language_cache[language]:getCanonicalName() .. ']]')
		elseif code_in_page_name ~= language then
			mw.log(('Error! Line “%s” does not have the same language code as the page name.'):format(line))
		end
		
		local link = require 'Module:links'.full_link {
			term = link_page:gsub("^Reconstruction:[^/]+/", "*"),
			lang = language_cache[language],
		}
		output:insert('| class="link-target" | ' .. link)
		
		
		entries = entries
			:map(function (entry)
				return tag_link(entry)
			end)
			:concat ', '
		output:insert('| class="linked-from" | ' .. entries)
	end
	
	output:insert '|}'
	
	return output:concat '\n'
end

function export.show(frame)
	local title = frame:getParent():getTitle()
	local data_title = title .. '/data'
	local data = mw.title.new(data_title)
	
	local content = data:getContent()
	if not content then
		error("No content at " .. data_title .. ".")
	end
	local data = content:match '^%s*<pre><nowiki>(.-)</nowiki></pre>%s*$'
	if not data then
		data = content:match '^%s*<pre>(.-)</pre>%s*$'
	end
	if not data then
		error("Content at " .. data_title .. " doesn't match pattern.")
	end
	local code_in_page_name = title:match '[a-z-]+$'
	if not code_in_page_name then
		error("No code found in page name " .. title .. ".")
	end
	
	return export.generate_table(data, code_in_page_name)
end

return export