""" For preparing the mkdocs-generated seleniumbase.io website. """ import codecs import os import re from pathlib import Path GITHUB_URL = r"https://github.com/seleniumbase/SeleniumBase/blob/master/" ROOT_DIR = Path(__file__).parents[1] URL_PATTERN = re.compile( r"(?:\(|{}[\w/.]+\.md)(?:\)|\")".format(GITHUB_URL) ) MD_PATH_PATTERN = re.compile(r"\[.*\]\((?P[\w\\._/]+\.md)\)") HEADER_PATTERN = re.compile( r"^(?P#+)\s*(<[\w\s=\":/.]+>)?\s*\**(?P
.*[\w`]):?\**\s*$", flags=re.MULTILINE, ) PROCESSED_PATHS = set() def normalize_path(path): path = Path(path).absolute().relative_to(ROOT_DIR) return str(path).replace("\\", "/") def read_file(file_name): path = ROOT_DIR / file_name with path.open() as file_handle: content = file_handle.read() return content def process_file(file_name): content = read_file(file_name) urls = URL_PATTERN.findall(content) # content = content.replace("
", " \n") content = re.sub(HEADER_PATTERN, r"\g \g
", content) directory = "/".join(normalize_path(file_name).split("/")[:-1]) paths = set() md_paths = MD_PATH_PATTERN.findall(content) for md_path in md_paths: path = md_path.lstrip("/") if (ROOT_DIR / directory / path).exists(): path = ROOT_DIR / directory / path else: path = ROOT_DIR / path path = path.resolve().relative_to(ROOT_DIR) paths.add(normalize_path(path)) content = content.replace("(" + md_path + ")", normalize_path(path)) for url in urls: path = url[len(GITHUB_URL) :] # noqa: E203 paths.add(path) content = content.replace( url, normalize_path(os.path.relpath(path, directory)) ) output_path = ROOT_DIR / "mkdocs_build" / file_name if not output_path.parent.is_dir(): os.makedirs(output_path.parent) with output_path.open("w+") as output_file: output_file.write(content) PROCESSED_PATHS.add(normalize_path(file_name)) for path in paths: if path not in PROCESSED_PATHS: process_file(normalize_path(path)) def main(*args, **kwargs): files_to_process = ["README.md"] scanned_dir_list = [] scanned_dir_list.append("help_docs") scanned_dir_list.append("examples") scanned_dir_list.append("examples/behave_bdd") scanned_dir_list.append("examples/example_logs") scanned_dir_list.append("examples/presenter") scanned_dir_list.append("examples/chart_maker") scanned_dir_list.append("examples/tour_examples") scanned_dir_list.append("examples/visual_testing") scanned_dir_list.append("integrations/google_cloud") scanned_dir_list.append("seleniumbase/console_scripts") for scanned_dir in scanned_dir_list: for dir_ in os.listdir(ROOT_DIR / scanned_dir): files_to_process.append(os.path.join(scanned_dir, dir_)) video_embed = ( '
' '
" ) updated_files_to_process = [] for file_ in files_to_process: if file_.endswith(".md"): updated_files_to_process.append(file_) for file_ in updated_files_to_process: process_file(file_) for file_ in updated_files_to_process: readme_file = "./mkdocs_build/" + file_ with open(readme_file, "r", encoding="utf-8") as f: all_code = f.read() code_lines = all_code.split("\n") changed = False seleniumbase_lines = [] for line in code_lines: if ' href="' in line and '.md"' in line: changed = True line = line.replace('.md"', '/"') if "" in line: changed = True new_lines = [] new_lines.append("---") new_lines.append("hide:") new_lines.append(" - toc") new_lines.append("---") for line in new_lines: seleniumbase_lines.append(line) continue if "" in line: changed = True line = ( r'

' r'' r'SeleniumBase on GitHub' r"

" ) alt_link_badge = ( '' 'SeleniumBase Docs' ) back_to_gh = ( r'' r'SeleniumBase on GitHub' r"" ) if alt_link_badge in line: line = line.replace(alt_link_badge, back_to_gh) if "" in line: changed = True continue if "" in line and "watch?v=" in line: start_pt = line.find("watch?v=") + len("watch?v=") end_pt = line.find('"', start_pt + 1) yt_code = line[start_pt:end_pt] changed = True line = video_embed.replace("yt_code", yt_code) if "" in line: changed = True line = ( '
' "

✅ Reliable Browser Testing

" "
" ) if "" in line: changed = True line = ( '

SeleniumBase Docs ' '

' ) seleniumbase_lines.append(line) if changed: out_file = codecs.open(readme_file, "w+", encoding="utf-8") out_file.writelines("\r\n".join(seleniumbase_lines)) out_file.close()