bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / utils / bsc_md / parsers.py

parsers.py (3966B)


      1 def read_block_comment(lines, start):
      2     result = []
      3     i = start
      4     n = len(lines)
      5     first = lines[i].strip()
      6 
      7     if first.startswith('/*') and '*/' in first:
      8         inner = first[2: first.index('*/')].strip().strip('*').strip()
      9         if inner:
     10             result.append(inner)
     11         return result, i + 1
     12 
     13     inner = first[2:].strip().strip('*').strip()
     14     if inner:
     15         result.append(inner)
     16     i += 1
     17 
     18     while i < n:
     19         line = lines[i].strip()
     20         if '*/' in line:
     21             text = line[: line.index('*/')].strip().strip('*').strip()
     22             if text:
     23                 result.append(text)
     24             return result, i + 1
     25         text = line.strip('*').strip()
     26         if text:
     27             result.append(text)
     28         i += 1
     29 
     30     return result, i
     31 
     32 
     33 def parse_c_style(content):
     34     lines = content.splitlines()
     35     n = len(lines)
     36     i = 0
     37     problem_statement = ""
     38 
     39     while i < n and not lines[i].strip():
     40         i += 1
     41 
     42     # Extract problem statement block (ensures it's not the actual code starting)
     43     if i < n and lines[i].strip().startswith('/*'):
     44         peek_block, peek_i = read_block_comment(lines, i)
     45         block_text = ' '.join(peek_block)
     46         if '#include' not in block_text and 'import ' not in block_text:
     47             problem_statement = ' '.join(p for p in peek_block if p).strip()
     48             i = peek_i
     49 
     50     # Locate the beginning of actual source code
     51     code_start = None
     52     for j in list(range(i, n)) + list(range(0, i)):
     53         line_strip = lines[j].strip()
     54         if line_strip.startswith('#include') or line_strip.startswith('import '):
     55             code_start = j
     56             break
     57 
     58     code = '\n'.join(lines[code_start:]).strip() if code_start is not None else content.strip()
     59     return problem_statement, code
     60 
     61 
     62 def parse_hash_style(content):
     63     lines = content.splitlines()
     64     n = len(lines)
     65     i = 0
     66     problem_statement = ""
     67 
     68     while i < n and not lines[i].strip():
     69         i += 1
     70 
     71     # Extract problem statement block
     72     ps_lines = []
     73     while i < n and lines[i].strip().startswith('#'):
     74         text = lines[i].strip()[1:].strip()
     75         if text:
     76             ps_lines.append(text)
     77         i += 1
     78 
     79     if ps_lines:
     80         problem_statement = ' '.join(ps_lines).strip()
     81 
     82     code = content.strip()
     83     return problem_statement, code
     84 
     85 
     86 def parse_algo_md(content):
     87     """Parse a GitHub-style algorithm .md file and convert to VitePress format."""
     88     lines = content.splitlines()
     89     title = ""
     90     problem_statement = ""
     91     body_lines = []
     92     i = 0
     93     n = len(lines)
     94 
     95     # Extract title from first # heading
     96     while i < n:
     97         line = lines[i]
     98         if line.startswith("# "):
     99             title = line[2:].strip()
    100             i += 1
    101             break
    102         i += 1
    103 
    104     # Parse rest: find problem statement in > blockquote under ### Problem Statement
    105     in_problem_section = False
    106     ps_lines = []
    107 
    108     while i < n:
    109         line = lines[i]
    110         stripped = line.strip()
    111 
    112         if stripped.lower().startswith("### problem statement"):
    113             in_problem_section = True
    114             i += 1
    115             continue
    116 
    117         if in_problem_section:
    118             # Allow blank lines between heading and blockquote
    119             if stripped == "":
    120                 i += 1
    121                 continue
    122             if stripped.startswith("> "):
    123                 ps_lines.append(stripped[2:].strip())
    124                 i += 1
    125                 continue
    126             elif stripped == ">":
    127                 i += 1
    128                 continue
    129             else:
    130                 # Non-blockquote, non-blank line ends problem section
    131                 in_problem_section = False
    132                 problem_statement = " ".join(ps_lines).strip()
    133                 body_lines.append(line)
    134         else:
    135             body_lines.append(line)
    136 
    137         i += 1
    138 
    139     if in_problem_section and ps_lines:
    140         problem_statement = " ".join(ps_lines).strip()
    141 
    142     return title, problem_statement, body_lines
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror