mattrguerra/meridian-skillsmainPrivate on GitHub, published here in full

research_to_bbcode.py

Language Python2.1 KB63 lines

skills/partner-pipeline-push/scripts/research_to_bbcode.py

  1. #!/usr/bin/env python3
  2. """research_to_bbcode.py: convert a client stub's `## Research` block into a
  3. Bitrix timeline-comment BBCode body for add_deal_comment.
  4. Stubs live flat in `03-Clients/client-stubs/`, one .md per client.
  5. Usage:
  6. python3 research_to_bbcode.py --stub "/path/to/03-Clients/client-stubs/Stub.md" \
  7. --title "First Last"
  8. Output (stdout): BBCode with a bold title/provenance line, then each `###`
  9. subsection as a [B]HEADER[/B] followed by a [LIST] of its lines. Bare URLs are
  10. left as-is (Bitrix auto-links them). `[TBD: ...]` markers pass through literally.
  11. """
  12. import argparse, re
  13. def get_research(text):
  14. m = re.search(r'(?m)^## Research\b(.*?)(?=^## )', text, re.S)
  15. return m.group(1) if m else ''
  16. def to_bbcode(research, title):
  17. body = re.sub(r'<!--.*?-->', '', research, flags=re.S)
  18. secs, cur, items = [], None, []
  19. def flush():
  20. if cur is not None:
  21. block = "[B]" + cur.upper() + "[/B]"
  22. if items:
  23. block += "\n[LIST]\n" + "\n".join("[*]" + i for i in items) + "\n[/LIST]"
  24. secs.append(block)
  25. for line in body.splitlines():
  26. s = line.strip()
  27. if not s:
  28. continue
  29. if s.startswith('###'):
  30. flush(); cur = s.lstrip('#').strip(); items = []
  31. else:
  32. items.append(re.sub(r'^[-*]\s+', '', s))
  33. flush()
  34. head = ("[B]CLIENT ENRICHMENT PROFILE: " + title + "[/B]\n"
  35. "Source: Apollo + Apify (LinkedIn) + web. Pulled from the Meridian vault stub.")
  36. return head + "\n\n" + "\n\n".join(secs)
  37. def main():
  38. ap = argparse.ArgumentParser()
  39. ap.add_argument("--stub", required=True,
  40. help="path to a stub in 03-Clients/client-stubs/")
  41. ap.add_argument("--title", required=True)
  42. a = ap.parse_args()
  43. with open(a.stub, encoding='utf-8') as fh:
  44. text = fh.read()
  45. r = get_research(text)
  46. if not r.strip():
  47. raise SystemExit("ERROR: no '## Research' content found in " + a.stub)
  48. print(to_bbcode(r, a.title))
  49. if __name__ == "__main__":
  50. main()