""" * Version: 1.0.0.0.0 * Purposes: * Intenral Utilities for pingcap.pe.flash module, used for Markdown translation workflows. """ import re import pingcap.pe.flash.util as util import pingcap.pe.flash.util_advisor as advisor inline_code_count_suffix = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ] def is_skippable(line: str) -> bool: """Skip the translation if needed. Args: line (str): The source language line for evaluation. Returns: bool: True|False """ if ( re.match("^ *$", line) # Empty line or re.match( "^ *\|[\-\:\| ]+$", line ) # Markdown table header and row separator line or re.match("^ *-+$", line) # Markdown table line or re.match("^ *\++$", line) # Markdown table line or re.match("^ *\*+$", line) # Markdown table line or re.match("^ *=+$", line) # Markdown table line or re.match("^\[.*$", line) # Markdown link line or re.match("^ *\ str: payload = line.strip() mark = "_" + dest_lang_code + "_" if "_en_" in line and "_en_" != mark: return payload.replace("_en_", mark) elif "_zh_" in line and "_zh_" != mark: return payload.replace("_zh_", mark) elif "_ja_" in line and "ja_" != mark: return payload.replace("_zh_", mark) return payload def is_md_image(line: str) -> bool: if re.match("^ *\!\[.*$", line): return True else: return False def is_table_row(line: str) -> bool: """Is input string a Markdown line for a table? Args: line (str): The input Markdown line. Returns: bool: True|False """ payload = line.strip() if payload.startswith("|") and payload.endswith("|"): return True else: return False def is_ja_desumasu(line: str) -> bool: """Is Japanese desumasu sentence? Args: line (str): The input sentence line. Returns: bool: True|False """ payload = line.strip().strip("。").strip(":") if ( payload.endswith("ます") or payload.endswith("です") or payload.endswith("ましょう") or payload.endswith("でしょう") or payload.endswith("ません") or payload.endswith("しました") ): return True else: False def translate_table_row(line: str, cell_translator, en_1gram_list: list) -> str: """Translate Markdown table row cell by cell. Args: line (str): Mardown table row. cell_translator (_type_): Should be the same interface of util_lang2lang.translate_lang2lang_md_line(). en_1gram_list (list): The EN terms list. Returns: str: The translated Markdown table row. """ translated_table_row = ["| "] for cell in line.split("|"): if not len(cell) == 0: print(util.speak_result("Translating cell: " + cell)) translated_cell = ( cell_translator( cell, en_1gram_list, "standard", True, None, False, False ) .rstrip(".") .rstrip("。") ) translated_table_row += [translated_cell.strip() + " |"] print(util.speak_result("Translated cell: " + translated_cell)) result = "".join(translated_table_row) print(util.speak_result("Translated table row: " + result)) return result def pack_prefix_whitespace(line: str) -> dict: prefix_whitespace_pattern = re.findall("^ +", line) prefix_whitespace_count = len(prefix_whitespace_pattern) processed_line = line if prefix_whitespace_count > 0: processed_line = line.lstrip(prefix_whitespace_pattern[0]).strip() ret = { "count": prefix_whitespace_count, "payload": prefix_whitespace_pattern[0] if prefix_whitespace_count > 0 else "", "processed_line": processed_line, } print(util.speak_result("Pack prefix whitespace: <" + str(ret) + ">")) return ret def pack_prefix_plus(line: str) -> dict: prefix_plus_pattern = re.findall("^ *\++ ", line) prefix_plus_count = len(prefix_plus_pattern) if prefix_plus_count > 0: line = line.replace(prefix_plus_pattern[0], "").strip() ret = { "count": prefix_plus_count, "payload": prefix_plus_pattern[0] if prefix_plus_count > 0 else "", "processed_line": line, } print(util.speak_result("Pack prefix plus: <" + str(ret) + ">")) return ret def pack_prefix_minus(line: str) -> dict: prefix_minus_pattern = re.findall("^ *\-+ ", line) prefix_minus_count = len(prefix_minus_pattern) if prefix_minus_count > 0: line = line.replace(prefix_minus_pattern[0], "").strip() ret = { "count": prefix_minus_count, "payload": prefix_minus_pattern[0] if prefix_minus_count > 0 else "", "processed_line": line, } print(util.speak_result("Pack prefix minus: <" + str(ret) + ">")) return ret def pack_prefix_star(line: str) -> dict: prefix_star_pattern = re.findall("^ *\*+ ", line) prefix_star_count = len(prefix_star_pattern) if prefix_star_count > 0: line = line.replace(prefix_star_pattern[0], "").strip() ret = { "count": prefix_star_count, "payload": prefix_star_pattern[0] if prefix_star_count > 0 else "", "processed_line": line, } print(util.speak_result("Pack prefix star: <" + str(ret) + ">")) return ret def pack_prefix_number(line: str) -> dict: prefix_number_pattern = re.findall("^ *[0-9]+\. ", line) prefix_number_count = len(prefix_number_pattern) if prefix_number_count == 1: line = line.replace(prefix_number_pattern[0], "").strip() ret = { "count": prefix_number_count, "payload": prefix_number_pattern[0] if prefix_number_count > 0 else "", "processed_line": line, } print(util.speak_result("Pack prefix number: <" + str(ret) + ">")) return ret def pack_inline_codeline(line: str) -> dict: keep_inline_code_patterns = re.findall("`[^`]+`", line) inline_code_count = 0 for p in keep_inline_code_patterns: line = line.replace(p, "pd1x" + inline_code_count_suffix[inline_code_count]) inline_code_count += 1 ret = { "count": inline_code_count, "payload": keep_inline_code_patterns, "processed_line": line, } print(util.speak_result("Pack inline codeline: <" + str(ret) + ">")) return ret def pack_prefix_hash(line: str) -> dict: prefix_hash_title_pattern = re.findall("^ *#+ ", line) prefix_hash_title_count = len(prefix_hash_title_pattern) if prefix_hash_title_count > 0: line = line.replace(prefix_hash_title_pattern[0], "").strip() ret = { "count": prefix_hash_title_count, "payload": prefix_hash_title_pattern[0] if prefix_hash_title_count > 0 else "", "processed_line": line, } print(util.speak_result("Pack prefix hash: <" + str(ret) + ">")) return ret def unpack_prefix_common(line: str, package: dict) -> str: if package["count"] > 0: line = package["payload"] + line print(util.speak_result("Unpack prefix common: <" + line + ">")) return line def unpack_inline_codeline(line: str, package: dict) -> str: inline_code_count = 0 for p in package["payload"]: line = re.sub( "pd1x" + inline_code_count_suffix[inline_code_count], p, line, flags=re.IGNORECASE, ).strip() inline_code_count += 1 print(util.speak_result("Unpack inline codeline: <" + line + ">")) return line def unpack_prefix_hash( line: str, package: dict, en_1gram_list: list, md4ppt=True ) -> str: if package["count"] == 1: if len(package["payload"].strip()) < 3: line = package["payload"] + advisor.pretty_format_title( line, en_1gram_list, md4ppt ) else: line = package["payload"] + advisor.pretty_format_bulletin( line, en_1gram_list, md4ppt ) print(util.speak_result("Unpack prefix hash: <" + line + ">")) return line