Created
July 9, 2020 14:44
-
-
Save SebDeclercq/d141a8e43ec744b091c75409b04fa43a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import csv\n", | |
"from collections import defaultdict\n", | |
"from typing import DefaultDict, List, Tuple" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"HEADER: str = '''<!DOCTYPE html>\n", | |
" <html>\n", | |
" <head>\n", | |
" <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">\n", | |
" <title>Preface</title>\n", | |
" <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css\" integrity=\"sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j\" crossorigin=\"anonymous\">\n", | |
" <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css\">\n", | |
" <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css\">\n", | |
" <link href=\"https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.css\" rel=\"stylesheet\" type=\"text/css\">\n", | |
" <style>\n", | |
".task-list-item { list-style-type: none; } .task-list-item-checkbox { margin-left: -20px; vertical-align: middle; }\n", | |
"</style>\n", | |
" <style>\n", | |
" body {\n", | |
" font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', 'Ubuntu', 'Droid Sans', sans-serif;\n", | |
" font-size: 14px;\n", | |
" line-height: 1.6;\n", | |
" }\n", | |
" </style>\n", | |
" \n", | |
" <script src=\"https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.js\"></script>\n", | |
" </head>\n", | |
" <body>'''\n", | |
"\n", | |
"FOOTER: str = '''\n", | |
" </body>\n", | |
" </html>'''\n", | |
"\n", | |
"TITLE: str = 'Extreme Programming Explained: Embrace Change, Second Edition'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"highlights: Dict[str, Tuple[str, List[str]]] = {}\n", | |
"with open('safari-annotations-export.csv', encoding='utf8') as file:\n", | |
" reader: csv.DictReader = csv.DictReader(file)\n", | |
" for record in reader:\n", | |
" title: str = record['Book Title']\n", | |
" chapter: str = record['Chapter Title']\n", | |
" highlight: str = record['Highlight']\n", | |
" if title == TITLE:\n", | |
" if title not in highlights:\n", | |
" highlights[title] = [(chapter, [])]\n", | |
" if highlights[title][-1][0] != chapter:\n", | |
" highlights[title].append((chapter, [],))\n", | |
" highlights[title][-1][1].append(highlight)\n", | |
"\n", | |
"fname: str = f'{\"Extreme Programming Explained\".replace(\" \", \"-\")}.html'\n", | |
"with open(fname, 'w', encoding='utf8') as file:\n", | |
" file.write(HEADER)\n", | |
" for title, chapter_highlights in highlights.items():\n", | |
" file.write(f'<h1>{title}</h1>\\n')\n", | |
" for chapter, highlights_ in chapter_highlights[::-1]:\n", | |
" file.write(f'<h2>{chapter}</h2>\\n')\n", | |
" file.write('<ul>\\n')\n", | |
" for highlight in highlights_:\n", | |
" file.write('<li>\\n<blockquote>\\n')\n", | |
" fmt_highlight = ''.join([f'<p>{line}</p>\\n' for line in highlight.split('\\n')])\n", | |
" file.write(fmt_highlight)\n", | |
" file.write('</blockquote>\\n</li>\\n')\n", | |
" file.write('</ul>\\n')\n", | |
" file.write(FOOTER)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment