• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2021-2023 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 markdown
18import os
19import re
20import sys
21
22def get_chapters(source_dir, target, build_dir):
23    names = []
24    with open(os.path.join(source_dir, target, "index.rst")) as ind:
25        for line in ind:
26            line = line.strip()
27            if line.startswith("/"):
28                names.append(line[1:])
29
30    result = []
31    for name in names:
32        result.append(os.path.join(build_dir, target + "-md", name + ".md"))
33
34    return result
35
36def merge_chapters_helper(dest_file, chapter):
37    with open(chapter, "r") as chapter_file:
38        for line in chapter_file:
39            # Hacks to mitigate Markdown builder bugs
40            line = line.replace("`a | b`", "`a \| b`")
41            line = line.replace("`a || b`", "`a \|\| b`")
42
43            # Hacks to avoid recipes subheaders
44            if (line.startswith("### Rule")):
45                line = line.strip().replace("### Rule", "**Rule") + "**\n"
46            if (line == "### TypeScript\n"):
47                line = "**TypeScript**\n"
48            if (line == "### ArkTS\n"):
49                line = "**ArkTS**\n"
50            if (line == "### See also\n"):
51                line = "**See also**\n"
52
53            # A hack to cut off hyperlinks
54            if (line.startswith("<a id=")):
55                next(chapter_file)
56                continue
57
58            # A hack to cut off keyword comments
59            if (line.startswith("<!--")):
60                next(chapter_file)
61                next(chapter_file)
62                continue
63
64            # A hack to make recipe links a simple text
65            line = re.sub("\[(.+)\]\(#r[0-9]+\)", "\\1", line)
66
67            # Fix links to recipes
68            line = line.replace("(recipes.md)", "(#recipes)")
69
70            # Fix link to quick-start dir
71            link = "https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/"
72            line = line.replace(link, "../")
73
74            dest_file.write(line)
75
76        dest_file.write("\n")
77
78
79def merge_chapters(chapters, dest_path):
80    with open(dest_path, "w") as dest_file:
81        for chapter in chapters:
82            merge_chapters_helper(dest_file, chapter)
83
84def main():
85    if len(sys.argv) != 4:
86        sys.exit("Usage:\n" + sys.argv[0] + "source_dir target build_dir")
87
88    source_dir = sys.argv[1]
89    target = sys.argv[2]
90    build_dir = sys.argv[3]
91
92    chapters = get_chapters(source_dir, target, build_dir)
93
94    dest_file = os.path.join(build_dir, target + ".md")
95    merge_chapters(chapters, dest_file)
96
97if __name__ == "__main__":
98    main()
99