Created
April 2, 2021 21:26
-
-
Save driazati/edf5bb298cba3f051b2c3e5cafbf9dc0 to your computer and use it in GitHub Desktop.
convert a list of flat nested items to markdown bullet points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The ideal usage is to create an index of all the files | |
# from the current dir for describing in some .md file: | |
# find . | grep -v .git | python nested_list_to_md_bullets.py | |
import sys | |
import argparse | |
import pprint | |
parser = argparse.ArgumentParser(description='') | |
parser.add_argument('--file', default='-') | |
parser.add_argument('--separator', default='/') | |
args = parser.parse_args() | |
if args.file == "-": | |
c = sys.stdin.readlines() | |
else: | |
c = open(args.file, "r").readlines() | |
res = {} | |
for line in c: | |
parts = line.strip().split(args.separator) | |
curr = res | |
for part in parts: | |
if part in curr: | |
pass | |
else: | |
curr[part] = {} | |
curr = curr[part] | |
def helper(curr, key, fullpath, level=0): | |
new_level = level | |
if key is not None: | |
print(f"{' ' * level * 2} * [`{key}`]({args.separator.join(fullpath)}) - ") | |
new_level = level + 1 | |
for k, v in curr.items(): | |
helper(v, k, fullpath + [k], new_level) | |
helper(res, None, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment