Skip to content

Commit

Permalink
Complete Chinese numerals:
Browse files Browse the repository at this point in the history
* Use standard table override instead of pre-config hooks.
* Add few test strings.
  • Loading branch information
scossu committed Apr 12, 2024
1 parent 4f3d021 commit de20990
Show file tree
Hide file tree
Showing 5 changed files with 45,722 additions and 45,677 deletions.
24 changes: 1 addition & 23 deletions scriptshifter/hooks/chinese/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,20 @@


from logging import getLogger
from os import path
from re import I, compile, search, sub

from yaml import load
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader


HOOK_DIR = path.dirname(path.realpath(__file__))

logger = getLogger(__name__)


def merge_numerals_pre_config(tdata):
"""
Add numerals mapping to configuration.
This overrides the existing character mappings.
"""
num_map_yml = path.join(HOOK_DIR, "numerals.yml")
with open(num_map_yml, "r") as fh:
num_map = load(fh, Loader=Loader)

tdata["script_to_roman"]["map"].update(num_map)


def parse_numerals(ctx):
"""
Parse Chinese numerals in the already romanized result.
This is run at post-assembly.
"""
# Only apply to specific MARC fields.
use_num_v = ctx.options.get("marc_field") in ("245", "830")
use_num_v = ctx.options.get("marc_field") in ("245n", "830n")

# tokens = split(r"[\W^#]", ctx.dest) # Original logic.
tokens = [tk.strip() for tk in ctx.dest_ls]
Expand Down
21 changes: 18 additions & 3 deletions scriptshifter/tables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
except ImportError:
from yaml import Loader

from scriptshifter.exceptions import ConfigError
from scriptshifter.exceptions import BREAK, ConfigError


__doc__ = """
Expand All @@ -28,6 +28,7 @@

# Available hook names.
HOOKS = (
"pre_config",
"post_config",
"post_normalize",
"begin_input_token",
Expand Down Expand Up @@ -126,7 +127,10 @@ def __hash__(self):
@cache
def list_tables():
"""
List all the available tables.
List all the indexed tables.
Note that this may not correspond to all the table files in the data
folder, but only those exposed in the index.
"""
with open(path.join(TABLE_DIR, "index.yml")) as fh:
tdata = load(fh, Loader=Loader)
Expand All @@ -150,7 +154,18 @@ def load_table(tname):
with open(fname) as fh:
tdata = load(fh, Loader=Loader)

# NOTE Only one level of inheritance. No need for recursion for now.
# Pre-config hooks.
# If any of these hooks returns BREAK, interrupt the configuration
# parsing and return whatever is obtained so far.
if "hooks" in tdata:
tdata["hooks"] = load_hook_fn(tname, tdata)
pre_cfg_hooks = tdata.get("hooks", {}).get("pre_config", [])
for hook_def in pre_cfg_hooks:
kwargs = hook_def[1] if len(hook_def) > 1 else {}
ret = hook_def[0](tdata, **kwargs)
if ret == BREAK:
return tdata

parents = tdata.get("general", {}).get("parents", [])

if "script_to_roman" in tdata:
Expand Down
Loading

0 comments on commit de20990

Please sign in to comment.