Skip to content

Commit

Permalink
Merge pull request #172 from brandur/detect-image-locations
Browse files Browse the repository at this point in the history
Detect image locations for hook/Twitter card

Former-commit-id: a360f91
  • Loading branch information
brandur authored Nov 24, 2017
2 parents 0e56fbd + a843258 commit 38b3833
Show file tree
Hide file tree
Showing 26 changed files with 45 additions and 58 deletions.
60 changes: 43 additions & 17 deletions cmd/sorg-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ type Article struct {
// Hook is a leading sentence or two to succinctly introduce the article.
Hook string `yaml:"hook"`

// HookImage is a boolean indicating whether there's a preview image for
// the article that can be shown on the index page.
HookImage bool `yaml:"hook_image"`
// HookImageURL is the URL for a hook image for the article (to be shown on
// the article index) if one was found.
HookImageURL string `yaml:"-"`

// Image is an optional image that may be included with an article.
Image string `yaml:"image"`
Expand All @@ -81,10 +81,6 @@ type Article struct {
// included as YAML frontmatter, but rather calculated from the article's
// content, rendered, and then added separately.
TOC string `yaml:"-"`

// TwitterImage is a boolean indicating whether there's an image for
// the article that can be shown in a Twitter card.
TwitterImage bool `yaml:"twitter_image"`
}

// PublishingInfo produces a brief spiel about publication which is intended to
Expand Down Expand Up @@ -193,10 +189,6 @@ type Fragment struct {

// Title is the fragment's title.
Title string `yaml:"title"`

// TwitterImage is a boolean indicating whether there's an image for
// the article that can be shown in a Twitter card.
TwitterImage bool `yaml:"twitter_image"`
}

// PublishingInfo produces a brief spiel about publication which is intended to
Expand Down Expand Up @@ -583,12 +575,26 @@ func compileArticle(dir, name string, draft bool) (*Article, error) {
return nil, err
}

format, ok := pathAsImage(
path.Join(sorg.ContentDir, "images", article.Slug, "hook"),
)
if ok {
article.HookImageURL = "/assets/" + article.Slug + "/hook." + format
}

if err != nil && !os.IsNotExist(err) {
return nil, err
}

card := &twitterCard{
Title: article.Title,
Description: article.Hook,
}
if article.TwitterImage {
card.ImageURL = sorg.AbsoluteURL + "/assets/" + article.Slug + "/[email protected]"
format, ok = pathAsImage(
path.Join(sorg.ContentDir, "images", article.Slug, "twitter@2x"),
)
if ok {
card.ImageURL = sorg.AbsoluteURL + "/assets/" + article.Slug + "/twitter@2x." + format
}

locals := getLocals(article.Title, map[string]interface{}{
Expand Down Expand Up @@ -710,14 +716,15 @@ func compileFragment(dir, name string, draft bool) (*Fragment, error) {
// A lot of fragments still have unwritten hooks, so only add a card where
// a fragment has a configured Twitter image for the time being.
var card *twitterCard
if fragment.TwitterImage {
format, ok := pathAsImage(
path.Join(sorg.ContentDir, "fragments", fragment.Slug, "twitter@2x"),
)
if ok {
card = &twitterCard{
ImageURL: "/assets/fragments/" + fragment.Slug + "/twitter@2x." + format,
Title: fragment.Title,
Description: fragment.Hook,
}
if fragment.TwitterImage {
card.ImageURL = sorg.AbsoluteURL + "/assets/fragments/" + fragment.Slug + "/[email protected]"
}
}

locals := getLocals(fragment.Title, map[string]interface{}{
Expand Down Expand Up @@ -2113,6 +2120,25 @@ create:
return os.Symlink(source, dest)
}

// Checks if the path exists as a common image format (.jpg or .png only). If
// so, returns the discovered extension (e.g. "jpg") and boolean true.
// Otherwise returns an empty string and boolean false.
func pathAsImage(extensionlessPath string) (string, bool) {
// extensions must be lowercased
formats := []string{"jpg", "png"}

for _, format := range formats {
_, err := os.Stat(extensionlessPath + "." + format)
if err != nil {
continue
}

return format, true
}

return "", false
}

func renderView(layout, view, target string, locals map[string]interface{}) error {
log.Debugf("Rendering: %v", target)

Expand Down
2 changes: 0 additions & 2 deletions content/articles/acid.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ hook: On ensuring system integrity, operability, and
correctness through a solid foundational database, and
how ACID transactions and strong constraints work in your
favor. Why to prefer Postgres over MongoDB.
hook_image: true
twitter_image: true
---

In 1983, Andreas Reuter and Theo Härder coined the acronym
Expand Down
2 changes: 0 additions & 2 deletions content/articles/api-paradigms.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ location: San Francisco
hook: Musings on the next API technology, and whether REST-ish
JSON over HTTP is just "good enough" to never be displaced
in a significant way.
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=14003134
---

Expand Down
1 change: 0 additions & 1 deletion content/articles/breaktime.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ published_at: 2014-02-02T18:15:28Z
location: San Francisco
hook: In search of an alternative to BreakTime. The
discovery of a very classical solution.
hook_image: true
---

A few years ago I took the plunge and started using
Expand Down
2 changes: 0 additions & 2 deletions content/articles/heroku-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ image: "/assets/heroku-values/heroku-values.jpg"
location: San Francisco
published_at: 2015-11-05T06:20:16Z
title: My Heroku Values
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=14286143
---

Expand Down
2 changes: 0 additions & 2 deletions content/articles/idempotency-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ hook: Building resilient services by identifying foreign
state mutations and grouping local changes into
restartable atomic phases so that every request can be
driven to completion.
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=15569478
---

Expand Down
2 changes: 0 additions & 2 deletions content/articles/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ location: San Francisco
hook: How we overvalue the wrong technology and novel
aspects of interface design at the expense of substantial
gains to our productivity.
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=13733777
---

Expand Down
2 changes: 0 additions & 2 deletions content/articles/minimalism.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ location: San Francisco
hook: Practicing minimalism with the lofty goal of total
ephemeralization to build coherent, stable, and operable
stacks.
hook_image: true
twitter_image: true
attributions: Photographs by <strong><a href="https://www.flickr.com/photos/i-am-mclovin/14601998033/">Ben Harrington</a></strong> (SR-71), <strong><a href="https://www.flickr.com/photos/learnscope/5032942270/">Robyn Jay</a></strong> (embers of a burning fire), and <strong><a href="https://www.flickr.com/photos/alamin_bd/22969073683/">Md. Al Amin</a></strong> (boat and sky). Licensed under Creative Commons BY-NC-ND 2.0, BY-SA 2.0, and CC BY 2.0 respectively.
---

Expand Down
2 changes: 0 additions & 2 deletions content/articles/newsletters.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: "Pseudo-HTML and Pidgin CSS: Building an Email Newsletter"
published_at: 2017-08-02T14:52:31Z
hook: Building a toolchain for sending a newsletter, and
the dismal state of HTML and CSS in email.
hook_image: true
twitter_image: true
---

After a recent trip to Portland, I decided to try writing a
Expand Down
2 changes: 0 additions & 2 deletions content/articles/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ hook: How the page almost transitioned successfully to the
digital world, but is in decline in new media. The
lessons that we can learn from this age-old design
element, and why we should hope for its re-emergence.
hook_image: true
twitter_image: true
image: "/assets/page/page.jpg"
location: San Francisco
published_at: 2014-01-26T18:56:46Z
Expand Down
2 changes: 0 additions & 2 deletions content/articles/postgres-atomicity.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ location: San Francisco
hook: A dive into the mechanics that allow Postgres to
provide strong atomic guarantees despite the chaotic
entropy of production.
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=15027870
---

Expand Down
2 changes: 0 additions & 2 deletions content/articles/postgres-reads.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ published_at: 2017-11-17T22:02:56Z
hook: Scaling out operation with read replicas and avoiding
the downside of stale reads by observing replication
progress.
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=15726376
---

Expand Down
2 changes: 0 additions & 2 deletions content/articles/redis-streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ location: San Francisco
hook: Building a log-based architecture that's fast,
efficient, and resilient on the new stream data structure
in Redis.
hook_image: true
twitter_image: true
hn_link: https://news.ycombinator.com/item?id=15653544
---

Expand Down
1 change: 0 additions & 1 deletion content/articles/stripe-running.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ hook: Crunching running data with prepared statements in Postgres.
location: San Francisco
published_at: 2015-10-24T20:55:32Z
title: Running at Stripe
hook_image: true
---

One pleasant surprise of Stripe's internal culture was the existence of a
Expand Down
2 changes: 0 additions & 2 deletions content/articles/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ hook: When it comes to streaming APIs, there's now a lot of
great options like SSE, GraphQL subscriptions, and GRPC
streams. Let's examine whether webhooks are still a good
choice in 2017.
hook_image: true
twitter_image: true
attributions: Thanks to <a
href="https://twitter.com/spencercdixon">Spencer
Dixon</a> for review.
Expand Down
2 changes: 0 additions & 2 deletions content/articles/x100s-hack.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ hook: If you find that the price tag for a Fuji-official adapter ring for the X1
location: San Francisco
published_at: 2014-08-03T16:50:19Z
title: A Cheap X100S Filter Ring Hack
hook_image: true
twitter_image: true
---

The [official Fujifilm X100/X100S 49 mm adapter ring](http://www.amazon.com/Fujifilm-AR-X100-Adapter-Ring-49mm/dp/B004MME69S) which allows you to mount extra filters onto your lens will run you about $40, which is a little steep considering that its entire role in life is to act as an expensive spacer.
Expand Down
2 changes: 0 additions & 2 deletions content/drafts/microservices-and-the-monolith.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Microservices and the Monolith
published_at: 2017-01-05T16:41:25Z
hook: Microservices may be out of vogue, but we should be
wary of overcompensation.
hook_image: true
twitter_image: true
---

About three years ago, the idea of a service-oriented
Expand Down
2 changes: 0 additions & 2 deletions content/drafts/ruby-scale.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ published_at: 2017-04-18T14:23:28Z
location: San Francisco
hook: The challenges of scaling and operating a big Ruby
codebase (that are not related to performance).
hook_image: true
twitter_image: true
---

Ruby is a beautiful language. Speaking from experience,
Expand Down
1 change: 0 additions & 1 deletion content/fragments/airpods.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: AirPods
published_at: 2017-03-29T14:44:33Z
image: /assets/fragments/airpods/vista.jpg
twitter_image: true
hook: I'm happy to be cheering from the bleachers as Apple
makes their first home run in years.
---
Expand Down
1 change: 0 additions & 1 deletion content/fragments/ipad-mini.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: The iPad Mini
published_at: 2016-06-12T22:26:17Z
image: /assets/fragments/ipad-mini/vista.jpg
twitter_image: true
hook: An ode to one of my favorite Apple devices.
---

Expand Down
1 change: 0 additions & 1 deletion content/fragments/ivy.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
title: Ivy
published_at: 2016-08-24T03:12:31Z
twitter_image: true
image: /assets/fragments/ivy/vista.jpg
hook: Stripe's new home in SOMA.
---
Expand Down
1 change: 0 additions & 1 deletion content/fragments/monkeybrains.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: MonkeyBrains
published_at: 2016-01-23T01:15:58Z
image: /assets/fragments/monkeybrains/vista.jpg
twitter_image: true
hook: A very brief review of the local San Francisco ISP.
---

Expand Down
1 change: 0 additions & 1 deletion content/fragments/sprawl-blues.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: The Sprawl Blues
published_at: 2016-01-03T22:18:36Z
image: /assets/fragments/sprawl-blues/vista.jpg
twitter_image: true
hook: On sprawl and commute times in North America.
---

Expand Down
1 change: 0 additions & 1 deletion content/fragments/wgt-2015.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: WGT 2015 Abstract
published_at: 2015-05-29T13:34:59Z
image: /assets/fragments/wgt-2015/vista.jpg
twitter_image: true
hook: A whirlwind tour of Wave-Gotik-Treffen 2015.
---

Expand Down
1 change: 0 additions & 1 deletion content/fragments/your-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: Your Name
published_at: 2017-04-11T01:26:59Z
image: /assets/fragments/your-name/vista.jpg
twitter_image: true
hook: A short review of Makoto Shinka's latest animated
film.
---
Expand Down
4 changes: 2 additions & 2 deletions views/articles/index.ace
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
ul
{{range .Articles}}
li
{{if .HookImage}}
{{if .HookImageURL}}
a href="/{{.Slug}}"
img src="/assets/{{.Slug}}/hook.jpg" data-rjs="2"
img src="{{.HookImageURL}}" data-rjs="2"
{{end}}
.title
a href="/{{.Slug}}"
Expand Down

0 comments on commit 38b3833

Please sign in to comment.