on master help sign in
Fix `anchors.py` functionality bug

- basically, `insert_anchor_links` is only applicable on a section, but I didn't RTFM hard enough. (and there was a really cache glitch?? which made it take 5x longer to figure this out)

commitc73d6953203b910f923906c67bca5b6de0bc404c authorYaseen <yaseen.nvt@gmail.com> dateThu 21 Jan 2021, 20:19 (6 years ago) parentd41e038
1 file changed+11−8
anchors.py+11−8
anchors.py+11−8
@@ -34,8 +34,8 @@ def validate_entry(setting: str, file: str):
3434
3535
3636def find_all_mds() -> List[str]:
37− # **/_index.md for all index files
38− return [str(p) for p in Path('./').rglob('*.md')] # only applicable on `_index.md` files (i.e. sections)
37+ # the `insert_anchor_links` is only applicable on `_index.md` files (i.e. sections)
38+ return [str(p) for p in Path('./').rglob('**/_index.md')]
3939
4040def setting_from_frontmatter(lines: List[str]) -> str:
4141 # 'insert_anchor_links = "none"' -> 'none'
@@ -57,7 +57,7 @@ def anchors_from_frontmatter(md_files: List[str]) -> List[Tuple[str, str]]:
5757 anchor_settings = []
5858
5959 for md_file in md_files:
60− with open(md_file, "r") as f:
60+ with open(md_file, "r") as f:
6161 lines = f.readlines()
6262 setting = setting_from_frontmatter(lines)
6363 anchor_settings.append((setting, md_file))
@@ -81,7 +81,7 @@ def read_anchor_file() -> List[Tuple[str, str]]:
8181 with open(ANCHOR_FILE, "r") as f:
8282 for line in f:
8383 line = line.replace('\n', '')
84− setting, file = line.split(' ')
84+ setting, file = line.split(' ')
8585 anchor_settings.append((setting, file))
8686
8787 return anchor_settings
@@ -90,19 +90,22 @@ def read_anchor_file() -> List[Tuple[str, str]]:
9090def write_entry(setting: str, file: str):
9191 validate_entry(setting, file)
9292
93+
9394 # not optimized
9495 with open(file, "r+") as f:
9596 lines = f.readlines()
96− # 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−
97+ # lines = list(filter(bool, text.split('\n'))) # split text by lines,
98+ fm_end = nth_occurrence(lines, "+++\n", 2) # get index of end of front matter (signaled by second '+++')
99+
99100 # remove existing setting if it exists
100101 for line in lines[:fm_end]:
101102 if line.startswith(ANCHOR_KEY):
102103 lines.remove(line)
103104 fm_end -= 1 # correct the index for the removal
104105
105− lines.insert(fm_end, ANCHOR_KEY + f'"{setting}"' + '\n') # insert the setting right before the end of front matter
106+ if setting != "none":
107+ # by default, there is no need to specify "none"
108+ lines.insert(fm_end, ANCHOR_KEY + f'"{setting}"' + '\n') # insert the setting right before the end of front matter
106109 f.seek(0) # go to beginning of file
107110 f.writelines(lines) # overwrite with new contents
108111 f.truncate() # idk why but necessary for bug-free behviour (otherwise it adds excess newlines and stuff)