sphinx_gallery.py_source_parser#
用於解析 Python 原始碼檔案的解析器。
函式#
- sphinx_gallery.py_source_parser.extract_file_config(content)#
提取 docstring 中指定的檔案特定設定。
- sphinx_gallery.py_source_parser.parse_source_file(filename)#
將原始碼檔案解析為 AST 節點。
- 參數:
filename (str) – 檔案路徑
- 回傳值:
node (AST 節點)
content (utf-8 編碼字串)
- sphinx_gallery.py_source_parser.remove_config_comments(code_block)#
回傳已移除檔案內設定註解的 code_block 內容。
會移除符合 '# sphinx_gallery_[option] = [val]' 模式的註解行,但保留周圍的空白行。
- 參數:
code_block (str) – 一段程式碼。
- sphinx_gallery.py_source_parser.remove_ignore_blocks(code_block)#
回傳已移除忽略區域的 code_block 內容。
忽略區塊以 # sphinx_gallery_start_ignore 開始,並以 # sphinx_gallery_end_ignore 結束。這些行以及它們之間的任何內容都將被移除,但保留周圍的空白行。
- 參數:
code_block (str) – 一段程式碼。
- sphinx_gallery.py_source_parser.split_code_and_text_blocks(source_file, return_node=False)#
回傳清單,其中原始碼檔案已分隔為程式碼和文字區塊。
- 參數:
source_file (str) – 原始碼檔案的路徑。
return_node (bool) – 如果為 True,則回傳 ast 節點。
- 回傳值:
file_conf (dict) – 檔案特定設定,在原始碼檔案註解中指定,格式為:
# sphinx_gallery_<name> = <value>
blocks (list) – (label, content, line_number) 清單,其中每個元素都是一個元組,包含標籤('text' 或 'code')、區塊的對應內容字串以及起始行號。
node (ast.Module) – 已解析的 ast 節點。