-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_translate
executable file
·62 lines (44 loc) · 1.53 KB
/
api_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
#!/usr/bin/env python
import os, glob, subprocess
import pypandoc
"""
Translate Pipeline Using Pandoc API
Use glob to find all markdown files in `rmm/docs/`
For each one, run the pipeline using api:
- for each document in `rmm/docs/`
- pandoc markdown-to-json
- panflute filter to translate json and extract links
- pandoc json-to-markdown
- output documents in translated_docs/
TODO:
- call the pandoc API directly to avoid subprocess
"""
SOURCE_DOCS = 'source'
TARGET_DOCS = 'target'
TRANSLATE_FILTER = 'filters/shift_links.py'
if not os.path.isdir(os.path.join(os.getcwd(),SOURCE_DOCS)):
err = "ERROR: No source docs folder found.\n"
err += "Looked in %s\n"%(os.path.join(os.getcwd(),SOURCE_DOCS))
raise Exception(err)
cwd = os.getcwd()
print("[+] Welcome to translate the docs!")
print("[+] Making directory for translated documentation: %s"%(TARGET_DOCS))
subprocess.call(['mkdir','-p',TARGET_DOCS], cwd=cwd)
markdown_files = []
for fdir,fdirnames,fnames in os.walk(os.path.join(os.getcwd(),SOURCE_DOCS)):
for f in fnames:
##########
## FIXME
#if f[-3:]=='.md':
if f=='index.md':
markdown_files.append( os.path.join( fdir, f ) )
for en_md in markdown_files:
filters = [TRANSLATE_FILTER]
output = pypandoc.convert_file(en_md,
to='gfm',
format='gfm',
filters=filters)
print(output)
print("="*40)
print("="*40)
print("[+] All done!")