Last active
April 8, 2022 15:18
-
-
Save JohnnyHowe/ffa2c3b9ebfa9953f0c8424d745b76fc to your computer and use it in GitHub Desktop.
Place me in a Obsidian md vault and when run I will create an index.md file. Filenames shown as headings, and headins shown as bullet points with appropriate indenting.
This file contains hidden or 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
| """ | |
| Place me in a Obsidian md vault and when run I will create an index.md file. | |
| Filenames shown as headings, and headins shown as bullet points with appropriate indenting. | |
| Just try it out | |
| Jonathon Howe | |
| """ | |
| import os | |
| import re | |
| def get_markdown_filenames(dir_path): | |
| """ What .md files are in the path? | |
| Returns a list of tuples. First item is full path, second is filename.""" | |
| # return [("C:/Users/jonat/Google Drive/uni/COSC362 (Data and Network Security)/notes", "tls.md")] | |
| full_filenames = [] | |
| for root, _, files in os.walk(dir_path): | |
| for file in files: | |
| if re.match(".*\.md", file) and file != "index.md": | |
| full_filenames.append((root, file)) | |
| return full_filenames | |
| def get_file_contents(filename): | |
| """ Returns a list containing strings of each line of the file. | |
| Lines end with newline. """ | |
| file = open(filename, encoding="utf8") | |
| lines = file.readlines() | |
| file.close() | |
| return lines | |
| def get_file_headings(contents): | |
| """ Given a list of each line in the file, return a list of all headings. | |
| Each heading represented as a tuple as (level, title) where level is at least 1. """ | |
| headings = [] | |
| for line in contents: | |
| re_match = re.match("#+", line) | |
| if re_match: | |
| level = len(re_match.group(0)) | |
| title = process_heading(line) | |
| headings.append((level, title)) | |
| return headings | |
| def process_heading(raw_heading): | |
| re_match = re.match("#+", raw_heading) | |
| level = len(re_match.group(0)) | |
| heading = raw_heading[level + 1:].replace("\n", "").strip() | |
| return heading | |
| def get_filename_heading(filename): | |
| return filename[:-3] | |
| def format_heading_title(raw_title): | |
| return raw_title.replace("[", " ").replace("]", " ").replace("|", " ").replace("#", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ") | |
| def get_heading_alias(heading): | |
| alias_match = re.findall("\[\[.+\|(.+)\]\]", heading) | |
| if (alias_match): return alias_match[0].strip() | |
| alias_match = re.search("\[\[(.+)\]\]", heading) | |
| if (alias_match): return heading.replace("[", "").replace("]", "").replace("#", "") | |
| return heading | |
| def get_bullet_point(level, heading_title, filename): | |
| heading = format_heading_title(heading_title) | |
| alias = get_heading_alias(heading_title) | |
| return "{}* [[{}#{}|{}]]".format(" " * level, filename, heading, alias) | |
| def generate_index_markdown(dir_path): | |
| index_lines = [] | |
| for path, filename in get_markdown_filenames(dir_path): | |
| # Filename headings | |
| heading = get_filename_heading(filename) | |
| index_lines.append("# [[{}]]".format(heading)) | |
| # Heading bullet points | |
| contents = get_file_contents(path + "\\" + filename) | |
| headings = get_file_headings(contents) | |
| indent_offset = 0 | |
| last_indent = 0 | |
| for heading_indent, heading_title in headings: | |
| if heading_indent > last_indent + 1: | |
| indent_offset = heading_indent - last_indent - 1 | |
| new_indent = heading_indent - indent_offset | |
| index_lines.append(get_bullet_point(new_indent, heading_title, filename)) | |
| last_indent = heading_indent | |
| return "\n".join(index_lines) | |
| def generate_to_file(newfile): | |
| file = open(newfile, "w+") | |
| file.write(generate_index_markdown(os.getcwd())) | |
| file.close() | |
| def main(): | |
| generate_to_file("index.md") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment