on master help sign in
Fix `anchors.py` functionality bug
c73d695Yaseen
anchors.py+11−8
@@ -34,8 +34,8 @@ def validate_entry(setting: str, file: str):
34
34
35
35
36def find_all_mds() -> List[str]:
36def find_all_mds() -> List[str]:
37− # **/_index.md for all index files
37+ # the `insert_anchor_links` is only applicable on `_index.md` files (i.e. sections)
38− return [str(p) for p in Path('./').rglob('*.md')] # only applicable on `_index.md` files (i.e. sections)
38+ return [str(p) for p in Path('./').rglob('**/_index.md')]
39
39
40def setting_from_frontmatter(lines: List[str]) -> str:
40def setting_from_frontmatter(lines: List[str]) -> str:
41 # 'insert_anchor_links = "none"' -> 'none'
41 # 'insert_anchor_links = "none"' -> 'none'
@@ -57,7 +57,7 @@ def anchors_from_frontmatter(md_files: List[str]) -> List[Tuple[str, str]]:
57 anchor_settings = []
57 anchor_settings = []
58
58
59 for md_file in md_files:
59 for md_file in md_files:
60− with open(md_file, "r") as f:
60+ with open(md_file, "r") as f:
61 lines = f.readlines()
61 lines = f.readlines()
62 setting = setting_from_frontmatter(lines)
62 setting = setting_from_frontmatter(lines)
63 anchor_settings.append((setting, md_file))
63 anchor_settings.append((setting, md_file))
@@ -81,7 +81,7 @@ def read_anchor_file() -> List[Tuple[str, str]]:
81 with open(ANCHOR_FILE, "r") as f:
81 with open(ANCHOR_FILE, "r") as f:
82 for line in f:
82 for line in f:
83 line = line.replace('\n', '')
83 line = line.replace('\n', '')
84− setting, file = line.split(' ')
84+ setting, file = line.split(' ')
85 anchor_settings.append((setting, file))
85 anchor_settings.append((setting, file))
86
86
87 return anchor_settings
87 return anchor_settings
@@ -90,19 +90,22 @@ def read_anchor_file() -> List[Tuple[str, str]]:
90def write_entry(setting: str, file: str):
90def write_entry(setting: str, file: str):
91 validate_entry(setting, file)
91 validate_entry(setting, file)
92
92
93+
93 # not optimized
94 # not optimized
94 with open(file, "r+") as f:
95 with open(file, "r+") as f:
95 lines = f.readlines()
96 lines = f.readlines()
96− # lines = list(filter(bool, text.split('\n'))) # split text by lines,
97+ # lines = list(filter(bool, text.split('\n'))) # split text by lines,
97− fm_end = nth_occurrence(lines, "+++\n", 2) # get index of end of front matter (signaled by second '+++')
98+ fm_end = nth_occurrence(lines, "+++\n", 2) # get index of end of front matter (signaled by second '+++')
98−
99+
99 # remove existing setting if it exists
100 # remove existing setting if it exists
100 for line in lines[:fm_end]:
101 for line in lines[:fm_end]:
101 if line.startswith(ANCHOR_KEY):
102 if line.startswith(ANCHOR_KEY):
102 lines.remove(line)
103 lines.remove(line)
103 fm_end -= 1 # correct the index for the removal
104 fm_end -= 1 # correct the index for the removal
104
105
106+ if setting != "none":
107+ # by default, there is no need to specify "none"
105− lines.insert(fm_end, ANCHOR_KEY + f'"{setting}"' + '\n') # insert the setting right before the end of front matter
108+ lines.insert(fm_end, ANCHOR_KEY + f'"{setting}"' + '\n') # insert the setting right before the end of front matter
106 f.seek(0) # go to beginning of file
109 f.seek(0) # go to beginning of file
107 f.writelines(lines) # overwrite with new contents
110 f.writelines(lines) # overwrite with new contents
108 f.truncate() # idk why but necessary for bug-free behviour (otherwise it adds excess newlines and stuff)
111 f.truncate() # idk why but necessary for bug-free behviour (otherwise it adds excess newlines and stuff)