-
機能: 初めて
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