om4r932 commited on
Commit
f0a2998
·
1 Parent(s): 28face5

Edit endpoint

Browse files
Files changed (2) hide show
  1. app.py +52 -1
  2. static/script.js +1 -1
app.py CHANGED
@@ -244,7 +244,58 @@ class SpecRequest(BaseModel):
244
  def main_page():
245
  return FileResponse(os.path.join("templates", "index.html"))
246
 
247
- @app.post("/from-search")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  def get_file_from_spec_id_version(req: SpecRequest) -> Dict:
249
  spec = req.specification
250
  version = req.version
 
244
  def main_page():
245
  return FileResponse(os.path.join("templates", "index.html"))
246
 
247
+ @app.post("/online/plain")
248
+ def get_file_from_spec_id_version(req: SpecRequest) -> Dict[str, str]:
249
+ spec = req.specification
250
+ version = req.version
251
+ if not version:
252
+ version = get_latest_version(spec)
253
+
254
+ text = get_text(spec, version)
255
+ forewords = []
256
+ for x in range(len(text)):
257
+ line = text[x]
258
+ if "Foreword" in line:
259
+ forewords.append(x)
260
+ if len(forewords) >= 2:
261
+ break
262
+
263
+ toc_brut = text[forewords[0]:forewords[1]]
264
+ chapters = []
265
+ for line in toc_brut:
266
+ x = line.split("\t")
267
+ if re.fullmatch(r"^\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
268
+ chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
269
+ if re.fullmatch(r"^\d\.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
270
+ chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
271
+ if re.fullmatch(r"^\d\.\d\.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
272
+ chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
273
+ if re.fullmatch(r"^\d\.\d\.\d.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
274
+ chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
275
+ if re.fullmatch(r"^\d\.\d\.\d.\d.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
276
+ chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
277
+
278
+ real_toc_indexes = {}
279
+
280
+ for chapter in chapters:
281
+ try:
282
+ x = text.index(chapter)
283
+ real_toc_indexes[chapter] = x
284
+ except ValueError as e:
285
+ real_toc_indexes[chapter] = -float("inf")
286
+
287
+ document = {}
288
+ toc = list(real_toc_indexes.keys())
289
+ index_toc = list(real_toc_indexes.values())
290
+ curr_index = 0
291
+ for x in range(1, len(toc)):
292
+ document[toc[curr_index].replace("\t", " ")] = re.sub(r"[\ \t]+", " ", "\n".join(text[index_toc[curr_index]+1:index_toc[x]]))
293
+ curr_index = x
294
+
295
+ document[toc[curr_index].replace("\t"," ")] = re.sub(r"\s+", " ", " ".join(text[index_toc[curr_index]+1:]))
296
+ return document
297
+
298
+ @app.post("/online")
299
  def get_file_from_spec_id_version(req: SpecRequest) -> Dict:
300
  spec = req.specification
301
  version = req.version
static/script.js CHANGED
@@ -27,7 +27,7 @@ function handleSpecSearch(){
27
  body["version"] = document.getElementById("versionInput").value;
28
  }
29
 
30
- fetch("/from-search", {
31
  method: "POST",
32
  headers: {"Content-Type": "application/json"},
33
  body: JSON.stringify(body)
 
27
  body["version"] = document.getElementById("versionInput").value;
28
  }
29
 
30
+ fetch("/online", {
31
  method: "POST",
32
  headers: {"Content-Type": "application/json"},
33
  body: JSON.stringify(body)