Skip to content

Instantly share code, notes, and snippets.

@tpb1908
Created May 3, 2017 20:46
Show Gist options
  • Save tpb1908/e9a3337f8936710b67f866c85c719a8c to your computer and use it in GitHub Desktop.
Save tpb1908/e9a3337f8936710b67f866c85c719a8c to your computer and use it in GitHub Desktop.
Walk a directory and create a markdown file for given file types
#!/usr/bin/env python
import os
# Valid file types
file_types = ["java", "xml", "gradle"]
# Only include paths containing one of these strings
path_whitelist = ["src"]
def write_file(output, path, name):
with open(os.path.join(path, name), "r") as code:
snippet = "# " + os.path.join(path, name) # Full path header
snippet += "\n**" + str(name) + "**\n" # File name bold
snippet += "\n```" + str(name.rsplit('.', 1)[-1]) # Code with language
snippet += "\n"
snippet += code.read()
snippet += "```\n\n\n---\n\n" # Break
output.write(snippet)
def load_code(output, dir_name, files):
if any(sub in dir_name for sub in path_whitelist):
for file in [f for f in files if os.path.isfile(os.path.join(dir_name, f))]:
if file.rsplit('.', 1)[-1] in file_types:
write_file(output, dir_name, file)
f = open("output.md", "w+")
os.path.walk("path/to/your/code", load_code, f)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment