Skip to content

Commit

Permalink
Books v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
niclake committed Jan 9, 2025
1 parent 245bcf4 commit 45c659d
Show file tree
Hide file tree
Showing 8 changed files with 2,482 additions and 19 deletions.
1 change: 1 addition & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ module.exports = function (eleventyConfig) {
dir: {
input: "src",
output: "_site",
data: "_data",
}
}
};
34 changes: 20 additions & 14 deletions cli/backlogs/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ const booksSheet = books.sheetsByTitle['Books'] // or use `doc.sheetsById[id]` o
const allBooks = await booksSheet.getRows()
const jsonArr = []
for (var i = 0; i < allBooks.length; i++) {
// Skip unless there's a status or read status
if (allBooks[i].get("Status") === "" && allBooks[i].get("Read?") === "") { continue; }

jsonArr.push({
title: allBooks[i].get("Title"),
authorFirst: allBooks[i].get("Author First"),
authorLast: allBooks[i].get("Author Last"),
read: (allBooks[i].get("Read?") === "X") ? true : false,
compDate: allBooks[i].get("Comp Date"),
rating: getStars(allBooks[i].get("Rating")),
status: trimStatus(allBooks[i].get("Status")),
pages: allBooks[i].get("Pages"),
isbn: allBooks[i].get("ISBN"),
})
if (allBooks[i].get("Status") === "1 In Progress" || allBooks[i].get("Read?") === "X") {
jsonArr.push({
title: allBooks[i].get("Title"),
authorFirst: allBooks[i].get("Author First"),
authorLast: allBooks[i].get("Author Last"),
read: (allBooks[i].get("Read?") === "X") ? true : false,
compDate: formatDate(allBooks[i].get("Comp Date")),
rating: getStars(allBooks[i].get("Rating")),
status: trimStatus(allBooks[i].get("Status")),
pages: allBooks[i].get("Pages"),
isbn: allBooks[i].get("ISBN"),
})
}
}

fs.writeFile(__siteroot + __target + __targetFile, JSON.stringify(jsonArr), function(err) {
Expand All @@ -65,6 +64,13 @@ function getStars(rating) {
return stars
}

function formatDate(date) {
if (date === "") {
return ""
}
return new Date(date).toLocaleDateString('en-za')
}

function trimStatus(status) {
if (status === "") {
return ""
Expand Down
34 changes: 34 additions & 0 deletions src/_data/catalog/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const theBooks = require('./books.json')

module.exports = async function() {
const currentBook = theBooks.filter(book => book.status === 'In Progress')
const datedBooks = theBooks
.filter(
(book) => book.read === true && book.compDate !== ""
)
.sort((a, b) => {
return a.compDate > b.compDate ? -1 : 1;
});

// generate an array of books with yearRead as 'undated'
const undatedBooks = theBooks.filter(
(book) => book.read === true && book.compDate === ""
);

// Extract unique years and format them
const uniqueYears = [
...new Set(datedBooks.map((book) => book.compDate.split("/")[0])),
]
.map((year) => `y${year}`)
.sort((a, b) => b.slice(1) - a.slice(1)); // Sort in descending order

// Generate the result array
const years = uniqueYears.map((year) => ({ year }));
years.push({ year: "undated" });
return {
current: currentBook,
datedBooks: datedBooks,
undatedBooks: undatedBooks,
years: years,
}
}
2,368 changes: 2,367 additions & 1 deletion src/_data/catalog/books.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/_data/site/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = function() {
author: 'Nic Lake',
description: 'Life, technology, family, and more.',
email: 'niclake13@gmail.com',
mastodon: '@niclake@mastodon.social'
mastodon: '@niclake@mastodon.social',
bluesky: '@niclake.me',
}
}
3 changes: 0 additions & 3 deletions src/_data/site/robots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ const fetch = require("node-fetch")
const { AssetCache } = require("@11ty/eleventy-fetch")

module.exports = async function() {
console.log("Fetching robots.txt")

let asset = new AssetCache("robots.txt")

if (asset.isCacheValid('1d'))
{
console.log("Returning robots.txt from cache" )
return await asset.getCachedValue()
}

Expand Down
14 changes: 14 additions & 0 deletions src/_includes/bookitem.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="item bookitem">
{% set bookshop_base = "https://bookshop.org/a/109591/" %}
<a href="{{ bookshop_base }}{{ book.ISBN }}">
{% if book.localCover %}
<img src="/assets/images/catalog/books/{{ book.title | slugify }}.jpg" alt="{{ book.title | safe }}">
{% else %}
<img src="https://covers.openlibrary.org/b/isbn/{{ book.isbn }}-M.jpg" alt="{{ book.title | safe }}">
{% endif %}
</a>
<p class="bktitle"><a href="{{ bookshop_base }}{{ book.ISBN }}">{{ book.title | safe }}</a></p>
<p class="bkauthor">by {{ book.authorFirst }} {{ book.authorLast }}</p>
<p class="bkdate">{{ book.compDate }}</p>
{% if book.rating != "" %}<p class="bkrating">{{ book.rating }}</p>{% endif %}
</div>
44 changes: 44 additions & 0 deletions src/pages/bookshelf.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Bookshelf
permalink: /bookshelf/index.html
---

<p>A whole bunch of people on the internet have started to have bookshelves and almanacs on their websites, such as <a href="https://rknight.me/almanac/">Robb Knight</a>, <a href="https://bobmonsour.com/books/">Bob Monsour</a>, and <a href="https://thisguise.wtf/bookshelf/">Swyx</a>. I love this idea; I've had aspirations to do something similar for ages, but I've always managed to just fall back on linking to my Goodreads or <a href="">StoryGraph</a> profiles, or just pointing people towards <a href="https://docs.google.com/spreadsheets/d/1-1PcHF6xzFKTaTvxnfjm6bVgo4pd5yIr3nbxsbckoFo/edit?usp=sharing">my reading spreadsheet</a>. I'm going to try to do better.

<p>One day, I'll go into detail about how this is constructed, but for now, I'm just going to list the books I've read recently, along with my ratings. Eventually, I'll have it updating auto-magically, but for now, I'm just going to update it manually.

<div class="bookyears">
{% for year in catalog.books.years %}
<a href="#{{ year.year }}">{{ year.year | replace("y", "") }}</a>{% if not loop.last %} / {% endif %}
{% endfor %}
</div>

{% set book = catalog.books.current[0] %}
<div class="bklist">
{% include "bookitem.njk" %}
</div>

{% set previous_year = "" %}
{%- for book in catalog.books.datedBooks %}
{# extract the year from the yearRead property that is formatted as yyyy/mm/dd #}
{%- set current_year = book.compDate | truncate(4, true, '') -%}
{%- if current_year != previous_year %}
{# if we're changing years and it's not the first year,
close the 'bklist' div on the prior year, set the heading id
to the new year, update the previous_year, and open a new bklist div #}
{% if not loop.first %}</div>{% endif %}
<h2 id="y{{ current_year }}" class="bookyear">{{ current_year }}</h2>
{% set previous_year = current_year %}
<div class="bklist">
{%- endif %}
{% include "bookitem.njk" %}
{# close the 'bklist' div on the last book in the list #}
{% if loop.last %}</div>{% endif %}
{%- endfor %}

<h2 id="undated" class="bookyear">Undated: don't know when I read these</h2>
<div class="bklist">
{%- for book in catalog.books.undatedBooks %}
{% include "bookitem.njk" %}
{%- endfor %}
</div>

0 comments on commit 45c659d

Please sign in to comment.