Skip to content

Commit

Permalink
Changes toc generation to place the toc before the first heading in a…
Browse files Browse the repository at this point in the history
… file, rather than at the top of the file
  • Loading branch information
derpyzza committed Oct 29, 2024
1 parent a469ccd commit ed6fab0
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 74 deletions.
17 changes: 5 additions & 12 deletions assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -474,26 +474,19 @@ footer nav {

hgroup {
margin-bottom: 1rem;
}

#toc {
position: unset;
padding: 0;
width: 100%;
margin: 2rem;
}
}

@media screen and (max-width: 900px) {
#toc {
}
#toc {
position: unset;
padding: 0;
width: 100%;
float: unset;
clear: unset;
margin: 2rem;
}
}

@media screen and (max-width: 900px) {

main {
padding: 0;
margin-top: 10px;
Expand Down
50 changes: 25 additions & 25 deletions jenny.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ def pretty_date( date ):
# extract any variables in a file.
def preprocess_file(file):
vars = {
"post_content": "",
"file_name": file.name,
'post_content': "",
'generate_toc': True,
'post_languages': ['en'],
'file_name': file.name,
}
keep_scanning = True
for line in file:
Expand All @@ -66,7 +68,14 @@ def preprocess_file(file):
if len(args) == 1:
args = "".join(args)

vars[command] = args
if command == 'generate_toc':
if args[0].strip().lower() == 'false':
vars['generate_toc'] = False;
# default case
else:
vars['generate_toc'] = True
else:
vars[command] = args
elif stripped.startswith("---"):
keep_scanning = False
continue
Expand Down Expand Up @@ -187,28 +196,10 @@ def build_footnotes(post, links, has_links, has_notes):
soup.append(sec)
return str(soup)

def generate_table_links ( headings, ul ):
soup = Soup(str(ul), 'html.parser')
for h in headings:
if h['has_children']:
subl = soup.new_tag('ul')
soup.append(generate_table_links(h, subl))
else:
a = soup.new_tag('a', href=h['href'])
li = soup.new_tag('li')
li.append(h['title'])

a.append("#")
a.append(" ")
a.append(li)

soup.append(a)
return str(soup)

"""
Turns all headings into links to the header, plus generates a table of content for the page
"""
def process_headings( post, is_index = False ):
def process_headings( post, generate_toc = True ):
soup = Soup(post, 'html.parser')

count = 0
Expand All @@ -232,6 +223,8 @@ def process_headings( post, is_index = False ):
ul_stack = [table_root]
prev_level = 1

first = None

for h in headings:
if h.text == 'Archive' or h.text == "Notice" or h.text == "Note" or h.text == 'Hint':
continue
Expand All @@ -255,6 +248,8 @@ def process_headings( post, is_index = False ):
head.append(h_text)

h.replace_with(head)
if first == None:
first = head
if level > prev_level:

new_ul = table_soup.new_tag("ul")
Expand All @@ -270,12 +265,17 @@ def process_headings( post, is_index = False ):
current_ul.append(a_tag)
prev_level = level

# skip toc generation
if not generate_toc:
return str(soup)


table = soup.new_tag('div')
table['id'] = "toc"

table.append(table_soup)

soup.insert(0, table)
first.insert_before(table)

return str(soup)
pass
Expand Down Expand Up @@ -331,7 +331,7 @@ def process_posts():

if not (file.name == "src/archive.md"):
(has_links, has_notes, links, post['post_content']) = process_notes(post['post_content'])
post['post_content'] = process_headings(post['post_content'])
post['post_content'] = process_headings(post['post_content'], post['generate_toc'])
post['post_content'] = build_footnotes(post['post_content'], links, has_links, has_notes)
post['post_content'] = process_code_blocks(post['post_content'])

Expand Down Expand Up @@ -377,7 +377,7 @@ def process_index(file: str):
with open (file_name, 'w') as f:
index = index.replace("{{content}}", content)
index = index.replace("{{title}}", file.capitalize())
index = index.replace("{{posts}}", process_headings(list, True))
index = index.replace("{{posts}}", process_headings(list, False))
f.write(index)

def create_new_post(name: str):
Expand Down
5 changes: 2 additions & 3 deletions public/about-jenny.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ <h4>My tiny static site generator</h4>
</hgroup>

<article>
<div id="toc"><ul class="toc"><a href=" ">Table of contents</a><a href="#jenny"><li>Jenny</li></a><a href="#what"><li>What</li></a><a href="#why"><li>Why</li></a><a href="#how"><li>How</li></a><a href="#final-notes"><li>Final notes</li></a></ul></div><h2 id="jenny"><a href="#jenny">#</a> Jenny</h2>
<p>i've been wanting to start a small blog to kinda document my projects and stuff for a while now, and a couple days ago i decided to just sit down and finally make one. going into it however, i had some requirements i needed to satisfy:</p>
<p>i've been wanting to start a small blog to kinda document my projects and stuff for a while now, and a couple days ago i decided to just sit down and finally make one. going into it however, i had some requirements i needed to satisfy:</p>
<ul>
<li>the site needed to be lightweight. like <em>really</em> lightweight. like fits-in-under-512-kb-so-i-can-join-the-<a href="https://512kb.club" id="link-1">512kb-club</a> lightweight.</li>
<li>quick and easy to use / deploy. i didn't want to learn a super cool fancy website builder tool that reads your mind and spits out your dream site.</li>
Expand All @@ -39,7 +38,7 @@ <h4>My tiny static site generator</h4>
<p>essentially, i wanted a very minimal setup. i write a post, post appears on my website. simple. before writing jenny i tried out <a href="https://jekyllrb.com/" id="link-2">jekyll</a>, and while jekyll sort of ticked off all my requirements,
i still didn't like it too much. the main reason was because i didn't really understand how it worked too well, and i really did not want to sit down and read the documentation for it ( im lazy ).
so as a fix for my problems, i just wrote jenny :D <sup id="note-1-src"><a href="#note-1-dest">[1]</a></sup></p><aside><b>1.</b> jenny is derived from the word 'generator'. as in, 'jenny-rator'. i uwu-fied it kinda ig</aside>
<h2 id="what"><a href="#what">#</a> What</h2>
<div id="toc"><ul class="toc"><a href=" ">Table of contents</a><a href="#what"><li>What</li></a><a href="#why"><li>Why</li></a><a href="#how"><li>How</li></a><a href="#final-notes"><li>Final notes</li></a></ul></div><h2 id="what"><a href="#what">#</a> What</h2>
<p>is jenny? jenny, is a small, super opinionated <sup id="note-2-src"><a href="#note-2-dest">[2]</a></sup> static site generator.
it reads in every <mark>.md</mark> file from a <mark>src/</mark> directory, translates them to a <mark>.html</mark> with the help of a template <mark>.html</mark> file which tells the script how to generate the output files, and places them into a <mark>public/</mark> directory. this way, i can just write all my posts in markdown (which is essentially just plain text with extra features), and have the script turn my posts to a web friendly format for
others like you to see!</p><aside><b>2.</b> opinionated as in i was too lazy to add any sort of user friendly features for anyone that isn't me</aside>
Expand Down
2 changes: 1 addition & 1 deletion public/archive.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>Archive</h1>
<p>Here's all of my blogposts. They're written on various subjects, mostly on whatever catches my interest at the time of writing the post.</p>
<br>
<ul>
<div id="toc"><ul class="toc"><a href=" ">Table of contents</a><ul><a href="#2024"><li>2024</li></a><a href="#2023"><li>2023</li></a><a href="#2022"><li>2022</li></a></ul></ul></div><h4 id="2024"><a href="#2024">#</a> 2024</h4><li><a href="/public/on-writing.html">On writing, or something</a></li><li><a href="/public/star-wars-visions-season-2.html">Star Wars: Visions volume II</a></li><h4 id="2023"><a href="#2023">#</a> 2023</h4><li><a href="/public/writing-systems-evolved-from-material-limitations.html">Writing systems evolved from material limitations</a></li><li><a href="/public/hacky-script-perfect-script.html">Hacky script, Perfect script</a></li><h4 id="2022"><a href="#2022">#</a> 2022</h4><li><a href="/public/about-jenny.html">Writing Jenny</a></li>
<h4 id="2024"><a href="#2024">#</a> 2024</h4><li><a href="/public/on-writing.html">On writing, or something</a></li><li><a href="/public/star-wars-visions-season-2.html">Star Wars: Visions volume II</a></li><h4 id="2023"><a href="#2023">#</a> 2023</h4><li><a href="/public/writing-systems-evolved-from-material-limitations.html">Writing systems evolved from material limitations</a></li><li><a href="/public/hacky-script-perfect-script.html">Hacky script, Perfect script</a></li><h4 id="2022"><a href="#2022">#</a> 2022</h4><li><a href="/public/about-jenny.html">Writing Jenny</a></li>
</ul>
</section>
</main>
Expand Down
8 changes: 4 additions & 4 deletions public/hacky-script-perfect-script.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ <h4>a tale of one of those rare moments where i broke through the endless cycle
</hgroup>

<article>
<div id="toc"><ul class="toc"><a href=" ">Table of contents</a><ul><a href="#narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg"><li>Narrator voice: He did not, in fact, make the ultimate tiny ssg</li></a><a href="#the-inevitability-of-jenny"><li>The inevitability of jenny</li></a><a href="#this-website-runs-on-jenny"><li>This website runs on jenny</li></a></ul></ul></div><p>Back in 2022 i decided to start a tiny little personal blog. i designed a lightweight website, and spun up a
<p>Back in 2022 i decided to start a tiny little personal blog. i designed a lightweight website, and spun up a
quick little python script that acted as a static site generator <sup id="note-1-src"><a href="#note-1-dest">[1]</a></sup> where i wrote my blogposts in markdown in a source folder somewhere, and my script then translated them into html files with the help of predefined templates. that blog only lasted one blogpost before i abandoned it / forgot about it.</p><aside><b>1.</b> I named it jenny!</aside>
<p>Fast forward to april 2023. i started cutting out social media from my devices, and consuming a lot more medium-to-long-form-content, like books and blogs, which made me want to re-start my blog. there was, however, a problem with my old setup: my beloved little python script <strong><em>sucked</em></strong>. it was a tiny hacky terribly-slow little script designed solely to work on that specific website with that specific design, with no error handling, no configuration ability, no nothing!</p>
<p>That was, obviously, completely unacceptable. i decided to create the ultimate tiny static site generator, one that is both flexible and configurable <em>and</em> tiny. the one tiny ssg to rule them all!!!</p>
<h3 id="narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg"><a href="#narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg">#</a> Narrator voice: He did not, in fact, make the ultimate tiny ssg</h3>
<div id="toc"><ul class="toc"><a href=" ">Table of contents</a><a href="#narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg"><li>Narrator voice: He did not, in fact, make the ultimate tiny ssg</li></a><a href="#the-inevitability-of-jenny"><li>The inevitability of jenny</li></a><a href="#this-website-runs-on-jenny"><li>This website runs on jenny</li></a></ul></div><h2 id="narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg"><a href="#narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg">#</a> Narrator voice: He did not, in fact, make the ultimate tiny ssg</h2>
<p>So, i failed. Yup. My plans were big and grandiose, but my skills and patience were painfully limited.
originally, the plan was to simply remove all of my original painpoints with jenny, and to just make it more user friendly. the plan was to rewrite jenny in Go, as it is, and just add some tiny little QOL features, such as</p>
<ul>
Expand All @@ -44,10 +44,10 @@ <h3 id="narrator-voice:-he-did-not,-in-fact,-make-the-ultimate-tiny-ssg"><a href
<p>Wrong.</p>
<p>You see while it worked (mostly. i did not finish the whole thing), it had a major, unforgivable flaw: it was <strong>boring</strong>. working on it was not exciting in the least, as i was essentially just copying the python script to Go, and sprinkling a couple QOL features here and there. Absolutely unacceptable; where's the pizzaz? the glamour? the uniqueness and the quirkyness?? where's the excitement of it all???</p>
<p>I abandoned the go generator (surprise surprise), and that is when i started my slow descent to madness. i spent about two weeks staring at other ssgs, reading blogposts about ssgs, comparing programming languages, furiously scribbling notes, daydreaming about working on my ssg, etc. etc. all the while starting random little implementations of my ideas before scrapping them and going back to the procrastination/research loop.</p>
<h3 id="the-inevitability-of-jenny"><a href="#the-inevitability-of-jenny">#</a> The inevitability of jenny</h3>
<h2 id="the-inevitability-of-jenny"><a href="#the-inevitability-of-jenny">#</a> The inevitability of jenny</h2>
<p>After struggling with fennel one day <sup id="note-2-src"><a href="#note-2-dest">[2]</a></sup>, i wandered over to my original python repo. i decided to either use jenny, or some popular static site generator <sup id="note-3-src"><a href="#note-3-dest">[3]</a></sup> to atleast get a blog up and running while i messed around with my better-stronger-faster-one-ssg-to-rule-them-all-ssg. </p><aside><b>3.</b> But but my customization!! my minimalism!! noooooooooooo</aside><aside><b>2.</b> I wanted to write a custom markdown parser, along with a lisp-based templating language in fennel</aside>
<p>I went ahead and pulled down the original jenny repo, and quickly tweaked it so that it was a tiny hacky terribly-slow little script designed solely to work on this specific website with this specific design, with no error handling, no configuration ability, no nothing.</p>
<h3 id="this-website-runs-on-jenny"><a href="#this-website-runs-on-jenny">#</a> This website runs on jenny</h3>
<h2 id="this-website-runs-on-jenny"><a href="#this-website-runs-on-jenny">#</a> This website runs on jenny</h2>
<p>It worked pretty well. i was able to get my website up and running, the way i wanted it to be. i can now post my notes and thoughts on this site, and have them go online without any problems, which is precisely what jenny is meant to do.</p>
<p>So perhaps i did not in fact need a super high powered ultra mega minimal exciting perfect static site generator to rule them all that will be done soon™. perhaps all i needed was a hacky little python script i wrote one year ago that works perfectly fine, right now.</p>
<hr/>
Expand Down
Loading

0 comments on commit ed6fab0

Please sign in to comment.