Skip to content

Instantly share code, notes, and snippets.

@OriPekelman
Created May 26, 2015 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OriPekelman/74d1f0f6280f39ebee74 to your computer and use it in GitHub Desktop.
Save OriPekelman/74d1f0f6280f39ebee74 to your computer and use it in GitHub Desktop.
Recursively creates an md file for all rst files in the source t directory and below.
#!/usr/bin/env python
'''
Recursively creates an md file for all rst
files in the source directory and below.
'''
import sys, os, re, subprocess
target = 'gitbook/'
source = './'
if len(sys.argv) >= 2: target = sys.argv[1]
rst_files = []
for root, dirnames, filenames in os.walk(source):
for name in filenames:
if name.endswith("rst"):
rst_files.append(os.path.join(root, name))
for rst in rst_files:
md_file = target + re.sub('.(rst)$','',rst) + ".md"
cmd=('pandoc --from=rst --to=markdown "{0}"').format(rst)
markdown= subprocess.check_output(cmd, shell=True)
if not os.path.exists(os.path.dirname(md_file)):
os.makedirs(os.path.dirname(md_file))
file = open(md_file, "w")
file.write(markdown)
print(cmd + " Wrote to :" + md_file)
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment