-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate
executable file
·78 lines (55 loc) · 2.38 KB
/
translate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
import os, glob, subprocess
"""
Carry Out the Translate Task
Use glob to find all markdown files in `rmm/docs/`
For each one, run the pipeline using subprocess:
- run pipeline on each document in `rmm/docs/`
- pandoc markdown-to-json
- panflute filter to translate json and extract links
- pandoc json-to-markdown
- output documents in ruskie_docs/
TODO:
- call the pandoc API directly to avoid subprocess
"""
RMM_DOCS = 'rmm/docs'
RRMM_DOCS = 'ruskie_docs'
if not os.path.isdir(os.path.join(os.getcwd(),RMM_DOCS)):
err = "ERROR: No rmm/docs/ folder was found.\n"
err += "Looked in %s\n"%(os.path.join(os.getcwd(),RMM_DOCS))
err += "Try cloning with --recursive,\n"
err += "or running git submodule update --init\n"
raise Exception(err)
cwd = os.getcwd()
print("[+] Welcome to russian rainbow mind machine!")
print("[+] Making directory for Russian documentation")
subprocess.call(['mkdir','-p',RRMM_DOCS], cwd=cwd)
markdown_files = []
for fdir,fdirnames,fnames in os.walk(os.path.join(os.getcwd(),RMM_DOCS)):
for f in fnames:
if f[-3:]=='.md':
markdown_files.append( os.path.join( fdir, f ) )
for en_md in markdown_files:
basename = os.path.split(en_md)[-1]
target = os.path.join(RRMM_DOCS,basename)
pname = os.path.join(RMM_DOCS,basename)
print(" [+] Now making documentation for %s to %s"%(pname,target))
# Command 1:
# cat <md>
cat_cmd = ['cat', en_md]
cat_proc = subprocess.Popen(cat_cmd, stdout=subprocess.PIPE)
# pandoc: english markdown to json
# -f from_format
# -t to_format
pandoc_from_en_cmd = ['pandoc','-f','gfm','-t','json','-s']
pandoc_from_en_proc = subprocess.Popen(pandoc_from_en_cmd, stdin=cat_proc.stdout, stdout=subprocess.PIPE)
# pandoc filter to translate with google cloud
pandoc_filter_cmd = ['filters/ruskie.py']
pandoc_filter_proc = subprocess.Popen(pandoc_filter_cmd, stdin=pandoc_from_en_proc.stdout, stdout=subprocess.PIPE)
# pandoc: json to russian markdown
pandoc_to_ru_cmd = ['pandoc','-f','json','-t','gfm']
pandoc_to_ru_proc = subprocess.Popen(pandoc_to_ru_cmd, stdin=pandoc_filter_proc.stdout, stdout=subprocess.PIPE)
with open(target,'wb') as f:
f.write(pandoc_to_ru_proc.stdout.read())
print(" [+] Finished with file %s, translated to %s"%(pname,target))
print("[+] All done!")