from flask import Flask, render_template, request, jsonify import requests import base64 import markdown from bs4 import BeautifulSoup import os import mimetypes import json app = Flask(__name__) GITHUB_API = "https://api.github.com/repos/" def get_repo_contents(repo_url): """Extract contents from GitHub repo URL""" try: parts = repo_url.rstrip('/').split('/') owner, repo = parts[-2], parts[-1] 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': 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: file_extension = file_path.split('.')[-1] if '.' in file_path else '' mime_type, _ = mimetypes.guess_type(file_path) is_text = (mime_type and mime_type.startswith('text')) or file_extension in ['py', 'md', 'txt', 'js', 'html', 'css', 'json'] if is_text: try: decoded_content = base64.b64decode(file_data['content']).decode('utf-8') # Special handling for JSON files if file_extension == 'json': try: json_data = json.loads(decoded_content) formatted_json = json.dumps(json_data, indent=2) content = f"### File: {file_path}\n```json\n{formatted_json}\n```\n\n" except json.JSONDecodeError: content = f"### File: {file_path}\n```json\n{decoded_content}\n```\n[Note: Invalid JSON format]\n\n" else: content = f"### File: {file_path}\n```{(file_extension if file_extension else 'text')}\n{decoded_content}\n```\n\n" except UnicodeDecodeError: content = f"### File: {file_path}\n[Text content could not be decoded - possibly corrupted or non-UTF-8 text]\n\n" else: content = f"### File: {file_path}\n[Binary file - {file_data['size']} bytes]\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): 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" for item in contents: markdown_content += process_file_content(item, owner, repo) return markdown_content @app.route('/') def index(): return render_template('index.html') @app.route('/process', methods=['POST']) def process_repo(): 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) html_content = markdown.markdown(markdown_content) return jsonify({ 'markdown': markdown_content, 'html': html_content }) html_template = """ GitHub Repo to Markdown

GitHub Repository to Markdown Converter

Enter a GitHub repository URL (e.g., https://github.com/username/repository)

Markdown Output:

Preview:

""" 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(host="0.0.0.0", port=7860, debug=True)