Streamline site generation

This commit is contained in:
Sami Samhuri 2019-12-12 23:16:30 -08:00
parent fd785fcf2f
commit 5f2ca2e44d
7 changed files with 10 additions and 113 deletions

View file

@ -2,17 +2,19 @@ all: blog
blog: sitegen
@echo
./bin/compile . www
rm -rf www
./bin/sitegen . www
publish: blog
@echo
./bin/publish --delete
./bin/publish --delete www/
publish_beta:
@echo
./bin/build-sitegen
./bin/compile . www "https://beta.samhuri.net"
./bin/publish --beta --delete
rm -rf www
./bin/sitegen . www "https://beta.samhuri.net"
./bin/publish --beta --delete www/
sitegen:
@echo

View file

@ -105,11 +105,11 @@ Execution, trying TDD for the first time:
- [x] Munge HTML files to make them available without an extension (index.html hack, do it in the SiteGenerator)
- [ ] Use perf tools on beta.samhuri.net and compare to samhuri.net to see if inlining css and minifying JS is actually worthwhile
- [x] Use perf tools on beta.samhuri.net and compare to samhuri.net to see if inlining css and minifying JS is actually worthwhile
- [ ] Inline CSS?
- [x] Inline CSS? Nope
- [ ] Minify JS? Now that we're keeping node, why not ...
- [x] Minify JS? Now that we're keeping node, why not ... Nope! Ditched node too
- [ ] Add a server for local use and simple production setups (or use a file watcher + `python -m SimpleHTTPServer`?)

View file

@ -1,30 +0,0 @@
#!/bin/bash
# bail on errors
set -e
THIS_DIR=$(dirname "$0")
SOURCE_DIR="$1"
TARGET_DIR="$2"
URL_OVERRIDE="$3"
function main() {
rm -rf "$TARGET_DIR"
echo "* generate site from $SOURCE_DIR into $TARGET_DIR"
"$THIS_DIR/sitegen" "$SOURCE_DIR" "$TARGET_DIR" "$URL_OVERRIDE"
# echo "* inline CSS"
# ruby -w $THIS_DIR/inline-css "$TARGET_DIR"
# echo "* minify js"
# minify_js
}
# function minify_js() {
# for FILE in "$TARGET_DIR"/js/*.js; do
# $THIS_DIR/minify-js.sh "$FILE" > /tmp/minified.js && mv /tmp/minified.js "$FILE" || echo "* failed to minify $FILE"
# done
# }
main

View file

@ -1,54 +0,0 @@
#!/usr/bin/env ruby -w
require 'rubygems'
require 'bundler/setup'
require 'nokogiri'
require 'css_parser'
# Styles are so small, inline them all.
def main
root_dir = ARGV.shift
CSSInliner.new(root_dir).inline_all_css
end
class CSSInliner
def initialize(root_dir)
@root_dir = root_dir
end
def inline_all_css
Dir[File.join(@root_dir, '**/*.html')].each do |html_path|
next if html_path =~ /\/Chalk\/|\/tweets\//
inline_css(html_path)
end
end
def inline_css(html_path)
html = File.read(html_path)
doc = Nokogiri::HTML.parse(html)
css_parser = CssParser::Parser.new
doc.css('link').each do |link|
if link['rel'] == 'stylesheet'
path = File.join(@root_dir, link['href'])
css = File.read(path)
css_parser.add_block!(css)
style_node = Nokogiri::HTML.parse("
<style>
#{css}
</style>
").css('style')
link.replace(style_node)
end
end
File.write(html_path, doc.to_html)
end
end
main if $0 == __FILE__

View file

@ -50,5 +50,4 @@ if [[ $ECHO -eq 1 ]]; then
echo "$CMD"
fi
cp .git/$(cat .git/HEAD | cut -d' ' -f2) www/version.txt
$CMD

View file

@ -1,20 +0,0 @@
#!/usr/bin/env ruby
def pad(n)
n = n.to_i
if n < 10
"0#{n}"
else
"#{n}"
end
end
Dir.chdir File.join(File.dirname(__FILE__), '../public/posts')
Dir['*.html.md'].each do |filename|
name = filename.sub('.html.md', '')
date, *rest = name.split('-')
year, month, _ = date.split('.')
slug = rest.join('-')
puts "Redirect 301 /blog/#{name} /posts/#{year}/#{pad(month)}/#{slug}"
end

View file

@ -3,7 +3,7 @@
set -e
for site in Tests/test-*; do
bin/compile "$site/in" "$site/actual"
bin/sitegen "$site/in" "$site/actual"
diff -bru "$site/expected" "$site/actual"
rm -r "$site/actual"
done