File size: 7,056 Bytes
10099e5
 
 
 
 
 
f1d73d7
540d342
10099e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1d73d7
 
540d342
f1d73d7
 
 
 
540d342
 
 
 
 
 
 
 
 
 
f1d73d7
 
 
 
10099e5
 
 
 
 
 
 
540d342
10099e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540d342
 
 
 
 
 
 
 
 
 
 
 
 
 
10099e5
 
 
 
 
 
 
 
540d342
10099e5
 
 
 
 
 
 
 
 
 
 
540d342
 
10099e5
540d342
 
 
10099e5
540d342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10099e5
 
 
 
 
 
 
 
 
 
 
 
 
6321348
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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 = """
<!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;
        }
        .spinner {
            display: none;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 20px auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </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>
        <div id="spinner" class="spinner"></div>
        
        <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 spinner = document.getElementById('spinner');
            const button = document.querySelector('button');
            
            // Show spinner, disable button
            spinner.style.display = 'block';
            button.disabled = true;
            
            try {
                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;
            } catch (error) {
                alert('An error occurred: ' + error.message);
            } finally {
                // Hide spinner, enable button
                spinner.style.display = 'none';
                button.disabled = false;
            }
        }
    </script>
</body>
</html>
"""

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)