research_to_bbcode.py
Language Python2.1 KB63 lines
#!/usr/bin/env python3"""research_to_bbcode.py: convert a client stub's `## Research` block into aBitrix timeline-comment BBCode body for add_deal_comment.Stubs live flat in `03-Clients/client-stubs/`, one .md per client.Usage:python3 research_to_bbcode.py --stub "/path/to/03-Clients/client-stubs/Stub.md" \--title "First Last"Output (stdout): BBCode with a bold title/provenance line, then each `###`subsection as a [B]HEADER[/B] followed by a [LIST] of its lines. Bare URLs areleft as-is (Bitrix auto-links them). `[TBD: ...]` markers pass through literally."""import argparse, redef get_research(text):m = re.search(r'(?m)^## Research\b(.*?)(?=^## )', text, re.S)return m.group(1) if m else ''def to_bbcode(research, title):body = re.sub(r'<!--.*?-->', '', research, flags=re.S)secs, cur, items = [], None, []def flush():if cur is not None:block = "[B]" + cur.upper() + "[/B]"if items:block += "\n[LIST]\n" + "\n".join("[*]" + i for i in items) + "\n[/LIST]"secs.append(block)for line in body.splitlines():s = line.strip()if not s:continueif s.startswith('###'):flush(); cur = s.lstrip('#').strip(); items = []else:items.append(re.sub(r'^[-*]\s+', '', s))flush()head = ("[B]CLIENT ENRICHMENT PROFILE: " + title + "[/B]\n""Source: Apollo + Apify (LinkedIn) + web. Pulled from the Meridian vault stub.")return head + "\n\n" + "\n\n".join(secs)def main():ap = argparse.ArgumentParser()ap.add_argument("--stub", required=True,help="path to a stub in 03-Clients/client-stubs/")ap.add_argument("--title", required=True)a = ap.parse_args()with open(a.stub, encoding='utf-8') as fh:text = fh.read()r = get_research(text)if not r.strip():raise SystemExit("ERROR: no '## Research' content found in " + a.stub)print(to_bbcode(r, a.title))if __name__ == "__main__":main()