1. Add build script for Java.

2. Add height limitation for code blocks in extra.css.
3. Fix "节点" to "结点".
This commit is contained in:
krahets
2023-02-07 04:43:52 +08:00
parent b14568151c
commit ecbf2d1560
54 changed files with 457 additions and 1633 deletions

View File

@ -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)

View File

@ -0,0 +1,145 @@
"""
File: extract_code_python.py
Created Time: 2023-02-07
Author: Krahets (krahets@163.com)
"""
import re
import os
import os.path as osp
class ExtractCodeBlocksJava:
def __init__(self) -> None:
self.langs = ["java"]
# Pattern to match function names and class names
self.func_pattern = r'(\s+)(public|private|)\s*(static|)\s*(\S+)\s+(\w+)(\(.*\))\s+\{'
self.class_pattern = r'(public|)\s*class\s+(\w+)\s*\{'
# Pattern to match the start and end of a block
self.block_end_pattern = '^\s{ind}\}'
self.block_start_pattern = '^\s{ind}\/\*.+\*\/'
def extract(self, file_path):
self.file_path = file_path
with open(file_path) as f:
self.lines = f.readlines()
self.content = "".join(self.lines)
# Detect and extract all the classes along with its fucntions
classes = self.extract_class_blocks()
# Remove 'static'
self.post_process(classes)
return {
"classes": classes
}
def post_process(self, classes):
for clas in classes.values():
funcs = clas["funcs"]
for func in funcs.values():
for i, line in enumerate(func["block"]):
if "static " in line:
func["block"][i] = line.replace("static ", "")
break
def search_block(self, header_line, indentation):
"""
Search class/function block given the header_line and indentation
"""
start_line, end_line = 0, len(self.lines)
block_end_pattern = re.compile(self.block_end_pattern.replace("ind", str(indentation)))
block_start_pattern = re.compile(self.block_start_pattern.replace("ind", str(indentation)))
# Search the code
for i in range(header_line + 1, len(self.lines)):
if re.match(block_end_pattern, self.lines[i]) is not None:
end_line = i
break
# Search the header comment
for i in range(header_line - 1, -1, -1):
if re.search(block_start_pattern, self.lines[i]) is not None:
start_line = i
break
code_block = self.lines[start_line:end_line + 1]
# Remove empty lines at bottom
for i in range(len(code_block) - 1, -1, -1):
if re.search("^\s*\n", code_block[i]) is None:
break
end_line -= 1
return start_line, end_line, self.lines[start_line:end_line + 1]
def extract_function_blocks(self, indentation=4, start_line=-1, end_line=-1):
"""
Extract all the functions with given indentation
"""
funcs = {}
if start_line == -1:
start_line = 0
if end_line == -1:
end_line = len(self.lines) - 1
func_pattern = re.compile(self.func_pattern)
for line_num in range(start_line, end_line + 1):
# Search the function header
func_match = func_pattern.match(self.lines[line_num])
if func_match is None: continue
# The function should match the input indentation
if len(func_match.group(1)) != indentation: continue
header_line = line_num
# Search the block from the header line
start_line, end_line, func_block = self.search_block(header_line, indentation)
# Construct the funcs dict
func_label = func_match.group(5)
funcs[func_label] = {
"indentation": indentation,
"line_number": {
"start": start_line,
"end": end_line,
"header": header_line,
},
"block": func_block,
}
return funcs
def extract_class_blocks(self):
"""
Extract all the classes with given indentation
"""
classes = {}
class_pattern = re.compile(self.class_pattern)
for line_num, line in enumerate(self.lines):
# Search the class header
class_match = class_pattern.match(line)
if class_match is None: continue
header_line = line_num
# Search the block from the header line
start_line, end_line, class_block = self.search_block(header_line, 0)
# Construct the classes dict
class_label = class_match.group(2)
classes[class_label] = {
"indentation": 0,
"line_number": {
"start": start_line,
"end": end_line,
"header": header_line,
},
"block": class_block,
"funcs": self.extract_function_blocks(
indentation=4, start_line=start_line, end_line=end_line)
}
return classes
# ext = ExtractCodeBlocksJava()
# ext.extract("codes/java/chapter_array_and_linkedlist/array.java")
# ext.extract("codes/java/chapter_array_and_linkedlist/my_list.java")

View File

@ -1,44 +1,55 @@
"""
File: extract_code_python.py
Created Time: 2023-02-06
Created Time: 2023-02-07
Author: Krahets (krahets@163.com)
"""
import re
import os
import os.path as osp
import glob
class ExtractCodeBlocksPython:
def __init__(self, file_path) -> None:
def __init__(self) -> None:
self.langs = ["python"]
# Pattern to match function names and class names
self.func_pattern = r'(\s*)def\s+(\w+)\s*\('
self.class_pattern = r'class\s+(\w+)'
# Pattern to match the start and end of a block
self.block_end_pattern = '^\s{0,ind}\S+.*\n'
self.block_start_pattern = '^\s{ind}""".+'
def extract(self, file_path):
self.file_path = file_path
with open(file_path) as f:
self.lines = f.readlines()
self.content = "".join(self.lines)
# Regular expression pattern to match function names and class names
self.func_pattern = re.compile(r'(\s*)def\s+(\w+)\s*\(')
self.class_pattern = re.compile(r'class\s+(\w+)')
# Detect and extract all the classes and fucntions
self.classes = self.extract_class_blocks()
self.functions = self.extract_function_blocks()
classes = self.extract_class_blocks()
funcs = self.extract_function_blocks()
return {
"classes": classes,
"funcs": funcs,
}
def search_block(self, header_line, indentation):
"""
Search class/function block given the header_line and indentation
"""
start_line, end_line = 0, len(self.lines)
block_end_pattern = re.compile(self.block_end_pattern.replace("ind", str(indentation)))
block_start_pattern = re.compile(self.block_start_pattern.replace("ind", str(indentation)))
# Search the code
for i in range(header_line + 1, len(self.lines)):
if re.search("^\s*\n|^\s{ind}\s+.+\n".replace("ind", str(indentation)),
self.lines[i]) is None:
if re.match(block_end_pattern, self.lines[i]) is not None:
end_line = i
break
# Search the header comment
for i in range(header_line - 1, -1, -1):
if re.search('^\s{ind}""".+'.replace("ind", str(indentation)),
self.lines[i]) is not None:
if re.search(block_start_pattern, self.lines[i]) is not None:
start_line = i
break
func_block = self.lines[start_line:end_line]
@ -55,16 +66,18 @@ class ExtractCodeBlocksPython:
"""
Extract all the functions with given indentation
"""
functions = {}
funcs = {}
if start_line == -1:
start_line = 0
if end_line == -1:
end_line = len(self.lines) - 1
func_pattern = re.compile(self.func_pattern)
for line_num in range(start_line, end_line + 1):
# Search the function header
func_match = self.func_pattern.match(self.lines[line_num])
func_match = func_pattern.match(self.lines[line_num])
if func_match is None: continue
# The function should match the input indentation
if len(func_match.group(1)) != indentation: continue
@ -72,9 +85,9 @@ class ExtractCodeBlocksPython:
# Search the block from the header line
start_line, end_line, func_block = self.search_block(header_line, indentation)
# Construct the functions dict
# Construct the funcs dict
func_label = func_match.group(2)
functions[func_label] = {
funcs[func_label] = {
"indentation": indentation,
"line_number": {
"start": start_line,
@ -84,7 +97,7 @@ class ExtractCodeBlocksPython:
"block": func_block,
}
return functions
return funcs
def extract_class_blocks(self):
"""
@ -92,9 +105,11 @@ class ExtractCodeBlocksPython:
"""
classes = {}
class_pattern = re.compile(self.class_pattern)
for line_num, line in enumerate(self.lines):
# Search the class header
class_match = self.class_pattern.match(line)
class_match = class_pattern.match(line)
if class_match is None: continue
header_line = line_num
@ -110,7 +125,7 @@ class ExtractCodeBlocksPython:
"header": header_line,
},
"block": class_block,
"functions": self.extract_function_blocks(
"funcs": self.extract_function_blocks(
indentation=4, start_line=start_line, end_line=end_line)
}