https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3AScripts Module:Scripts - Revision history 2025-05-25T03:52:21Z Revision history for this page on the wiki MediaWiki 1.45.0-wmf.2 https://en.wikipedia.org/w/index.php?title=Module:Scripts&diff=1185974829&oldid=prev Alexis Jazz: Imported page from https://en.wiktionary.org/wiki/Module:scripts AJsImportTool] 2023-11-20T03:47:19Z <p>Imported page from https://en.wiktionary.org/wiki/Module:scripts [<a href="/w/index.php?title=User:Alexis_Jazz/AJsImportTool.js&amp;action=edit&amp;redlink=1" class="new" title="User:Alexis Jazz/AJsImportTool.js (page does not exist)">AJsImportTool</a>]</p> <p><b>New page</b></p><div>local export = {}<br /> <br /> local Script = {}<br /> <br /> --[==[Returns the script code of the language. Example: {{code|lua|&quot;Cyrl&quot;}} for Cyrillic.]==]<br /> function Script:getCode()<br /> return self._code<br /> end<br /> <br /> --[==[Returns the canonical name of the script. This is the name used to represent that script on Wiktionary. Example: {{code|lua|&quot;Cyrillic&quot;}} for Cyrillic.]==]<br /> function Script:getCanonicalName()<br /> return self._rawData[1] or self._rawData.canonicalName<br /> end<br /> <br /> --[==[Returns the display form of the script. For scripts, this is the same as the value returned by &lt;code&gt;:getCategoryName(&quot;nocap&quot;)&lt;/code&gt;, i.e. it reads &quot;NAME script&quot; (e.g. {{code|lua|&quot;Arabic script&quot;}}). For regular and etymology languages, this is the same as the canonical name, and for families, it reads &quot;NAME languages&quot; (e.g. {{code|lua|&quot;Indo-Iranian languages&quot;}}). The displayed text used in &lt;code&gt;:makeCategoryLink&lt;/code&gt; is always the same as the display form.]==]<br /> function Script:getDisplayForm()<br /> return self:getCategoryName(&quot;nocap&quot;)<br /> end<br /> <br /> function Script:getOtherNames(onlyOtherNames)<br /> return require(&quot;Module:language-like&quot;).getOtherNames(self, onlyOtherNames)<br /> end<br /> <br /> function Script:getAliases()<br /> return self._rawData.aliases or {}<br /> end<br /> <br /> function Script:getVarieties(flatten)<br /> return require(&quot;Module:language-like&quot;).getVarieties(self, flatten)<br /> end<br /> <br /> --[==[Returns the parent of the script. Example: {{code|lua|&quot;Latn&quot;}} for {{code|lua|&quot;Latnx&quot;}} and {{code|lua|&quot;Arab&quot;}} for {{code|lua|&quot;fa-Arab&quot;}}. It returns {{code|lua|&quot;top&quot;}} for scripts without a parent, like {{code|lua|&quot;Latn&quot;}}, {{code|lua|&quot;Grek&quot;}}, etc.]==]<br /> function Script:getParent()<br /> return self._rawData.parent<br /> end<br /> <br /> function Script:getSystemCodes()<br /> if not self._systemCodes then<br /> if type(self._rawData[2]) == &quot;table&quot; then<br /> self._systemCodes = self._rawData[2]<br /> elseif type(self._rawData[2]) == &quot;string&quot; then<br /> self._systemCodes = mw.text.split(self._rawData[2], &quot;%s*,%s*&quot;)<br /> else<br /> self._systemCodes = {}<br /> end<br /> end<br /> return self._systemCodes<br /> end<br /> <br /> function Script:getSystems()<br /> if not self._systemObjects then<br /> local m_systems = require(&quot;Module:writing systems&quot;)<br /> self._systemObjects = {}<br /> <br /> for _, ws in ipairs(self:getSystemCodes()) do<br /> table.insert(self._systemObjects, m_systems.getByCode(ws))<br /> end<br /> end<br /> <br /> return self._systemObjects<br /> end<br /> <br /> --function Script:getAllNames()<br /> -- return self._rawData.names<br /> --end<br /> <br /> --[==[Given a list of types as strings, returns true if the script has all of them. <br /> <br /> Currently the only possible type is {script}; use {{lua|hasType(&quot;script&quot;)}} to determine if an object that<br /> may be a language, family or script is a script.<br /> ]==] <br /> function Script:hasType(...)<br /> if not self._type then<br /> self._type = {script = true}<br /> if self._rawData.type then<br /> for _, type in ipairs(mw.text.split(self._rawData.type, &quot;%s*,%s*&quot;)) do<br /> self._type[type] = true<br /> end<br /> end<br /> end<br /> for _, type in ipairs{...} do<br /> if not self._type[type] then<br /> return false<br /> end<br /> end<br /> return true<br /> end<br /> <br /> --[==[Returns the name of the main category of that script. Example: {{code|lua|&quot;Cyrillic script&quot;}} for Cyrillic, whose category is at [[:Category:Cyrillic script]].<br /> Unless optional argument &lt;code&gt;nocap&lt;/code&gt; is given, the script name at the beginning of the returned value will be capitalized. This capitalization is correct for category names, but not if the script name is lowercase and the returned value of this function is used in the middle of a sentence. (For example, the script with the code &lt;code&gt;Semap&lt;/code&gt; has the name &lt;code&gt;&quot;flag semaphore&quot;&lt;/code&gt;, which should remain lowercase when used as part of the category name [[:Category:Translingual letters in flag semaphore]] but should be capitalized in [[:Category:Flag semaphore templates]].) If you are considering using &lt;code&gt;getCategoryName(&quot;nocap&quot;)&lt;/code&gt;, use &lt;code&gt;getDisplayForm()&lt;/code&gt; instead.]==]<br /> function Script:getCategoryName(nocap)<br /> local name = self._rawData[1] or self._rawData.canonicalName<br /> <br /> -- If the name already has &quot;script&quot;, &quot;code&quot; or &quot;semaphore&quot; at the end, don&#039;t add it.<br /> if not (<br /> name:find(&quot;[ %-][Ss]cript$&quot;) or<br /> name:find(&quot;[ %-][Cc]ode$&quot;) or<br /> name:find(&quot;[ %-][Ss]emaphore$&quot;)<br /> ) then<br /> name = name .. &quot; script&quot;<br /> end<br /> if not nocap then<br /> name = mw.getContentLanguage():ucfirst(name)<br /> end<br /> return name<br /> end<br /> <br /> function Script:makeCategoryLink()<br /> return &quot;[[:Category:&quot; .. self:getCategoryName() .. &quot;|&quot; .. self:getDisplayForm() .. &quot;]]&quot;<br /> end<br /> <br /> --[==[Returns the {{code|lua|wikipedia_article}} item in the language&#039;s data file, or else calls {{code|lua|Script:getCategoryName()}}.]==]<br /> function Script:getWikipediaArticle()<br /> return self._rawData.wikipedia_article or self:getCategoryName()<br /> end<br /> <br /> --[==[Returns the regex defining the script&#039;s characters from the language&#039;s data file.<br /> This can be used to search for words consisting only of this script, but see the warning above.]==]<br /> function Script:getCharacters()<br /> if self._rawData.characters then<br /> return self._rawData.characters<br /> else<br /> return nil<br /> end<br /> end<br /> <br /> --[==[Returns the number of characters in the text that are part of this script.<br /> &#039;&#039;&#039;Note:&#039;&#039;&#039; You should never rely on text consisting entirely of the same script. Strings may contain spaces, punctuation and even wiki markup or HTML tags. HTML tags will skew the counts, as they contain Latin-script characters. So it&#039;s best to avoid them.]==]<br /> function Script:countCharacters(text)<br /> if not self._rawData.characters then<br /> return 0<br /> -- Due to the number of Chinese characters, a different determination method is used when differentiating between traditional (&quot;Hant&quot;) and simplified (&quot;Hans&quot;) Chinese.<br /> elseif self:getCode() == &quot;Hant&quot; or self:getCode() == &quot;Hans&quot; then<br /> local num, charData = 0, self:getCode() == &quot;Hant&quot; and mw.loadData(&quot;Module:zh/data/ts&quot;) or mw.loadData(&quot;Module:zh/data/st&quot;)<br /> for char in text:gmatch(&quot;[\194-\244][\128-\191]*&quot;) do<br /> num = num + (charData[char] and 1 or 0)<br /> end<br /> return num<br /> end<br /> return select(2, mw.ustring.gsub(text, &quot;[&quot; .. self._rawData.characters .. &quot;]&quot;, &quot;&quot;))<br /> end<br /> <br /> function Script:hasCapitalization()<br /> return not not self._rawData.capitalized<br /> end<br /> <br /> function Script:hasSpaces()<br /> return self._rawData.spaces ~= false<br /> end<br /> <br /> function Script:isTransliterated()<br /> return self._rawData.translit ~= false<br /> end<br /> <br /> --[==[Returns true if the script is (sometimes) sorted by scraping page content, meaning that it is sensitive to changes in capitalization during sorting.]==]<br /> function Script:sortByScraping()<br /> return not not self._rawData.sort_by_scraping<br /> end<br /> <br /> --[==[Returns the text direction, if any. Currently, left-to-right scripts are unmarked, while most right-to-left scripts have direction specified as {{code|lua|&quot;rtl&quot;}} and Mongolian as {{code|lua|&quot;down&quot;}}.]==]<br /> function Script:getDirection()<br /> return self._rawData.direction<br /> end<br /> <br /> <br /> function Script:getRawData()<br /> return self._rawData<br /> end<br /> <br /> --[==[Returns {{code|lua|true}} if the script contains characters that require fixes to Unicode normalization under certain circumstances, {{code|lua|false}} if it doesn&#039;t.]==]<br /> function Script:hasNormalizationFixes()<br /> return not not self._rawData.normalizationFixes<br /> end<br /> <br /> --[==[Corrects discouraged sequences of Unicode characters to the encouraged equivalents.]==]<br /> function Script:fixDiscouragedSequences(text)<br /> if self:hasNormalizationFixes() and self._rawData.normalizationFixes.from then<br /> local gsub = require(&quot;Module:string utilities&quot;).gsub<br /> for i, from in ipairs(self._rawData.normalizationFixes.from) do<br /> text = gsub(text, from, self._rawData.normalizationFixes.to[i] or &quot;&quot;)<br /> end<br /> end<br /> return text<br /> end<br /> <br /> -- Implements a modified form of Unicode normalization for instances where there are identified deficiencies in the default Unicode combining classes.<br /> local function fixNormalization(text, self)<br /> if self:hasNormalizationFixes() and self._rawData.normalizationFixes.combiningClasses then<br /> local combiningClassFixes = self._rawData.normalizationFixes.combiningClasses<br /> local charsToFix = table.concat(require(&quot;Module:table&quot;).keysToList(combiningClassFixes))<br /> if require(&quot;Module:string utilities&quot;).match(text, &quot;[&quot; .. charsToFix .. &quot;]&quot;) then<br /> local codepoint, u = mw.ustring.codepoint, mw.ustring.char<br /> -- Obtain the list of default combining classes.<br /> local combiningClasses = mw.loadData(&quot;Module:scripts/data/combiningClasses&quot;)<br /> -- For each character that needs fixing, find all characters with combining classes equal to or lower than its default class, but greater than its new class (i.e. intermediary characters).<br /> for charToFix, newCombiningClass in pairs(combiningClassFixes) do<br /> local intermediaryChars = {}<br /> for character, combiningClass in pairs(combiningClasses) do<br /> if newCombiningClass &lt; combiningClass and combiningClass &lt;= combiningClasses[codepoint(charToFix)] then<br /> table.insert(intermediaryChars, u(character))<br /> end<br /> end<br /> -- Swap the character with any intermediary characters that are immediately before it.<br /> text = require(&quot;Module:string utilities&quot;).gsub(text, &quot;([&quot; .. table.concat(intermediaryChars) .. &quot;]+)(&quot; .. charToFix .. &quot;)&quot;, &quot;%2%1&quot;)<br /> end<br /> end<br /> end<br /> return text<br /> end<br /> <br /> function Script:toFixedNFC(text)<br /> return fixNormalization(mw.ustring.toNFC(text), self)<br /> end<br /> <br /> function Script:toFixedNFD(text)<br /> return fixNormalization(mw.ustring.toNFD(text), self)<br /> end<br /> <br /> function Script:toFixedNFKC(text)<br /> return fixNormalization(mw.ustring.toNFKC(text), self)<br /> end<br /> <br /> function Script:toFixedNFKD(text)<br /> return fixNormalization(mw.ustring.toNFKD(text), self)<br /> end<br /> <br /> function Script:toJSON()<br /> if not self._type then<br /> self:hasType()<br /> end<br /> local types = {}<br /> for type in pairs(self._type) do<br /> table.insert(types, type)<br /> end<br /> <br /> local ret = {<br /> canonicalName = self:getCanonicalName(),<br /> categoryName = self:getCategoryName(&quot;nocap&quot;),<br /> code = self:getCode(),<br /> otherNames = self:getOtherNames(true),<br /> aliases = self:getAliases(),<br /> varieties = self:getVarieties(),<br /> type = types,<br /> direction = self:getDirection(),<br /> characters = self:getCharacters(),<br /> parent = self:getParent(),<br /> systems = self:getSystemCodes(),<br /> wikipediaArticle = self._rawData.wikipedia_article,<br /> }<br /> <br /> return require(&quot;Module:JSON&quot;).toJSON(ret)<br /> end<br /> <br /> Script.__index = Script<br /> <br /> function export.makeObject(code, data, useRequire)<br /> return data and setmetatable({_rawData = data, _code = code}, Script) or nil<br /> end<br /> <br /> -- Track scripts with anomalous names.<br /> local scriptsToTrack = {<br /> -- scripts already renamed<br /> [&quot;IPAchar&quot;] = true,<br /> [&quot;musical&quot;] = true,<br /> [&quot;Ruminumerals&quot;] = true,<br /> [&quot;polytonic&quot;] = true,<br /> -- scripts not yet renamed<br /> [&quot;Latinx&quot;] = true,<br /> }<br /> <br /> -- Temporary aliases from canonicalized names to (existing) anomalous names. Once we have converted everything we will<br /> -- rename the scripts and remove the alias code.<br /> local scriptAliases = {<br /> -- scripts already renamed; we now alias the old names to the new ones<br /> [&quot;IPAchar&quot;] = &quot;Ipach&quot;,<br /> [&quot;musical&quot;] = &quot;Music&quot;,<br /> [&quot;Ruminumerals&quot;] = &quot;Rumin&quot;,<br /> [&quot;polytonic&quot;] = &quot;Polyt&quot;,<br /> [&quot;Latinx&quot;] = &quot;Latnx&quot;,<br /> }<br /> <br /> --[==[Finds the script whose code matches the one provided. If it exists, it returns a {{code|lua|Script}} object representing the script. Otherwise, it returns {{code|lua|nil}}, unless &lt;span class=&quot;n&quot;&gt;paramForError&lt;/span&gt; is given, in which case an error is generated. If &lt;code class=&quot;n&quot;&gt;paramForError&lt;/code&gt; is {{code|lua|true}}, a generic error message mentioning the bad code is generated; otherwise &lt;code class=&quot;n&quot;&gt;paramForError&lt;/code&gt; should be a string or number specifying the parameter that the code came from, and this parameter will be mentioned in the error message along with the bad code.]==]<br /> function export.getByCode(code, paramForError, disallowNil, useRequire)<br /> if code == nil and not disallowNil then<br /> return nil<br /> end<br /> if scriptsToTrack[code] then<br /> require(&quot;Module:debug/track&quot;)(&quot;scripts/&quot; .. code)<br /> end<br /> code = scriptAliases[code] or code<br /> <br /> local data<br /> if useRequire then<br /> data = require(&quot;Module:scripts/data&quot;)[code]<br /> else<br /> data = mw.loadData(&quot;Module:scripts/data&quot;)[code]<br /> end<br /> <br /> local retval = export.makeObject(code, data, useRequire)<br /> <br /> if not retval and paramForError then<br /> require(&quot;Module:languages/error&quot;)(code, paramForError, &quot;script code&quot;, nil, &quot;not real lang&quot;)<br /> end<br /> <br /> return retval<br /> end<br /> <br /> function export.getByCanonicalName(name, useRequire)<br /> local code<br /> if useRequire then<br /> code = require(&quot;Module:scripts/by name&quot;)[name]<br /> else<br /> code = mw.loadData(&quot;Module:scripts/by name&quot;)[name]<br /> end<br /> <br /> return export.getByCode(code, nil, nil, useRequire)<br /> end<br /> <br /> --[==[<br /> Takes a codepoint or a character and finds the script code (if any) that is<br /> appropriate for it based on the codepoint, using the data module<br /> [[Module:scripts/recognition data]]. The data module was generated from the<br /> patterns in [[Module:scripts/data]] using [[Module:User:Erutuon/script recognition]].<br /> <br /> Converts the character to a codepoint. Returns a script code if the codepoint<br /> is in the list of individual characters, or if it is in one of the defined<br /> ranges in the 4096-character block that it belongs to, else returns &quot;None&quot;.<br /> ]==]<br /> function export.charToScript(char)<br /> return require(&quot;Module:scripts/charToScript&quot;).charToScript(char)<br /> end<br /> <br /> --[==[Returns the code for the script that has the greatest number of characters in &lt;code&gt;text&lt;/code&gt;. Useful for script tagging text that is unspecified for language. Uses [[Module:scripts/recognition data]] to determine a script code for a character language-agnostically.]==]<br /> function export.findBestScriptWithoutLang(text)<br /> return require(&quot;Module:scripts/charToScript&quot;).findBestScriptWithoutLang(text)<br /> end<br /> <br /> return export</div> Alexis Jazz