-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadme.md-File-Creator.py
More file actions
177 lines (141 loc) · 6.13 KB
/
Readme.md-File-Creator.py
File metadata and controls
177 lines (141 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import json
import sys
import google.generativeai as genai
# JSON dosyasını oku
CONFIG_FILE = "Config.json"
SCRIPT_FILE = "Readme.md-File-Creator.py"
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
config = json.load(f)
project_name = config.get("project_name", "Unnamed Project")
topic = config.get("topic", "No topic specified")
description = config.get("description", "No description provided.")
YOUR_API_KEY = config.get("API", "").strip()
features = config.get("features", [])
installation = config.get("installation", [])
usage = config.get("usage", [])
contribution = config.get("contribution", "No contribution info provided.")
license_name = config.get("license", "No license specified.")
languages = config.get("languages", ["en"])
ignore_list = config.get("ignore", [])
# Yeni eklenen alanlar
github_repository = config.get("github_repository", "").strip()
author = config.get("Author", "").strip()
date = config.get("Date", "").strip()
# Zorunlu alan kontrolleri
if not YOUR_API_KEY:
print("❌ API key has not been provided. Please add your API key in the JSON config.")
sys.exit(1)
if not github_repository:
print("❌ GitHub repository URL has not been provided. Please add 'github_repository' in the JSON config.")
sys.exit(1)
if not author:
print("❌ Author has not been provided. Please add 'Author' in the JSON config.")
sys.exit(1)
# Mevcut çalışma klasörü (script'in bulunduğu)
current_dir = os.path.abspath(os.getcwd())
parent_dir = os.path.dirname(current_dir) # Bir üst klasör
# Proje klasöründeki dosya ve klasörleri al (config, script ve ignore edilenleri hariç tut)
project_files = []
for root, dirs, files in os.walk(parent_dir):
# ignore edilen klasörleri tamamen atla
dirs[:] = [d for d in dirs if os.path.relpath(os.path.join(root, d), parent_dir) not in ignore_list]
for d in dirs:
path = os.path.relpath(os.path.join(root, d), parent_dir)
if path not in [CONFIG_FILE, SCRIPT_FILE] and path not in ignore_list:
project_files.append(path)
for file in files:
path = os.path.relpath(os.path.join(root, file), parent_dir)
if path not in [CONFIG_FILE, SCRIPT_FILE] and path not in ignore_list:
project_files.append(path)
# Eğer klasörde yalnızca config ve script varsa README oluşturmayı atla
if not project_files:
print("ℹ️ No project files found (excluding config, script, and ignored). README generation skipped.")
sys.exit(0)
project_file_tree = "\n".join(project_files)
# Google Gemini API anahtarını ayarla
genai.configure(api_key=YOUR_API_KEY)
model = genai.GenerativeModel("gemini-2.5-flash")
# Kod bloğu kontrol ve düzeltme fonksiyonu
def fix_code_blocks(text: str) -> str:
"""
Eğer README içeriğinde ``` ile başlayan ama kapanmayan kod blokları varsa
sonuna ``` ekler.
"""
if not text:
return text
open_count = text.count("```")
if open_count % 2 != 0:
text += "\n```"
return text.strip()
# README.md içeriğini oluşturacak değişken
final_readme = f"# {project_name}\n\n"
# İlk dil (ana dil) ile içerik oluştur
main_lang = languages[0]
prompt_main = f"""
You are an expert AI technical writer. Create a professional and visually appealing README.md
for a GitHub project, in the language: {main_lang}.
Project info:
- Name: {project_name}
- Topic: {topic}
- Description: {description}
- Features: {', '.join(features) if features else 'No features provided.'}
- Installation: {', '.join(installation) if installation else 'No installation steps provided.'}
- Usage: {', '.join(usage) if usage else 'No usage info provided.'}
- Contribution: {contribution}
- License: {license_name}
- GitHub Repository: {github_repository}
- Author: {author}
- Date: {date if date else 'Not specified'}
Project file structure:
{project_file_tree}
Project directory:
{parent_dir}
Rules:
1. Include all relevant sections (Description, Features, Installation, Usage, Contribution, License, Project Structure, Repository, Author, Date).
2. If some sections are missing, generate content intelligently based on project topic.
3. Format with Markdown: headings, lists, code blocks, emojis, badges.
4. Output the README content as plain Markdown (do NOT wrap it in triple backticks or markdown code fences).
5. The output must be ready to be saved directly as a README.md file without extra wrappers.
"""
try:
response_main = model.generate_content(prompt_main)
main_content = response_main.text.strip()
# Güvenlik: Başta/sonda ``` bloklarını temizle
if main_content.startswith("```"):
main_content = main_content.split("\n", 1)[1]
if main_content.endswith("```"):
main_content = main_content.rsplit("\n", 1)[0]
# Kod bloklarını kapat
main_content = fix_code_blocks(main_content)
except Exception as e:
print(f"❌ Failed to generate README: {e}")
sys.exit(1)
# Ana dili README'ye ekle
final_readme += f"## {main_lang.upper()}\n\n{main_content}\n"
# Diğer dillere çeviri yap
for lang in languages[1:]:
prompt_translate = f"""
Translate the following README content into {lang} while preserving formatting, headings, lists, code blocks, emojis, and badges.
Do NOT change the content, only translate the text.
Content to translate:
{main_content}
"""
try:
response_translate = model.generate_content(prompt_translate)
translated_content = response_translate.text.strip()
# Güvenlik: Başta/sonda ``` bloklarını temizle
if translated_content.startswith("```"):
translated_content = translated_content.split("\n", 1)[1]
if translated_content.endswith("```"):
translated_content = translated_content.rsplit("\n", 1)[0]
# Kod bloklarını kapat
translated_content = fix_code_blocks(translated_content)
final_readme += f"\n---\n\n## {lang.upper()}\n\n{translated_content}\n"
except Exception as e:
print(f"❌ Failed to translate README to {lang}: {e}")
continue
# README.md dosyasına yaz
with open("../README.md", "w", encoding="utf-8") as f:
f.write(final_readme)
print("✅ README.md file created with Google Gemini (with translations)")