Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ class. This may change in the future as specific needs arise.

The following members of the context object are available in all the hooks:

- `ctx.src`: Source text. Read only.
- `ctx.orig`: Original source text. Read only.
- `ctx.src`: Source text, initially equal to `ctx.orig` but it may be modified,
e.g. during normalization.
- `ctx.general`: Configuration general options.
- `ctx.langsec`: language section (S2R or R2S) of configuration.
- `ctx.options`: language-specific options defined in configuration and set
Expand Down
39 changes: 39 additions & 0 deletions scriptshifter/hooks/gurmukhi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from logging import getLogger
from os import path
from re import compile

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


logger = getLogger(__name__)

MOD_BASEDIR = path.dirname(__file__)

with open(path.join(MOD_BASEDIR, "gurmukhi_nasalization_pre.yml")) as fh:
nas_config = {"initial": {}, "default": {}}

for k, v in yload(fh, Loader=Loader).items():
if "%" in k:
nas_config["initial"][compile(k.replace("%", "\\b"))] = v
else:
nas_config["default"][k] = v


def nasalize_post_config(ctx):
"""
Preprocess Roman input to get a uniform nasalization.

The result is not a correct romanization: it is an intermediate stage
to be passed to the Gurmukhi table.
"""
for k, v in nas_config["initial"].items():
ctx.src = k.sub(v, ctx.src)
for k, v in nas_config["default"].items():
ctx.src = ctx.src.replace(k, v)

if ctx.orig != ctx.src:
logger.debug(f"Corrected nasalization: {ctx.orig} -> {ctx.src}")
Loading