Now has serve --watch, plus syncs the static files. Various fixes.

- running ruby ./build.rb serve --watch will rebuild when
   manual pages are changed
 - no need to manually cp -r the static pages
 - various fixes, clean-ups and optimization
This commit is contained in:
Simon Kågedal Reimer 2015-03-11 21:59:17 +01:00
parent 0d0a1048b8
commit 935ee4599f

154
build.rb
View File

@ -2,33 +2,39 @@ require 'pathname'
require 'fileutils' require 'fileutils'
require 'yaml' require 'yaml'
require 'liquid' require 'liquid'
require 'optparse'
def split_frontmatter(txt) CONFIG = {
re = /\A---[ \t\r]*\n(?<frontmatter>.*?)^---[ \t\r]*\n(?<content>.*)\z/m pages_dir: '_manual',
match = re.match txt layouts_dir: '_layouts',
match ? [match['frontmatter'], match['content']] : nil static_dir: 'source',
end output_dir: '_site' # will get wiped!
}
def child_url?(a, b) def child_url?(a, b)
a.start_with?(b) && b.count('/') + 1 == a.count('/') a.start_with?(b) && b.count('/') + 1 == a.count('/')
end end
class Site class Site
attr_reader :pages attr_reader :pages, :layouts
def initialize() def initialize()
@pages = [] @pages = []
@config = {
'pages_dir' => '_manual',
'layouts_dir' => '_layouts',
'static_dir' => 'source',
'output_dir' => '_site'
}
@layouts = {} @layouts = {}
end end
def build()
print "Building... "
read_layouts()
read_pages()
copy_static()
process_pages()
puts "done."
end
def read_layouts() def read_layouts()
layouts_dir = Pathname(@config['layouts_dir'])
Pathname.glob(layouts_dir + Pathname('*.html')) do |path| Pathname.glob(layouts_dir + Pathname('*.html')) do |path|
next if !path.file? next if !path.file?
layout = Layout.new(self, path) layout = Layout.new(self, path)
@ -37,12 +43,7 @@ class Site
end end
end end
def find_layout(name)
@layouts[name]
end
def read_pages() def read_pages()
pages_dir = Pathname.new(@config['pages_dir'])
pages_dir.find do |path| pages_dir.find do |path|
if path.file? && path.extname == '.html' if path.file? && path.extname == '.html'
page = Page.new(self, path) page = Page.new(self, path)
@ -52,33 +53,25 @@ class Site
end end
end end
def find_children(url)
@pages.select{ |p| child_url?(p.url, url) }.sort_by{ |p| p.path.basename }
end
def process_pages() def process_pages()
@pages.each {|page| page.process} @pages.each {|page| page.process}
end end
def copy_static() def copy_static()
# http://ruby-doc.org/stdlib-2.2.1/libdoc/fileutils/rdoc/index.html `rsync -a --delete --exclude='*~' #{static_dir}/. #{output_dir}`
end end
def pages_dir() def find_children(url)
Pathname(@config['pages_dir']) sorted_pages.select { |p| child_url?(p.url, url) }
end end
def output_dir() def toplevel() @toplevel_memo ||= find_children('/') end
Pathname(@config['output_dir']) def sorted_pages() @sorted_pages_memo ||= @pages.sort_by{ |p| p.sort_url } end
end
def run() def pages_dir() @pages_dir_memo ||= Pathname(CONFIG[:pages_dir]) end
#read_config() def layouts_dir() @layouts_dir_memo ||= Pathname(CONFIG[:layouts_dir]) end
read_layouts() def static_dir() @static_dir_memo ||= Pathname(CONFIG[:static_dir]) end
read_pages() def output_dir() @output_dir_memo ||= Pathname(CONFIG[:output_dir]) end
copy_static()
process_pages()
end
end end
class Page class Page
@ -108,27 +101,32 @@ class Page
end end
def title() def title()
if !@page_context
puts 'nil page context: ' + @path.to_s
end
@page_context['title'] || "" @page_context['title'] || ""
end end
def menu_title()
@page_context['menu_title'] || title
end
def read() def read()
content = @path.read content = @path.read
split = split_frontmatter content frontmatter, @content = split_frontmatter(content) || abort("File not well-formatted: #{@path}")
split || abort("Not a Jekyll-formatted file: #{@path}")
frontmatter, @content = split
@page_context = YAML.load(frontmatter) @page_context = YAML.load(frontmatter)
@template = Liquid::Template.parse(@content) @template = Liquid::Template.parse(@content)
end end
def split_frontmatter(txt)
@split_regex ||= /\A---[ \t\r]*\n(?<frontmatter>.*?)^---[ \t\r]*\n(?<content>.*)\z/m
match = @split_regex.match txt
match ? [match['frontmatter'], match['content']] : nil
end
def find_layout() def find_layout()
@site.find_layout(@page_context['layout'] || 'default') @site.layouts[@page_context['layout'] || 'default']
end end
def children() def children()
@site.find_children(@url) @children ||= @site.find_children(@url)
end end
def render() def render()
@ -141,7 +139,7 @@ class Page
def process() def process()
path = out_path path = out_path
path.dirname.mkpath path.dirname.mkpath
path.write(render) path.open('w') { |f| f.write(render) }
end end
end end
@ -175,7 +173,7 @@ class Tag_tree < Liquid::Tag
%{ %{
<dt#{css}> <dt#{css}>
<a href='#{page.url}'>#{page.title}</a> <a href='#{page.url}'>#{page.menu_title}</a>
</dt> </dt>
<dd#{css}> <dd#{css}>
#{children_html} #{children_html}
@ -183,7 +181,7 @@ class Tag_tree < Liquid::Tag
} }
end end
join(site.find_children('/').map(&format_entry)) join(site.toplevel.map(&format_entry))
end end
end end
@ -204,28 +202,66 @@ end
class Tag_prevnext < Liquid::Tag class Tag_prevnext < Liquid::Tag
def render(context) def render(context)
site = context.registers[:site]
current = context.registers[:page] current = context.registers[:page]
pages = context.registers[:site].sorted_pages
pages = site.pages.sort_by{ |p| p.sort_url } index = pages.index { |page| page == current }
return '' if !index
ind = pages.index { |page| page == current } link = lambda do |p, cls, txt|
return '' if !ind
lnk = lambda do |p, cls, txt|
"<li><a title='#{p.title}' href='#{p.url}' class='#{cls}'>#{txt}</a></li>" "<li><a title='#{p.title}' href='#{p.url}' class='#{cls}'>#{txt}</a></li>"
end end
prev_link = ind > 0 ? lnk.call(pages[ind-1], "previous", " &lt; Previous ") : "" prev_link = index > 0 ? link.call(pages[index-1], "previous", " &lt; Previous ") : ""
next_link = ind < pages.length-1 ? lnk.call(pages[ind+1], "next", " Next &gt; ") : "" next_link = index < pages.length-1 ? link.call(pages[index+1], "next", " Next &gt; ") : ""
"<ul class='pager'>#{prev_link}#{next_link}</ul>" "<ul class='pager'>#{prev_link}#{next_link}</ul>"
end end
end end
Liquid::Template.register_tag('tree', Tag_tree) class Server
Liquid::Template.register_tag('children', Tag_children) def start_watcher()
Liquid::Template.register_tag('prevnext', Tag_prevnext) require 'listen'
Liquid::Template.error_mode = :strict listener = Listen.to(CONFIG[:pages_dir], wait_for_delay: 1.0, only: /.html$/) do |modified, added, removed|
Site.new.build
end
listener.start
listener
end
Site.new.run def run(options)
require 'webrick'
listener = options[:watch] && start_watcher
puts "Serving at http://localhost:8000/ ..."
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => CONFIG[:output_dir]
trap 'INT' do
server.shutdown
end
server.start
listener.stop if listener
end
end
def main
Liquid::Template.register_tag('tree', Tag_tree)
Liquid::Template.register_tag('children', Tag_children)
Liquid::Template.register_tag('prevnext', Tag_prevnext)
if defined? Liquid::Template.error_mode
Liquid::Template.error_mode = :strict
end
options = {}
OptionParser.new do |opts|
opts.on("--watch", "Watch for changes") { options[:watch] = true }
end.parse!
Site.new.build
if ARGV == ['serve']
Server.new.run(options)
end
end
main