User:Ohconfucius/test/MOSNUM utils.js: Difference between revisions

Content deleted Content added
m fix
add code documentation, tweaked docs on regex method
Line 7:
 
var ohc = ohc || {};
 
/**
* Provides internal utilities for manipulating dates in wikitext.
*/
ohc.dateutil = {
/**
/** ------------------------------------------------------------------------- */
* Get the raw pattern from a regular expression object. For example, this
/// REGEX utility functions
* function will convert /x+/ig into "x+".
* @param {object} s The regular expression to convert.
*/
regex_to_string: function(s) {
return s.toString()
Line 17 ⟶ 23:
},
/**
* Show an alert containing an error message.
* @param {string} s The message to display.
* @param {object} reg The regular expression that caused the error.
*/
alert_error: function(s, reg) { //reg can be undefined
var message = "DATES SCRIPT:\n" + s;
Line 26 ⟶ 37:
/**
* Replace regular expression patterns in the given text. The given regex
Main worker routine. The routine implements custom regex language to
* and substitution strings can contain tokens that specify the date format
simplify date transformations. The given regex and substitution strings
can contain* magicto wordsaccept thatand specify whatthe date format to convert the routinedate to. Leading shouldand
* trailing slashes are not needed for the regex.
accept and to what date format the routine should convert the date to.
*
Aside from magic strings, ordinary regular expressions and substitution
* The tokens can be either capturing or non-capturing. The capturing
strings are accepted. Leading and trailing slashes are not needed for the
* tokens make their output available for later processing. Most of the
regex.
* capturing magic strings can be used in the output with equivalent
* meaning.
The magic string can be either capturing or non-capturing. The capturing
*
magic strings make their output available for later processing. Most of
* Internally, the routine maintains information about several dates, so
the capturing magic strings can be used in the output with equivalent
* the regex can contain more than one token specifying day, month or year.
meaning.
* The first date will be assigned the first occurrences of tokens from
* each group, the second date will be assigned the second occurrences and
Internally, the routine maintains information about several dates, so the
* so on. For example, if the regex is '@MM @DD @YYYY, @Month @ZM @Day',
regex can contain more than one magic string specifying day, month or year.
The * the first date will be assigned the@MM firstas months, @DD occurrencesas ofdays magicand strings@YYYY fromas
each group,* theyears. The second date will be assigned the@Month secondas occurrencesmonths and so@Day as
* days. The third date will be assigned @ZM as months.
on. For example, if the regex is '@MM @DD @YYYY, @Month @ZM @Day', the
*
first date will be assigned @MM as months, @DD as days and @YYYY as years.
* If used in the replacement string, all tokens in the format @XX will
The second date will be assigned @Month as months and @Day as days. The
* output the data of the first date. To access the subsequent dates,
third date will be assigned @ZM as months.
* tokens in the format @XX2, @XX3, and so on must be used. @XX1 is
* provided as alias to @XX for convenience.
If used in the replacement string, all magic strings in the format @XX will
*
output the data of the first date. To access the subsequent dates, magic
* AVAILABLE REGEX MAGIC STRINGS
strings in the format @XX2, @XX3, and so on must be used. @XX1 is provided
* =============================
as alias to @XX for convenience.
* Note: The capturing tokens start with an uppercase letter whereas the
* equivalent non-capturing tokens start with a lowercase letter.
AVAILABLE REGEX MAGIC STRINGS
*
=============================
* Days:
* • @SD : Matches a day in numeric format without leading zero (1..31).
Note: The capturing magic strings start with an uppercase letter whereas
* • @ZD : Matches a day in numeric format with leading zero (01..31).
the equivalent non-capturing magic strings start with a lowercase
* • @DD : Matches a day in numeric format with optional leading zero (@SD or @ZD).
letter.
* • @Day : Matches a day in numeric format with optional leading zero, with optional st, nd or th suffix. Equivalent to @DD@th?
* • @sd, @zd, ... : noncapturing equivalents.
Days:
* • @th : Matches st, nd, rd or th.
@SD Matches a day in numeric format without leading zero (1..31).
*
* Months:
@ZD Matches a day in numeric format with leading zero (01..31).
* • @SM : Matches a month in numeric format without leading zero (1,2..12).
@DD * • @ZM : Matches a daymonth in numeric format with optional leading zero (@SD or @ZD01,02..12).
* • @MM : Matches a month in numeric format with optional leading zero (@SM or @ZM).
* • @FullMonth : Matches a full month name (January, February).
@Day A day in numeric format with optional leading zero, with optional st,
* • @Mon : Matches a short month name (Jan, Feb, ..). Also optionally matches dot (Jan., Feb., ..) and 'Sept', 'Sept.'.
nd or th suffix. Equivalent to @DD@th?
* • @Month : Matches a full or short month name (@FullMonth or @Mon).
* • @sdsm, @zdzm, ... : noncapturing equivalents.
*
* Years:
@th Matches st, nd, rd or th
* • @YYYY : Matches a 4-digit year.
* • @YY : Matches a 2-digit year. 50-99 are interpreted as 1950..1999, 0-49 are interpreted as 2000-2049.
Months:
* • @YYNN : Matches a 4 or 2-digit year (@YYYY or @YY).
@SM A month in numeric format without leading zero (1,2..12).
* • @Year : Matches a 1 to 4 digit year.
* • @yyyy, @yy, ... : noncapturing equivalents.
@ZM A month in numeric format with leading zero (01,02..12).
*
* Special tokens:
@MM A month in numeric format with optional leading zero (@SM or @ZM)
* • @@ : Matches a literal @.
*
@FullMonth Matches full name of a month (January, February)
* AVAILABLE REPLACEMENT STRING MAGIC STRINGS
* ==========================================
@Mon Matches short name of a month (Jan, Feb, ..). Also optionally
* Days:
matches dot (Jan., Feb., ..) and 'Sept', 'Sept.'.
* • @SD : Outputs a day in numeric format without leading zero (1-31).
* • @ZD : Outputs a day in numeric format with leading zero (01-31).
@Month Matches full or short name of a month (@FullMonth or @Mon)
* • @DD : Equivalent to @ZD.
* • @Day : Equivalent to @SD.
@sm, @zm, ... : noncapturing equivalents
* • @LDay : Outputs the matched day string without any transformations.
* • @SDn, @ZDn, ... : Where n is integer, outputs the day of the nth date in the specified format.
Years:
*
@YYYY Matches a 4-digit year
* Months:
* • @SM : Outputs a month in numeric format without leading zero (1-12).
@YY Matches a 2-digit year. 50-99 are interpreted as 1950..1999,
* • @ZM : Outputs a month in numeric format with leading zero (01-12).
0-49 are interpreted as 2000-2049.
* • @MM : Equivalent to @ZM.
* • @FullMonth : Outputs a full name of a month (January, February).
@YYNN Matches 4 or 2-digit year (@YYYY or @YY).
* • @Mon : Outputs a short name of a month (Jan, Feb).
* • @Month : Equivalent to @FullMonth.
@Year Matches 1 to 4 digit year
* • @LMonth : Outputs the matched month string without any transformations.
* • @SMn, @ZMn, ... : Where n is integer, outputs the month of the nth date in the specified format.
@yyyy, @yy, ... : noncapturing equivalents
*
* Years:
@@ Matches literal @
* • @YYYY Outputs 4-digit year. Valid only if the year was not captured by @Year.
* • @YY Outputs 2-digit year. Outputs the last two digits of a year. Valid only if the year was not captured by @Year.
AVAILABLE REPLACEMENT STRING MAGIC STRINGS
* • @YYNN Equivalent to @YYYY
==========================================
* • @Year Outputs 1 to 4 digit number identifying a year. Equivalent to @YYYY if the year is between 1000 and 9999.
* • @LYear Outputs the matched year string without any transformations.
Days:
* • @YYYYn, @YYn, ... : Where n is integer, outputs the year of the nth date in the specified format.
@SD: Outputs a day in numeric format without leading zero (1-31).
*
* Special tokens:
@ZD: Outputs a day in numeric format with leading zero (01-31).
* • @@ : Outputs a literal @.
*
@DD: Equivalent to @ZD
* All tokens in the replacement string which refer to data which isn't
* defined will be replaced with '@ERROR@'.
@Day: Equivalent to @SD
*
* An optional function defining whether to make the replacement in particular
@LDay: Outputs the matched day string without any transformations
* cases can be provided. The function is supplied a number of parameters, each
* of which is an object defining the nth date as parsed by the routine. Each
@SDn, @ZDn, ..., where n is integer
* object contains numeric values of days, months and years as 'd', 'm' and 'y'
Outputs the day of the nth date in the specified format.
* properties respectively. Each value can be -1 if a magic string for that
Months:
* date value was not specified in the regex, or an error occurs. The function
@SM Outputs a month in numeric format without leading zero (1-12)
* should return true if the replacement should be done, false otherwise.
*
@ZM Outputs a month in numeric format with leading zero (01-12)
* @param {string} text The text to change.
* @param {object|string} rg The regular expression to search in the text.
@MM Equivalent to @ZM
* @param {string} sub The pattern to replace matches with.
* @param {function} func A callback passed the matching date information
@FullMonth Outputs a full name of a month (January, February)
* which returns whether to continue with the replacement.
*/
@Mon Outputs a short name of a month (Jan, Feb)
@Month Equivalent to @FullMonth
@LMonth Outputs the matched month string without any transformations
@SMn, @ZMn, ..., where n is integer
Outputs the month of the nth date in the specified format.
Years:
@YYYY Outputs 4-digit year. Valid only if the year was not captured
by @Year
@YY Outputs 2-digit year. Outputs the last two digits of a year.
Valid only if the year was not captured by @Year
@YYNN Equivalent to @YYYY
@Year Outputs 1 to 4 digit number identifying a year. Equivalent to
@YYYY if the year is between 1000 and 9999
@LYear Outputs the matched year string without any transformations.
@YYYYn, @YYn, ..., where n is integer
Outputs the year of the nth date in the specified format.
@@ Outputs literal @
All magic strings in the replacement string, which refer to data which was
not defined, will be replaced witht '@ERROR@'
An optional function defining whether to make replacement in particular
case can be provided. The function is supplied a number of parameters, each
of which is an object defining the nth date as parsed by the routine. Each
object contains numeric values of days, months and years as 'd', 'm' and 'y'
properties respectively. Each value can be -1 if a magic string for that
date value was not specified in the regex, or an error occurs. The function
should return true if the replacement should be done, false otherwise.
*/
regex: function(text, rg, sub, func) {
var reg = ohc.dateutil.regex_to_string(rg);
Line 647 ⟶ 620:
};
 
/**
* Extends string objects with a chainable method to replace patterns containing
* special date tokens.
* @see ohc.dateutils.regex
*/
String.prototype.ohc_regex = function(rg, sub, func) {
return ohc.dateutil.regex(this.toString(), rg, sub, func);