230226 - ChatGPT を使用してマークダウン ファイルと reStructuredText ファイル間の一方向変換とインデックス ファイルの生成を実現する

  • 機能: 初めてChatGPTコードを書いてみましたが、このコードはインデックスの変換と自動生成を実現markdown文件できreStructuredText文件ますindex

  • 目的: Markdown を使用してメモを取り、Sphinx を使用して植字とオンライン展開を行います。プレビュー効果は次の図のようになります。

ここに画像の説明を挿入

  • プロセス: 生成されたコードのダイアログ レコードについては、以下のスクリーンショットを参照してください。

画像の説明を追加してください

  • コード: ChatGPT によって生成されたコード
    • 1: マークダウンフォルダーへのパスを指定します。
    • 定式 2: reStructuredText フォルダーへのパス
import os
import subprocess


# Convert .md files to .rst files
input_folder = 'markdown'
output_folder = 'source/Years'

if not os.path.exists(output_folder):
    os.makedirs(output_folder)

for root, dirs, files in os.walk(input_folder):
    for filename in files:
        if filename.endswith('.md'):
            input_path = os.path.join(root, filename)
            rel_path = os.path.relpath(input_path, input_folder)
            output_path = os.path.join(output_folder, os.path.splitext(rel_path)[0] + '.rst')
            output_folder_path = os.path.dirname(output_path)
            if not os.path.exists(output_folder_path):
                os.makedirs(output_folder_path)
            subprocess.run(['pandoc', '-f', 'markdown', '-t', 'rst', '-o', output_path, input_path])


# Generate .rst index files 
import os
def generate_index_files(root_path):
    for dirpath, dirnames, filenames in os.walk(root_path):
        # Sort directories and files alphabetically
        dirnames.sort()
        filenames.sort()
        
        # Create the index file
        index_file_path = os.path.join(dirpath, "index.rst")
        with open(index_file_path, 'w') as f:
            # Write the folder name to the index file
            folder_name = os.path.basename(dirpath)
            f.write(f"{
      
      folder_name}\n")
            f.write("=" * len(folder_name) + "\n\n")
            
            # Write the table of contents to the index file
            f.write(".. toctree::\n")
            f.write("   :maxdepth: 2\n\n")
            
            for filename in filenames:
                # Ignore hidden files and non-.rst files
                if filename.startswith(".") or not filename.endswith(".rst") or filename == "index.rst":
                    continue
                
                # Write the file name to the index file
                f.write(f"   {
      
      os.path.splitext(filename)[0]}\n")
            
            # Write a blank line at the end of the file
            f.write("\n")
            
            # Generate index files for subfolders
            for dirname in dirnames:
                generate_index_files(os.path.join(dirpath, dirname))
                
                # Write a link to the subfolder's index file in the parent index file
                subfolder_index_path = os.path.join(dirname, "index")
                f.write(f"   {
      
      subfolder_index_path}\n")
            
            # Write a blank line at the end of the file
            f.write("\n")

generate_index_files(output_folder)

  • 構成:source/conf.pyの構成
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'MacSphinx'
copyright = '2023, LiuGuokai'
author = 'LiuGuokai'
release = '0.1'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = []

templates_path = ['_templates']
exclude_patterns = []

language = 'zh_CN'

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output



# import sphinx_theme
# html_theme = 'stanford_theme'
# html_theme_path = [sphinx_theme.get_html_theme_path('stanford-theme')]
# html_theme = 'alabaster'
html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']


# support markdown
extensions = [
     'recommonmark',
     'sphinx.ext.autosummary',
    #  'sphinx_markdown_tables'
 ]
autodoc_default_flags = ['members']
autosummary_generate = True


html_sidebars = {
    
    
    '**': [
        'versioning.html',
    ],
}
smv_latest_version = 'v3.0' 
sitemap_url_scheme = "{link}"
  • 構成:source/index.rstの構成
.. diary documentation master file, created by
sphinx-quickstart on Sat Oct 10 22:31:33 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to LiuGuokai's documentation!
=================================
.. toctree::
   :maxdepth: 2
   :caption: Contents:

   Years/index

おすすめ

転載: blog.csdn.net/qq_33039859/article/details/129229673