mirror of
https://github.com/Vonng/ddia.git
synced 2024-12-06 15:20:12 +08:00
add: translate file or repo
This commit is contained in:
parent
df568afb62
commit
36f2daaeca
54
transform.py
54
transform.py
@ -4,18 +4,60 @@ Refer to https://github.com/BYVoid/OpenCC
|
||||
import click
|
||||
import opencc
|
||||
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
|
||||
@click.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 main(infile, outfile, cfg):
|
||||
|
||||
@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
|
||||
"""
|
||||
converter = opencc.OpenCC(cfg)
|
||||
with open(infile, "r") as inf, open(outfile, "w+") as outf:
|
||||
data = inf.readlines()
|
||||
data = list(map(converter.convert, data))
|
||||
outf.writelines(data)
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
cli()
|
||||
|
Loading…
Reference in New Issue
Block a user