broadfield-dev commited on
Commit
cbc73b6
Β·
verified Β·
1 Parent(s): 8d30f92

Update app_logic.py

Browse files
Files changed (1) hide show
  1. app_logic.py +40 -49
app_logic.py CHANGED
@@ -62,8 +62,6 @@ def load_token_from_image_and_set_env(image_pil_object: Image.Image, password: s
62
 
63
 
64
 
65
- import re
66
-
67
  def process_commented_markdown(commented_input):
68
  """Process a commented markdown string by stripping '# ' from lines where present if '# # Space:' is detected."""
69
  # Check for '# # Space:' or '# Space:' to identify a space definition
@@ -78,7 +76,6 @@ def process_commented_markdown(commented_input):
78
  # Detect code block boundaries (handle '# ```' or '```')
79
  if line.strip() in ("# ```", "```"):
80
  in_code_block = not in_code_block
81
- # Preserve backticks as-is (strip '# ' if present)
82
  cleaned_line = line.lstrip("# ").rstrip()
83
  cleaned_lines.append(cleaned_line)
84
  continue
@@ -94,36 +91,34 @@ def process_commented_markdown(commented_input):
94
 
95
  return "\n".join(cleaned_lines)
96
 
 
 
97
  def parse_markdown(markdown_input):
98
  """Parse markdown input to extract space details and file structure."""
99
- # Preprocess to remove comments
100
  cleaned_markdown = process_commented_markdown(markdown_input)
101
- space_info = {"repo_name": "", "owner": "", "files": []}
102
  current_file = None
103
  file_content = []
104
  in_file_content = False
105
  in_code_block = False
106
- language = None
107
 
108
  lines = cleaned_markdown.strip().split("\n")
109
- i = 0
110
- while i < len(lines):
111
- line = lines[i].strip()
112
 
113
  # Extract space name
114
  if line.startswith("# Space:"):
115
  space_info["repo_name"] = line.replace("# Space:", "").strip()
116
  if "/" in space_info["repo_name"]:
117
  space_info["owner"], space_info["repo_name"] = space_info["repo_name"].split("/", 1)
118
- i += 1
119
  continue
120
 
121
  # Skip file structure header
122
  if line.startswith("## File Structure"):
123
- i += 1
124
  continue
125
 
126
- # Parse file structure
127
  if line.startswith("πŸ“„") or line.startswith("πŸ“"):
128
  if current_file and file_content and in_file_content:
129
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
@@ -131,11 +126,9 @@ def parse_markdown(markdown_input):
131
  current_file = line[2:].strip()
132
  in_file_content = False
133
  in_code_block = False
134
- language = None
135
- i += 1
136
  continue
137
 
138
- # Start of a new file content section
139
  if line.startswith("### File:"):
140
  if current_file and file_content and in_file_content:
141
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
@@ -143,42 +136,40 @@ def parse_markdown(markdown_input):
143
  current_file = line.replace("### File:", "").strip()
144
  in_file_content = True
145
  in_code_block = False
146
- language = None
147
- i += 1
148
-
149
- # Handle content (code block or plain text)
150
- if i < len(lines) and lines[i].strip() == "```":
151
- in_code_block = True
152
- i += 1
153
- # Check for language identifier
154
- if i < len(lines) and lines[i].strip() and not lines[i].strip().startswith("```"):
155
- language = lines[i].strip()
156
- i += 1
157
- # Collect content until closing backtick
158
- while i < len(lines) and lines[i].strip() != "```":
159
- file_content.append(lines[i].rstrip())
160
- i += 1
161
- if i < len(lines) and lines[i].strip() == "```":
162
- i += 1
163
- in_code_block = False
164
- in_file_content = False
 
 
 
165
  else:
166
  # Handle non-code-block content (e.g., binary file)
167
- while i < len(lines) and not (
168
- lines[i].strip().startswith("### File:")
169
- or lines[i].strip().startswith("#")
170
- or lines[i].strip().startswith("##")
171
- or lines[i].strip() == "```"
172
- ):
173
- if lines[i].strip():
174
- file_content.append(lines[i].rstrip())
175
- i += 1
176
- in_file_content = False
177
-
178
- else:
179
- i += 1
180
-
181
- # Save the last file if present
182
  if current_file and file_content and in_file_content:
183
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
184
 
 
62
 
63
 
64
 
 
 
65
  def process_commented_markdown(commented_input):
66
  """Process a commented markdown string by stripping '# ' from lines where present if '# # Space:' is detected."""
67
  # Check for '# # Space:' or '# Space:' to identify a space definition
 
76
  # Detect code block boundaries (handle '# ```' or '```')
77
  if line.strip() in ("# ```", "```"):
78
  in_code_block = not in_code_block
 
79
  cleaned_line = line.lstrip("# ").rstrip()
80
  cleaned_lines.append(cleaned_line)
81
  continue
 
91
 
92
  return "\n".join(cleaned_lines)
93
 
94
+
95
+
96
  def parse_markdown(markdown_input):
97
  """Parse markdown input to extract space details and file structure."""
98
+ # Preprocess to handle commented markdown
99
  cleaned_markdown = process_commented_markdown(markdown_input)
100
+ space_info = {"repo_name": "", "owner": "", "sdk": "gradio", "files": []}
101
  current_file = None
102
  file_content = []
103
  in_file_content = False
104
  in_code_block = False
 
105
 
106
  lines = cleaned_markdown.strip().split("\n")
107
+ for line in lines:
108
+ line = line.strip()
 
109
 
110
  # Extract space name
111
  if line.startswith("# Space:"):
112
  space_info["repo_name"] = line.replace("# Space:", "").strip()
113
  if "/" in space_info["repo_name"]:
114
  space_info["owner"], space_info["repo_name"] = space_info["repo_name"].split("/", 1)
 
115
  continue
116
 
117
  # Skip file structure header
118
  if line.startswith("## File Structure"):
 
119
  continue
120
 
121
+ # Detect file in structure
122
  if line.startswith("πŸ“„") or line.startswith("πŸ“"):
123
  if current_file and file_content and in_file_content:
124
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
 
126
  current_file = line[2:].strip()
127
  in_file_content = False
128
  in_code_block = False
 
 
129
  continue
130
 
131
+ # Detect file content section
132
  if line.startswith("### File:"):
133
  if current_file and file_content and in_file_content:
134
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
 
136
  current_file = line.replace("### File:", "").strip()
137
  in_file_content = True
138
  in_code_block = False
139
+ continue
140
+
141
+ # Handle code block boundaries
142
+ if in_file_content and line == "```":
143
+ in_code_block = not in_code_block
144
+ if in_code_block:
145
+ file_content = [] # Start fresh for code block content
146
+ else:
147
+ # End of code block, save content if any
148
+ if current_file and file_content:
149
+ space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
150
+ file_content = []
151
+ in_file_content = False
152
+ continue
153
+
154
+ # Collect content
155
+ if in_file_content:
156
+ if in_code_block:
157
+ # Skip language identifier (first non-empty line after opening backtick)
158
+ if not file_content and line and not line.startswith("```"):
159
+ continue # Skip language line (e.g., `md`, `py`)
160
+ file_content.append(line)
161
  else:
162
  # Handle non-code-block content (e.g., binary file)
163
+ if line:
164
+ file_content.append(line)
165
+ else:
166
+ # End non-code-block content at empty line or next section
167
+ if current_file and file_content:
168
+ space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
169
+ file_content = []
170
+ in_file_content = False
171
+
172
+ # Append the last file
 
 
 
 
 
173
  if current_file and file_content and in_file_content:
174
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
175