본문으로 이동

모듈:category tree/utilities

위키낱말사전, 말과 글의 누리

이 모듈에 대한 설명문서는 모듈:category tree/utilities/설명문서에서 만들 수 있습니다

local export = {}

local unpack = unpack or table.unpack -- Lua 5.2 compatibility

--[=[
굴절 클래스에 대한 레이블을 생성합니다. `data`는 다음 필드를 가진 테이블입니다:
* `labels`: 레이블이 기록될 테이블.
* `pos`: 품사의 단수형, 예: "noun".
* `stem_classes`: 가능한 어간 클래스와 관련 속성 테이블.
* `principal_parts`: 주요 부분 필드와 설명 목록.
* `mark_up_spec`: (선택) 명세에 마크업을 추가하는 함수.
* `make_spec_bare`: (선택) 명세에서 마크업을 제거하는 함수.
* `addl`: (선택) 각 분류 페이지의 바닥글에 표시될 추가 텍스트.

`stem_classes` is a table describing the various stem classes and how to format the category description of each. It is
a table with keys specifying the stem classes and values consisting of an object containing properties of the stem
class. If the stem class contains the word 'GENDER' in it, it expands into labels both for a parent category that
subsumes several genders (obtained by removing the word 'GENDER' and following whitespace) as well as gender-specific
children categories (obtained by replacing the word 'GENDER' with the genders specified in the `possible_genders`
field). The stem class can contain literal text (e.g. suffixes), which will be marked up appropriately (e.g. italicized)
in breadcrumbs and titles. The fields of the property object for a given stem class are as follows:
* `gender`: The description of the gender(s) of the stem class. If preceded by ~, the description is preceded by
  "most commonly". This appears in the `additional` field of the label properties. It is not used in gender-specific
  children categories; instead the gender of that category is used.
* `possible_genders`: The possible genders this class occurs in. If this is specified, the word 'GENDER' must occur in
  the stem class, and gender-specific variants of the stem class (with GENDER replaced by the possible genders) are
  handled along with a parent category subsuming all genders. 
* `PRINCIPAL_PART`: The ending for the specified principal part. Use <...> to enclose literal Latin-script text (e.g.
  suffixes), which will be italicized. There will be one field for each principal part listed in the `principal_parts`
  table described above.
* `GENDER_PRINCIPAL_PART`: The ending for the GENDER variant of the specified principal part. If not specified, the
  value of `PRINCIPAL_PART` is used.
* `breadcrumb`: The breadcrumb for the category, appearing in the trail of breadcrumbs at the top of the page. If this
  stem has gender-specific variants, the breadcrumb specified here is used only for the parent category, while the
  gender-specific child categories use the gender as the breadcrumb. If not specified, it defaults to `sortkey`. If that
  is also not specified, or if the breadcrumb has the value "+", the stem class (without the word 'GENDER') is used.
  (Use "+" when a sortkey is specified but the stem class should be used as the breadcrumb.)
* `parent`: The parent category or categories. If specified, the actual category label is formed by appending the part
  of speech (e.g. "nouns"). Defaults to "POS by inflection type" where POS is the part of speech. Note that
  gender-specific child categories do not use this, but always have the gender-subsuming parent stem class category as
  their parent.
* `sortkey`: The sort key used for sorting this category among its parent's children. Defaults to the stem class
  (without the word 'GENDER'). Note that gender-specific child categories do nto use this, but always use the gender
  as the sort key.
* `addl`: Optional additional text to be displayed in the footer of the category page.
* `GENDER_addl`: Optional additional text to be displayed in the footer of a gender-specific category page, defaulting
  to `addl`. Use the value `false` to cancel out a non-gender-specific value.
]=]

function export.add_inflection_labels(data)
	for _, reqfield_spec in ipairs {
		{"labels", "레이블 테이블"},
		{"pos_ko", "단수형 한국어 품사"},
		{"pos_en", "단수형 영어 품사 키"},
		{"stem_classes", "가능한 어간 클래스와 속성 테이블"},
		{"principal_parts", "주요 부분 필드와 이름 목록"},
	} do
		local reqfield, gloss = unpack(reqfield_spec)
		if not data[reqfield] then
			error(("내부 오류: '%s' 필드가 누락되었습니다. 이 필드는 %s을(를) 포함해야 합니다."):format(reqfield, gloss))
		end
	end
	
	local function default_mark_up_spec(spec, nolink) return (spec:gsub("<(.-)>", "''%1''")) end
	local function default_make_spec_bare(spec) return (spec:gsub("<(.-)>", "%1")) end
	local function mark_up_spec(spec, nolink) return (data.mark_up_spec or default_mark_up_spec)(spec, nolink) end
	local function make_spec_bare(spec) return (data.make_spec_bare or default_make_spec_bare)(spec) end
	
	local plpos_en = data.pos_en .. "s"
	
	for full_infl, spec in pairs(data.stem_classes) do
		local subgenders = spec.possible_genders
		local infl = subgenders and full_infl:gsub("GENDER ", "") or full_infl
		local breadcrumb = spec.breadcrumb or spec.sortkey or "+"
		if breadcrumb == "+" then breadcrumb = infl end

		local parents = {}
		local spec_parents = spec.parent
		if type(spec_parents) == "string" then spec_parents = {spec_parents} end
		local parent_sort = make_spec_bare(spec.sortkey or infl)
		if spec_parents then
			for _, parent in ipairs(spec_parents) do
				table.insert(parents, {name = make_spec_bare(parent) .. " " .. plpos_en, sort = parent_sort})
			end
		else
			table.insert(parents, {name = plpos_en .. " by inflection type", sort = parent_sort})
		end

		local function create_addl(gender_spec, subgender_prefix)
			local addl_parts = {}
			local function ins(txt) table.insert(addl_parts, txt) end
			
			local function insert_header(gender_spec)
				if gender_spec then
					local most_commonly, gender = gender_spec:match("^(~)(.*)$")
					most_commonly = most_commonly and "가장 일반적으로 " or ""
					gender = gender or gender_spec
					ins(("이 %s은 %s%s이며, 보통 다음과 같은 어미를 가집니다:\n"):format(data.pos_ko, most_commonly,
						mark_up_spec(gender)))
				else
					ins(("이 %s은 보통 다음과 같은 어미를 가집니다:\n"):format(data.pos_ko))
				end
			end
			
			local function process_ending(field, desc, is_last)
				local ending_spec = spec[subgender_prefix .. field] or spec[field]
				if not ending_spec then
					error(("내부 오류: 굴절 '%s'에 대해, 주요 부분 '%s'의 필드 '%s'가 누락되었습니다."):format(
						full_infl, desc, field))
				end
				return ("* %s에서: %s%s"):format(desc, mark_up_spec(ending_spec), is_last and "." or ";\n")
			end
			
			local function insert_principal_part_info(subgender_prefix)
				for i, ppart_spec in ipairs(data.principal_parts) do
					local ppart_field, ppart_desc = unpack(ppart_spec)
					ins(process_ending(ppart_field, ppart_desc, i == #data.principal_parts))
				end
			end
			
			insert_header(gender_spec)
			insert_principal_part_info("")
			
			local spec_addl = spec[subgender_prefix .. "addl"]
			if spec_addl == nil then spec_addl = spec.addl end
			
			if data.addl or spec_addl then
				local footer_parts = {}
				if data.addl then table.insert(footer_parts, mark_up_spec(data.addl)) end
				if spec_addl then table.insert(footer_parts, mark_up_spec(spec_addl)) end
				ins("\n" .. table.concat(footer_parts, " "))
			end
			return table.concat(addl_parts)
		end
		
		data.labels[make_spec_bare(infl) .. " " .. plpos_en] = {
			description = "{{{langname}}} " .. mark_up_spec(infl) .. " " .. data.pos_ko .. ".",
			displaytitle = "{{{langname}}} " .. mark_up_spec(infl, "nolink") .. " " .. data.pos_ko,
			additional = create_addl(spec.gender, ""),
			breadcrumb = mark_up_spec(breadcrumb, "nolink"),
			parents = parents,
		}
		if subgenders then
			for _, subgender in ipairs(subgenders) do
				local gender_infl = full_infl:gsub("GENDER", subgender)
				data.labels[make_spec_bare(gender_infl) .. " " .. plpos_en] = {
					description = "{{{langname}}} " .. mark_up_spec(gender_infl) .. " " .. data.pos_ko .. ".",
					displaytitle = "{{{langname}}} " .. mark_up_spec(gender_infl, "nolink") .. " " .. data.pos_ko,
					additional = create_addl(subgender, subgender .. "_"),
					breadcrumb = subgender,
					parents = {{
						name = make_spec_bare(infl) .. " " .. plpos_en,
						sort = subgender,
					}},
				}
			end
		end
	end
end

return export