Module:headword: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
mNo edit summary
No edit summary
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
local languages = mw.loadData("Module:languages")
local languages = mw.loadData("Module:languages")
local m_links = require("Module:links")


local export = {}
local export = {}
Line 77: Line 78:
if term ~= "" then
if term ~= "" then
-- Does this term contain a link?
-- Apply processing to the term, for formatting links and such
term = m_links.language_link(term, nil, lang, langinfo.names[1])
-- Todo: Replace this by a call to [[Module:links]], which is more sophisticated.
if not term:find("[[", nil, true) then
-- If not, then link the term
term = "[[" .. term .. (term ~= PAGENAME and "#" .. langinfo.names[1] .. "|" .. term or "") .. "]]"
end
-- Format the term with the script code template
-- Format the term with the script code template
Line 125: Line 122:
head = SUBPAGENAME
head = SUBPAGENAME
end
end
end
-- Apply processing to the headword, for formatting links and such
if head:find("[[", nil, true) then
head = m_links.language_link(head, nil, langinfo)
end
end

Revision as of 16:36, 16 July 2013


This module is used to show headword lines, along with any annotations like genders, transliterations and inflections. It's used by the template {{head}}, via the submodule Module:headword/templates. It's also used by many other headword modules; for a full list, see Category:Headword-line modules. Some of the data used by this module is found in Module:headword/data.

export.show

function export.show(frame)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.format_headword

function export.format_headword(head, lang, sc)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.format_transliteration

function export.format_transliteration(tr)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.format_genders

function export.format_genders(genders, lang)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.format_inflections

function export.format_inflections(inflections, lang, sc)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

export.format_categories

function export.format_categories(categories, sort_key)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

Further explanations for full_headword()

The sole argument, data, is a table containing the following items (WARNING: they will be destructively modified):

{
	lang = language_object,
	pagename = nil or "pagename",
	heads = { "head1", "head2", "head3", ... } or {
		{
			term = nil or "head1",
			tr = nil or "translit1",
			ts = nil or "transcription1",
			sc = nil or script_object,
			q = nil or {"left_qualifier1", "left_qualifier2", ...},
			qq = nil or {"right_qualifier1", "right_qualifier2", ...},
			refs = nil or {{text = "ref_text1" or "", name = nil or "ref_name1", group = nil or "ref_group1"}, ...},
			separator = nil or "separator",
		},
		...
	},
	translits = { [1] = "translit1", [3] = "translit3", ... },
	transcriptions = { [2] = "transcription2", [3] = "transcription3", ... },
	sc = script_object,
	inflections = {
		enable_auto_translit = boolean,
		{ label = "grammatical_category", "inflected_form1", "inflected_form2", ... },
		{ label = "grammatical_category", accel = {form = "tag|tag", lemma = "lémma"}, "inflected_form1", "inflected_form2", ... },
		{
			label = "grammatical_category",
			accel = {
				form = "tag|tag",
				target = "form_target",
				tr = nil or "form_manual_translit",
				gender = "gender_spec" or {"gender_spec1", "gender_spec2", ...},
				pos = "form_part_of_speech",
				lemma = nil or "lémma",
				lemma_translit = nil or "lemma_manual_translit",
				no_store = boolean,
			},
			sc = nil or inflection_specific_script_object,
			enable_auto_translit = boolean,
			"inflected_form1",
			{
				term = "inflected_form2",
				alt = nil or "display_text",
				translit = nil or "manual_transliteration",
				transcription = nil or "manual_transcription",
				genders = {"gender1", "gender2", {spec = "gender3", qualifiers = nil or {"qualifier1", "qualifier2", ... }}},
				accel = {form = "tag|tag|tag", lemma = "lemma_of_inflected_form", lemma_translit = "manual_translit" },
				lang = nil or term_specific_lang_object,
				sc = nil or term_specific_script_object,
				id = "sense_id",
				gloss = "gloss",
				pos = "part_of_speech",
				lit = "literal meaning",
				q = nil or {"left_qualifier1", "left_qualifier2", ... },
				qq = nil or {"right_qualifier1", "right_qualifier2", ... },
				refs = nil or {{text = "ref_text1" or "", name = nil or "ref_name1", group = nil or "ref_group1"}, ...},
				separator = nil or "separator",
				nolinkinfl = boolean,
				face = nil or "plain" or "hypothetical",
			},
			{
				label = "raw_textual_label",
				q = nil or {"left_qualifier1", "left_qualifier2", ... },
				qq = nil or {"right_qualifier1", "right_qualifier2", ... },
				refs = nil or {{text = "ref_text1" or "", name = nil or "ref_name1", group = nil or "ref_group1"}, ...},
				separator = nil or "separator",
			},
			...
		},
		{ label = "grammatical_category", request = true },
		...
	},
	genders = {
		"gender1",
		{spec = "gender2", qualifiers = {"qualifier1", "qualifier2", ...}},
		...
	},
	pos_category = "plural_part_of_speech",
	categories = { "category1", "category2", ... },
	whole_page_categories = { "category1", "category2", ... },
	force_cat_output = boolean,
	sccat = boolean,
	noposcat = boolean,
	nogendercat = boolean,
	nomultiwordcat = boolean,
	nopalindromecat = boolean,
	nolinkhead = boolean,
	nolinkinfl = boolean,
	sort_key = "sort_key",
	id = "sense_id",
}

Further explanation:

  • data.lang is required and is a language object from Module:languages corresponding to a given language. For example, use require("Module:languages").getByCode("ru") to retrieve the object corresponding to Russian.
  • data.pagename is optional and allows you to override the pagename used variously in the module (e.g. as the default value when a head is omitted, for setting categories such as palindromes and terms spelled with CHAR, etc.).
  • data.heads is a table listing the heads of the headword. Each element is either a string specifying only the headword itself (old-style), or an object specifying all the properties of the headword (new-style). You cannot mix and match these two styles; all elements should be of one type or the other. If no heads are specified at all (data.heads is omitted or is an empty array), a default head is set based on the assumed pagename (either the actual pagename or the value of data.pagename, if set). When using old-style head strings, a given head in the array can be nil, in which case a default head is set as above. When head objects are used, a given object can have the following properties:
    • .term: A string specifying the headword. This can be omitted, in which case a default head is set as above. Explicit headwords are generally used to specify extra diacritics (in languages with such diacritics, e.g. Russian, Arabic, Latin, Ancient Greek, Old English, etc.), or to link individual words of a multiword term, particularly when the words are inflected forms. Note that by default, each word is linked individually to itself, so there is no need to specify links for a term like a golden key can open any door. Some additional notes:
      • If a headword string contains wikilinks, they are converted into language-section links for the given language (using Module:links#language_link, which is also used by {{l}}). For example, giving "[[give]] [[up]]", if the language provided is English, will produce: "[[give#English|give]] [[up#English|up]]". If string is prefixed with * or if any of the links are, then they are interpreted as reconstructed terms and it will create links to the Reconstruction namespace as appropriate.
      • If the page name contains spaces or punctuation marks (except for punctuation marks that are used inside of words), it is split and each individual word is automatically wikilinked as above.
      • If the current page is in the Reconstruction: namespace, then an asterisk "*" will be prepended to the headword to indicate that it is a reconstructed term.
    • .tr: A string specifying the transliteration of the headword. This is only needed when the headword is in a non-Latin script, and even then only when the automatic transliteration specified using the language's transliteration module is incorrect (or the language has no transliteration module, such as with Persian and Hebrew). For languages with a transliteration module, pass in "-" to suppress the transliteration entirely.
    • .ts: A string specifying the transcription of the headword. This is only used in a few languages with non-Latin scripts where the spelling is significantly different from the pronunciation, such as Akkadian, Old Persian or Hittite. In cases like this, the transliteration usually reflects the spelling and the transcription reflects the pronunciation. For this reason, transcriptions are displayed between slashes. Transcriptions should NOT be used simply to display IPA pronunciation of a language like Russian or Arabic. Unlike for transliterations, there are no automatic transcription modules.
    • .sc: An optional script object from Module:scripts corresponding to a given script, specifying the script that the headword is in. If omitted, defaults to the top-level data.sc value. Most of the time, neither the per-headword script nor top-level script need to be specified: If both are omitted, Module:scripts will determine the script(s) using the list of scripts in the language's data file and the characters that are in the headword. Specifically, if there are multiple possible scripts for a language, the script with the largest number of characters in the headword is chosen.
    • .q: An optional array specifying one of more qualifiers displayed to the left of the headword. Qualifiers are displayed in italics and with parentheses around them, and are intended to specify relevant properties of the headword, especially when there is more than one headword.
    • .qq: An optional array specifying one of more qualifiers displayed to the right of the headword, as above.
    • .refs: An optional array specifying one of more references (i.e. footnotes) for the headword. This is similar to using <ref>...</ref> to specify a reference/footnote after a given word in the text. Each element of the array is either a string (the text of the reference) or an object of the form {text = "ref_text" or "", name = nil or "ref_name", group = nil or "ref_group"}. In this latter format:
      • .text specifies the reference text (which cannot be nil; use a blank string when cross-referencing to another reference);
      • .name gives an optional name to the reference for cross-reference purposes, if the reference text is non-empty, similarly to <ref name="ref_name">ref_text</ref>; however, if the reference text is empty, it specifies a cross-reference to a previously-named reference, similarly to <ref name="ref_name"/>;
      • .group gives an optional group to the reference for grouping purposes, similarly to <ref name="ref_name" group="ref_group">ref_text</ref>; however, if the reference text is empty, it specifies the group of a cross-reference to a previously-named reference, similarly to <ref name="ref_name" group="ref_group"/>.
    • .separator: The separator preceding the headword. If omitted, the default value is  <i>or</i>  (i.e. the italicized word or surrounded by spaces) for the second and higher headword, and a blank string for the first headword. Use a blank string to request no separator at all.
  • data.translits is an optional table listing the transliterations corresponding to each headword in data.heads, when old-style head strings are used; omitting this field is equivalent to setting it to an empty list. If new-style head objects are used, this field must be omitted. The Nth numbered entry should be either a string specifying the transliteration of headword N, or may be omitted, as with the .tr property described above. Note that, if there are multiple headwords, the table in data.translits might have entries in the middle of the list that are nil. A list of this sort cannot be created with table.insert(), as attempting to insert nil this way does nothing. Instead, each transliteration must be explicitly assigned using a number as index, e.g. { [1] = "string", [3] = "string", ... }; here, item 2 is nil, because no value was assigned to it.
  • data.transcriptions is an optional table listing the transcriptions corresponding to each headword in data.heads, when old-style head strings are used; omitting this field is equivalent to setting it to an empty list. If new-style head objects are used, this field must be omitted. It is of the same format as data.translits, and can have holes in it as needed. The meaning of the transcription field is as described abobe for .ts.
  • data.sc is an optional script object from Module:scripts corresponding to a given script. If specified, this applies equally to all heads specified using data.heads; if you need to specify per-head scripts, use the head object format documented above. Most of the time you can omit this item, and Module:scripts will determine the script(s) as specified above for the .sc headword property.
  • data.genders is a table listing the gender/number specifications for the headwords. This can be omitted for no genders or numbers. Each element is either a string specifying a gender/number spec, or a table of the form {spec = "gender/number_spec", qualifiers = nil or {"qualifier1", "qualifier2", ...}. In either case, the accepted values for genders or numbers are given in Module:gender and number; examples are "m" for masculine, "f-an-p" for feminine animate plural and "c2" for noun class 2 in languages such as Swahili that have noun classes. If the format with qualifiers is given, the qualifiers are displayed to the left of the gender/number specification. Categories are automatically added according to the specific genders, e.g. LANG masculine nouns for the language specified in data.lang if the gender is masculine and the part of speech (see below) is nouns or reconstructed nouns. To suppress the addition of these categories, specify data.nogendercat = true.
  • data.inflections is a table listing the inflections to be displayed in the headword entry. The format of this table is somewhat complex and is described below under format_inflections.
  • data.pos_category is the part-of-speech category for the entry. This is one of the lemma and nonlemma parts of speech listed in Module:headword/data. It should be in the plural: for example, "nouns". If this item is omitted, the part of speech category must be included in as the first item in data.categories.
  • data.categories is a table listing the categories to which the entry containing the headword will be added. The first category should be a part-of-speech category, with the canonical name of the language at the beginning – "Russian nouns" – unless the part of speech is given in the field data.pos_category.
  • data.whole_page_categories is a table listing language-agnostic categories to which the page will be added, which it is nevertheless useful for the headword module to handle (e.g. Category:Unsupported titles). Because they are not tied to a language, pages in them should be sorted according to their {{DEFAULTSORT:}} values for the sake of consistency. Note that some of these - including "Category:Unsupported titles" - are already handled automatically.
  • data.sort_key is a string specifying a sort key for the categories listed in data.categories. Sort keys should usually be omitted, because the format_categories function in Module:utilities will generate a suitable sortkey in most cases. The sortkey is used to ensure that the page is listed in the correct order in the categories to which it belongs.
  • data.nolinkhead is a boolean value determining whether or not to link the individual words of a multiword headword, which is done by default.
  • data.nolinkinfl is a boolean value determining whether or not to link the inflections of the entire headword. Not to be confused with part.nolinkinfl, which disables linking only for one of the inflections. It is used, for example, by Module:la-headword for reconstructed terms.

Examples

A simple example

full_headword{
	lang = require("Module:languages").getByCode("en"),	-- language code
	heads = {"book"},									-- headwords
	inflections = {
		{label = "plural", "books"}						-- inflections
	},
	categories = {"English nouns"},						-- part-of-speech category
}

might give (depending on the page it's run on):

<strong class="Latn headword" lang="en">book</strong> (''plural'' <b class="Latn" lang="en">[[books#English|books]]</b>)[[Category:English lemmas|HEADWORD]][[Category:English nouns|HEADWORD]]

which displays as:

book (plural books)

A fuller example

full_headword{
	lang = require("Module:languages").getByCode("de"),
	heads = {"Hund"},
	genders = {"m"},
	inflections = {
		{label = "genitive", "Hundes", "Hunds"},
        {label = "plural", "Hunde", {term="Hünde", q="nonstandard"}},
		{label = "diminutive",
			{term = "Hündchen", genders = {"n"}},
			{nolinkinfl=true, term = "Hündlein", genders = {"n"}}
		}
	},
	categories = {"German nouns"},
}

might give (depending on the page it's run on):

<strong class="Latn headword" lang="de">Hund</strong>&nbsp;<span class="gender"><abbr title="masculine gender">m</abbr></span> (''genitive'' <b class="Latn" lang="de">[[Hundes#German|Hundes]]</b> ''or'' <b class="Latn" lang="de">[[Hunds#German|Hunds]]</b>, ''plural'' <b class="Latn" lang="de">[[Hunde#German|Hunde]] </b>''or (nonstandard)''<b> [[Hünde#German|Hünde]]</b>, ''diminutive'' <b class="Latn" lang="de">[[Hündchen#German|Hündchen]]</b>&nbsp;<span class="gender"><abbr title="neuter gender">n</abbr></span> ''or'' <b class="Latn" lang="de">Hündlein</b>&nbsp;<span class="gender"><abbr title="neuter gender">n</abbr></span>)[[Category:German lemmas|HEADWORD]][[Category:German nouns|HEADWORD]]

which displays as:

Hund m (genitive Hundes or Hunds, plural Hunde or (nonstandard) Hünde, diminutive Hündchen n or Hündlein n)

An example in a non-Latin script

This example is in Russian, which has automatic transliteration:

full_headword{
	lang = require("Module:languages").getByCode("ru"),
	heads = {"кни́га"},
	genders = {"f-in"},
	inflections = {
		{label = "genitive", "кни́ги"},
		{label = "nominative plural", "кни́ги"},
		{label = "genitive plural", "книг"}
	},
	categories = {"Russian nouns"},
}

might give (depending on the page it's run on):

<strong class="Cyrl headword" lang="ru">кни́га</strong> [[Wiktionary:Russian transliteration|•]] (<span class="tr" lang=""><span class="tr" lang="">kníga</span></span>)&nbsp;<span class="gender"><abbr title="feminine gender">f</abbr>&nbsp;<abbr title="inanimate">inan</abbr></span> (''genitive'' <b class="Cyrl" lang="ru">[[книги#Russian|кни́ги]]</b>, ''nominative plural'' <b class="Cyrl" lang="ru">[[книги#Russian|кни́ги]]</b>, ''genitive plural'' <b class="Cyrl" lang="ru">[[книг#Russian|книг]]</b>)[[Category:Russian lemmas|HEADWORD]][[Category:Russian nouns|HEADWORD]]

which displays as

кни́га (knígaf inan (genitive кни́ги, nominative plural кни́ги, genitive plural книг)

Note a few things about the transliteration:

  • If the transliteration is specified and non-empty, Module:headword adds some stuff before and after it. For example, if the transliteration is "foo" and the language is Hebrew, produces
     [[Wiktionary:Hebrew transliteration|•]] (<span lang="">foo</span>)
    
    which looks like “ (foo)”.
  • The bullet linking to a transliteration policy page is only added if the page actually exists.

A fuller example in a non-Latin script

This example is in Russian, with two headwords, each of which requires manual transliteration:

full_headword{
	lang = require("Module:languages").getByCode("ru"),
	heads = {
		{term = "интервьюе́р", tr = "intɛrvʹjuér"},
		{term = "интервью́ер", "intɛrvʹjújer"},
	},
	genders = {"m-an"},
	inflections = {
		{label = "genitive", "интервьюе́ра", "интервью́ера"},
		{label = "nominative plural", "интервьюе́ры", "интервью́еры"},
		{label = "genitive plural", "интервьюе́ров", "интервью́еров"},
	},
	categories = {"Russian nouns"},
}

might give (depending on the page it's run on):

<strong class="Cyrl headword" lang="ru">интервьюе́р</strong> ''or'' <strong class="Cyrl headword" lang="ru">интервью́ер</strong> [[Wiktionary:Russian transliteration|•]] (<span class="tr" lang=""><span class="tr" lang="">intɛrvʹjuér</span> ''or'' <span class="tr" lang="">intɛrvʹjújer</span></span>)&nbsp;<span class="gender"><abbr title="masculine gender">m</abbr>&nbsp;<abbr title="animate">anim</abbr></span> (''genitive'' <b class="Cyrl" lang="ru">[[интервьюера#Russian|интервьюе́ра]]</b> ''or'' <b class="Cyrl" lang="ru">[[интервьюера#Russian|интервью́ера]]</b>, ''nominative plural'' <b class="Cyrl" lang="ru">[[интервьюеры#Russian|интервьюе́ры]]</b> ''or'' <b class="Cyrl" lang="ru">[[интервьюеры#Russian|интервью́еры]]</b>, ''genitive plural'' <b class="Cyrl" lang="ru">[[интервьюеров#Russian|интервьюе́ров]]</b> ''or'' <b class="Cyrl" lang="ru">[[интервьюеров#Russian|интервью́еров]]</b>)[[Category:Russian lemmas|HEADWORD]][[Category:Russian nouns|HEADWORD]]

which displays as

интервьюе́р or интервью́ер (intɛrvʹjuér or intɛrvʹjújerm anim (genitive интервьюе́ра or интервью́ера, nominative plural интервьюе́ры or интервью́еры, genitive plural интервьюе́ров or интервью́еров)

Another fuller example in a non-Latin script

This example is in Arabic, with embedded links in the headword, manual transliteration in an inflection and use of enable_auto_translit:

full_headword{
	lang = require("Module:languages").getByCode("ar"),
	heads = {
		{term = "[[غُدّة]] [[بَصَلِيّ|بَصَلِيّة]] [[إحْلِيلِيّ|إحْلِيلِيّة]]", tr = "ḡudda baṣaliyya ʾiḥlīliyya"},
	},
	genders = {"f"},
	inflections = {
		enable_auto_translit = true,
		{label = "plural", {term="غُدَد بَصَلِيَّة إِحْلِيلِيَة", translit="ḡudad baṣaliyya ʾiḥlīliyya"}},
	},
	categories = {"Arabic nouns"},
}

might give (depending on the page it's run on):

<strong class="Arab headword" lang="ar">[[غدة#Arabic|غُدّة]] [[بصلي#Arabic|بَصَلِيّة]] [[إحليلي#Arabic|إحْلِيلِيّة]]</strong> [[Wiktionary:Arabic transliteration|•]] (<span class="tr" lang=""><span class="tr" lang="">ḡudda baṣaliyya ʾiḥlīliyya</span></span>)&nbsp;<span class="gender"><abbr title="feminine gender">f</abbr></span> (''plural'' <b class="Arab" lang="ar">[[غدد بصلية إحليلية#Arabic|غُدَد بَصَلِيَّة إِحْلِيلِيَة]]</b> (<span lang="" class="tr">ḡudad baṣaliyya ʾiḥlīliyya</span>))[[Category:Arabic lemmas|HEADWORD]][[Category:Arabic nouns|HEADWORD]]

which displays as

غُدّة بَصَلِيّة إحْلِيلِيّة (ḡudda baṣaliyya ʾiḥlīliyyaf (plural غُدَد بَصَلِيَّة إِحْلِيلِيَة (ḡudad baṣaliyya ʾiḥlīliyya))

Proposed/planned changes

  • Checking for invalid genders, given a list of genders that are valid for a particular language.

local languages = mw.loadData("Module:languages")
local m_links = require("Module:links")

local export = {}

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
    local args = frame:getParent().args
    PAGENAME = mw.title.getCurrentTitle().text
    SUBPAGENAME = mw.title.getCurrentTitle().subpageText
    NAMESPACE = mw.title.getCurrentTitle().nsText
    local ret = ""
    local categories = {}
    
    -- Get language and script information
    local lang = args[1] or (NAMESPACE == "Template" and "und")  or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
    local langinfo = languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    local sc = args["sc"] or ""; if sc == "" then sc = langinfo.scripts[1] end
    
    -- Gather basic parameters
    local head = args["head"]; if head == "" then head = nil end
    local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
    local pos = args[2]; if pos == "" then pos = nil end
    local cat2 = args["cat2"]; if cat2 == "" then cat2 = nil end
    local cat3 = args["cat3"]; if cat3 == "" then cat3 = nil end
    local tr = args["tr"]; if tr == "" then tr = nil end
    
    -- Get/set categories
    if pos then
        -- Make the plural form of the part of speech
        if pos:find("x$") then
            pos = pos .. "es"
        elseif pos ~= "punctuation" or pos ~= "phrasebook" then
            pos = pos .. "s"
        end
        
        table.insert(categories, langinfo.names[1] .. " " .. pos)
    end
    
    if cat2 then
        table.insert(categories, langinfo.names[1] .. " " .. cat2)
    end
    
    if cat3 then
        table.insert(categories, langinfo.names[1] .. " " .. cat3)
    end
    
    -- Gather gender and number specifications
    -- Iterate over all gn parameters (g2, g3 and so on) until one is empty
    local genders = {}
    local g = args["g"] or ""
    local i = 2
    
    while g ~= "" do
        -- If any of the specifications contains a "?", add the entry
        -- to a cleanup category.
        if g:find("?", nil, true) then
            table.insert(categories, langinfo.names[1] .. " terms with incomplete gender")
        end
        
        table.insert(genders, g)
        g = args["g" .. i] or ""
        i = i + 1
    end
    
    -- Gather inflected forms
    local inflections = {}
    
    local i = 3
    local label = args[i] or ""
    local term = args[i+1] or ""
    local parts = {}
    
    while label ~= "" do
        table.insert(parts, label)
        
        if term ~= "" then
            
            -- Apply processing to the term, for formatting links and such
            term = m_links.language_link(term, nil, lang, langinfo.names[1])
            
            -- Format the term with the script code template
            term = frame:expandTemplate{ title = sc, args = { term, lang = lang, face = "bold" } }
            
            table.insert(parts, term)
        end
        
        i = i + 2
        label = args[i] or ""
        term = args[i+1] or ""
        
        -- If the next label is "or" then continue adding to the current parts list.
        -- Otherwise, insert the previous one and create a new one.
        if label ~= "or" then
            table.insert(inflections, parts)
            parts = {}
        end
    end
    
    table.insert(inflections, part)
    
    -- Format and return all the gathered information
    return
        export.format_headword(head, lang, sc) ..
        export.format_transliteration(tr) ..
        export.format_genders(genders) ..
        export.format_inflections(inflections, lang, sc) ..
        export.format_categories(categories, sort_key)
end

-- Format a headword
function export.format_headword(head, lang, sc)
    if not head then
        NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
        SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
        local langinfo = languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
        
        if NAMESPACE == "Appendix" and langinfo.type ~= "appendix-constructed" then
            head = "*" .. SUBPAGENAME
        else
            head = SUBPAGENAME
        end
    end
    
    -- Apply processing to the headword, for formatting links and such
    if head:find("[[", nil, true) then
        head = m_links.language_link(head, nil, langinfo)
    end
    
    -- Call the script code template
    return mw.getCurrentFrame():expandTemplate{ title = sc, args = { head, lang = lang, face = "head" } }
end

-- Format transliteration
function export.format_transliteration(tr)
    if tr then
        return " (" .. tr .. ")"
    else
        return ""
    end
end

function export.format_genders(genders, lang)
    if #genders > 0 then
        local gen = require("Module:gender and number")
        return " " .. gen.format_list(genders)
    else
        return ""
    end
end

-- Format the inflections following the headword
function export.format_inflections(inflections, lang, sc)
    if #inflections > 0 then
        -- Format each inflection "part" individually
        for key, part in ipairs(inflections) do
            -- If this "part" is a table, it contains alternating label-term pairs.
            -- Format them too.
            if type(part) == "table" then
                for key2, text in ipairs(part) do
                    -- Odd-numbered keys are labels, even-numbered keys are terms
                    if key2 % 2 == 1 then
                        part[key2] = "''" .. text .. "''"
                    end
                end
                
                inflections[key] = table.concat(part, " ")
            end
        end

        return " (" .. table.concat(inflections, ", ") .. ")"
    else
        return ""
    end
end

-- Format the categories with the appropriate sort key
function export.format_categories(categories, sort_key)
    NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
    
    if NAMESPACE == "" or NAMESPACE == "Appendix" then
        PAGENAME = PAGENAME or mw.title.getCurrentTitle().text
        SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
        
        -- Generate a default sort key
        if not sort_key then
            sort_key = mw.ustring.gsub(SUBPAGENAME, "^[-־]", "")
        end
        
        -- If the key is the same as the wiki software's default, remove it
        if sort_key == PAGENAME then
            sort_key = nil
        end
        
        for key, cat in ipairs(categories) do
            categories[key] = "[[Category:" .. cat .. (sort_key and "|" .. sort_key or "") .. "]]"
        end
        
        return table.concat(categories, "")
    else
        return ""
    end
end

return export