@@ -253,6 +253,36 @@ def get_language_specific_dir(language, dir):
253
253
dir = os .path .join (language , dir )
254
254
return dir
255
255
256
+ def update_file_links_with_lang (filename , pattern , language ):
257
+ # Open the file and read its content
258
+ with open (filename , 'r' ) as f :
259
+ content = f .read ()
260
+
261
+ def is_valid_path_and_lang (path , language ):
262
+ # Normalize the path to ensure it doesn't end with a slash
263
+ normalized_path = path .rstrip ('/' ).lstrip ('/' )
264
+ if os .path .exists (os .path .join (language , normalized_path + ".md" )) or os .path .exists (os .path .join (language , normalized_path )):
265
+ return True
266
+ return False
267
+
268
+
269
+ # Use regex to find all patterns and update them if valid
270
+ def replacement (match ):
271
+ path = match .group (0 )
272
+ # Check if the path exists and has the specified language
273
+ if is_valid_path_and_lang (path , language ):
274
+ return '/{}/{}' .format (language , path .lstrip ('/' ))
275
+ else :
276
+ return path
277
+
278
+ # Compile the pattern and substitute
279
+ compiled_pattern = re .compile (pattern )
280
+ updated_content = compiled_pattern .sub (replacement , content )
281
+
282
+ # Optionally, write the updated content back to the file or return it
283
+ with open (filename , 'w' ) as f :
284
+ f .write (updated_content )
285
+
256
286
257
287
def process_docs (download = False ):
258
288
if download :
@@ -307,7 +337,8 @@ def process_docs(download = False):
307
337
replace_in_file (filename , r"title\:" , r"language: {}\ntitle:" .format (language ))
308
338
replace_in_file (filename , r"title\:" , r"github: {}\ntitle:" .format ("https://github.com/defold/doc" ))
309
339
if language != "en" :
310
- replace_in_file (filename , r"\/manuals\/" , r"/{}/manuals/" .format (language ))
340
+ # replace_in_file(filename, r"\/manuals\/", r"/{}/manuals/".format(language))
341
+ update_file_links_with_lang (filename , r'/manuals/[^)#]+' , language )
311
342
replace_in_file (filename , r"\.\.\/images\/" , r"/manuals/images/" .format (language ))
312
343
replace_in_file (filename , r"\.\.\/assets\/" , r"/manuals/assets/" .format (language ))
313
344
@@ -321,7 +352,8 @@ def process_docs(download = False):
321
352
replace_in_file (filename , r"title\:" , r"language: {}\ntitle:" .format (language ))
322
353
replace_in_file (filename , r"title\:" , r"layout: faq\ntitle:" )
323
354
if language != "en" :
324
- replace_in_file (filename , r"\/manuals\/" , r"/{}/manuals/" .format (language ))
355
+ # replace_in_file(filename, r"\/manuals\/", r"/{}/manuals/".format(language))
356
+ update_file_links_with_lang (filename , r'\/manuals\/[^)#]+' , language )
325
357
replace_in_file (filename , r"\.\.\/images\/" , r"/manuals/images/" .format (language ))
326
358
replace_in_file (filename , r"\.\.\/assets\/" , r"/manuals/assets/" .format (language ))
327
359
0 commit comments