broadfield-dev commited on
Commit
10099e5
·
verified ·
1 Parent(s): 444a911

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +172 -0
app.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import requests
3
+ import base64
4
+ import markdown
5
+ from bs4 import BeautifulSoup
6
+ import os
7
+
8
+ app = Flask(__name__)
9
+
10
+ # GitHub API base URL
11
+ GITHUB_API = "https://api.github.com/repos/"
12
+
13
+ def get_repo_contents(repo_url):
14
+ """Extract contents from GitHub repo URL"""
15
+ try:
16
+ # Extract owner and repo name from URL
17
+ parts = repo_url.rstrip('/').split('/')
18
+ owner, repo = parts[-2], parts[-1]
19
+
20
+ # Get repository contents
21
+ api_url = f"{GITHUB_API}{owner}/{repo}/contents"
22
+ response = requests.get(api_url)
23
+ response.raise_for_status()
24
+
25
+ return owner, repo, response.json()
26
+ except Exception as e:
27
+ return None, None, str(e)
28
+
29
+ def process_file_content(file_info, owner, repo):
30
+ """Process individual file content"""
31
+ content = ""
32
+ file_path = file_info['path']
33
+
34
+ if file_info['type'] == 'file':
35
+ # Get file content
36
+ file_url = f"{GITHUB_API}{owner}/{repo}/contents/{file_path}"
37
+ file_response = requests.get(file_url)
38
+ file_data = file_response.json()
39
+
40
+ if 'content' in file_data:
41
+ # Decode base64 content
42
+ decoded_content = base64.b64decode(file_data['content']).decode('utf-8')
43
+ content = f"### File: {file_path}\n```{(file_path.split('.')[-1] if '.' in file_path else 'text')}\n{decoded_content}\n```\n\n"
44
+
45
+ return content
46
+
47
+ def create_markdown_document(repo_url):
48
+ """Create markdown document from repo contents"""
49
+ owner, repo, contents = get_repo_contents(repo_url)
50
+
51
+ if isinstance(contents, str): # Error case
52
+ return f"Error: {contents}"
53
+
54
+ markdown_content = f"# Repository: {owner}/{repo}\n\n"
55
+ markdown_content += "Below are the contents of all files in the repository:\n\n"
56
+
57
+ # Process each file recursively
58
+ for item in contents:
59
+ markdown_content += process_file_content(item, owner, repo)
60
+
61
+ return markdown_content
62
+
63
+ @app.route('/')
64
+ def index():
65
+ """Render the main page"""
66
+ return render_template('index.html')
67
+
68
+ @app.route('/process', methods=['POST'])
69
+ def process_repo():
70
+ """Process the repository URL and return markdown"""
71
+ repo_url = request.json.get('repo_url')
72
+ if not repo_url:
73
+ return jsonify({'error': 'Please provide a repository URL'}), 400
74
+
75
+ markdown_content = create_markdown_document(repo_url)
76
+
77
+ # Convert markdown to HTML for display
78
+ html_content = markdown.markdown(markdown_content)
79
+
80
+ return jsonify({
81
+ 'markdown': markdown_content,
82
+ 'html': html_content
83
+ })
84
+
85
+ # HTML template
86
+ html_template = """
87
+ <!DOCTYPE html>
88
+ <html>
89
+ <head>
90
+ <title>GitHub Repo to Markdown</title>
91
+ <style>
92
+ body {
93
+ font-family: Arial, sans-serif;
94
+ margin: 20px;
95
+ max-width: 1200px;
96
+ margin: 0 auto;
97
+ }
98
+ .container {
99
+ padding: 20px;
100
+ }
101
+ textarea {
102
+ width: 100%;
103
+ height: 400px;
104
+ margin-top: 20px;
105
+ font-family: monospace;
106
+ }
107
+ button {
108
+ padding: 10px 20px;
109
+ background-color: #4CAF50;
110
+ color: white;
111
+ border: none;
112
+ cursor: pointer;
113
+ }
114
+ button:hover {
115
+ background-color: #45a049;
116
+ }
117
+ #output {
118
+ margin-top: 20px;
119
+ border: 1px solid #ddd;
120
+ padding: 20px;
121
+ background-color: #f9f9f9;
122
+ }
123
+ </style>
124
+ </head>
125
+ <body>
126
+ <div class="container">
127
+ <h1>GitHub Repository to Markdown Converter</h1>
128
+ <p>Enter a GitHub repository URL (e.g., https://github.com/username/repository)</p>
129
+ <input type="text" id="repoUrl" style="width: 100%; padding: 8px;" placeholder="Enter GitHub repository URL">
130
+ <button onclick="processRepo()">Convert to Markdown</button>
131
+
132
+ <h2>Markdown Output:</h2>
133
+ <textarea id="markdownOutput" readonly></textarea>
134
+
135
+ <h2>Preview:</h2>
136
+ <div id="output"></div>
137
+ </div>
138
+
139
+ <script>
140
+ async function processRepo() {
141
+ const repoUrl = document.getElementById('repoUrl').value;
142
+ const response = await fetch('/process', {
143
+ method: 'POST',
144
+ headers: {
145
+ 'Content-Type': 'application/json',
146
+ },
147
+ body: JSON.stringify({ repo_url: repoUrl })
148
+ });
149
+
150
+ const data = await response.json();
151
+
152
+ if (data.error) {
153
+ alert(data.error);
154
+ return;
155
+ }
156
+
157
+ document.getElementById('markdownOutput').value = data.markdown;
158
+ document.getElementById('output').innerHTML = data.html;
159
+ }
160
+ </script>
161
+ </body>
162
+ </html>
163
+ """
164
+
165
+ # Create templates directory and write the HTML file
166
+ if not os.path.exists('templates'):
167
+ os.makedirs('templates')
168
+ with open('templates/index.html', 'w') as f:
169
+ f.write(html_template)
170
+
171
+ if __name__ == '__main__':
172
+ app.run(debug=True)