mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 18:55:36 +08:00
1. Add build script for Java.
2. Add height limitation for code blocks in extra.css. 3. Fix "节点" to "结点".
This commit is contained in:
@ -11,6 +11,8 @@ import re
|
||||
import glob
|
||||
import shutil
|
||||
from docs.utils.extract_code_python import ExtractCodeBlocksPython
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
def build_markdown(md_path):
|
||||
with open(md_path, "r") as f:
|
||||
@ -19,6 +21,7 @@ def build_markdown(md_path):
|
||||
code_blocks_dict = {}
|
||||
file_pattern = re.compile(r'\s*```(\w+)\s+title="(.+)"')
|
||||
src_pattern = re.compile(r'\s*\[class\]\{(.*?)\}-\[func\]\{(.*?)\}')
|
||||
|
||||
for i in range(len(lines)):
|
||||
# Find the line target to the source codes
|
||||
src_match = src_pattern.match(lines[i])
|
||||
@ -28,48 +31,43 @@ def build_markdown(md_path):
|
||||
file_match = file_pattern.match(lines[j])
|
||||
if file_match is not None:
|
||||
break
|
||||
|
||||
# Get code blocks
|
||||
# Get the coresponding language code extractor
|
||||
lang = file_match[1]
|
||||
file_name = file_match[2]
|
||||
extractor = extractor_dict[lang]
|
||||
# Get code blocks
|
||||
if file_name not in code_blocks_dict:
|
||||
code_blocks_dict[file_name] = ExtractCodeBlocksPython(
|
||||
code_blocks_dict[file_name] = extractor.extract(
|
||||
file_path=osp.dirname(md_path).replace("docs/", f"codes/{lang}/") + f"/{file_name}")
|
||||
|
||||
header_line = i
|
||||
class_label = src_match[1]
|
||||
class_label = src_match[1]
|
||||
func_label = src_match[2]
|
||||
code_blocks = code_blocks_dict[file_name]
|
||||
src_info = {
|
||||
"line_number": i,
|
||||
"class_label": src_match[1],
|
||||
"func_label": src_match[2],
|
||||
"code_blocks": code_blocks_dict[file_name]
|
||||
}
|
||||
|
||||
# Add the class to the doc
|
||||
if not func_label and class_label:
|
||||
if class_label in code_blocks.classes:
|
||||
if class_label in code_blocks["classes"]:
|
||||
lines.pop(header_line)
|
||||
class_block = code_blocks.classes[class_label]["block"]
|
||||
class_block = code_blocks["classes"][class_label]["block"]
|
||||
for code_line in class_block[::-1]:
|
||||
ind = " " * 4 if code_line != "\n" else ""
|
||||
lines.insert(header_line, ind + code_line)
|
||||
# Add the function to the doc
|
||||
elif func_label and not class_label:
|
||||
if func_label in code_blocks.functions:
|
||||
if func_label in code_blocks["funcs"]:
|
||||
lines.pop(header_line)
|
||||
func_block = code_blocks.functions[func_label]
|
||||
func_block = code_blocks["funcs"][func_label]
|
||||
for code_line in func_block["block"][::-1]:
|
||||
ind = " " * 4 if code_line != "\n" else ""
|
||||
lines.insert(header_line, ind + code_line)
|
||||
# Add the class method to the doc
|
||||
elif func_label and class_label:
|
||||
if class_label in code_blocks.classes:
|
||||
class_dict = code_blocks.classes[class_label]
|
||||
if func_label in class_dict["functions"]:
|
||||
if class_label in code_blocks["classes"]:
|
||||
class_dict = code_blocks["classes"][class_label]
|
||||
if func_label in class_dict["funcs"]:
|
||||
lines.pop(header_line)
|
||||
func_block = class_dict["functions"][func_label]
|
||||
func_block = class_dict["funcs"][func_label]
|
||||
for code_line in func_block["block"][::-1]:
|
||||
lines.insert(header_line, code_line)
|
||||
|
||||
@ -78,10 +76,17 @@ def build_markdown(md_path):
|
||||
print(f"Built {md_path}")
|
||||
|
||||
|
||||
extractor_dict = {
|
||||
"java": ExtractCodeBlocksJava(),
|
||||
"python": ExtractCodeBlocksPython(),
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Copy files to the build dir
|
||||
shutil.copytree("docs", "build", dirs_exist_ok=True)
|
||||
shutil.rmtree("build/utils")
|
||||
|
||||
# Build docs
|
||||
for md_path in glob.glob("docs/chapter_*/*.md"):
|
||||
for md_path in glob.glob("docs/chapter_*/*.md"):
|
||||
build_markdown(md_path)
|
||||
|
Reference in New Issue
Block a user