Jezra.net

compiling, and styling code

While creating my static site, I soon started having problems when switching between development computers. The ruby gem I was using for site compilation and test serving started having dependency issues that I couldn't seem to resolve. After a depressingly frustrating evening, I thought I would cheer myself up by rolling my own solution with a bit of coding. :)

It isn't slick, it it's robust, bit it does what I need it to do, namely:

Since code on a page rather dull, a bit of web searching resulted in https://highlightjs.org being utilized for styling code on a page. Hot diggity, it's a fairly simple set up process!

Enter the Ruby

#!/usr/bin/env ruby

#define directory and layout file names
SOURCE = "source"
PUBLIC = "public"
LAYOUT = SOURCE+"/_layout.html.haml"

#require Tilt https://github.com/rtomayko/tilt
require 'tilt/haml'
require 'tilt/sass'

#take a source file, and compile the output the the public dir
def parse_to_public(file)
	puts "parse: #{file}"
	#what is the output file name?
	bits = file.split(".")
	type = bits.pop()
	o_file = bits.join(".").sub(SOURCE,PUBLIC)
	if type == "haml"
		#get the haml from the template
		tmp = Tilt::HamlTemplate.new(file)
		content = tmp.render(self)
		#load the layout
		l_tmp = Tilt::HamlTemplate.new(LAYOUT)
		output = l_tmp.render(self, {content: content})
		#open the output file for writing
		File.open(o_file, "w") do |f|
			f.write(output)
		end
		#reset global vars defined in templates
		@date = nil
		@title = nil
		@tags = nil
		@description = nil

	elsif type == "sass"
		tmp = Tilt::SassTemplate.new(file)
		output = tmp.render
		#open the output file for writing
		File.open(o_file, "w") do |f|
			f.write(output)
		end
	end
end	

#this method is used within templates to render partials
def render file
	tmp = Tilt::HamlTemplate.new SOURCE+"/"+file
	tmp.render
end

#this method is used within templates to render code
def render_code file
	 File.open(SOURCE+"/_code/"+file,'r').read()
end

#copy a file or directory from SOURCE to PUBLIC
def mk_public_file(file)
	#what would the public file be?
	p_file = file.sub(SOURCE,PUBLIC)
	#is this a directory?
	if File.directory? file
		#does the PUBLIC version exist
		unless Dir.exist? p_file
			#make the directory
			puts "`mkdir -p  #{p_file}`"
			`mkdir -p  #{p_file}`
		end
	else
		#just cp the file?
		unless File.identical? file, p_file
			puts "`cp -v #{file} #{p_file}`"
			`cp -v #{file} #{p_file}` 
		end
	end
end

#is the file a parsable template file?
def is_parsable?(file)
	file.end_with? ".haml" or file.end_with? ".sass"
end

#should the file be ignored?
def ignore_file?(file)
	file.start_with? "_" or file.start_with? "."
end

#walk through a directory
def walk_directory(d)
	puts "Walking: #{d}"
	Dir.foreach d do |f|
		#skip this file if it is supposed to be ignored
		if ignore_file? f
			puts "ignore: #{f}"
			next
		end
		#get the files path
		f_path = d+"/"+f
		#is the file parsable?
		if is_parsable? f
			parse_to_public f_path
		else
			#make the PUBLIC copy
			mk_public_file f_path
		end 
		#is this a directory?
		if Dir.exist? f_path
			#recurse!
			walk_directory f_path
		end
	end
end

#mk the PUBLIC dir if it doesn't exist
`mkdir #{PUBLIC}` unless File.exists? PUBLIC
#walk the SOURCE directory
walk_directory(SOURCE)