Skip to content

Instantly share code, notes, and snippets.

@scootcho
Forked from framp/daily-stoic
Created August 10, 2020 23:05
Show Gist options
  • Select an option

  • Save scootcho/90525bd93b207c5ac7ec5aee5c0c8fe8 to your computer and use it in GitHub Desktop.

Select an option

Save scootcho/90525bd93b207c5ac7ec5aee5c0c8fe8 to your computer and use it in GitHub Desktop.
#!/bin/bash
leapyear=1
year=${1:-$(date '+%Y')}
day=${2:-$(date '+%j')}
([ $(($day)) -lt 60 ]) || ([ $(($year % 4)) -eq 0 ] && ([ $(($year % 100)) -ne 0 ] || [ $(($year % 400)) -eq 0 ])) &&
leapyear=0
file=$(($day + leapyear)).jpg
qimgv /usr/local/share/daily-stoic/$file
const fs = require('fs')
const filename = process.argv[2];
const lastPhraseIncipit = 'we offer this book.'
const firstPhraseEnd = 'STAYING STOIC'
const content = fs.readFileSync(filename)
.toString()
.split(lastPhraseIncipit)[1]
.split(firstPhraseEnd)[0]
.split(/\s{3,}/)
.filter(String)
const months = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']
const toNormalCase = (str) => str[0].toUpperCase() + str.toLowerCase().slice(1)
const { result } = content.reduce((acc, token) => {
if (acc.next === 'text-or-date') {
if (months.includes(token)) {
return { ...acc, next: 'section' }
}
const words = token.split(' ')
if (months.includes(words[0].toUpperCase()) && words.length === 2) {
return { ...acc, next: 'title', current: token, result: { ...acc.result, [token]: { text: [], section: toNormalCase(acc.section), section_uppercase: acc.section.toUpperCase() } } }
}
return { ...acc, next: 'text-or-date', result: { ...acc.result, [acc.current]: { ...acc.result[acc.current], text: acc.result[acc.current].text.concat(token) } } }
}
if (acc.next === 'section') {
return { ...acc, next: 'text-or-date', section: token }
}
if (acc.next === 'title') {
return { ...acc, next: 'quote', result: { ...acc.result, [acc.current]: { ...acc.result[acc.current], title: toNormalCase(token), title_uppercase: token.toUpperCase() } } }
}
if (acc.next === 'quote') {
return { ...acc, next: 'author', result: { ...acc.result, [acc.current]: { ...acc.result[acc.current], quote: token } } }
}
if (acc.next === 'author') {
return { ...acc, next: 'text-or-date', result: { ...acc.result, [acc.current]: { ...acc.result[acc.current], author: token } } }
}
}, { result: {}, next: 'text-or-date' })
console.log(JSON.stringify(result, null, '\t'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment