Permalink
Browse files

Broken apart main parsing method

Should make code easier to reuse.
Templates starting with "-" now drop the . delimter and adopt the -
  • Loading branch information...
1 parent a566db8 commit 142ade7fcf64f53e035e4d57e5c69d8f1ebc8493 @chriskempson committed Oct 12, 2012
View
87 base16
@@ -36,7 +36,8 @@ class Theme
def build_single_scheme(scheme_file)
puts scheme_file
scheme_data = read_scheme_file(scheme_file)
- parse_template_files(scheme_data)
+ populate_template_variables(scheme_data)
+ create_output_files
end
def read_scheme_file(scheme_file)
@@ -80,53 +81,67 @@ class Theme
abort(read_error_message(template_file))
end
end
-
- def parse_template_files(scheme_data)
-
+
+ def populate_template_variables(scheme_data)
# Define ERB vars
- scheme = scheme_data["scheme"]
- author = scheme_data["author"]
- slug = slug(scheme_data["scheme"])
- uuid = SecureRandom.uuid
- base = {}
+ @scheme = scheme_data["scheme"]
+ @author = scheme_data["author"]
+ @slug = slug(scheme_data["scheme"])
+ # Define ERB color vars
+ @base = {}
[
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B",
"0C", "0D", "0E", "0F"
].each do |key|
hex = scheme_data["base" + key];
- base[key] = {
+ @base[key] = {
"hex" => hex,
"dhex" => to_dhex(hex),
"rgb" => to_rgb(hex),
"srgb" => to_srgb(hex)
}
end
-
+ end
+
+ def create_output_files
+ # Read each
read_template_dir.each do |template_file|
- puts " - " + template_file
+ puts " - " + template_file # Show which file we are parsing
- template_contents = read_template_file(template_file)
- parsed = ERB.new(template_contents)
-
- dir_name = File.dirname(template_file);
- file_name = File.basename(template_file, ".erb");
- scheme_name = slug(scheme)
+ # Grab the results of the parsed ERB file
+ contents = parse_template_file(template_file)
- make_dir("#{@output_dir}/#{dir_name}")
-
- #Emacs themes require -theme to appended to end of filename
- if dir_name=="emacs"
- scheme_name << "-theme"
- end
-
- output_file = File.open(
- "#{@output_dir}/#{dir_name}/base16-#{scheme_name}.#{file_name}",
- "w"
- )
- output_file.write parsed.result(binding)
+ write_output_file(template_file, contents)
end
+ end
+
+ def parse_template_file(template_file)
+ # Define ERB vars
+ @uuid = SecureRandom.uuid
+
+ template_contents = read_template_file(template_file)
+ parsed = ERB.new(template_contents)
+
+ return parsed.result(binding)
+ end
+
+ def write_output_file(template_file, contents)
+ dir_name = File.dirname(template_file);
+ file_name = File.basename(template_file, ".erb");
+ scheme_name = slug(@scheme)
+
+ make_dir("#{@output_dir}/#{dir_name}")
+
+ # If the filename starts with a dash, we use the dash to separate
+ delimiter = file_name.starts_with?("-") ? "" : "."
+ output_file = File.open(
+ "#{@output_dir}/#{dir_name}/base16-#{scheme_name}#{delimiter}#{file_name}",
+ "w"
+ )
+
+ output_file.write(contents)
end
def make_dir(name)
@@ -152,12 +167,24 @@ class Theme
hex.scan(/../).map {|color| color.to_i(16).to_f / 255 }
end
+ def split_by_slash(hex)
+ hex.scan(/.{1,2}/).join('/')
+ end
+
def read_error_message(file)
"Error reading #{file}"
end
end
+# Provide starts_with? method borrowed from Rails
+class String
+ def starts_with?(prefix)
+ prefix = prefix.to_s
+ self[0, prefix.length] == prefix
+ end
+end
+
help_message = <<-EOF
Base16 Builder v0.1
https://github.com/chriskempson/base16-builder
@@ -1,33 +1,31 @@
<%
# Emacs Template
# Neil Bhakta
-%>
+%>;; Base16 <%= @scheme %> (https://github.com/chriskempson/base16)
+;; Scheme: <%= @author %>
-;; Base16 <%= scheme %> (https://github.com/chriskempson/base16)
-;; Scheme: <%= author %>
-
-;;; base16-<%= slug %>-theme.el
+;;; base16-<%= @slug %>-theme.el
;;; Code:
-(deftheme base16-<%= slug %>)
-
-(let ((background "#<%= base["01"]["hex"] %>")
- (current-line "#<%= base["03"]["hex"] %>")
- (selection "#<%= base["04"]["hex"] %>")
- (foreground "#<%= base["06"]["hex"] %>")
- (comment "#<%= base["04"]["hex"] %>")
- (cursor "#<%= base["06"]["hex"] %>")
- (red "#<%= base["08"]["hex"] %>")
- (orange "#<%= base["09"]["hex"] %>")
- (yellow "#<%= base["0A"]["hex"] %>")
- (green "#<%= base["0B"]["hex"] %>")
- (aqua "#<%= base["0C"]["hex"] %>")
- (blue "#<%= base["0D"]["hex"] %>")
- (purple "#<%= base["0E"]["hex"] %>"))
+(deftheme base16-<%= @slug %>)
+
+(let ((background "#<%= @base["01"]["hex"] %>")
+ (current-line "#<%= @base["03"]["hex"] %>")
+ (selection "#<%= @base["04"]["hex"] %>")
+ (foreground "#<%= @base["06"]["hex"] %>")
+ (comment "#<%= @base["04"]["hex"] %>")
+ (cursor "#<%= @base["06"]["hex"] %>")
+ (red "#<%= @base["08"]["hex"] %>")
+ (orange "#<%= @base["09"]["hex"] %>")
+ (yellow "#<%= @base["0A"]["hex"] %>")
+ (green "#<%= @base["0B"]["hex"] %>")
+ (aqua "#<%= @base["0C"]["hex"] %>")
+ (blue "#<%= @base["0D"]["hex"] %>")
+ (purple "#<%= @base["0E"]["hex"] %>"))
(custom-theme-set-faces
- 'base16-<%= slug %>
+ 'base16-<%= @slug %>
;; Built-in stuff (Emacs 23)
`(default ((t (:background ,background :foreground ,foreground))))
@@ -76,7 +74,7 @@
`(rainbow-delimiters-depth-9-face ((t (:foreground ,foreground)))))
(custom-theme-set-variables
- 'base16-<%= slug %>
+ 'base16-<%= @slug %>
`(ansi-color-names-vector
;; black, red, green, yellow, blue, magenta, cyan, white
@@ -85,6 +83,6 @@
;; black, red, green, yellow, blue, magenta, cyan, white
[unspecified ,background ,red ,green ,yellow ,blue ,purple ,blue ,foreground])))
-(provide-theme 'base16-<%= slug %>)
+(provide-theme 'base16-<%= @slug %>)
-;;; base16-<%= slug %>-theme.el ends here
+;;; base16-<%= @slug %>-theme.el ends here
@@ -3,22 +3,22 @@
# Chris Kempson (http://chriskempson.com)
%>GIMP Palette
-Name: Base16 <%= scheme %>
+Name: Base16 <%= @scheme %>
Columns: 16
#
- <%= base["00"]["rgb"][0] %> <%= base["00"]["rgb"][1] %> <%= base["00"]["rgb"][2] %> base00
- <%= base["01"]["rgb"][0] %> <%= base["01"]["rgb"][1] %> <%= base["01"]["rgb"][2] %> base01
- <%= base["02"]["rgb"][0] %> <%= base["02"]["rgb"][1] %> <%= base["02"]["rgb"][2] %> base02
- <%= base["03"]["rgb"][0] %> <%= base["03"]["rgb"][1] %> <%= base["03"]["rgb"][2] %> base03
- <%= base["04"]["rgb"][0] %> <%= base["04"]["rgb"][1] %> <%= base["04"]["rgb"][2] %> base04
- <%= base["05"]["rgb"][0] %> <%= base["05"]["rgb"][1] %> <%= base["05"]["rgb"][2] %> base05
- <%= base["06"]["rgb"][0] %> <%= base["06"]["rgb"][1] %> <%= base["06"]["rgb"][2] %> base06
- <%= base["07"]["rgb"][0] %> <%= base["07"]["rgb"][1] %> <%= base["07"]["rgb"][2] %> base07
- <%= base["08"]["rgb"][0] %> <%= base["08"]["rgb"][1] %> <%= base["08"]["rgb"][2] %> base08
- <%= base["09"]["rgb"][0] %> <%= base["09"]["rgb"][1] %> <%= base["09"]["rgb"][2] %> base09
- <%= base["0A"]["rgb"][0] %> <%= base["0A"]["rgb"][1] %> <%= base["0A"]["rgb"][2] %> base0A
- <%= base["0B"]["rgb"][0] %> <%= base["0B"]["rgb"][1] %> <%= base["0B"]["rgb"][2] %> base0B
- <%= base["0C"]["rgb"][0] %> <%= base["0C"]["rgb"][1] %> <%= base["0C"]["rgb"][2] %> base0C
- <%= base["0D"]["rgb"][0] %> <%= base["0D"]["rgb"][1] %> <%= base["0D"]["rgb"][2] %> base0D
- <%= base["0E"]["rgb"][0] %> <%= base["0E"]["rgb"][1] %> <%= base["0E"]["rgb"][2] %> base0E
- <%= base["0F"]["rgb"][0] %> <%= base["0F"]["rgb"][1] %> <%= base["0F"]["rgb"][2] %> base0F
+ <%= @base["00"]["rgb"][0] %> <%= @base["00"]["rgb"][1] %> <%= @base["00"]["rgb"][2] %> base00
+ <%= @base["01"]["rgb"][0] %> <%= @base["01"]["rgb"][1] %> <%= @base["01"]["rgb"][2] %> base01
+ <%= @base["02"]["rgb"][0] %> <%= @base["02"]["rgb"][1] %> <%= @base["02"]["rgb"][2] %> base02
+ <%= @base["03"]["rgb"][0] %> <%= @base["03"]["rgb"][1] %> <%= @base["03"]["rgb"][2] %> base03
+ <%= @base["04"]["rgb"][0] %> <%= @base["04"]["rgb"][1] %> <%= @base["04"]["rgb"][2] %> base04
+ <%= @base["05"]["rgb"][0] %> <%= @base["05"]["rgb"][1] %> <%= @base["05"]["rgb"][2] %> base05
+ <%= @base["06"]["rgb"][0] %> <%= @base["06"]["rgb"][1] %> <%= @base["06"]["rgb"][2] %> base06
+ <%= @base["07"]["rgb"][0] %> <%= @base["07"]["rgb"][1] %> <%= @base["07"]["rgb"][2] %> base07
+ <%= @base["08"]["rgb"][0] %> <%= @base["08"]["rgb"][1] %> <%= @base["08"]["rgb"][2] %> base08
+ <%= @base["09"]["rgb"][0] %> <%= @base["09"]["rgb"][1] %> <%= @base["09"]["rgb"][2] %> base09
+ <%= @base["0A"]["rgb"][0] %> <%= @base["0A"]["rgb"][1] %> <%= @base["0A"]["rgb"][2] %> base0A
+ <%= @base["0B"]["rgb"][0] %> <%= @base["0B"]["rgb"][1] %> <%= @base["0B"]["rgb"][2] %> base0B
+ <%= @base["0C"]["rgb"][0] %> <%= @base["0C"]["rgb"][1] %> <%= @base["0C"]["rgb"][2] %> base0C
+ <%= @base["0D"]["rgb"][0] %> <%= @base["0D"]["rgb"][1] %> <%= @base["0D"]["rgb"][2] %> base0D
+ <%= @base["0E"]["rgb"][0] %> <%= @base["0E"]["rgb"][1] %> <%= @base["0E"]["rgb"][2] %> base0E
+ <%= @base["0F"]["rgb"][0] %> <%= @base["0F"]["rgb"][1] %> <%= @base["0F"]["rgb"][2] %> base0F
@@ -3,11 +3,11 @@
# Chris Kempson (http://chriskempson.com)
%>#!/usr/bin/env bash
-# Base16 <%= scheme %> - Gnome Terminal color scheme install script
-# <%= author %>
+# Base16 <%= @scheme %> - Gnome Terminal color scheme install script
+# <%= @author %>
-[[ -z "$PROFILE_NAME" ]] && PROFILE_NAME="Base 16 <%= scheme %>"
-[[ -z "$PROFILE_SLUG" ]] && PROFILE_SLUG="base-16-<%= slug %>"
+[[ -z "$PROFILE_NAME" ]] && PROFILE_NAME="Base 16 <%= @scheme %>"
+[[ -z "$PROFILE_SLUG" ]] && PROFILE_SLUG="base-16-<%= @slug %>"
[[ -z "$GCONFTOOL" ]] && GCONFTOOL=gconftool
[[ -z "$BASE_KEY" ]] && BASE_KEY=/apps/gnome-terminal/profiles
@@ -41,10 +41,10 @@ glist_append() {
glist_append string /apps/gnome-terminal/global/profile_list "$PROFILE_SLUG"
gset string visible_name "$PROFILE_NAME"
-gset string palette "#<%= base["00"]["dhex"] %>:#<%= base["01"]["dhex"] %>:#<%= base["02"]["dhex"] %>:#<%= base["03"]["dhex"] %>:#<%= base["04"]["dhex"] %>:#<%= base["05"]["dhex"] %>:#<%= base["06"]["dhex"] %>:#<%= base["07"]["dhex"] %>:#<%= base["08"]["dhex"] %>:#<%= base["09"]["dhex"] %>:#<%= base["0A"]["dhex"] %>:#<%= base["0B"]["dhex"] %>:#<%= base["0C"]["dhex"] %>:#<%= base["0D"]["dhex"] %>:#<%= base["0E"]["dhex"] %>:#<%= base["0F"]["dhex"] %>"
-gset string background_color "#<%= base["00"]["dhex"] %>"
-gset string foreground_color "#<%= base["05"]["dhex"] %>"
-gset string bold_color "#<%= base["05"]["dhex"] %>"
+gset string palette "#<%= @base["00"]["dhex"] %>:#<%= @base["01"]["dhex"] %>:#<%= @base["02"]["dhex"] %>:#<%= @base["03"]["dhex"] %>:#<%= @base["04"]["dhex"] %>:#<%= @base["05"]["dhex"] %>:#<%= @base["06"]["dhex"] %>:#<%= @base["07"]["dhex"] %>:#<%= @base["08"]["dhex"] %>:#<%= @base["09"]["dhex"] %>:#<%= @base["0A"]["dhex"] %>:#<%= @base["0B"]["dhex"] %>:#<%= @base["0C"]["dhex"] %>:#<%= @base["0D"]["dhex"] %>:#<%= @base["0E"]["dhex"] %>:#<%= @base["0F"]["dhex"] %>"
+gset string background_color "#<%= @base["00"]["dhex"] %>"
+gset string foreground_color "#<%= @base["05"]["dhex"] %>"
+gset string bold_color "#<%= @base["05"]["dhex"] %>"
gset bool bold_color_same_as_fg "true"
gset bool use_theme_colors "false"
gset bool use_theme_background "false"
Oops, something went wrong.

0 comments on commit 142ade7

Please sign in to comment.