@@ -81,7 +81,6 @@ def update_lang_footer(args, filename, content, lang_tags):
8181 FOOTER_COMMENTS) with a list of links based on the legalcode files
8282 currently present.
8383 """
84- print (f"{ filename } : inserting language footer links" )
8584 current_language = lang_tags_from_filenames (filename )[0 ]
8685 footer = ""
8786 for lang_tag in lang_tags :
@@ -109,6 +108,13 @@ def update_lang_footer(args, filename, content, lang_tags):
109108 # Use ASCII period
110109 period = "."
111110 replacement = f"{ start } \n { footer } { period } \n { end } "
111+ if target == replacement :
112+ print (
113+ f"{ filename } : Skipping unneeded insertion of language footer"
114+ " links"
115+ )
116+ else :
117+ print (f"{ filename } : Inserting language footer links" )
112118 if args .debug :
113119 new_content = content .replace (target , replacement , 1 )
114120 diff_changes (filename , content , new_content )
@@ -131,7 +137,10 @@ def insert_missing_lang_footer_comments(args, filename, content):
131137 present.
132138 """
133139 if has_footer_comments (content ):
134- print (f"{ filename } : language footer comments present: skipping insert" )
140+ print (
141+ f"{ filename } : Skipping unneeded language footer comments"
142+ "insertion"
143+ )
135144 return content
136145 print (f"{ filename } : inserting language footer HTML comments" )
137146 re_pattern = re .compile (
@@ -185,7 +194,8 @@ def normalize_faq_translation_link(args, filename, content):
185194 """
186195 if has_correct_faq_officialtranslations (content ):
187196 print (
188- f"{ filename } : correct translation FAQ link: skipping normalization"
197+ f"{ filename } : Skipping unneeded translation FAQ link"
198+ " normalization"
189199 )
190200 return content
191201 print (f"{ filename } : normalizing translation FAQ link" )
@@ -221,13 +231,78 @@ def normalize_faq_translation_link(args, filename, content):
221231 return content .replace (target , replacement , 1 )
222232
223233
234+ def has_correct_languages_anchor (content ):
235+ """Determine if the link to the tranlsation FAQ is correct.
236+ """
237+ if content .find ('id="languages"' ) == - 1 :
238+ return False
239+ return True
240+
241+
242+ def normalize_languages_anchor (args , filename , content ):
243+ """Replace various incorrect translation FAQ links with the correct link
244+ (FAQ_TRANSLATION_LINK).
245+ """
246+ if has_correct_languages_anchor (content ):
247+ print (
248+ f"{ filename } : Skipping unneeded language anchor normalization"
249+ )
250+ return content
251+ print (f"{ filename } : normalizing language anchor id" )
252+ re_pattern = re .compile ("name=['\" ]languages['\" ]" , re .IGNORECASE )
253+ matches = re_pattern .search (content )
254+ if matches is None :
255+ print (
256+ f"{ filename } : ERROR: languages anchor not matched. Aborting"
257+ " processing"
258+ )
259+ return
260+ target = matches .group ()
261+ replacement = 'id="languages"'
262+ if args .debug :
263+ new_content = content .replace (target , replacement , 1 )
264+ diff_changes (filename , content , new_content )
265+ return new_content
266+ else :
267+ return content .replace (target , replacement , 1 )
268+
269+
270+ def normalize_line_endings (args , filename , content ):
271+ """Normalize line endings to unix (\\ n)
272+ """
273+ re_pattern = re .compile ("\r (?!\n )" )
274+ matches = re_pattern .findall (content )
275+ message = ""
276+ if matches :
277+ message = f" { len (matches )} mac newlines (CR)"
278+ re_pattern = re .compile ("\r \n " )
279+ matches = re_pattern .findall (content )
280+ if matches :
281+ if message :
282+ message = f"{ message } and"
283+ message = f"{ message } { len (matches )} windows newlines (CRLF)"
284+ if message :
285+ print (f"{ filename } : Converting{ message } to unix newlines (LF)" )
286+ return "\n " .join (content .split ("\r \n " ))
287+ else :
288+ print (f"{ filename } : Skipping unneeded newline conversion" )
289+ return content
290+
291+
224292def process_file_contents (args , file_list , lang_tags ):
225293 """Process each of the CC0 legalcode files and update them, as necessary.
226294 """
227295 for filename in file_list :
228- with open (filename , "r" , encoding = "utf-8" ) as file_in :
296+ with open (filename , "r" , encoding = "utf-8" , newline = "" ) as file_in :
229297 content = file_in .read ()
230- new_content = normalize_faq_translation_link (args , filename , content )
298+ new_content = content
299+ new_content = normalize_line_endings (args , filename , new_content )
300+ new_content = normalize_languages_anchor (args , filename , new_content )
301+ if new_content is None :
302+ sys .exit (1 )
303+ new_content = normalize_faq_translation_link (
304+ args , filename , new_content
305+ )
231306 if new_content is None :
232307 sys .exit (1 )
233308 new_content = insert_missing_lang_footer_comments (
@@ -241,9 +316,11 @@ def process_file_contents(args, file_list, lang_tags):
241316 if new_content is None :
242317 sys .exit (1 )
243318 if content == new_content :
244- print (f"{ filename } : No changes: skipping writing back to file" )
319+ print (
320+ f"{ filename } : Skipping writing back to file (no changes)"
321+ )
245322 elif args .debug :
246- print (f"{ filename } : DEBUG: skipping writing changes to file" )
323+ print (f"{ filename } : DEBUG: Skipping writing changes to file" )
247324 else :
248325 print (f"{ filename } : Writing changes to file" )
249326 with open (filename , "w" , encoding = "utf-8" ) as file_out :
0 commit comments