Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import markdown
|
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import os
|
| 7 |
import mimetypes
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
@@ -33,15 +34,23 @@ def process_file_content(file_info, owner, repo):
|
|
| 33 |
file_data = file_response.json()
|
| 34 |
|
| 35 |
if 'content' in file_data:
|
| 36 |
-
# Guess the file type
|
| 37 |
file_extension = file_path.split('.')[-1] if '.' in file_path else ''
|
| 38 |
mime_type, _ = mimetypes.guess_type(file_path)
|
| 39 |
-
is_text = mime_type and mime_type.startswith('text') or file_extension in ['py', 'md', 'txt', 'js', 'html', 'css']
|
| 40 |
|
| 41 |
if is_text:
|
| 42 |
try:
|
| 43 |
decoded_content = base64.b64decode(file_data['content']).decode('utf-8')
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except UnicodeDecodeError:
|
| 46 |
content = f"### File: {file_path}\n[Text content could not be decoded - possibly corrupted or non-UTF-8 text]\n\n"
|
| 47 |
else:
|
|
@@ -53,7 +62,7 @@ def create_markdown_document(repo_url):
|
|
| 53 |
"""Create markdown document from repo contents"""
|
| 54 |
owner, repo, contents = get_repo_contents(repo_url)
|
| 55 |
|
| 56 |
-
if isinstance(contents, str):
|
| 57 |
return f"Error: {contents}"
|
| 58 |
|
| 59 |
markdown_content = f"# Repository: {owner}/{repo}\n\n"
|
|
@@ -119,6 +128,20 @@ html_template = """
|
|
| 119 |
padding: 20px;
|
| 120 |
background-color: #f9f9f9;
|
| 121 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
</style>
|
| 123 |
</head>
|
| 124 |
<body>
|
|
@@ -127,6 +150,7 @@ html_template = """
|
|
| 127 |
<p>Enter a GitHub repository URL (e.g., https://github.com/username/repository)</p>
|
| 128 |
<input type="text" id="repoUrl" style="width: 100%; padding: 8px;" placeholder="Enter GitHub repository URL">
|
| 129 |
<button onclick="processRepo()">Convert to Markdown</button>
|
|
|
|
| 130 |
|
| 131 |
<h2>Markdown Output:</h2>
|
| 132 |
<textarea id="markdownOutput" readonly></textarea>
|
|
@@ -138,23 +162,38 @@ html_template = """
|
|
| 138 |
<script>
|
| 139 |
async function processRepo() {
|
| 140 |
const repoUrl = document.getElementById('repoUrl').value;
|
| 141 |
-
const
|
| 142 |
-
|
| 143 |
-
headers: {
|
| 144 |
-
'Content-Type': 'application/json',
|
| 145 |
-
},
|
| 146 |
-
body: JSON.stringify({ repo_url: repoUrl })
|
| 147 |
-
});
|
| 148 |
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
-
|
| 156 |
-
document.getElementById('markdownOutput').value = data.markdown;
|
| 157 |
-
document.getElementById('output').innerHTML = data.html;
|
| 158 |
}
|
| 159 |
</script>
|
| 160 |
</body>
|
|
|
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import os
|
| 7 |
import mimetypes
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
|
|
|
| 34 |
file_data = file_response.json()
|
| 35 |
|
| 36 |
if 'content' in file_data:
|
|
|
|
| 37 |
file_extension = file_path.split('.')[-1] if '.' in file_path else ''
|
| 38 |
mime_type, _ = mimetypes.guess_type(file_path)
|
| 39 |
+
is_text = (mime_type and mime_type.startswith('text')) or file_extension in ['py', 'md', 'txt', 'js', 'html', 'css', 'json']
|
| 40 |
|
| 41 |
if is_text:
|
| 42 |
try:
|
| 43 |
decoded_content = base64.b64decode(file_data['content']).decode('utf-8')
|
| 44 |
+
# Special handling for JSON files
|
| 45 |
+
if file_extension == 'json':
|
| 46 |
+
try:
|
| 47 |
+
json_data = json.loads(decoded_content)
|
| 48 |
+
formatted_json = json.dumps(json_data, indent=2)
|
| 49 |
+
content = f"### File: {file_path}\n```json\n{formatted_json}\n```\n\n"
|
| 50 |
+
except json.JSONDecodeError:
|
| 51 |
+
content = f"### File: {file_path}\n```json\n{decoded_content}\n```\n[Note: Invalid JSON format]\n\n"
|
| 52 |
+
else:
|
| 53 |
+
content = f"### File: {file_path}\n```{(file_extension if file_extension else 'text')}\n{decoded_content}\n```\n\n"
|
| 54 |
except UnicodeDecodeError:
|
| 55 |
content = f"### File: {file_path}\n[Text content could not be decoded - possibly corrupted or non-UTF-8 text]\n\n"
|
| 56 |
else:
|
|
|
|
| 62 |
"""Create markdown document from repo contents"""
|
| 63 |
owner, repo, contents = get_repo_contents(repo_url)
|
| 64 |
|
| 65 |
+
if isinstance(contents, str):
|
| 66 |
return f"Error: {contents}"
|
| 67 |
|
| 68 |
markdown_content = f"# Repository: {owner}/{repo}\n\n"
|
|
|
|
| 128 |
padding: 20px;
|
| 129 |
background-color: #f9f9f9;
|
| 130 |
}
|
| 131 |
+
.spinner {
|
| 132 |
+
display: none;
|
| 133 |
+
border: 4px solid #f3f3f3;
|
| 134 |
+
border-top: 4px solid #3498db;
|
| 135 |
+
border-radius: 50%;
|
| 136 |
+
width: 30px;
|
| 137 |
+
height: 30px;
|
| 138 |
+
animation: spin 1s linear infinite;
|
| 139 |
+
margin: 20px auto;
|
| 140 |
+
}
|
| 141 |
+
@keyframes spin {
|
| 142 |
+
0% { transform: rotate(0deg); }
|
| 143 |
+
100% { transform: rotate(360deg); }
|
| 144 |
+
}
|
| 145 |
</style>
|
| 146 |
</head>
|
| 147 |
<body>
|
|
|
|
| 150 |
<p>Enter a GitHub repository URL (e.g., https://github.com/username/repository)</p>
|
| 151 |
<input type="text" id="repoUrl" style="width: 100%; padding: 8px;" placeholder="Enter GitHub repository URL">
|
| 152 |
<button onclick="processRepo()">Convert to Markdown</button>
|
| 153 |
+
<div id="spinner" class="spinner"></div>
|
| 154 |
|
| 155 |
<h2>Markdown Output:</h2>
|
| 156 |
<textarea id="markdownOutput" readonly></textarea>
|
|
|
|
| 162 |
<script>
|
| 163 |
async function processRepo() {
|
| 164 |
const repoUrl = document.getElementById('repoUrl').value;
|
| 165 |
+
const spinner = document.getElementById('spinner');
|
| 166 |
+
const button = document.querySelector('button');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
// Show spinner, disable button
|
| 169 |
+
spinner.style.display = 'block';
|
| 170 |
+
button.disabled = true;
|
| 171 |
|
| 172 |
+
try {
|
| 173 |
+
const response = await fetch('/process', {
|
| 174 |
+
method: 'POST',
|
| 175 |
+
headers: {
|
| 176 |
+
'Content-Type': 'application/json',
|
| 177 |
+
},
|
| 178 |
+
body: JSON.stringify({ repo_url: repoUrl })
|
| 179 |
+
});
|
| 180 |
+
|
| 181 |
+
const data = await response.json();
|
| 182 |
+
|
| 183 |
+
if (data.error) {
|
| 184 |
+
alert(data.error);
|
| 185 |
+
return;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
document.getElementById('markdownOutput').value = data.markdown;
|
| 189 |
+
document.getElementById('output').innerHTML = data.html;
|
| 190 |
+
} catch (error) {
|
| 191 |
+
alert('An error occurred: ' + error.message);
|
| 192 |
+
} finally {
|
| 193 |
+
// Hide spinner, enable button
|
| 194 |
+
spinner.style.display = 'none';
|
| 195 |
+
button.disabled = false;
|
| 196 |
}
|
|
|
|
|
|
|
|
|
|
| 197 |
}
|
| 198 |
</script>
|
| 199 |
</body>
|