2020-10-06 00:57:18 +08:00
|
|
|
"""Convert zh-cn to zh-tw
|
|
|
|
Refer to https://github.com/BYVoid/OpenCC
|
|
|
|
"""
|
|
|
|
import click
|
|
|
|
import opencc
|
|
|
|
|
2020-10-06 01:21:47 +08:00
|
|
|
from pathlib import Path
|
|
|
|
from pprint import pprint
|
2020-10-06 00:57:18 +08:00
|
|
|
|
2020-10-06 01:21:47 +08:00
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def convert(infile: str, outfile: str, cfg: str):
|
|
|
|
"""read >> convert >> write file
|
|
|
|
Args:
|
|
|
|
infile (str): input file
|
|
|
|
outfile (str): output file
|
|
|
|
cfg (str): config
|
|
|
|
"""
|
2020-10-06 00:57:18 +08:00
|
|
|
converter = opencc.OpenCC(cfg)
|
|
|
|
with open(infile, "r") as inf, open(outfile, "w+") as outf:
|
2021-08-04 02:22:27 +08:00
|
|
|
outf.write("\n".join(converter.convert(line) for line in inf))
|
2020-10-06 01:21:47 +08:00
|
|
|
print(f"Convert to {outfile}")
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.option("-i", "--input", "infile", required=True)
|
|
|
|
@click.option("-o", "--output", "outfile", required=True)
|
|
|
|
@click.option("-c", "--config", "cfg", required=True, default="s2twp.json")
|
|
|
|
def file(infile: str, outfile: str, cfg: str):
|
|
|
|
"""read >> convert >> write file
|
|
|
|
Args:
|
|
|
|
infile (str): input file
|
|
|
|
outfile (str): output file
|
|
|
|
cfg (str): config
|
|
|
|
"""
|
|
|
|
convert(infile, outfile, cfg)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.option("-i", "--input", "infolder", required=True)
|
|
|
|
@click.option("-o", "--output", "outfolder", required=True)
|
|
|
|
@click.option("-c", "--config", "cfg", required=True, default="s2twp.json")
|
|
|
|
def repo(infolder, outfolder, cfg):
|
|
|
|
if not Path(outfolder).exists():
|
|
|
|
Path(outfolder).mkdir(parents=True)
|
|
|
|
print(f"Create {outfolder}")
|
|
|
|
infiles = Path(infolder).resolve().glob("*.md")
|
|
|
|
pair = [
|
|
|
|
{"infile": str(infile), "outfile": str(Path(outfolder).resolve() / infile.name)}
|
|
|
|
for idx, infile in enumerate(infiles)
|
|
|
|
]
|
|
|
|
for p in pair:
|
|
|
|
convert(p["infile"], p["outfile"], cfg)
|
2020-10-06 00:57:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-06 01:21:47 +08:00
|
|
|
cli()
|