main.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import sys
  2. import os
  3. from bs4 import BeautifulSoup, NavigableString
  4. def sanitize_filename(title: str) -> str:
  5. return "".join(c if c.isalnum() or c in (' ', '-', '_') else '_' for c in title).strip()[:100]
  6. def get_text_with_formatting(element) -> str:
  7. def walk(node):
  8. if isinstance(node, NavigableString):
  9. return str(node)
  10. elif node.name in ("strong", "b"):
  11. return f"**{''.join(walk(c) for c in node.children)}**"
  12. elif node.name in ("em", "i"):
  13. return f"*{''.join(walk(c) for c in node.children)}*"
  14. elif node.name == "code":
  15. return f"`{''.join(walk(c) for c in node.children)}`"
  16. elif node.name == "a":
  17. href = node.get("href", "#")
  18. label = ''.join(walk(c) for c in node.children)
  19. return f"[{label}]({href})"
  20. elif node.name == "img":
  21. alt = node.get("alt", "")
  22. src = node.get("src", "")
  23. return f"![{alt}]({src})"
  24. else:
  25. return ''.join(walk(c) for c in node.children)
  26. return walk(element).strip()
  27. return "".join(result).strip()
  28. def extract_markdown_from_prose(prose: BeautifulSoup) -> str:
  29. md_lines = []
  30. for element in prose.children:
  31. if isinstance(element, NavigableString):
  32. text = element.strip()
  33. if text:
  34. md_lines.append(text)
  35. continue
  36. tag = element.name
  37. # Headings
  38. if tag.startswith("h") and tag[1:].isdigit():
  39. level = int(tag[1:])
  40. md_lines.append(f"{'#' * level} {get_text_with_formatting(element)}")
  41. # Paragraphs with possible links or inline formatting
  42. elif tag == "p":
  43. for link in element.find_all("a"):
  44. href = link.get("href", "#")
  45. link_text = link.get_text(strip=True)
  46. link.replace_with(f"[{link_text}]({href})")
  47. for code in element.find_all("code"):
  48. code_text = code.get_text()
  49. code.replace_with(f"`{code_text}`")
  50. md_lines.append(get_text_with_formatting(element))
  51. # Preformatted code blocks
  52. elif tag == "pre":
  53. code = element.find("code")
  54. if code:
  55. lang_class = code.get("class", [])
  56. language = ""
  57. for cls in lang_class:
  58. if cls.startswith("language-"):
  59. language = cls.replace("language-", "")
  60. break
  61. code_text = code.get_text()
  62. md_lines.append(f"```{language}\n{code_text.strip()}\n```")
  63. # Images
  64. elif tag == "img":
  65. src = element.get("src", "")
  66. alt = element.get("alt", "")
  67. if src:
  68. md_lines.append(f"![{alt}]({src})")
  69. # Inline code
  70. elif tag == "code":
  71. code_text = get_text_with_formatting(element)
  72. md_lines.append(f"`{code_text}`")
  73. # Fallback
  74. else:
  75. text = get_text_with_formatting(element)
  76. if text:
  77. md_lines.append(text)
  78. return "\n\n".join(md_lines)
  79. def convert_chat_html_to_markdown(html_path: str) -> str:
  80. with open(html_path, "r", encoding="utf-8") as f:
  81. soup = BeautifulSoup(f, "html.parser")
  82. title = soup.title.string.strip() if soup.title else "chatgpt_conversation"
  83. filename = sanitize_filename(title) + ".md"
  84. main = soup.find("main")
  85. if not main:
  86. raise ValueError("Could not find <main> in HTML. Is this a valid saved ChatGPT conversation?")
  87. h3_tags = main.find_all("h3")
  88. prose_blocks = main.find_all("div", class_="prose")
  89. messages = []
  90. for h3, prose in zip(h3_tags, prose_blocks):
  91. role = h3.get_text(strip=True)
  92. if "chatgpt" in role.lower():
  93. prefix = "**ChatGPT:**"
  94. else:
  95. prefix = "**You:**"
  96. body = extract_markdown_from_prose(prose).strip()
  97. if not body:
  98. continue
  99. messages.append(f"{prefix}\n\n{body}")
  100. markdown = f"# {title}\n\n" + "\n\n---\n\n".join(messages)
  101. return filename, markdown
  102. if __name__ == "__main__":
  103. if len(sys.argv) != 2:
  104. print("Usage: python html_to_markdown.py <path_to_saved_html>")
  105. sys.exit(1)
  106. input_html = sys.argv[1]
  107. if not os.path.isfile(input_html):
  108. print(f"File not found: {input_html}")
  109. sys.exit(1)
  110. output_name, markdown_text = convert_chat_html_to_markdown(input_html)
  111. os.makedirs("output", exist_ok=True)
  112. output_path = os.path.join("output", output_name)
  113. with open(output_path, "w", encoding="utf-8") as f:
  114. f.write(markdown_text)
  115. print(f"Markdown saved to: {output_path}")