repo_to_md / app.py
broadfield-dev's picture
Create app.py
10099e5 verified
raw
history blame
5.11 kB
from flask import Flask, render_template, request, jsonify
import requests
import base64
import markdown
from bs4 import BeautifulSoup
import os
app = Flask(__name__)
# GitHub API base URL
GITHUB_API = "https://api.github.com/repos/"
def get_repo_contents(repo_url):
"""Extract contents from GitHub repo URL"""
try:
# Extract owner and repo name from URL
parts = repo_url.rstrip('/').split('/')
owner, repo = parts[-2], parts[-1]
# Get repository contents
api_url = f"{GITHUB_API}{owner}/{repo}/contents"
response = requests.get(api_url)
response.raise_for_status()
return owner, repo, response.json()
except Exception as e:
return None, None, str(e)
def process_file_content(file_info, owner, repo):
"""Process individual file content"""
content = ""
file_path = file_info['path']
if file_info['type'] == 'file':
# Get file content
file_url = f"{GITHUB_API}{owner}/{repo}/contents/{file_path}"
file_response = requests.get(file_url)
file_data = file_response.json()
if 'content' in file_data:
# Decode base64 content
decoded_content = base64.b64decode(file_data['content']).decode('utf-8')
content = f"### File: {file_path}\n```{(file_path.split('.')[-1] if '.' in file_path else 'text')}\n{decoded_content}\n```\n\n"
return content
def create_markdown_document(repo_url):
"""Create markdown document from repo contents"""
owner, repo, contents = get_repo_contents(repo_url)
if isinstance(contents, str): # Error case
return f"Error: {contents}"
markdown_content = f"# Repository: {owner}/{repo}\n\n"
markdown_content += "Below are the contents of all files in the repository:\n\n"
# Process each file recursively
for item in contents:
markdown_content += process_file_content(item, owner, repo)
return markdown_content
@app.route('/')
def index():
"""Render the main page"""
return render_template('index.html')
@app.route('/process', methods=['POST'])
def process_repo():
"""Process the repository URL and return markdown"""
repo_url = request.json.get('repo_url')
if not repo_url:
return jsonify({'error': 'Please provide a repository URL'}), 400
markdown_content = create_markdown_document(repo_url)
# Convert markdown to HTML for display
html_content = markdown.markdown(markdown_content)
return jsonify({
'markdown': markdown_content,
'html': html_content
})
# HTML template
html_template = """
<!DOCTYPE html>
<html>
<head>
<title>GitHub Repo to Markdown</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
max-width: 1200px;
margin: 0 auto;
}
.container {
padding: 20px;
}
textarea {
width: 100%;
height: 400px;
margin-top: 20px;
font-family: monospace;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#output {
margin-top: 20px;
border: 1px solid #ddd;
padding: 20px;
background-color: #f9f9f9;
}
</style>
</head>
<body>
<div class="container">
<h1>GitHub Repository to Markdown Converter</h1>
<p>Enter a GitHub repository URL (e.g., https://github.com/username/repository)</p>
<input type="text" id="repoUrl" style="width: 100%; padding: 8px;" placeholder="Enter GitHub repository URL">
<button onclick="processRepo()">Convert to Markdown</button>
<h2>Markdown Output:</h2>
<textarea id="markdownOutput" readonly></textarea>
<h2>Preview:</h2>
<div id="output"></div>
</div>
<script>
async function processRepo() {
const repoUrl = document.getElementById('repoUrl').value;
const response = await fetch('/process', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ repo_url: repoUrl })
});
const data = await response.json();
if (data.error) {
alert(data.error);
return;
}
document.getElementById('markdownOutput').value = data.markdown;
document.getElementById('output').innerHTML = data.html;
}
</script>
</body>
</html>
"""
# Create templates directory and write the HTML file
if not os.path.exists('templates'):
os.makedirs('templates')
with open('templates/index.html', 'w') as f:
f.write(html_template)
if __name__ == '__main__':
app.run(debug=True)