Modul:reconstruction

local export = {}

-- from [[WT:POS]]
local POS_headers = require "Module:table".listToSet {
	"Kata sifat", "Kata adverba", "Ambiposition", "Article", "Circumfix",
	"Circumposition", "Classifier", "Combining form", "Conjunction",
	"Contraction", "Counter", "Determiner", "Diacritical mark", "Han character",
	"Hanja", "Hanzi", "Ideophone", "Infix", "Interfix", "Interjection", "Kanji",
	"Letter", "Ligature", "Noun", "Number", "Numeral", "Participle", "Particle",
	"Phrase", "Postposition", "Prefix", "Preposition", "Prepositional phrase",
	"Pronoun", "Proper noun", "Proverb", "Punctuation mark", "Romanization",
	"Root", "Suffix", "Syllable", "Symbol", "Verb", 
}

-- This isn't a perfect pattern, but should work in entries that don't have
-- bad syntax.
local title_pattern = "%f[^\n%z]==+%s*(.-)%s*==+"
local function count_POS_headers(title)
	local POS_count = 0
	local content = title:getContent()
	if content then
		for header in content:gmatch(title_pattern) do
			if POS_headers[header] then
				POS_count = POS_count + 1
			end
		end
	end
	
	return POS_count
end

local function has_header(title, header_to_find)
	local content = title:getContent()
	if content then
		for header in content:gmatch(title_pattern) do
			if header == header_to_find then
				return true
			end
		end
	end
	
	return false
end

-- Track Proto-Indo-European entries with more than one part-of-speech header.
-- Invoked by {{reconstruction}}, requested by Victar.
function export.main(frame)
	local title = mw.title.getCurrentTitle()
	local cats = {}
	
	local language = title.text:match "^[^/]+"
	local langcode = require("Module:languages").getByCanonicalName(language)
	if not langcode then
		-- Can happen e.g. if used on a user page
		return
	end
	if language == "Indo-Eropah Purba" and count_POS_headers(title) > 1 then
		table.insert(cats, "Proto-Indo-European entries with more than one part of speech")
	end
	
	local has_references_header = has_header(title, "Rujukan")
	local has_further_reading_header = has_header(title, "Bacaan lanjut")
	if not has_references_header then
		table.insert(cats, "Entri bahasa " .. language .. " tanpa pengepala Rujukan")
	end
	
	local temp_rfref = ""
	if not (has_references_header or has_further_reading_header) then
		table.insert(cats, "Entri bahasa " .. language .. " tanpa pengepala Rujukan atau Bacaan lanjut")
		if langcode and langcode._code=="sla-pro" then
			local book_search = "[https://google.com/search?tbm=bks&q="..mw.uri.encode("\""..mw.title.getCurrentTitle().subpageText.."\"","QUERY").." Google Books]"
			temp_rfref = frame:expandTemplate{ title = "Template:rfref", args = { langcode._code, "Semak [[:Kategori:Templat rujukan bahasa "..language.."]] atau "..book_search } }
		end
	end
	
	return temp_rfref .. require("Module:utilities").format_categories(cats, langcode)
end

return export