Module:Scripts/documentation: Difference between revisions

From The Languages of David J. Peterson
Jump to navigation Jump to search
m (Djpwikiadmin moved page Module:Scripts/doc to Module:Scripts/documentation)
No edit summary
 
Line 9: Line 9:
The module exports a number of functions that are used to find scripts.
The module exports a number of functions that are used to find scripts.


===getByCode===
{{module documentation|identifier=^export|section_level=3}}
{{code|lua|getByCode(code)}}
 
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}}.
 
===findBestScript===
{{code|lua|findBestScript(text, lang)}}
 
Given some text and a [[Module:languages#Language objects|language object]], this function iterates through the scripts of the given language and tries to find the script that best matches the text. It returns a {{code|lua|Script}} object representing the script. If no match is found at all, it returns the {{code|lua|None}} script object.


==Script objects==
==Script objects==
Line 23: Line 15:
A {{code|lua|Script}} object is returned from one of the functions above. It is a Lua representation of a script and the data associated with it. It has a number of methods that can be called on it, using the {{code|lua|:}} syntax. For example:
A {{code|lua|Script}} object is returned from one of the functions above. It is a Lua representation of a script and the data associated with it. It has a number of methods that can be called on it, using the {{code|lua|:}} syntax. For example:


<source lang="lua">
<syntaxhighlight lang="lua">
local m_scripts = require("Module:scripts")
local m_scripts = require("Module:scripts")
local sc = m_scripts.getByCode("Latn")
local sc = m_scripts.getByCode("Latn")
local name = sc:getCanonicalName()
local name = sc:getCanonicalName()
-- "name" will now be "Latin"
-- "name" will now be "Latin"
</source>
</syntaxhighlight>
 
===Script:getCode===
{{code|lua|:getCode()}}
 
Returns the script code of the language. Example: {{code|lua|"Cyrl"}} for Cyrillic.
 
===Script:getCanonicalName===
{{code|lua|:getCanonicalName()}}
 
Returns the canonical name of the script. This is the name used to represent that script on Wiktionary. Example: {{code|lua|"Cyrillic"}} for Cyrillic.
 
===Script:getParent===
{{code|lua|:getParent()}}
 
Returns the parent of the script. Example: {{code|lua|"Latn"}} for {{code|lua|"Latinx"}} and {{code|lua|"Arab"}} for {{code|lua|"fa-Arab"}}. It returns {{code|lua|"top"}} for scripts without a parent, like {{code|lua|"Latn"}}, {{code|lua|"Grek"}}, etc.
<!--
===Script:getAllNames===
{{code|lua|:getAllNames()}}
 
Returns a table of all names that the script is known by, including the canonical name. The names are not guaranteed to be unique, sometimes more than one script is known by the same name. Example: {{code|lua|{"Latin", "Roman"}}} for the Latin script.
-->
 
===Script:getWikipediaArticle===
{{code|lua|:getWikipediaArticle()}}
 
Returns the {{code|lua|wikipedia_article}} item in the language's data file, or else calls {{code|lua|Script:getCategoryName()}}.
 
===Script:countCharacters===
{{code|lua|:countCharacters(text)}}
 
Returns the number of characters in the text that are part of this script.
 
'''Note:''' 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's best to avoid them.


===Script:getCharacters===
{{module documentation|identifier=^Script|section_level=3}}
{{code|lua|:getCharacters()}}
 
Returns the regex defining the script's characters from the language's data file.
 
This can be used to search for words consisting only of this script, but see the warning above.
 
===Script:getCategoryName===
{{code|lua|:getCategoryName()}}
 
Returns the name of the main category of that script. Example: {{code|lua|"Cyrillic script"}} for Cyrillic, whose category is at [[:Category:Cyrillic script]].
 
===Script:getDirection===
{{code|lua|:getDirection()}}
 
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|"rtl"}} and Mongolian as {{code|lua|"down"}}.


==Subpages==
==Subpages==
Line 88: Line 32:
<includeonly>
<includeonly>
[[Category:General utility modules]]
[[Category:General utility modules]]
[[Category:Modules dealing with languages and scripts]]
[[Category:Language and script modules]]
 
[[Category:Wiktionary modules]]
[[af:Module:Skripte]]
</includeonly>
</includeonly>

Latest revision as of 20:51, 18 September 2023

This module is used to retrieve and manage Wiktionary's various writing systems and the information associated with them. See Wiktionary:Scripts for more information.

The information itself is stored in Module:scripts/data. The data module should not be used directly by any other module, the data should only be accessed through the functions provided by Module:scripts.

For functions that allow templates to use this module, see Module:scripts/templates.

Finding and retrieving scripts

The module exports a number of functions that are used to find scripts.

(The generated documentation is located at the module page.)

Script objects

A Script object is returned from one of the functions above. It is a Lua representation of a script and the data associated with it. It has a number of methods that can be called on it, using the : syntax. For example:

local m_scripts = require("Module:scripts")
local sc = m_scripts.getByCode("Latn")
local name = sc:getCanonicalName()
-- "name" will now be "Latin"

(The generated documentation is located at the module page.)

Subpages

See also