sphinx_gallery.block_parser#

BlockParser 將非 .py 原始檔分割成程式碼和標記文字的區塊。

類別#

class sphinx_gallery.block_parser.BlockParser(source_file, gallery_conf)[原始碼]#

將原始檔分割成程式碼和標記文字區塊的解析器。

使用 pygments 判斷原始語言並識別註解區塊。

參數:
  • source_file (str) – 副檔名相容於後續解析檔案的檔案名稱

  • gallery_conf (dict) – 包含 Sphinx-Gallery 的設定。

extract_file_config(content)[原始碼]#

取出在文件字串中指定的檔案特定設定。

remove_config_comments(code_block)[原始碼]#

傳回已移除檔案內設定註解的 code_block 內容。

行註解字元後的模式 sphinx_gallery_[option] = [val] 的註解行會被移除,但周圍的空行會被保留。

參數:

code_block (str) – 程式碼片段。

remove_ignore_blocks(code_block)[原始碼]#

傳回已移除忽略區域的 code_block 內容。

忽略區塊以 ?? sphinx_gallery_start_ignore 開始,並以 ?? sphinx_gallery_end_ignore 結束,其中 ?? 是作用中語言的行註解標記。這些行以及它們之間的任何內容都會被移除,但周圍的空行會被保留。

參數:

code_block (str) – 程式碼片段。

split_code_and_text_blocks(source_file, return_node=False)[原始碼]#

傳回將原始檔分成程式碼和文字區塊的列表。

參數:
  • source_file (str) – 原始檔的路徑。

  • return_node (bool) – 已忽略;不支援傳回 ast 節點

傳回:

  • file_conf (dict) – 來源檔案註解中給定的檔案特定設定,如:# sphinx_gallery_<name> = <value>

  • blocks (list) – (標籤、內容、行號) 列表,其中每個元素都是一個元組,包含標籤(「文字」或「程式碼」)、區塊的對應內容字串和前導行號。

  • node (None) – 不支援傳回 ast 節點。