Add centuries.lua
This commit is contained in:
parent
82e25014bb
commit
9575922941
21
centuries/LICENSE.txt
Normal file
21
centuries/LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright © 2022 Bastien Dumont
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
26
centuries/Makefile
Normal file
26
centuries/Makefile
Normal file
|
@ -0,0 +1,26 @@
|
|||
.PHONY: test test-internal test-fr test-en
|
||||
|
||||
SHELL=/bin/bash
|
||||
nblines := $(shell wc -l centuries.lua | cut -d ' ' -f 1)
|
||||
but_four_last_lines := $(shell echo $$(($(nblines) - 4)))
|
||||
|
||||
test: test-fr test-en test-internal
|
||||
|
||||
test-internal: centuries.lua test/test-functions.lua
|
||||
sed -n 1,$(but_four_last_lines)p centuries.lua > test/tmp.lua
|
||||
cat test/test-functions.lua >> test/tmp.lua
|
||||
pandoc -L test/tmp.lua <<< ''
|
||||
@echo -e '==========================\nAll internal tests passed.\n==========================\n'
|
||||
rm test/tmp.lua
|
||||
|
||||
test-en: centuries.lua test/test-english.md
|
||||
pandoc -t native -L centuries.lua test/test-english.md > test/tmp.native
|
||||
diff test/tmp.native test/test-english.native
|
||||
@echo -e '\n===============\ntest-en passed.\n===============\n'
|
||||
rm test/tmp.native
|
||||
|
||||
test-fr: centuries.lua test/test-french.md
|
||||
pandoc -t native -L centuries.lua test/test-french.md > test/tmp.native
|
||||
diff test/tmp.native test/test-french.native
|
||||
@echo -e '\n===============\ntest-fr passed.\n===============\n'
|
||||
rm test/tmp.native
|
128
centuries/README.md
Normal file
128
centuries/README.md
Normal file
|
@ -0,0 +1,128 @@
|
|||
# Formatting references to centuries
|
||||
|
||||
`centuries` is a Lua filter for Pandoc that automatically formats references to centuries in a homogeneous, yet flexible way across the document. Its numerous customization options should make it adaptable to any language.
|
||||
|
||||
## Terminology
|
||||
|
||||
In “20th century AD”, we call “20” the _number_, “th” the _suffix_, “ century” (including the space) the _unit_ and “ AD” (including the space) the _era_.
|
||||
|
||||
## Basic use
|
||||
|
||||
Most of the time, you only have to put the century number into a span with class `.cty`. You can prefix it with `+` or `-` if you want to indicate explicitly if it is in the reference era (by default the Christian era) or before it.
|
||||
|
||||
``` md
|
||||
Rome conquered Gaul in the [-1]{.cty}.
|
||||
|
||||
Emperor Hadrian lived in the [+2]{.cty}.
|
||||
|
||||
Two world wars occurred during the first half of the [20]{.cty}.
|
||||
```
|
||||
|
||||
Output with default configuration:
|
||||
|
||||
> Rome conquered Gaul in the 1rst century BC.
|
||||
|
||||
> Emperor Hadrian lived in the 2nd century AD.
|
||||
|
||||
> Two world wars occurred during the first half of the 20th century.
|
||||
|
||||
You can set the attribute `ctyunit` to `sg` if you want the unit to be singular (the default in the default configuration), `pl` for the plural form, and `none` if you don't want to display the unit.
|
||||
|
||||
``` md
|
||||
During the [4]{.cty ctyunit=none}–[8]{.cty ctyunit=pl}.
|
||||
```
|
||||
|
||||
> During the 4th–8th centuries.
|
||||
|
||||
There is an other attribute `ctyera` as well. For more details, see below [Formatting the era](#formatting-the-era).
|
||||
|
||||
## Customization options
|
||||
|
||||
You can customize the behavior of the filter through metadata fields. The metadata in `test/test-english.lua` reflects the defaults, except for `cty-other-eras`. For an example of user-defined configuration, see `test/test-french.lua`.
|
||||
|
||||
### Formatting the number
|
||||
|
||||
The value of the field `cty-numeral-type` determines whether the number must be displayed as an `arabic` or `roman` numeral or `spelled-out` (e.g. “twenty-first”).
|
||||
|
||||
In the latter case, you may provide a list of ordinals as the value of the field `cty-spelled-out-ordinals` (see the test files for examples). The list needs to go as far as the highest century number you use: so, if you have to speak about the “thirtieth century BC”, you will have to provide the list of the 30 first ordinal numbers.
|
||||
|
||||
To determine whether the numeral should be printed in `lowercase`, `uppercase` or `smallcaps`, you can set the `cty-numeral-style` field to the desired value.
|
||||
|
||||
### Formatting the suffix
|
||||
|
||||
The value of `cty-ordinal-suffixes` determines what the suffixes are according to the number. The first item in the list is used for 1, the second for 2, and so on. If the century number is superior to the number of items in the list, the last one will be used instead. So, with the default configuration:
|
||||
|
||||
``` yaml
|
||||
cty-ordinal-suffixes:
|
||||
- rst
|
||||
- nd
|
||||
- rd
|
||||
- th
|
||||
```
|
||||
|
||||
the suffix of 7 will be “th”.
|
||||
|
||||
For language where the ordinal suffix depends on the last digit in the number, the field `cty-number-part-for-ordinal-suffix` should be set to `last-digit` (which is the default), so that you get “21rst” and not “21th”. Otherwise (e.g. in French), it should be set to `whole`.
|
||||
|
||||
The suffix is printed in `exponent` if it is the value of the field `cty-ordinal-suffix-pos`. Default is `normal`.
|
||||
|
||||
Naturally, the suffix is skipped if `cty-numeral-type` is set to `spelled-out`.
|
||||
|
||||
### Formatting the unit
|
||||
|
||||
You can define the default value of `ctyunit` by setting the `cty-default-unit-form` metadata field.
|
||||
|
||||
`cty-unit-sg` and `cty-unit-pl` let you redefine the singular and plural forms of the unit. Since the unit begins immediately after the suffix, any space should be included. If it is a normal space, it should be encoded as the HTML entity ` `, otherwise it will be stripped by Pandoc. This is why the default value for `cty-unit-sg` is ` century`.
|
||||
|
||||
### Formatting the era
|
||||
|
||||
The only built-in era is the Christian era, which is the default value of `cty-default-era`. You can define other eras thanks to the metadata field `cty-other-eras`. It is composed of a list of structures made of the following fields:
|
||||
|
||||
* `name`: the name of the era;
|
||||
* `before-zero`: the value of the era for an explicitly negative century number;
|
||||
* `after-zero`: the value of the era for an explicitly positive century number;
|
||||
* `unspecified`: the value of the era for an unprefixed century number.
|
||||
|
||||
See the file `test/test-french.md` for examples. Note that the values of the three last fields are subjected to the same constraints as with [`cty-unit-sg` and `cty-unit-pl`](#formatting-the-unit).
|
||||
|
||||
Once a custom era has been set, you can call it via the `ctyera` attribute:
|
||||
|
||||
``` yaml
|
||||
cty-other-eras:
|
||||
- name: hijra
|
||||
before-zero: ''
|
||||
after-zero: ' AH'
|
||||
unspecified: ''
|
||||
```
|
||||
|
||||
``` md
|
||||
[2]{.cty ctyera=hijra}
|
||||
```
|
||||
|
||||
> 2nd century AH
|
||||
|
||||
(In this example, we have set an empty string as the value of `before-zero` because it is not customary to refer to centuries preceding the Hegira with this era.)
|
||||
|
||||
### Changing the order
|
||||
|
||||
The field `cty-parts-order` is a list composed of `number`, `suffix`, `unit` and `era` that determines the order in which the parts of the output are displayed. You can change this order if you need it.
|
||||
|
||||
## Alphabetic list of recognized metadata fields
|
||||
|
||||
* `cty-default-era`: the name of a defined era (defaults to `christian`);
|
||||
* `cty-default-unit-form`: default value of attribute `ctyunit` (defaults to `sg`);
|
||||
* `cty-number-part-for-ordinal-suffix`: either `whole` or `last-digit` (defaults to `last-digit`);
|
||||
* `cty-numeral-style`: either `lowercase`, `uppercase` or `smallcaps` (defaults to `lowercase`);
|
||||
* `cty-numeral-type`: either `roman`, `arabic` or `spelled-out` (defaults to `arabic`);
|
||||
* `cty-ordinal-suffix-pos`: either `normal` or `exponent` (defaults to `normal`);
|
||||
* `cty-ordinal-suffixes`: a list of strings;
|
||||
* `cty-other-eras`: a list of era definitions;
|
||||
* `cty-parts-order`: a list composed of `number`, `suffix`, `unit` and `era` (which is the default order);
|
||||
* `cty-spelled-out-ordinals`: a list of strings enumerating the ordinal numbers;
|
||||
* `cty-unit-pl`: string (defaults to `' centuries'`);
|
||||
* `cty-unit-sg`: string (defaults to `' century'`);
|
||||
|
||||
## License
|
||||
|
||||
All files in this repository and its sub-repositories are copyrighted by Bastien Dumont and distributed under the MIT license. See LICENSE.txt for more details.
|
||||
|
304
centuries/centuries.lua
Normal file
304
centuries/centuries.lua
Normal file
|
@ -0,0 +1,304 @@
|
|||
-- centuries.lua
|
||||
-- A Pandoc Lua filter that automatically formats references to centuries.
|
||||
-- Copyright 2022 Bastien Dumont (bastien.dumont [at] posteo.net)
|
||||
-- This file is under the MIT License: see LICENSE for more details
|
||||
|
||||
local ERROR_CONTENT_SPAN =
|
||||
'The content of a span of class ".cty" must be a positive or negative ' ..
|
||||
'number other than 0 possibly preceded by "+" or "-".'
|
||||
|
||||
local eras = {}
|
||||
function eras:new(name, before, after, unspecified)
|
||||
self[name] = {
|
||||
before = before,
|
||||
after = after,
|
||||
unspecified = unspecified
|
||||
}
|
||||
end
|
||||
eras:new('christian', ' BC', ' AD', '')
|
||||
|
||||
local default_config = {
|
||||
-- default_unit_form is one of 'sg', 'pl' or 'none'
|
||||
default_unit_form = 'sg',
|
||||
-- default_era, unit_sg and unit_pl are plain strings
|
||||
default_era = 'christian',
|
||||
unit_sg = ' century',
|
||||
unit_pl = ' centuries',
|
||||
-- numeral_type is one of 'roman', 'arabic', 'spelled_out'
|
||||
numeral_type = 'arabic',
|
||||
-- numeral_style is one of 'lowercase', 'uppercase', 'smallcaps'
|
||||
numeral_style = 'lowercase',
|
||||
-- ordinal_suffix_pos is one of 'normal', 'exponent'
|
||||
ordinal_suffix_pos = 'normal',
|
||||
--[[
|
||||
ordinal_suffixes is a table of ordinal suffixes
|
||||
corresponding to their index number.
|
||||
If the requested number is superior to the length of the table or 0,
|
||||
the last entry will be used.
|
||||
--]]
|
||||
ordinal_suffixes = { 'rst', 'nd', 'rd', 'th' },
|
||||
-- spelled_out_ordinals is an array of strings
|
||||
spelled_out_ordinals = { 'first', 'second', 'third', 'fourth',
|
||||
'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh',
|
||||
'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth',
|
||||
'seventeenth', 'eighteenth', 'nineteenth', 'twentieth',
|
||||
'twenty-first' },
|
||||
-- number_part_for_ordinal_suffix is one of 'whole' or 'last_digit'
|
||||
number_part_for_ordinal_suffix = 'last_digit',
|
||||
-- parts_order is an array containing
|
||||
-- "number", "suffix", "unit" and "era" in any order
|
||||
parts_order = { "number", "suffix", "unit", "era" }
|
||||
}
|
||||
|
||||
local config = {}
|
||||
|
||||
local function restore_default_config(to_restore)
|
||||
if to_restore == nil then
|
||||
for key, value in pairs(default_config) do
|
||||
config[key] = value
|
||||
end
|
||||
else
|
||||
config[to_restore] = default_config[to_restore]
|
||||
end
|
||||
end
|
||||
|
||||
local function metadata_to_config(key)
|
||||
return string.gsub(string.gsub(key, '^cty%-', ''), '%-', '_')
|
||||
end
|
||||
|
||||
local function inlines_to_string(inlines)
|
||||
result = ''
|
||||
for i = 1, #inlines do
|
||||
if inlines[i].t == 'Str' then
|
||||
result = result .. inlines[i].text
|
||||
elseif inlines[i].t == 'Space' then
|
||||
result = result .. ' '
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
local function metalist_to_configlist(metalist)
|
||||
-- metalist is a MetaList of MetaInlines containing Str and Space
|
||||
-- objects.
|
||||
-- Returns an array containing the values of the corresponding
|
||||
-- strings.
|
||||
configlist = {}
|
||||
for i = 1, #metalist do
|
||||
configlist[i] = inlines_to_string(metalist[i])
|
||||
end
|
||||
return configlist
|
||||
end
|
||||
|
||||
local function get_metamap_entry_value(entry)
|
||||
if entry[1] then
|
||||
return entry[1].text
|
||||
else
|
||||
return ''
|
||||
end
|
||||
end
|
||||
|
||||
local function process_metadata_to_config(key, value)
|
||||
local config_key = metadata_to_config(key)
|
||||
if key == 'cty-ordinal-suffixes' then
|
||||
config.ordinal_suffixes = {}
|
||||
for i = 1, #value do
|
||||
table.insert(config.ordinal_suffixes, value[i][1].text)
|
||||
end
|
||||
elseif key == 'cty-other-eras' then
|
||||
for i = 1, #value do
|
||||
local this_era = value[i]
|
||||
eras:new(
|
||||
get_metamap_entry_value(this_era['name']),
|
||||
get_metamap_entry_value(this_era['before-zero']),
|
||||
get_metamap_entry_value(this_era['after-zero']),
|
||||
get_metamap_entry_value(this_era['unspecified'])
|
||||
)
|
||||
end
|
||||
elseif key == 'cty-number-part-for-ordinal-suffix'
|
||||
or key == 'cty-numeral-type' then
|
||||
config[config_key] = metadata_to_config(value[1].text)
|
||||
elseif key == 'cty-parts-order' then
|
||||
config[config_key] = metalist_to_configlist(value)
|
||||
else
|
||||
config[config_key] = value[1].text
|
||||
end
|
||||
end
|
||||
|
||||
local function set_config(meta)
|
||||
restore_default_config()
|
||||
for key, value in pairs(meta) do
|
||||
if string.match(key, '^cty%-') then
|
||||
process_metadata_to_config(key, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function get_abs_number(number)
|
||||
return math.ceil(math.abs(tonumber(number)))
|
||||
end
|
||||
|
||||
local function number_to_numeral(unformatted_century)
|
||||
if not tonumber(unformatted_century) or unformatted_century == '0' then
|
||||
error(ERROR_CONTENT_SPAN, 2)
|
||||
end
|
||||
local abs_century_number = get_abs_number(unformatted_century)
|
||||
local century_numeral
|
||||
if config.numeral_type == 'roman' then
|
||||
century_numeral = pandoc.utils.to_roman_numeral(abs_century_number)
|
||||
elseif config.numeral_type == 'arabic' then
|
||||
century_numeral = abs_century_number
|
||||
elseif config.numeral_type == 'spelled_out' then
|
||||
century_numeral = config.spelled_out_ordinals[abs_century_number]
|
||||
else
|
||||
error('"numeral_type" must be set either to "arabic", "roman" ' ..
|
||||
'or "spelled_out". Here set to "' .. config.numeral_type .. '".')
|
||||
end
|
||||
return century_numeral
|
||||
end
|
||||
|
||||
local function format_numeral(numeral)
|
||||
local formatted_numeral
|
||||
if config.numeral_style == 'lowercase' then
|
||||
formatted_numeral = pandoc.Str(string.lower(numeral))
|
||||
elseif config.numeral_style == 'uppercase' then
|
||||
formatted_numeral = pandoc.Str(string.upper(numeral))
|
||||
elseif config.numeral_style == 'smallcaps' then
|
||||
formatted_numeral = pandoc.SmallCaps(pandoc.Str(string.lower(numeral)))
|
||||
else
|
||||
error('"numeral_style" must be set either to "lowercase", "uppercase" ' ..
|
||||
'or "smallcaps". Here set to "' .. config.numeral_style .. '".')
|
||||
end
|
||||
return formatted_numeral
|
||||
end
|
||||
|
||||
local function format_century_number(unformatted_century)
|
||||
century_numeral = number_to_numeral(unformatted_century)
|
||||
formatted_century_number = format_numeral(century_numeral)
|
||||
return formatted_century_number
|
||||
end
|
||||
|
||||
local function get_last_digit(number)
|
||||
return tonumber(string.sub(tostring(number), -1))
|
||||
end
|
||||
|
||||
local function get_ordinal_suffix_for_number(raw_number)
|
||||
if config.numeral_type == 'spelled_out' then
|
||||
return ''
|
||||
else
|
||||
local abs_number = get_abs_number(raw_number)
|
||||
return config.ordinal_suffixes[abs_number]
|
||||
or config.ordinal_suffixes[#config.ordinal_suffixes]
|
||||
end
|
||||
end
|
||||
|
||||
local function get_raw_ordinal_suffix(unformatted_century)
|
||||
-- unformatted_century is an integer
|
||||
local raw_ordinal_suffix
|
||||
if config.number_part_for_ordinal_suffix == 'whole' then
|
||||
raw_ordinal_suffix = get_ordinal_suffix_for_number(unformatted_century)
|
||||
elseif config.number_part_for_ordinal_suffix == 'last_digit' then
|
||||
local the_last_digit = get_last_digit(unformatted_century)
|
||||
raw_ordinal_suffix = get_ordinal_suffix_for_number(the_last_digit)
|
||||
else
|
||||
error('"number_part_for_ordinal_suffix" must be set either to "whole" ' ..
|
||||
'or to "last_digit". Here set to "' ..
|
||||
config.number_part_for_ordinal_suffix .. '".')
|
||||
end
|
||||
return raw_ordinal_suffix
|
||||
end
|
||||
|
||||
local function format_ordinal_suffix(raw_ordinal_suffix)
|
||||
local formatted_ordinal_suffix
|
||||
if config.ordinal_suffix_pos == 'normal' then
|
||||
formatted_ordinal_suffix = pandoc.Str(raw_ordinal_suffix)
|
||||
elseif config.ordinal_suffix_pos == 'exponent' then
|
||||
formatted_ordinal_suffix = pandoc.Superscript(pandoc.Str(raw_ordinal_suffix))
|
||||
else
|
||||
error('"ordinal_suffix_pos" must be set either to "normal" ' ..
|
||||
'or to "exponent". Here set to "' .. config.ordinal_suffix_pos .. '".')
|
||||
end
|
||||
return formatted_ordinal_suffix
|
||||
end
|
||||
|
||||
local function create_ordinal_suffix(unformatted_century)
|
||||
-- unformatted_century is an integer
|
||||
local raw_ordinal_suffix = get_raw_ordinal_suffix(unformatted_century)
|
||||
local formatted_ordinal_suffix = format_ordinal_suffix(raw_ordinal_suffix)
|
||||
return formatted_ordinal_suffix
|
||||
end
|
||||
|
||||
local function format_unit(unit_form)
|
||||
local unit
|
||||
if unit_form == 'sg' then
|
||||
unit = config.unit_sg
|
||||
elseif unit_form == 'pl' then
|
||||
unit = config.unit_pl
|
||||
elseif unit_form == 'none' then
|
||||
unit = ''
|
||||
else
|
||||
error('Attribute "ctyunit" and "config.default_unit_form" must be set ' ..
|
||||
'to either "sg", "pl" or "none". Here set to "' .. unit_form .. '".')
|
||||
end
|
||||
return pandoc.Str(unit)
|
||||
end
|
||||
|
||||
local function get_era_indication(unformatted_century, era)
|
||||
local first_char = string.sub(unformatted_century, 1, 1)
|
||||
local era_indication
|
||||
if eras[era] == nil then
|
||||
error('Use of undefined era "' .. era .. '".')
|
||||
end
|
||||
if first_char == '+' then
|
||||
era_indication = eras[era].after
|
||||
elseif first_char == '-' then
|
||||
era_indication = eras[era].before
|
||||
else
|
||||
era_indication = eras[era].unspecified
|
||||
end
|
||||
return pandoc.Str(era_indication)
|
||||
end
|
||||
|
||||
local function reorder_parts(parts_array)
|
||||
local part_name_to_value = {
|
||||
number = parts_array[1],
|
||||
suffix = parts_array[2],
|
||||
unit = parts_array[3],
|
||||
era = parts_array[4]
|
||||
}
|
||||
local reordered_array = {}
|
||||
for i = 1, #config.parts_order do
|
||||
reordered_array[i] = part_name_to_value[config.parts_order[i]]
|
||||
end
|
||||
return reordered_array
|
||||
end
|
||||
|
||||
local function format_century(unformatted_century, unit_form, era)
|
||||
-- unformatted_century is an integer possibly preceded by '+' or '-'
|
||||
local century_number = format_century_number(unformatted_century)
|
||||
local ordinal_suffix = create_ordinal_suffix(unformatted_century)
|
||||
local unit = format_unit(unit_form)
|
||||
local era_indication = get_era_indication(unformatted_century, era)
|
||||
local formatted_century = reorder_parts(
|
||||
{ century_number, ordinal_suffix, unit, era_indication })
|
||||
return formatted_century
|
||||
end
|
||||
|
||||
function Span(span)
|
||||
if span.classes:includes('cty', 1) then
|
||||
if #span.content == 1 and span.content[1].t == 'Str' then
|
||||
local unformatted_century = span.content[1].text
|
||||
local unit_form = span.attributes.ctyunit or config.default_unit_form
|
||||
local era = span.attributes.ctyera or config.default_era
|
||||
span.content = format_century(unformatted_century, unit_form, era)
|
||||
return span
|
||||
else
|
||||
error(ERROR_CONTENT_SPAN)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
{ Meta = set_config },
|
||||
{ Span = Span }
|
||||
}
|
75
centuries/test/test-english.md
Normal file
75
centuries/test/test-english.md
Normal file
|
@ -0,0 +1,75 @@
|
|||
---
|
||||
cty-default-unit-form: sg
|
||||
cty-default-era: christian
|
||||
cty-other-eras:
|
||||
- name: hijra
|
||||
before-zero: ''
|
||||
after-zero: ' AH'
|
||||
unspecified: ''
|
||||
cty-unit-sg: ' century'
|
||||
cty-unit-pl: ' centuries'
|
||||
cty-numeral-type: arabic
|
||||
cty-numeral-style: lowercase
|
||||
cty-ordinal-suffix-pos: normal
|
||||
cty-ordinal-suffixes:
|
||||
- rst
|
||||
- nd
|
||||
- rd
|
||||
- th
|
||||
cty-number-part-for-ordinal-suffix: 'last-digit'
|
||||
cty-spelled-out-ordinals:
|
||||
- first
|
||||
- second
|
||||
- third
|
||||
- fourth
|
||||
- fifth
|
||||
- sixth
|
||||
- seventh
|
||||
- eighth
|
||||
- ninth
|
||||
- tenth
|
||||
- eleventh
|
||||
- twelfth
|
||||
- thirteenth
|
||||
- fourteenth
|
||||
- fifteenth
|
||||
- sixteenth
|
||||
- seventeenth
|
||||
- eighteenth
|
||||
- nineteenth
|
||||
- twentieth
|
||||
- twenty-first
|
||||
cty-parts-order:
|
||||
- number
|
||||
- suffix
|
||||
- unit
|
||||
- era
|
||||
---
|
||||
|
||||
<!--
|
||||
The metadata of this file corresponds to
|
||||
the built-in default configuration,
|
||||
except for the additional "hijra" era.
|
||||
-->
|
||||
|
||||
[1]{.cty}
|
||||
|
||||
[-1]{.cty}
|
||||
|
||||
[5]{.cty}
|
||||
|
||||
[20]{.cty}
|
||||
|
||||
[21]{.cty}
|
||||
|
||||
[+5]{.cty}
|
||||
|
||||
[-5]{.cty}
|
||||
|
||||
from the [5]{.cty ctyunit=none} to the [8]{.cty}
|
||||
|
||||
[5]{.cty ctyunit=none}–[8]{.cty ctyunit=pl}
|
||||
|
||||
[+3]{.cty ctyera=hijra}
|
||||
|
||||
[2]{.cty ctyera=hijra ctyunit=none}/[8]{.cty}
|
82
centuries/test/test-english.native
Normal file
82
centuries/test/test-english.native
Normal file
|
@ -0,0 +1,82 @@
|
|||
[ RawBlock
|
||||
(Format "html")
|
||||
"<!--\n The metadata of this file corresponds to\n the built-in default configuration,\n except for the additional \"hijra\" era.\n-->"
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "1" , Str "rst" , Str " century" , Str "" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "1" , Str "rst" , Str " century" , Str " BC" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "5" , Str "th" , Str " century" , Str "" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "20" , Str "th" , Str " century" , Str "" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "21" , Str "rst" , Str " century" , Str "" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "5" , Str "th" , Str " century" , Str " AD" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "5" , Str "th" , Str " century" , Str " BC" ]
|
||||
]
|
||||
, Para
|
||||
[ Str "from"
|
||||
, Space
|
||||
, Str "the"
|
||||
, Space
|
||||
, Span
|
||||
( "" , [ "cty" ] , [ ( "ctyunit" , "none" ) ] )
|
||||
[ Str "5" , Str "th" , Str "" , Str "" ]
|
||||
, Space
|
||||
, Str "to"
|
||||
, Space
|
||||
, Str "the"
|
||||
, Space
|
||||
, Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "8" , Str "th" , Str " century" , Str "" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [ ( "ctyunit" , "none" ) ] )
|
||||
[ Str "5" , Str "th" , Str "" , Str "" ]
|
||||
, Str "\8211"
|
||||
, Span
|
||||
( "" , [ "cty" ] , [ ( "ctyunit" , "pl" ) ] )
|
||||
[ Str "8" , Str "th" , Str " centuries" , Str "" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [ ( "ctyera" , "hijra" ) ] )
|
||||
[ Str "3" , Str "rd" , Str " century" , Str " AH" ]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( ""
|
||||
, [ "cty" ]
|
||||
, [ ( "ctyera" , "hijra" ) , ( "ctyunit" , "none" ) ]
|
||||
)
|
||||
[ Str "2" , Str "nd" , Str "" , Str "" ]
|
||||
, Str "/"
|
||||
, Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ Str "8" , Str "th" , Str " century" , Str "" ]
|
||||
]
|
||||
]
|
64
centuries/test/test-french.md
Normal file
64
centuries/test/test-french.md
Normal file
|
@ -0,0 +1,64 @@
|
|||
---
|
||||
cty-default-unit-form: sg
|
||||
cty-default-era: christ
|
||||
cty-other-eras:
|
||||
- name: christ
|
||||
before-zero: ' av. J.-C.'
|
||||
after-zero: ' ap. J.-C.'
|
||||
unspecified: ''
|
||||
- name: hegire
|
||||
before-zero: ''
|
||||
after-zero: ' A. H.'
|
||||
unspecified: ''
|
||||
cty-unit-sg: ' siècle'
|
||||
cty-unit-pl: ' siècles'
|
||||
cty-numeral-type: roman
|
||||
cty-numeral-style: smallcaps
|
||||
cty-ordinal-suffix-pos: exponent
|
||||
cty-ordinal-suffixes:
|
||||
- er
|
||||
- e
|
||||
cty-number-part-for-ordinal-suffix: 'whole'
|
||||
cty-spelled-out-ordinals:
|
||||
- premier
|
||||
- deuxième
|
||||
- troisième
|
||||
- quatrième
|
||||
- cinquième
|
||||
- sixième
|
||||
- septième
|
||||
- huitième
|
||||
- neuvième
|
||||
- dixième
|
||||
- onzième
|
||||
- douzième
|
||||
- treizième
|
||||
- quatorzième
|
||||
- quinzième
|
||||
- seizième
|
||||
- dix-septième
|
||||
- dix-huitième
|
||||
- dix-neuvième
|
||||
- vingtième
|
||||
- vingt-et-unième
|
||||
---
|
||||
|
||||
[1]{.cty}
|
||||
|
||||
[-1]{.cty}
|
||||
|
||||
[5]{.cty}
|
||||
|
||||
[21]{.cty}
|
||||
|
||||
[+5]{.cty}
|
||||
|
||||
[-5]{.cty}
|
||||
|
||||
du [5]{.cty ctyunit=none} au [8]{.cty}
|
||||
|
||||
[5]{.cty ctyunit=none}-[8]{.cty ctyunit=pl}
|
||||
|
||||
[+3]{.cty ctyera=hegire}
|
||||
|
||||
[2]{.cty ctyera=hegire ctyunit=none}/[8]{.cty}
|
122
centuries/test/test-french.native
Normal file
122
centuries/test/test-french.native
Normal file
|
@ -0,0 +1,122 @@
|
|||
[ Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "i" ]
|
||||
, Superscript [ Str "er" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str ""
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "i" ]
|
||||
, Superscript [ Str "er" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str " av.\160J.-C."
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "v" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str ""
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "xxi" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str ""
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "v" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str " ap.\160J.-C."
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "v" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str " av.\160J.-C."
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Str "du"
|
||||
, Space
|
||||
, Span
|
||||
( "" , [ "cty" ] , [ ( "ctyunit" , "none" ) ] )
|
||||
[ SmallCaps [ Str "v" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str ""
|
||||
, Str ""
|
||||
]
|
||||
, Space
|
||||
, Str "au"
|
||||
, Space
|
||||
, Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "viii" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str ""
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [ ( "ctyunit" , "none" ) ] )
|
||||
[ SmallCaps [ Str "v" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str ""
|
||||
, Str ""
|
||||
]
|
||||
, Str "-"
|
||||
, Span
|
||||
( "" , [ "cty" ] , [ ( "ctyunit" , "pl" ) ] )
|
||||
[ SmallCaps [ Str "viii" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cles"
|
||||
, Str ""
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( "" , [ "cty" ] , [ ( "ctyera" , "hegire" ) ] )
|
||||
[ SmallCaps [ Str "iii" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str " A.\160H."
|
||||
]
|
||||
]
|
||||
, Para
|
||||
[ Span
|
||||
( ""
|
||||
, [ "cty" ]
|
||||
, [ ( "ctyera" , "hegire" ) , ( "ctyunit" , "none" ) ]
|
||||
)
|
||||
[ SmallCaps [ Str "ii" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str ""
|
||||
, Str ""
|
||||
]
|
||||
, Str "/"
|
||||
, Span
|
||||
( "" , [ "cty" ] , [] )
|
||||
[ SmallCaps [ Str "viii" ]
|
||||
, Superscript [ Str "e" ]
|
||||
, Str "\160si\232cle"
|
||||
, Str ""
|
||||
]
|
||||
]
|
||||
]
|
65
centuries/test/test-functions.lua
Normal file
65
centuries/test/test-functions.lua
Normal file
|
@ -0,0 +1,65 @@
|
|||
restore_default_config()
|
||||
|
||||
assert(metadata_to_config('cty-suffix-sg') == 'suffix_sg')
|
||||
assert(metadata_to_config('last-digit') == 'last_digit')
|
||||
|
||||
local output = metalist_to_configlist(pandoc.MetaList({
|
||||
pandoc.MetaInlines(pandoc.Str('number')),
|
||||
pandoc.MetaInlines(pandoc.Str('suffix')),
|
||||
pandoc.MetaInlines(pandoc.Str('unit')),
|
||||
pandoc.MetaInlines(pandoc.Str('era'))}))
|
||||
assert(output[1] == 'number'
|
||||
and output[2] == 'suffix'
|
||||
and output[3] == 'unit'
|
||||
and output[4] == 'era')
|
||||
|
||||
local output = metalist_to_configlist(pandoc.MetaList({
|
||||
pandoc.MetaInlines({
|
||||
pandoc.Str('the'),
|
||||
pandoc.Space(),
|
||||
pandoc.Str('third')})}))
|
||||
assert(output[1] == 'the third')
|
||||
|
||||
assert(format_century_number(6) == pandoc.Str('6'))
|
||||
config.numeral_type = 'spelled_out'
|
||||
assert(format_century_number(6) == pandoc.Str('sixth'))
|
||||
config.numeral_type = 'roman'
|
||||
config.numeral_style = 'smallcaps'
|
||||
assert(format_century_number(6) == pandoc.SmallCaps(pandoc.Str('vi')))
|
||||
assert(format_century_number(-6) == pandoc.SmallCaps(pandoc.Str('vi')))
|
||||
config.numeral_style = 'uppercase'
|
||||
assert(format_century_number(6) == pandoc.Str('VI'))
|
||||
restore_default_config('numeral_type')
|
||||
restore_default_config('numeral_style')
|
||||
|
||||
assert(get_last_digit(235) == 5)
|
||||
assert(get_last_digit(5) == 5)
|
||||
|
||||
assert(get_raw_ordinal_suffix('1') == 'rst')
|
||||
assert(get_raw_ordinal_suffix('+1') == 'rst')
|
||||
assert(get_raw_ordinal_suffix('-1') == 'rst')
|
||||
assert(get_raw_ordinal_suffix('2') == 'nd')
|
||||
assert(get_raw_ordinal_suffix('9') == 'th')
|
||||
assert(get_raw_ordinal_suffix('20') == 'th')
|
||||
assert(get_raw_ordinal_suffix('21') == 'rst')
|
||||
config.number_part_for_ordinal_suffix = 'whole'
|
||||
assert(get_raw_ordinal_suffix('21') == 'th')
|
||||
assert(get_raw_ordinal_suffix('25') == 'th')
|
||||
config.numeral_type = 'spelled_out'
|
||||
assert(get_raw_ordinal_suffix('1') == '')
|
||||
restore_default_config('numberal_type')
|
||||
restore_default_config('number_part_for_ordinal_suffix')
|
||||
|
||||
assert(get_era_indication('+6', 'christian') == pandoc.Str(' AD'))
|
||||
assert(get_era_indication('-6', 'christian') == pandoc.Str(' BC'))
|
||||
assert(get_era_indication('6', 'christian') == pandoc.Str(''))
|
||||
|
||||
config.parts_order = { 'unit', 'era', 'number', 'suffix' }
|
||||
local input = { '3', 'rd', 'century', 'CE' }
|
||||
local output = reorder_parts(input)
|
||||
assert(output[1] == 'century'
|
||||
and output[2] == 'CE'
|
||||
and output[3] == '3'
|
||||
and output[4] == 'rd')
|
||||
restore_default_config('parts_order')
|
||||
|
Loading…
Reference in New Issue
Block a user