builders.py (2536B)
1 from .config import ALGO_ICON_SVG, FILE_ICON_SVG 2 from .formatting import esc_yaml, esc_html, format_tags_yaml 3 4 5 def build_algo_md(filename_base, title, problem_statement, body_lines, source_path="", tags=None): 6 """Build VitePress .md from parsed algo content.""" 7 desc = problem_statement if problem_statement else f"Algorithm — {title}" 8 9 fm = [ 10 "---", 11 f"title: '{ALGO_ICON_SVG} {esc_yaml(title)}'", 12 f"description: '{esc_yaml(desc)}'", 13 f"source: '{source_path}'", 14 format_tags_yaml(tags or []), 15 "---", 16 "", 17 ] 18 19 body = [f"# {title}", ""] 20 21 if problem_statement: 22 body += [ 23 "### Problem Statement", 24 "", 25 "::: tip Problem Statement", 26 esc_html(problem_statement), 27 ":::", 28 "", 29 ] 30 31 # Replace > blockquote problem section in body with nothing (already handled above) 32 # Just append the remaining body lines (Algorithm, Pseudocode, Complexity etc.) 33 cleaned = [] 34 blines = body_lines[:] 35 j = 0 36 while j < len(blines): 37 l = blines[j] 38 s = l.strip() 39 if s.lower().startswith("### problem statement"): 40 # skip until blockquote ends 41 j += 1 42 while j < len(blines) and (blines[j].strip().startswith(">") or blines[j].strip() == ""): 43 j += 1 44 continue 45 cleaned.append(l) 46 j += 1 47 48 # Remove leading blank lines from cleaned 49 while cleaned and not cleaned[0].strip(): 50 cleaned.pop(0) 51 52 body += cleaned 53 54 return "\n".join(fm + body) 55 56 57 def build_md(filename, lang_label, fence_lang, 58 problem_statement, code, rel_url, tags=None): 59 60 desc = problem_statement if problem_statement else f"{lang_label} program — {filename}" 61 62 fm_lines = [ 63 "---", 64 f"title: '{FILE_ICON_SVG} {esc_yaml(filename)}'", 65 f"description: '{esc_yaml(desc)}'", 66 f"source: '{rel_url}'", 67 format_tags_yaml(tags or []), 68 "---", 69 ] 70 71 body = [ 72 "", 73 f"# {filename}", 74 "" 75 ] 76 77 if problem_statement: 78 body += [ 79 "", 80 "### Problem Statement", 81 "", 82 f"::: tip {filename}", 83 esc_html(problem_statement), 84 ":::", 85 "", 86 ] 87 88 body += [ 89 "## Source Code", 90 "", 91 f"```{fence_lang} [{filename}]", 92 code, 93 "```", 94 "---", 95 "", 96 ] 97 98 return '\n'.join(fm_lines + body)