Skip to content

Move to a handrolled static site generator #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3a707ec
try to get a handrolled static site generator working
hjwp Mar 10, 2020
546d2c9
first cut of index.html with milligram
hjwp Mar 15, 2020
94298f1
up and running with milligram and pygments. done command handler blog
hjwp Mar 15, 2020
8e558d3
start on template inheritance structure
hjwp Mar 15, 2020
ce01de6
first stab at rendering index.html from index.md
hjwp Mar 15, 2020
6e7cae9
move stuff out of index.md into index.html
hjwp Mar 15, 2020
0656c02
Merge remote-tracking branch 'origin/master' into handrolled-static-s…
hjwp Mar 15, 2020
eae322f
continue moving stuff out of index.md
hjwp Mar 15, 2020
f0c6a59
more progress, looking ok tbh
hjwp Mar 15, 2020
6f2df32
move output dir to _site directory
hjwp Mar 16, 2020
4d1d1b1
rss too
hjwp Mar 16, 2020
3d4cf8b
add the html to version control
hjwp Mar 16, 2020
abf5f53
path to blog posts is /blog, match existing site
hjwp Mar 16, 2020
6a034ca
remove unused images folder
hjwp Mar 16, 2020
4dba4b7
remove old assets folder
hjwp Mar 16, 2020
4bce605
remove old jekyll config
hjwp Mar 16, 2020
c99755b
favicon. maybe
hjwp Mar 16, 2020
89a0ee5
makefile
hjwp Mar 16, 2020
97bdf18
update readme
hjwp Mar 16, 2020
cb28f68
add book as submodule
hjwp Mar 16, 2020
b284d61
add a script to copy the book sources across
hjwp Mar 16, 2020
b59bb68
book html
hjwp Mar 16, 2020
e879b41
book prepper mostly working, toc fixed
hjwp Mar 16, 2020
17a4cbd
rename book copier
hjwp Mar 16, 2020
1ecfef4
add buy-the-book banner
hjwp Mar 16, 2020
1aaee6a
mess about trying to style the front banner
hjwp Mar 16, 2020
6ff88f6
fix list styles
hjwp Mar 16, 2020
1f0eb65
add google analytics and disqus
hjwp Mar 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
book prepper mostly working, toc fixed
  • Loading branch information
hjwp committed Mar 16, 2020
commit e879b41fdbd6881c985f2814ddc360a9ab0d4d0a
6 changes: 3 additions & 3 deletions _site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ <h2>Simple patterns for building complex applications</h2>
<h1>The Book</h1>
<ul>
<li>
Read for free from the sources using Github previews:
<a href="https://github.com/cosmicpython/book#table-of-contents">github.com/cosmicpython/book</a>
Read for free
<a href="book/preface.html">on this site</a> (license: CC-By-NC-ND).
</li>

<li>
Read in online on O'Reilly Learning (aka Safari)
Read in online on O'Reilly Learning (aka Safari)
<a href="https://learning.oreilly.com/library/view/architecture-patterns-with/9781492052197/">learning.oreilly.com</a>
</li>

Expand Down
2 changes: 1 addition & 1 deletion _site/rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Simple patterns for building complex apps
</description>
<link>https://cosmicpython.com</link>
<lastBuildDate>Mon, 16 Mar 2020 10:44:20 -0000</lastBuildDate>
<lastBuildDate>Mon, 16 Mar 2020 13:15:11 -0000</lastBuildDate>
<pubDate>Sat, 4 Jan 2020 19:15:54 -0500</pubDate>
<atom:link href="https://cosmicpython.com/rss.xml" rel="self" type="application/rss+xml" />

Expand Down
6 changes: 3 additions & 3 deletions pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ <h2>Simple patterns for building complex applications</h2>
<h1>The Book</h1>
<ul>
<li>
Read for free from the sources using Github previews:
<a href="https://github.com/cosmicpython/book#table-of-contents">github.com/cosmicpython/book</a>
Read for free
<a href="book/preface.html">on this site</a> (license: CC-By-NC-ND).
</li>

<li>
Read in online on O'Reilly Learning (aka Safari)
Read in online on O'Reilly Learning (aka Safari)
<a href="https://learning.oreilly.com/library/view/architecture-patterns-with/9781492052197/">learning.oreilly.com</a>
</li>

Expand Down
44 changes: 25 additions & 19 deletions prep-book-html.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def get_anchor_targets(parsed_html):
class ChapterInfo:
href_id: str
chapter_title: str
old_title: str
subheaders: list
xrefs: list

Expand All @@ -63,12 +64,12 @@ def get_chapter_info():
href_id = parsed_html.cssselect('body')[0].get('id')
subheaders = [h.get('id') for h in parsed_html.cssselect('h3')]

chapter_title = header.text_content()
chapter_title = chapter_title.replace('Appendix A: ', '')
old_title = header.text_content()
chapter_title = old_title.replace('Appendix A: ', '')

if chapter.startswith('chapter_'):
chapter_no = chapter_numbers.pop(0)
chapter_title = f'Chapter {chapter_no}: {chapter_title}'
chapter_title = f'{chapter_no}: {chapter_title}'

if chapter.startswith('appendix_'):
appendix_no = appendix_numbers.pop(0)
Expand All @@ -82,7 +83,9 @@ def get_chapter_info():
chapter_title = f'Epilogue: {chapter_title}'

xrefs = get_anchor_targets(parsed_html)
chapter_info[chapter] = ChapterInfo(href_id, chapter_title, subheaders, xrefs)
chapter_info[chapter] = ChapterInfo(
href_id, chapter_title, old_title, subheaders, xrefs
)

return chapter_info

Expand Down Expand Up @@ -117,7 +120,6 @@ def copy_chapters_across_with_fixes(chapter_info, fixed_toc):
# comments_html = open('disqus_comments.html').read()
# buy_book_div = html.fromstring(open('buy_the_book_banner.html').read())
# analytics_div = html.fromstring(open('analytics.html').read())
# load_toc_script = open('load_toc.js').read()

for chapter in CHAPTERS:
old_contents = Path(f'book/{chapter}').read_text()
Expand All @@ -126,8 +128,6 @@ def copy_chapters_across_with_fixes(chapter_info, fixed_toc):
parsed = html.fromstring(new_contents)
body = parsed.cssselect('body')[0]
if header := parsed.cssselect('#header'):
# head = parsed.cssselect('head')[0]
# head.append(html.fragment_fromstring('<script>' + load_toc_script + '</script>'))
body.set('class', 'article toc2 toc-left')
header[0].append(fixed_toc)
# body.insert(0, buy_book_div)
Expand All @@ -150,19 +150,26 @@ def extract_toc_from_book():

def fix_toc(toc, chapter_info):
href_mappings = {}
title_mappings = {}
for chapter in CHAPTERS:
chap = chapter_info[chapter]
if chap.href_id:
href_mappings['#' + chap.href_id] = f'/book/{chapter}'
for subheader in chap.subheaders:
chapinfo = chapter_info[chapter]
if chapinfo.href_id:
href_mappings['#' + chapinfo.href_id] = f'/book/{chapter}'
for subheader in chapinfo.subheaders:
href_mappings['#' + subheader] = f'/book/{chapter}#{subheader}'

def fix_link(href):
if href in href_mappings:
return href_mappings[href]
return href

toc.rewrite_links(fix_link)
title_mappings[chapinfo.old_title] = chapinfo.chapter_title

for (el, attr, link, pos) in toc.iterlinks():
print(el.text, attr, link, pos)
el.set('href', href_mappings[link])
if 'Appendix' in el.text:
old_title = el.text.partition(':')[2].strip()
new_title = title_mappings.get(old_title, el.text)
print('old', old_title)
el.text = new_title
print(el.text)

# toc.rewrite_links(lambda href: href_mappings.get(href, href))
toc.set('class', 'toc2')
return toc

Expand All @@ -172,7 +179,6 @@ def main():
chapter_info = get_chapter_info()
fixed_toc = fix_toc(toc, chapter_info)
copy_chapters_across_with_fixes(chapter_info, fixed_toc)
rsync_images()


if __name__ == '__main__':
Expand Down