# me
# Pythonで以下の手続きを実行するプログラムを書いてください。
1. 指定されたフォルダ以下のMarkdownファイルを一つ読み出す。
2. Markdownのセクションに "## 1. Introduction"または"## I. INTRODUCTION"が含まれている、かつ、そのセクション以降の文字数が100文字以上かどうかを確認する。
3. 2.の条件を満たさないのであれば、1. へ戻す。
4. 指定されたフォルダのサブフォルダ"translations"以下に、現在のファイル名の末尾の".md"を"__translations.md"へ置換した新しいファイルを作成し、元のファイルのfrontmatterのうち"aliases"を除去した文字列をコピーし、改行後、本文に、"## 1. Introduction" セクション以降の文字列をコピーする。
5. 元のファイルの"## 1. Introduction"または"## I. INTRODUCTION"セクション以降の文字列を削除する。
6. 元のファイルの末尾に "[[{新しいファイルの名前}]]" を追記する。
7. 元のファイルのfrontmatterの"publish: false"の行を削除する。
7. 1. へ戻る。すべてのMarkdownファイルを読み終えていれば、プログラムを終了する。
## 条件
- プログラムのコメントとログメッセージは英語にすること
- 各処理を小さな関数として構成し、読みやすいプログラムにすること
# ai
```python
import os
import re
# Function to find markdown files in the specified folder
def find_md_files(folder):
return [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith(".md")]
# Function to check if the file contains the required section and has at least 100 characters after the section
def check_section_and_length(file_path):
with open(file_path, "r") as f:
content = f.read()
if re.search(r'## (1\. Introduction|I\. INTRODUCTION)', content):
section_start = re.search(r'## (1\. Introduction|I\. INTRODUCTION)', content).end()
return len(content) - section_start > 100
return False
# Function to create a new file and copy the required content
def create_new_file(file_path):
with open(file_path, "r") as f:
content = f.read()
frontmatter_end = content.find("---", 1) + 3
section_start = re.search(r'## (1\. Introduction|I\. INTRODUCTION)', content).start()
frontmatter = content[:frontmatter_end].replace("aliases", "")
section_content = content[section_start:]
new_file_name = os.path.basename(file_path).replace(".md", "__translations.md")
new_file_path = os.path.join(os.path.dirname(file_path), "translations", new_file_name)
with open(new_file_path, "w") as nf:
nf.write(frontmatter + "\n" + section_content)
return new_file_name
# Function to modify the original file
def modify_original_file(file_path, new_file_name):
with open(file_path, "r") as f:
content = f.read()
section_start = re.search(r'## (1\. Introduction|I\. INTRODUCTION)', content).start()
frontmatter_end = content.find("---", 1) + 3
content = content[:section_start] + f"[[{new_file_name}]]" + "\n"
content = re.sub(r"publish: false\n", "", content)
with open(file_path, "w") as f:
f.write(content)
# Main function
def main():
folder = "path/to/folder" # Replace with the path to the specified folder
md_files = find_md_files(folder)
for file_path in md_files:
if not check_section_and_length(file_path):
continue
new_file_name = create_new_file(file_path)
modify_original_file(file_path, new_file_name)
if __name__ == "__main__":
main()
```
Make sure to replace the `"path/to/folder"` with the actual path to the specified folder containing the Markdown files. This script should fulfill the requirements specified in the task.