@@ -62,18 +62,47 @@ def check_if_concept_in_other_concept(concepts_list):
6262 skip_concept = True
6363 print (f"Concept \" { i } \" is contained in \n { j } \n " )
6464 return skip_concept
65-
65+ # Check if occurrence is in code block, frontmatter, or monospace
66+ def check_if_skip_line (self , current_line , skip_line ):
67+ # Check if in frontmatter
68+
69+ if current_line == '---\n ' and skip_line == False :
70+ skip_line = True
71+ return skip_line
72+
73+ if current_line == '---\n ' and skip_line == True :
74+ skip_line = False
75+ return skip_line
76+
77+
78+ # Check if in code block
79+ if current_line == '```\n ' and skip_line == False :
80+ skip_line = True
81+ return skip_line
82+
83+ if current_line == '```\n ' and skip_line == True :
84+ skip_line = False
85+ return skip_line
86+
87+ else :
88+ return skip_line
89+
90+ # TODO Check if in inline code
91+
6692
6793 # WORKING - Version with line-by-line processing
6894 def line_by_line_replace (self , current_file , old_string , new_string ):
6995 with open (current_file , "r" ) as file_to_process :
7096 lines_of_file = file_to_process .readlines ()
97+ skip_line_toggle = False
7198 # iterates over each line of the file
7299 for i in range (len (lines_of_file )):
73- if old_string in lines_of_file [i ]:
74- # replace concept once in the line
75- lines_of_file [i ] = lines_of_file [i ].replace (old_string , new_string , 1 )
76- break
100+ skip_line = self .check_if_skip_line (lines_of_file [i ], skip_line_toggle )
101+ skip_line_toggle = skip_line
102+ if old_string in lines_of_file [i ] and skip_line == False :
103+ # replace concept once in the line
104+ lines_of_file [i ] = lines_of_file [i ].replace (old_string , new_string , 1 )
105+ break
77106 with open (current_file , "w" ) as file_to_write :
78107 file_to_write .writelines (lines_of_file )
79108 return
@@ -86,13 +115,13 @@ def replace(self):
86115 # Looks for each concept in each page
87116 for file in files_list :
88117 for concept in concepts_list :
89- skip_concept = False
90118 old_string = concept [0 ]
91119 new_string = f"[{ concept [0 ]} ]({ concept [1 ]} )"
92120 current_file = file
93121 # Check if concept already has link to concepts page
94122 with open (file ) as file_to_check :
95- if new_string not in file_to_check .read () and skip_concept == False :
123+ if new_string not in file_to_check .read ():
124+ # Replace first occurrence of concept
96125 self .line_by_line_replace (current_file , old_string , new_string )
97126 # Add test new content and error handling before printing line below
98127 print (f"{ old_string } replaced by { new_string } in file { file } ." )
@@ -101,7 +130,6 @@ def replace(self):
101130 file_to_check .close ()
102131 return
103132
104-
105133# TODO
106134
107135# - DONE - Check if concept is already present in file before replacing
@@ -112,6 +140,8 @@ def replace(self):
112140
113141# - Make sure capitalized concepts are properly matched and reproduced
114142
143+ # - Address case when concept is in plural form (e.g. [job](link)s ). concept must have space or punctuation after (or no letter).
144+
115145# - Try a different replace method using readlines(), then looping through each line to search for concept and easily exclude frontmatter
116146
117147# - Frontmatter:
0 commit comments