Skip to content

Instantly share code, notes, and snippets.

@deadbok
Created May 14, 2017 23:48
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 deadbok/99a36b7a097572af982f43bd0a2c183d to your computer and use it in GitHub Desktop.
Save deadbok/99a36b7a097572af982f43bd0a2c183d to your computer and use it in GitHub Desktop.
Directory tree in a nice MarkDown unordered list.

MarkDown tree

tree command that outputs the tree in a nice MarkDown unordered list.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2017 Martin Bo Kristensen Grønholdt
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Version 1.0.0
# * First working version.
"""
Name: mdtree.py
Author: Martin Bo Kristensen Grønholdt.
Version: 1.0.0 (2017-05-14)
`tree` command that outputs the tree in a nice MarkDown unordered list.
"""
import argparse
import os
# Program version.
__VERSION__ = '1.0.0'
def parse_commandline():
"""
Parse command line arguments.
:return: A tuple with a list of files path to include, and a list of paths
to exclude.
"""
# Set up the arguments.
parser = argparse.ArgumentParser(
description='mdtree.py v{}'.format(__VERSION__) + ' by Martin B.' +
' K. Grønholdt\nDirectory tree in a nice MarkDown ' +
'unordered list.')
parser.add_argument(dest='path', default=None,
help='Root directory to recurse.')
# Parse command line
args = parser.parse_args()
# Return the paths.
return ((args.path))
def tree(path, depth=0):
files = []
for entry in os.listdir(path):
if not entry.startswith('.'):
new_path = os.path.join(path, entry)
if os.path.isdir(new_path):
print(' ' * depth, end='')
print('* `{}`'.format(entry))
tree(new_path, depth + 1)
else:
files.append(entry)
for file in files:
print(' ' * depth, end='')
print('* `{}`'.format(file))
def main():
"""
Program main entry point.
"""
# Parse the command line.
path = parse_commandline()
tree(os.path.expanduser(path))
# Run this when invoked directly
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment