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

Update app_logic.py

Browse files
Files changed (1) hide show
  1. app_logic.py +56 -19
app_logic.py CHANGED
@@ -75,7 +75,7 @@ def process_commented_markdown(commented_input):
75
  in_code_block = False
76
 
77
  for line in lines:
78
- # Detect code block boundaries (handle both '# ```' and '```')
79
  if line.strip() in ("# ```", "```"):
80
  in_code_block = not in_code_block
81
  # Preserve backticks as-is (strip '# ' if present)
@@ -86,7 +86,7 @@ def process_commented_markdown(commented_input):
86
  # Strip '# ' from lines that have it
87
  cleaned_line = line.lstrip("# ")
88
 
89
- # In code blocks, remove additional '# ' if present (handles Gemini's over-commenting)
90
  if in_code_block and cleaned_line.startswith("# "):
91
  cleaned_line = cleaned_line[2:]
92
 
@@ -94,7 +94,6 @@ def process_commented_markdown(commented_input):
94
 
95
  return "\n".join(cleaned_lines)
96
 
97
- # --- `parse_markdown` (Unchanged from previous corrected version) ---
98
  def parse_markdown(markdown_input):
99
  """Parse markdown input to extract space details and file structure."""
100
  # Preprocess to remove comments
@@ -104,44 +103,82 @@ def parse_markdown(markdown_input):
104
  file_content = []
105
  in_file_content = False
106
  in_code_block = False
 
107
 
108
  lines = cleaned_markdown.strip().split("\n")
109
- for line in lines:
110
- line = line.strip()
 
 
 
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
- elif line.startswith("## File Structure"):
 
 
 
 
 
116
  continue
117
- elif line.startswith("πŸ“„") or line.startswith("πŸ“"):
 
 
118
  if current_file and file_content and in_file_content:
119
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
120
  file_content = []
121
  current_file = line[2:].strip()
122
  in_file_content = False
123
  in_code_block = False
124
- elif line.startswith("### File:"):
 
 
 
 
 
125
  if current_file and file_content and in_file_content:
126
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
127
  file_content = []
128
  current_file = line.replace("### File:", "").strip()
129
  in_file_content = True
130
  in_code_block = False
131
- elif in_file_content and line.startswith("```"):
132
- in_code_block = not in_code_block
133
- if in_code_block:
134
- # First line after ``` is the language
135
- file_content = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  else:
137
- # End of code block, store file content
138
- if current_file and file_content:
139
- space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
140
- file_content = []
 
 
 
 
 
 
141
  in_file_content = False
142
- elif in_file_content and in_code_block:
143
- file_content.append(line)
144
 
 
 
 
 
145
  if current_file and file_content and in_file_content:
146
  space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
147
 
 
75
  in_code_block = False
76
 
77
  for line in lines:
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)
 
86
  # Strip '# ' from lines that have it
87
  cleaned_line = line.lstrip("# ")
88
 
89
+ # In code blocks, remove additional '# ' if present (handles over-commenting)
90
  if in_code_block and cleaned_line.startswith("# "):
91
  cleaned_line = cleaned_line[2:]
92
 
 
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
 
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)})
130
  file_content = []
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)})
142
  file_content = []
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