admin管理员组

文章数量:1530518

  • 作用:第一次尝试使用ChatGPT写代码,本代码可实现markdown文件reStructuredText文件的转换及index索引的自动生成。

  • 目的:利用Markdown记笔记,利用Sphinx进行排版及线上部署,预览效果如图:

  • 过程:生成代码的对话记录参见下面截图:

  • 代码:ChatGPT生成的代码
    • 指定1:markdown文件夹的路径
    • 制定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/en/master/usage/configuration.html

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

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

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

extensions = []

templates_path = ['_templates']
exclude_patterns = []

language = 'zh_CN'

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc/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

  • 链接
    • Github: https://github/Liu-Guokai/MacSphinx
    • Read the docs: https://macsphinx.readthedocs.io/en/latest/Years/2021/%E6%98%A5/Spring.html
    • WeChat: https://mp.weixin.qq/s/TrY6_YLSaEzyqUkZDu8PkQ
    • Zhihu: https://zhuanlan.zhihu/p/264647009

本文标签: 文件索引chatGPTreStructuredTextMarkdown