parent
992d17c8ed
commit
8b5b00bab8
@ -1 +0,0 @@
|
||||
__pycache__
|
@ -1,123 +0,0 @@
|
||||
"""
|
||||
File: build_markdown_docs.py
|
||||
Created Time: 2023-02-06
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
|
||||
import re
|
||||
import glob
|
||||
import shutil
|
||||
from docs.utils.number_headings import number_headings
|
||||
from docs.utils.extract_code_python import ExtractCodeBlocksPython
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
from docs.utils.extract_code_cpp import ExtractCodeBlocksCpp
|
||||
from docs.utils.extract_code_jsts import ExtractCodeBlocksJSTS
|
||||
from docs.utils.extract_code_swift import ExtractCodeBlocksSwift
|
||||
from docs.utils.extract_code_csharp import ExtractCodeBlocksCSharp
|
||||
from docs.utils.extract_code_go import ExtractCodeBlocksGo
|
||||
from docs.utils.extract_code_zig import ExtractCodeBlocksZig
|
||||
|
||||
|
||||
def build_code_blocks(md_path):
|
||||
with open(md_path, "r") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
code_blocks_dict = {}
|
||||
file_pattern = re.compile(r'\s*```(\w+)\s+title="(.+)"')
|
||||
src_pattern = re.compile(r'\s*\[class\]\{(.*?)\}-\[func\]\{(.*?)\}')
|
||||
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
# Find the line target to the source codes
|
||||
src_match = src_pattern.match(lines[i])
|
||||
if src_match is None:
|
||||
i += 1
|
||||
continue
|
||||
for j in range(i - 1, -1 ,-1):
|
||||
file_match = file_pattern.match(lines[j])
|
||||
if file_match is not None:
|
||||
break
|
||||
# Get the coresponding language code extractor
|
||||
lang = file_match[1]
|
||||
file_name = file_match[2]
|
||||
|
||||
if lang not in extractor_dict:
|
||||
print(f"warning: {lang} is not in the extractor_dict")
|
||||
i += 1
|
||||
continue
|
||||
|
||||
extractor = extractor_dict[lang]
|
||||
# Get code blocks
|
||||
if file_name not in code_blocks_dict:
|
||||
code_blocks = extractor.extract(
|
||||
file_path=osp.dirname(md_path).replace("docs/", f"codes/{lang}/") + f"/{file_name}")
|
||||
if code_blocks is None:
|
||||
i += 1
|
||||
continue
|
||||
code_blocks_dict[file_name] = code_blocks
|
||||
|
||||
header_line = 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"]:
|
||||
lines.pop(header_line)
|
||||
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["funcs"]:
|
||||
lines.pop(header_line)
|
||||
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["funcs"]:
|
||||
lines.pop(header_line)
|
||||
func_block = class_dict["funcs"][func_label]
|
||||
for code_line in func_block["block"][::-1]:
|
||||
lines.insert(header_line, code_line)
|
||||
|
||||
i += 1
|
||||
|
||||
with open(md_path.replace("docs/", "build/"), "w") as f:
|
||||
f.writelines(lines)
|
||||
print(f"Built {md_path}")
|
||||
|
||||
|
||||
extractor_dict = {
|
||||
"java": ExtractCodeBlocksJava(),
|
||||
"python": ExtractCodeBlocksPython(),
|
||||
"cpp": ExtractCodeBlocksCpp(),
|
||||
"go": ExtractCodeBlocksGo(),
|
||||
"javascript": ExtractCodeBlocksJSTS(),
|
||||
"typescript": ExtractCodeBlocksJSTS(),
|
||||
"swift": ExtractCodeBlocksSwift(),
|
||||
"csharp": ExtractCodeBlocksCSharp(),
|
||||
"zig": ExtractCodeBlocksZig(),
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Copy files to the build dir
|
||||
shutil.copytree("docs", "build", dirs_exist_ok=True)
|
||||
shutil.rmtree("build/utils")
|
||||
|
||||
# Build code blocks
|
||||
for md_path in glob.glob("docs/chapter_*/*.md"):
|
||||
build_code_blocks(md_path)
|
||||
|
||||
# Build headings
|
||||
number_headings("mkdocs.yml", "build")
|
@ -1,27 +0,0 @@
|
||||
python docs/utils/build_markdown.py
|
||||
|
||||
while true; do
|
||||
read -p "Do you wish to deploy the site? [y] [n]" yn
|
||||
case $yn in
|
||||
[Yy]* ) make install; break;;
|
||||
[Nn]* ) exit;;
|
||||
* ) echo "Please answer yes[y] or no[n].";;
|
||||
esac
|
||||
done
|
||||
|
||||
# push the built docs
|
||||
cd build
|
||||
git add .
|
||||
git commit -m "build"
|
||||
git push -u origin docs
|
||||
cd ..
|
||||
|
||||
# Build mkdocs
|
||||
mkdocs build --clean
|
||||
|
||||
# deploy the site
|
||||
cd site
|
||||
git add .
|
||||
git commit -m "deploy"
|
||||
git push -u origin gh-pages
|
||||
cd ..
|
@ -1,28 +0,0 @@
|
||||
"""
|
||||
File: extract_code_cpp.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksCpp(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)(static|)\s*(|\S+)\s*(\w+)(\(.*\))\s+\{'
|
||||
self.class_pattern = r'(public|)\s*(class|struct)\s+(\w+)\s*\{'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "static", "return", "label", "args"]
|
||||
self.class_pattern_keys = ["total", "scope", "type", "label"]
|
||||
|
||||
|
||||
# for code_path in glob.glob("codes/cpp/chapter_*/my_heap.cpp"):
|
||||
# ext = ExtractCodeBlocksCpp()
|
||||
# ext.extract(code_path)
|
@ -1,28 +0,0 @@
|
||||
"""
|
||||
File: extract_code_csharp.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksCSharp(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)(public|private|)\s*(static|)\s*(|\S+)\s*(\w+)(\(.*\))'
|
||||
self.class_pattern = r'(public|)\s*(class|struct)\s+(\w+)\s*\n'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "scope", "static", "return", "label", "args"]
|
||||
self.class_pattern_keys = ["total", "scope", "type", "label"]
|
||||
|
||||
# for code_path in glob.glob("codes/csharp/chapter_*/array.cs"):
|
||||
# ext = ExtractCodeBlocksCSharp()
|
||||
# res = ext.extract(code_path)
|
||||
# pass
|
@ -1,178 +0,0 @@
|
||||
"""
|
||||
File: extract_code_go.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksGo(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)func\s+(\(.+\))*\s*(\w+)\((.*)\)\s*(\S*)\s*{\n'
|
||||
self.class_pattern = r'(\s*)type\s+(\w+)'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "class", "label", "params", "return"]
|
||||
self.class_pattern_keys = ["total", "ind", "label"]
|
||||
|
||||
def extract(self, file_path):
|
||||
"""
|
||||
Extract classes and functions from a markdown document
|
||||
"""
|
||||
if not osp.isfile(file_path):
|
||||
return None
|
||||
|
||||
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 and fucntions
|
||||
classes = self.extract_class_blocks()
|
||||
funcs = self.extract_function_blocks(classes=classes)
|
||||
|
||||
self.post_process(classes, funcs)
|
||||
|
||||
return {
|
||||
"classes": classes,
|
||||
"funcs": funcs,
|
||||
}
|
||||
|
||||
def extract_function_blocks(self, classes=None, class_label="", indentation=0):
|
||||
"""
|
||||
Extract all the functions with given indentation
|
||||
"""
|
||||
|
||||
funcs = {}
|
||||
func_pattern = re.compile(self.func_pattern)
|
||||
|
||||
for line_num in range(len(self.lines)):
|
||||
# Search the function header
|
||||
func_match = func_pattern.match(self.lines[line_num])
|
||||
if func_match is None:
|
||||
continue
|
||||
header_line = line_num
|
||||
|
||||
func_label = func_match.group(self.func_pattern_keys.index("label"))
|
||||
func_cls_label = func_match.group(self.func_pattern_keys.index("class"))
|
||||
func_return = func_match.group(self.func_pattern_keys.index("return"))
|
||||
|
||||
def check_func_blong_to_class(label):
|
||||
class_label_pattern = re.compile(f".*\*{label}\).*")
|
||||
func_return_pattern = re.compile(f".*{label}.*")
|
||||
constructor_pattern = re.compile(f".*new.*")
|
||||
|
||||
class_label_match = class_label_pattern.match(f"{func_cls_label}")
|
||||
func_return_match = func_return_pattern.match(f"{func_return}")
|
||||
constructor_match = constructor_pattern.match(func_label)
|
||||
|
||||
return class_label_match, func_return_match, constructor_match
|
||||
|
||||
if classes:
|
||||
# The function should not blong to any class
|
||||
flag = False
|
||||
for label in classes:
|
||||
# Match the target class label
|
||||
class_label_match, func_return_match, constructor_match = \
|
||||
check_func_blong_to_class(label)
|
||||
|
||||
if class_label_match is not None or \
|
||||
func_return_match is not None and constructor_match is not None:
|
||||
flag = True
|
||||
if flag:
|
||||
continue
|
||||
|
||||
elif class_label:
|
||||
# Match the target class label
|
||||
class_label_match, func_return_match, constructor_match = \
|
||||
check_func_blong_to_class(class_label)
|
||||
|
||||
if class_label_match is None and func_return_match is None or \
|
||||
func_return_match is not None and constructor_match is None:
|
||||
continue
|
||||
|
||||
# Search the block from the header line
|
||||
start_line, end_line, func_block = self.search_block(
|
||||
header_line, indentation)
|
||||
# Construct the funcs dict
|
||||
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
|
||||
_, _, class_block = self.search_block(
|
||||
header_line, 0)
|
||||
# Construct the classes dict
|
||||
class_label = class_match.group(self.class_pattern_keys.index("label"))
|
||||
funcs = self.extract_function_blocks(class_label=class_label)
|
||||
# Merge function blocks to class_block
|
||||
for func in funcs.values():
|
||||
class_block.append('\n')
|
||||
class_block += func["block"]
|
||||
|
||||
classes[class_label] = {
|
||||
"indentation": 0,
|
||||
"line_number": {
|
||||
"header": header_line,
|
||||
},
|
||||
"block": class_block,
|
||||
"funcs": funcs,
|
||||
}
|
||||
|
||||
return classes
|
||||
|
||||
def post_process(self, classes, funcs):
|
||||
"""
|
||||
Process the classes and functions
|
||||
"""
|
||||
def replace_tabs(x):
|
||||
for i, line in enumerate(x["block"]):
|
||||
x["block"][i] = line.replace("\t"," " * self.ind)
|
||||
|
||||
def add_inds(x):
|
||||
for i, line in enumerate(x["block"]):
|
||||
if line != "\n":
|
||||
x["block"][i] = " " * self.ind + line
|
||||
|
||||
for clas in classes.values():
|
||||
replace_tabs(clas)
|
||||
for func in clas["funcs"].values():
|
||||
replace_tabs(func)
|
||||
add_inds(func)
|
||||
for func in funcs.values():
|
||||
replace_tabs(func)
|
||||
|
||||
|
||||
for code_path in glob.glob("codes/*/chapter_*/graph_adjacency_matrix.go"):
|
||||
ext = ExtractCodeBlocksGo()
|
||||
res = ext.extract(code_path)
|
||||
pass
|
@ -1,168 +0,0 @@
|
||||
"""
|
||||
File: extract_code_java.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
|
||||
|
||||
class ExtractCodeBlocksJava:
|
||||
def __init__(self) -> None:
|
||||
self.ind = 4
|
||||
|
||||
# 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*\{'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "scope", "static", "return", "label", "args"]
|
||||
self.class_pattern_keys = ["total", "scope", "label"]
|
||||
|
||||
# Pattern to match the start and end of a block
|
||||
self.block_start_pattern = '^\s{ind}\/\*.+\*\/'
|
||||
self.block_end_pattern = '^\s{ind}\}'
|
||||
self.block_start_shift = 0
|
||||
self.block_end_shift = 0
|
||||
|
||||
def extract(self, file_path):
|
||||
"""
|
||||
Extract classes and functions from a markdown document
|
||||
"""
|
||||
if not osp.isfile(file_path):
|
||||
return None
|
||||
|
||||
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 and fucntions
|
||||
classes = self.extract_class_blocks()
|
||||
funcs = self.extract_function_blocks()
|
||||
|
||||
self.post_process(classes, funcs)
|
||||
|
||||
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.match(block_end_pattern, self.lines[i]) is not None:
|
||||
end_line = i + self.block_end_shift
|
||||
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 + self.block_start_shift
|
||||
break
|
||||
|
||||
return start_line, end_line, self.lines[start_line:end_line + 1]
|
||||
|
||||
def extract_function_blocks(self, indentation=0, 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(self.func_pattern_keys.index("ind"))) != 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(self.func_pattern_keys.index("label"))
|
||||
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(self.class_pattern_keys.index("label"))
|
||||
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=self.ind, start_line=start_line, end_line=end_line)
|
||||
}
|
||||
|
||||
return classes
|
||||
|
||||
def post_process(self, classes, funcs):
|
||||
"""
|
||||
Process the classes and functions
|
||||
"""
|
||||
def remove_keyword(func):
|
||||
block = func["block"]
|
||||
header_line = func["line_number"]["header"] - \
|
||||
func["line_number"]["start"]
|
||||
block[header_line] = block[header_line] \
|
||||
.replace("static ", "", 1).replace("public ", "", 1).replace("private ", "", 1)
|
||||
for clas in classes.values():
|
||||
remove_keyword(clas)
|
||||
for func in clas["funcs"].values():
|
||||
remove_keyword(func)
|
||||
for func in funcs.values():
|
||||
remove_keyword(func)
|
||||
|
||||
# ext = ExtractCodeBlocksJava()
|
||||
# ext.extract("codes/java/chapter_array_and_linkedlist/my_list.java")
|
@ -1,23 +0,0 @@
|
||||
"""
|
||||
File: extract_code_jsts.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksJSTS(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)(function|private|public|)\s*(\S*)\(.*\)(:|)\s*(.*)\s+{\s*\n'
|
||||
self.class_pattern = r'(public|)\s*class\s+(\w+)\s*\{'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "prefix", "label", ":", "return"]
|
||||
self.class_pattern_keys = ["total", "scope", "label"]
|
@ -1,51 +0,0 @@
|
||||
"""
|
||||
File: extract_code_python.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksPython(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)def\s+(\w+)\s*\('
|
||||
self.class_pattern = r'class\s+(\w+)'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "label"]
|
||||
self.class_pattern_keys = ["total", "label"]
|
||||
|
||||
# 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}""".+'
|
||||
self.block_start_shift = 0
|
||||
self.block_end_shift = -1
|
||||
|
||||
def post_process(self, classes, funcs):
|
||||
"""
|
||||
Process the classes and functions
|
||||
"""
|
||||
def remove_empty_lines(func):
|
||||
start_line, end_line = func["line_number"]["start"], func["line_number"]["end"]
|
||||
block = func["block"]
|
||||
# Remove empty lines at bottom
|
||||
for i in range(len(block) - 1, -1, -1):
|
||||
if re.search("^\s*\n", block[i]) is None:
|
||||
break
|
||||
end_line -= 1
|
||||
func["line_number"]["end"] = end_line
|
||||
func["block"] = block[:end_line - start_line + 1]
|
||||
|
||||
for clas in classes.values():
|
||||
remove_empty_lines(clas)
|
||||
for func in clas["funcs"].values():
|
||||
remove_empty_lines(func)
|
||||
for func in funcs.values():
|
||||
remove_empty_lines(func)
|
@ -1,23 +0,0 @@
|
||||
"""
|
||||
File: extract_code_swift.py
|
||||
Created Time: 2023-02-08
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksSwift(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)(public|private|)\s*(static|)\s*(func|)\s*(\w+)\(.*\).+{\s*\n'
|
||||
self.class_pattern = r'(public|)\s*class\s+(\w+)\s*\{'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "scope", "static", "func", "label"]
|
||||
self.class_pattern_keys = ["total", "scope", "label"]
|
@ -1,90 +0,0 @@
|
||||
"""
|
||||
File: extract_code_zig.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksZig(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
self.ind = 4
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s*)(pub|)\s*fn\s+(\w+)\(.+\)\s*(.+)\s*{\n'
|
||||
self.class_pattern = r'(\s*)(pub|)\s*(fn|const)\s+(\w+)\(*.+\)*\s*(type|struct)\s*{\n'
|
||||
|
||||
self.func_pattern_keys = ["total", "ind", "scope", "label", "return"]
|
||||
self.class_pattern_keys = ["total", "ind", "scope", "type", "label", "struct"]
|
||||
|
||||
# Pattern to match the start and end of a block
|
||||
self.block_start_pattern = '^\s*\n'
|
||||
self.block_start_shift = 1
|
||||
|
||||
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(self.class_pattern_keys.index("label"))
|
||||
# Define the indentation by the class type
|
||||
class_type = class_match.group(self.class_pattern_keys.index("type"))
|
||||
self.ind = 8 if class_type == "fn" else 4
|
||||
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=self.ind, start_line=start_line, end_line=end_line)
|
||||
}
|
||||
|
||||
return classes
|
||||
|
||||
def post_process(self, classes, funcs):
|
||||
"""
|
||||
Process the classes and functions
|
||||
"""
|
||||
def remove_keyword(func):
|
||||
block = func["block"]
|
||||
header_line = func["line_number"]["header"] - \
|
||||
func["line_number"]["start"]
|
||||
block[header_line] = block[header_line].replace("pub ", "", 1)
|
||||
|
||||
for clas in classes.values():
|
||||
remove_keyword(clas)
|
||||
for func in clas["funcs"].values():
|
||||
remove_keyword(func)
|
||||
if func["indentation"] == 8:
|
||||
for i, line in enumerate(func["block"]):
|
||||
func["block"][i] = line[4:]
|
||||
for func in funcs.values():
|
||||
remove_keyword(func)
|
||||
|
||||
# for code_path in glob.glob("codes/*/chapter_*/my_heap.zig"):
|
||||
# ext = ExtractCodeBlocksZig()
|
||||
# res = ext.extract(code_path)
|
||||
# pass
|
@ -1,87 +0,0 @@
|
||||
"""
|
||||
File: number_headings.py
|
||||
Created Time: 2023-02-16
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
def get_heading_info_from_nav(mkdocs_path):
|
||||
"""
|
||||
Get heading info from mkdocs navigation
|
||||
"""
|
||||
|
||||
with open(mkdocs_path) as f:
|
||||
lines = f.readlines()[125:]
|
||||
# Get nav lines
|
||||
for i, line in enumerate(lines):
|
||||
if "nav:" in line:
|
||||
break
|
||||
lines = lines[i:]
|
||||
|
||||
# Search articles
|
||||
articles = []
|
||||
for line in lines:
|
||||
level = 0
|
||||
level_re = None
|
||||
while level_re is None and level < 3:
|
||||
level += 1
|
||||
level_pat = level * " "
|
||||
level_pat = f"^{level_pat}- \d"
|
||||
level_re = re.search(level_pat, line)
|
||||
# Only add articles with heading level 2
|
||||
if level != 2:
|
||||
continue
|
||||
|
||||
number_pat = level * "\d+."
|
||||
number_re = re.search(number_pat, line)
|
||||
number = re.search(number_pat, line).group(0) if number_re else None
|
||||
file_path = re.search("\S+\/\S+\.md", line).group(0)
|
||||
|
||||
article = {
|
||||
"level": level,
|
||||
"number": number,
|
||||
"file_path": file_path
|
||||
}
|
||||
articles.append(article)
|
||||
|
||||
print(f"{file_path}, heading number is {number}")
|
||||
|
||||
return articles
|
||||
|
||||
def number_article(article, base_dir="build"):
|
||||
"""
|
||||
Number a doc
|
||||
"""
|
||||
|
||||
with open(f"{base_dir}/{article['file_path']}", "r") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# Add h1, h2 heading numbers
|
||||
h2_count = 1
|
||||
for i, line in enumerate(lines):
|
||||
h1_re = re.search("^(#)\s+\S+", line)
|
||||
if h1_re is not None:
|
||||
h1 = h1_re.group(1)
|
||||
lines[i] = line.replace(h1, f"# {article['number']}")
|
||||
continue
|
||||
|
||||
h2_re = re.search("^(##)\s+\S+", line)
|
||||
if h2_re is not None:
|
||||
h2 = h2_re.group(1)
|
||||
lines[i] = line.replace(h2, f"## {article['number']}{h2_count}.")
|
||||
h2_count += 1
|
||||
|
||||
with open(f"{base_dir}/{article['file_path']}", "w") as f:
|
||||
f.writelines(lines)
|
||||
|
||||
|
||||
def number_headings(mkdocs_path, build_dir):
|
||||
"""
|
||||
Build headings
|
||||
"""
|
||||
|
||||
articles = get_heading_info_from_nav(mkdocs_path)
|
||||
|
||||
for article in articles:
|
||||
number_article(article, base_dir=build_dir)
|
Loading…
Reference in new issue