Skip to content

Instantly share code, notes, and snippets.

@prasoon2211
Last active February 12, 2025 13:00
Show Gist options
  • Save prasoon2211/949c4f9b18dc77da7f93315f1b9b9000 to your computer and use it in GitHub Desktop.
Save prasoon2211/949c4f9b18dc77da7f93315f1b9b9000 to your computer and use it in GitHub Desktop.
Convert OpenAI Deep Research output to look nice for pasting in Google Docs
# /// script
# dependencies = [
# "pyperclip",
# ]
# ///
########
# README
# OpenAI Deep Research is great. When you copy from the Chat into, say, a Google Doc,
# the citations are all messed up (either in place or no citations if you select + copy)
# This script fixes that
#
# INSTALLATION AND USAGE INSTRUCTIONS:
# $ pip install pyperclip
# $ brew install pandoc
#
# Hit the copy button at the end of the deep research output
# Run this script: `$ python mdcite.py`
#
# This will make a temp HTML file and open it in your current browser
# You can then copy / paste the contents into a google doc
import pyperclip
import re
import tempfile
import subprocess
import webbrowser
import os
def process_markdown():
# Get markdown from clipboard
markdown = pyperclip.paste()
# Add two spaces at the end of each line that's part of a list
# This ensures proper line breaks in the HTML output
lines = markdown.split("\n")
for i in range(len(lines)):
if re.match(r"^\s*[-*+]\s", lines[i]) or re.match(r"^\s*\d+\.\s", lines[i]):
lines[i] = lines[i].rstrip() + " "
markdown = "\n".join(lines)
# Dictionary to store unique citations and their numbers
citations = {}
citation_count = 1
def replace_citation(match):
nonlocal citation_count
citation_text = match.group(1).strip()
citation_link = match.group(2).strip()
citation_key = (citation_text, citation_link)
# If this citation hasn't been seen before, add it to our dictionary
if citation_key not in citations:
citations[citation_key] = citation_count
citation_count += 1
# Return a direct HTML link instead of a footnote reference
return f'<a href="{citation_link}" title="{citation_text}">[{citations[citation_key]}]</a>'
# Replace all citations in the text
pattern = r"\(\[(.*?)\]\(([^)]+)\)\)"
new_markdown = re.sub(pattern, replace_citation, markdown)
# Add the references section at the end
new_markdown += "\n\n---\n\n"
for citation_key, number in citations.items():
citation_text, citation_link = citation_key
# Add numbered references with both text and link
new_markdown += f"{number}. [{citation_text}]({citation_link}) \n"
# Copy the new markdown back to clipboard
pyperclip.copy(new_markdown)
print(f"Processed {len(citations)} citations.")
print("New markdown has been copied to clipboard.")
# Create a temporary markdown file
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as md_file:
md_file.write(new_markdown)
md_path = md_file.name
# Create a temporary HTML file path
html_path = md_path.replace(".md", ".html")
try:
# Convert markdown to HTML using pandoc with additional options
subprocess.run(
[
"pandoc",
"-f",
"markdown+lists_without_preceding_blankline",
"-t",
"html",
"--wrap=none",
"-o",
html_path,
md_path,
],
check=True,
)
# Open the HTML file in the default browser
webbrowser.open("file://" + html_path)
print(f"Preview opened in your browser.")
except subprocess.CalledProcessError:
print(
"Error: Failed to convert markdown to HTML. Please make sure pandoc is installed."
)
except Exception as e:
print(f"Error opening preview: {str(e)}")
finally:
# Clean up temporary files after a delay (giving browser time to load)
try:
os.unlink(md_path)
except:
pass
if __name__ == "__main__":
try:
process_markdown()
except Exception as e:
print(f"An error occurred: {str(e)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment