Skip to content

Commit 110dd18

Browse files
authored
Merge pull request #108 from defold/exist_links
Use language specifix links only if such file exist
2 parents f61361d + f33079a commit 110dd18

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

update.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,36 @@ def get_language_specific_dir(language, dir):
253253
dir = os.path.join(language, dir)
254254
return dir
255255

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+
256286

257287
def process_docs(download = False):
258288
if download:
@@ -307,7 +337,8 @@ def process_docs(download = False):
307337
replace_in_file(filename, r"title\:", r"language: {}\ntitle:".format(language))
308338
replace_in_file(filename, r"title\:", r"github: {}\ntitle:".format("https://github.com/defold/doc"))
309339
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)
311342
replace_in_file(filename, r"\.\.\/images\/", r"/manuals/images/".format(language))
312343
replace_in_file(filename, r"\.\.\/assets\/", r"/manuals/assets/".format(language))
313344

@@ -321,7 +352,8 @@ def process_docs(download = False):
321352
replace_in_file(filename, r"title\:", r"language: {}\ntitle:".format(language))
322353
replace_in_file(filename, r"title\:", r"layout: faq\ntitle:")
323354
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)
325357
replace_in_file(filename, r"\.\.\/images\/", r"/manuals/images/".format(language))
326358
replace_in_file(filename, r"\.\.\/assets\/", r"/manuals/assets/".format(language))
327359

0 commit comments

Comments
 (0)