1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os 18import re 19import sys 20import markdown 21 22 23def get_chapters(source_dir, target, build_dir): 24 names = [] 25 with open(os.path.join(source_dir, target, "index.rst")) as ind: 26 for line in ind: 27 line = line.strip() 28 if line.startswith("/"): 29 names.append(line[1:]) 30 31 result = [] 32 for name in names: 33 result.append(os.path.join(build_dir, f"{target}-md", f'{name}.md')) 34 35 return result 36 37 38def process_chapter_file(chapter_file, dest_file): 39 for line in chapter_file: 40 # Hacks to mitigate Markdown builder bugs 41 line = line.replace("`a | b`", "`a \| b`") 42 line = line.replace("`a || b`", "`a \|\| b`") 43 44 # Hacks to avoid recipes subheaders 45 if (line.startswith("### Rule")): 46 line = '%s**\n' % line.strip().replace("### Rule", "**Rule") 47 if (line == "### TypeScript\n"): 48 line = "**TypeScript**\n" 49 if (line == "### ArkTS\n"): 50 line = "**ArkTS**\n" 51 if (line == "### See also\n"): 52 line = "**See also**\n" 53 54 # A hack to cut off hyperlinks 55 if (line.startswith("<a id=")): 56 next(chapter_file) 57 continue 58 59 # A hack to cut off any comments 60 if (line.startswith("<!--")): 61 while (not line.endswith("-->\n")): 62 line = next(chapter_file) 63 continue 64 65 # A hack to make recipe links a simple text 66 line = re.sub("\[(.+)\]\(#r[0-9]+\)", "\\1", line) 67 68 # Fix links to recipes 69 line = line.replace("(recipes.md)", "(#recipes)") 70 71 # Fix link to quick-start dir 72 line = line.replace( 73 "https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/", 74 "../") 75 76 dest_file.write(line) 77 78 79def merge_chapters(chapters, dest_path): 80 with os.fdopen(os.open(dest_path, os.O_WRONLY | os.O_CREAT, 0o755), "w", encoding="utf-8") as dest_file: 81 for chapter in chapters: 82 with os.fdopen(os.open(chapter, os.O_RDONLY, 0o755), "r", encoding="utf-8") as chapter_file: 83 process_chapter_file(chapter_file, dest_file) 84 85 dest_file.write("\n") 86 87 88def main(): 89 if len(sys.argv) != 4: 90 sys.exit("Usage:\n" + sys.argv[0] + "source_dir target build_dir") 91 92 source_dir = sys.argv[1] 93 target = sys.argv[2] 94 build_dir = sys.argv[3] 95 96 chapters = get_chapters(source_dir, target, build_dir) 97 98 dest_file = os.path.join(build_dir, target + ".md") 99 merge_chapters(chapters, dest_file) 100 101 102if __name__ == "__main__": 103 main() 104