hydeweb 0.0.1.pre4 → 0.0.1.pre5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1.pre4
1
+ 0.0.1.pre5
data/bin/hyde CHANGED
@@ -6,66 +6,12 @@ require 'optparse'
6
6
  require 'rubygems'
7
7
  require 'hyde'
8
8
 
9
- options = {
10
- }
11
-
12
- opts = OptionParser.new do |o|
13
- o.banner = "usage: hyde <action> [<options>]"
14
- o.separator ""
15
-
16
- #o.on("-w", "--watch", "watch for changes") do
17
- # options[:watch] = true
18
- #end
19
-
20
- o.separator "Actions:"
21
- o.separator " start - Starts a server"
22
- o.separator " build - Creates HTML files"
23
- o.separator " create <name> - Starts a project"
24
- o.separator ""
25
- o.separator "Options:"
26
-
27
- # Help
28
- o.on_tail("-h", "--help", "show this message") do
29
- puts opts
30
- exit
31
- end
9
+ if ARGV.size == 0
10
+ Hyde::CLICommands::Help.run
11
+ exit
32
12
  end
33
13
 
34
- opts.parse!
35
- options[:action] = ARGV[0]
36
- options[:params] = []
37
- (1...ARGV.size).each { |i| options[:params] << ARGV[i] }
38
-
39
- ostream = STDERR
40
-
41
- if options[:action] == 'start'
42
- system "ruby #{lib_path}/hyde/init.rb"
43
-
44
- elsif options[:action] == 'build'
45
- Hyde::Project.new.build ostream
46
-
47
- elsif options[:action] == 'create'
48
- unless options[:params].size == 1
49
- puts opts
50
-
51
- else
52
- site_name = options[:params][0]
53
- if File.directory? site_name
54
- ostream << "This directory already exists!\n"
55
- exit
56
- end
57
-
58
- from = File.join(File.dirname(__FILE__), '..', 'data', 'new_site')
59
- to = File.expand_path(File.join(Dir.pwd, site_name))
60
-
61
- require 'fileutils'
62
- FileUtils.cp_r from, to
63
-
64
- Dir[File.join(to, '**/*')].each do |file|
65
- ostream << " * " << file.gsub(Dir.pwd, '') << "\n"
66
- end
67
- end
68
-
69
- else
70
- puts opts
71
- end
14
+ # Can error
15
+ controller = Hyde::CLICommands.get_controller ARGV[0]
16
+ params = ARGV[1..-1]
17
+ controller.run(*params)
@@ -0,0 +1,42 @@
1
+ module Hyde
2
+ class CLICommand
3
+ @@description = nil
4
+
5
+ def self.ostream
6
+ STDERR
7
+ end
8
+
9
+ def self.log(str)
10
+ ostream << str << "\n"
11
+ end
12
+
13
+ def self.help
14
+ log "No help for this command."
15
+ end
16
+
17
+ def self.lib_path
18
+ @@lib_path ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
19
+ end
20
+
21
+ def self.project
22
+ begin
23
+ Hyde::Project.new
24
+ rescue NoRootError
25
+ ostream << "No Hyde config file found. (looking for: hyde.conf, _config.yml)\n"
26
+ exit
27
+ end
28
+ end
29
+
30
+ def self.desc(str)
31
+ class_eval %{
32
+ def self.description
33
+ "#{str.gsub('\"','\\\"')}"
34
+ end
35
+ }
36
+ end
37
+
38
+ def self.description
39
+ ""
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,87 @@
1
+ module Hyde
2
+ module CLICommands
3
+ extend self
4
+
5
+ def get_controller(str)
6
+ begin
7
+ class_name = str.downcase.capitalize.to_sym
8
+ controller = Hyde::CLICommands.const_get(class_name)
9
+ rescue NameError
10
+ STDERR << "Unknown command: #{str}\n"
11
+ STDERR << "Type `hyde` for a list of commands.\n"
12
+ exit
13
+ end
14
+ controller
15
+ end
16
+
17
+ class Help < CLICommand
18
+ desc "Shows help"
19
+
20
+ def self.run(*a)
21
+ if a.size == 0
22
+ general_help
23
+ exit
24
+ end
25
+
26
+ controller = CLICommands::get_controller a[0]
27
+ controller.help
28
+ end
29
+
30
+ def self.general_help
31
+ log "Usage: hyde <command> [arguments]"
32
+ log ""
33
+ log "Commands:"
34
+ CLICommands.constants.each do |class_name|
35
+ klass = CLICommands.const_get(class_name)
36
+ name = class_name.to_s.downcase
37
+ puts " #{name}%s#{klass.description}" % [' ' * (20-name.size)]
38
+ end
39
+
40
+ log ""
41
+ log "For more info on a command, type `hyde help <command>`."
42
+ end
43
+ end
44
+
45
+ class Build < CLICommand
46
+ desc "Builds the HTML files"
47
+ def self.run(*a)
48
+ project.build ostream
49
+ end
50
+ end
51
+
52
+ class Start < CLICommand
53
+ desc "Starts the server"
54
+ def self.run(*a)
55
+ project
56
+ system "ruby \"%s\"" % [File.join(lib_path, 'hyde', 'init.rb')]
57
+ end
58
+ end
59
+
60
+ class Create < CLICommand
61
+ desc "Starts a new Hyde project"
62
+ def self.run(*a)
63
+ unless a.size == 1
64
+ log "Usage: hyde create <sitename>"
65
+ exit
66
+
67
+ else
68
+ site_name = a[0]
69
+ if File.directory? site_name
70
+ log "Directory `#{site_name}` already exists!"
71
+ exit
72
+ end
73
+
74
+ from = File.join(lib_path, '..', 'data', 'new_site')
75
+ to = File.expand_path(File.join(Dir.pwd, site_name))
76
+
77
+ require 'fileutils'
78
+ FileUtils.cp_r from, to
79
+
80
+ Dir[File.join(to, '**/*')].each do |file|
81
+ log " * %s" % [file.gsub(/^#{Regexp.escape(Dir.pwd)}\/?/, '')]
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
data/lib/hyde/init.rb CHANGED
@@ -1,17 +1,25 @@
1
1
  require 'rubygems'
2
- require 'sinatra/base'
3
- require "logger"
2
+ begin
3
+ require 'sinatra/base'
4
+ rescue LoadError
5
+ STDERR << "You need the sinatra gem to use `hyde start`. Type: `gem install sinatra`\n"
6
+ exit
7
+ end
4
8
 
5
9
  $:.unshift File.dirname(__FILE__) + "/.."
6
10
  require 'hyde'
7
11
 
8
- puts "Starting server..."
9
- puts " http://127.0.0.1:4567 Homepage"
10
- puts " http://127.0.0.1:4567/- File list"
11
- puts ""
12
+ $project = Hyde::Project.new
12
13
 
13
14
  class Main < Sinatra::Base
14
- @@project ||= Hyde::Project.new
15
+ @@project ||= $project
16
+
17
+ def self.show_start
18
+ puts "Starting server..."
19
+ puts " http://127.0.0.1:4567 Homepage"
20
+ puts " http://127.0.0.1:4567/- File list"
21
+ puts ""
22
+ end
15
23
 
16
24
  get '/-' do
17
25
  @@project.files.inject("") do |a, path|
@@ -26,10 +34,15 @@ class Main < Sinatra::Base
26
34
  type = File.extname(path)[1..-1]
27
35
  content_type type.to_sym if type.is_a? String
28
36
  @@project.render path
37
+ rescue Hyde::RenderError => e
38
+ puts " * `#{path}` error"
39
+ puts " *** #{e.message}".gsub("\n","\n *** ")
40
+ e.message
29
41
  rescue Hyde::NotFound
30
42
  raise Sinatra::NotFound
31
43
  end
32
44
  end
33
45
  end
34
46
 
47
+ Main.show_start
35
48
  Main.run!
data/lib/hyde/project.rb CHANGED
@@ -19,14 +19,29 @@ module Hyde
19
19
  # The configuration k/v storage (Hash)
20
20
  attr_accessor :config
21
21
 
22
+ # Can raise a NoRootError
22
23
  def initialize( root = Dir.pwd )
23
24
  @config = OStruct.new defaults
25
+ @root, @config_file = find_root_from root
26
+ @config.merge! YAML::load_file(@config_file) if File.exists? @config_file
27
+ end
24
28
 
25
- # find_root
26
- @root = root
27
- @config_file ||= "#{@root}/_config.yml"
29
+ def find_root_from(start)
30
+ root = File.expand_path(start)
31
+ ret = nil
32
+ while ret.nil?
33
+ # See if any of these files exist
34
+ ['_config.yml', 'hyde.conf'].each do |config_name|
35
+ config_file = File.join(root, config_name)
36
+ ret ||= [root, config_file] if File.exists? config_file
37
+ end
28
38
 
29
- @config.merge! YAML::load_file(@config_file) if File.exists? @config_file
39
+ # Traverse back (die if we reach the root)
40
+ old_root = root
41
+ root = File.expand_path(File.join(root, '..'))
42
+ raise NoRootError if root == old_root
43
+ end
44
+ ret
30
45
  end
31
46
 
32
47
  def method_missing(meth, *args, &blk)
@@ -58,11 +73,17 @@ module Hyde
58
73
  Dir.mkdir root(:output) unless File.directory? root(:output)
59
74
 
60
75
  begin
76
+ continue = true
61
77
  files.each do |path|
62
78
  ostream << " * #{output_path}/#{path}\n" if ostream
63
- mfile = force_file_open(root(:output, path))
64
- mfile << render(path)
65
- mfile.close
79
+ begin
80
+ rendered = render(path)
81
+ mfile = force_file_open(root(:output, path))
82
+ mfile << rendered
83
+ mfile.close
84
+ rescue RenderError => e
85
+ ostream << " *** Error: #{e.message}".gsub("\n", "\n *** ") << "\n"
86
+ end
66
87
  end
67
88
  rescue NoGemError => e
68
89
  ostream << "Error: #{e.message}\n"
@@ -71,7 +92,7 @@ module Hyde
71
92
 
72
93
  # Returns a list of all URL paths
73
94
  def files
74
- @file_list ||= Dir[File.join(root(:site), '**', '*')].inject([]) do |a, match|
95
+ @file_list = Dir[File.join(root(:site), '**', '*')].inject([]) do |a, match|
75
96
  # Make sure its the canonical name
76
97
  path = File.expand_path(match)
77
98
  file = path.gsub /^#{Regexp.escape root(:site)}\/?/, ''
@@ -20,8 +20,12 @@ module Hyde
20
20
  class Less < Renderer::Base
21
21
  def evaluate(scope, data={}, &block)
22
22
  require_lib 'less'
23
- @engine = ::Less::Engine.new(File.open(filename))
24
- @engine.to_css
23
+ begin
24
+ @engine = ::Less::Engine.new(File.open(filename))
25
+ @engine.to_css
26
+ rescue ::Less::SyntaxError => e
27
+ raise Hyde::RenderError, e.message
28
+ end
25
29
  end
26
30
  end
27
31
 
data/lib/hyde.rb CHANGED
@@ -10,14 +10,13 @@ module Hyde
10
10
  autoload :Renderers, "#{prefix}/hyde/renderers"
11
11
  autoload :Utils, "#{prefix}/hyde/utils"
12
12
  autoload :Scope, "#{prefix}/hyde/scope"
13
+ autoload :CLICommand, "#{prefix}/hyde/clicommand"
14
+ autoload :CLICommands,"#{prefix}/hyde/clicommands"
13
15
  autoload :TemplateHelpers,"#{prefix}/hyde/template_helpers"
14
16
 
15
- class Exception < ::Exception
16
- end
17
-
18
- class NotFound < Exception
19
- end
20
-
21
- class NoGemError < Exception
22
- end
17
+ Error = Class.new(::StandardError)
18
+ RenderError = Class.new(Error)
19
+ NoGemError = Class.new(Error)
20
+ NotFound = Class.new(Error)
21
+ NoRootError = Class.new(Error)
23
22
  end
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 0
8
8
  - 1
9
- - pre4
10
- version: 0.0.1.pre4
9
+ - pre5
10
+ version: 0.0.1.pre5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rico Sta. Cruz
@@ -84,6 +84,8 @@ files:
84
84
  - data/new_site/layouts/default.haml
85
85
  - data/new_site/site/index.html.haml
86
86
  - lib/hyde.rb
87
+ - lib/hyde/clicommand.rb
88
+ - lib/hyde/clicommands.rb
87
89
  - lib/hyde/init.rb
88
90
  - lib/hyde/layout.rb
89
91
  - lib/hyde/ostruct.rb