spohlenz-sassic 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
File without changes
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # Options:
5
+ # $ sassic <target> - create a new sassic site
6
+ # $ sassic [-p 4000] - start the sassic server (from within a sassic site)
7
+ # $ sassic build [-relative] - produce static output files
8
+ #
9
+
10
+ require 'rubygems'
11
+ require 'optparse'
12
+
13
+ require File.dirname(__FILE__) + '/../lib/sassic'
14
+
15
+ Sassic::Exec.run!
@@ -0,0 +1,8 @@
1
+ module Sassic; end
2
+
3
+ require File.dirname(__FILE__) + '/sassic/server'
4
+ require File.dirname(__FILE__) + '/sassic/generator'
5
+ require File.dirname(__FILE__) + '/sassic/builder'
6
+ require File.dirname(__FILE__) + '/sassic/tasks'
7
+
8
+ require File.dirname(__FILE__) + '/sassic/exec'
@@ -0,0 +1,95 @@
1
+ require 'open-uri'
2
+ require 'hpricot'
3
+
4
+ class Sassic::Builder
5
+ def initialize(server, options={})
6
+ @server = server
7
+ @relative = options[:relative]
8
+ @output = options[:output] || 'output'
9
+ @stylesheets = []
10
+ end
11
+
12
+ def run!
13
+ puts "Creating output directory..."
14
+ create_output_directory
15
+
16
+ # Static pages must be generated before stylesheets
17
+ puts "Generating pages..."
18
+ generate_static_pages
19
+
20
+ puts "Generating stylesheets..."
21
+ generate_stylesheets
22
+
23
+ puts "Copying images..."
24
+ copy_images
25
+
26
+ puts "Copying javascripts..."
27
+ copy_javascripts
28
+
29
+ if @relative
30
+ puts "Relativizing paths..."
31
+ relativize_paths
32
+ end
33
+
34
+ puts "Build completed."
35
+ end
36
+
37
+ private
38
+ def create_output_directory
39
+ FileUtils.rm_r(@output) if File.exists?(@output)
40
+ FileUtils.mkdir_p(@output)
41
+ end
42
+
43
+ def generate_static_pages
44
+ template_files.each do |template|
45
+ file = template.sub(/^templates\//, '').sub(/\.erb$/, '')
46
+ path = file.sub(/\.html/, '')
47
+
48
+ content = fetch_and_write(path, file)
49
+ extract_stylesheets(content)
50
+ end
51
+ end
52
+
53
+ def generate_stylesheets
54
+ FileUtils.mkdir_p("#{@output}/stylesheets")
55
+ @stylesheets.each { |stylesheet| fetch_and_write(stylesheet, stylesheet) }
56
+ end
57
+
58
+ def copy_images
59
+ FileUtils.cp_r 'images', "#{@output}/images"
60
+ end
61
+
62
+ def copy_javascripts
63
+ FileUtils.cp_r 'javascripts', "#{@output}/javascripts"
64
+ end
65
+
66
+ def relativize_paths
67
+ files = Dir["#{@output}/**/*.{html,js,css}"]
68
+ files.each do |file|
69
+ dirs = File.dirname(file).count('/')
70
+ prefix = dirs == 0 ? './' : (['../'] * dirs).join
71
+
72
+ content = open("#{file}").read
73
+ replaced = content.gsub(/\/(images|javascripts|stylesheets)/, "#{prefix}\\1")
74
+ File.open(file, 'w') { |f| f.write(replaced) }
75
+ end
76
+ end
77
+
78
+ def template_files
79
+ Dir['templates/**/*.html.erb'] - ['templates/layout.html.erb'] - Dir['templates/**/_*.html.erb']
80
+ end
81
+
82
+ def fetch_and_write(path, file)
83
+ content = open("#{@server}/#{path}").read
84
+ File.open("#{@output}/#{file}", 'w') { |f| f.write(content) }
85
+ content
86
+ end
87
+
88
+ def extract_stylesheets(content)
89
+ doc = Hpricot(content)
90
+ (doc/'link[@rel="stylesheet"]').each do |stylesheet|
91
+ src = stylesheet.attributes['href'].gsub(/^\//, '')
92
+ @stylesheets << src unless @stylesheets.include?(src)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,49 @@
1
+ class Sassic::Exec
2
+ def self.run!
3
+ target, options = parse_options!
4
+
5
+ if target == 'build'
6
+ # Build static output
7
+ Sassic::Tasks.build(options)
8
+ elsif target
9
+ # Create new sassic site
10
+ Sassic::Tasks.generate(target)
11
+ else
12
+ # Start server
13
+ Sassic::Server.new(options[:port] && options[:port] > 0 ? options[:port] : 2000).start
14
+ end
15
+ end
16
+
17
+ private
18
+ def self.parse_options!
19
+ options = {}
20
+
21
+ OptionParser.new do |opts|
22
+ opts.banner = <<-EOF
23
+ Usage: sassic <target> # Create a new sassic site
24
+ sassic [options] # Starts the sassic server
25
+ sassic build [options] # Build static output
26
+
27
+ EOF
28
+
29
+ opts.on('-p', '--port N', 'Specify port number (defaults to 2000)') do |port|
30
+ options[:port] = port.to_i
31
+ end
32
+
33
+ opts.on('-r', '--relative', 'When used with build, makes all paths in the output relative') do
34
+ options[:relative] = true
35
+ end
36
+
37
+ opts.on('-o', '--output DIR', 'When used with build, output files in into specified directory') do |output|
38
+ options[:output] = output
39
+ end
40
+
41
+ opts.on_tail("-h", "--help", "Show this message") do
42
+ puts opts
43
+ exit
44
+ end
45
+ end.parse!
46
+
47
+ [ ARGV.first, options ]
48
+ end
49
+ end
@@ -0,0 +1 @@
1
+ <h1>Your home page!</h1>
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+
6
+ <head>
7
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
8
+ <title><%= title %></title>
9
+
10
+ <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/screen.css" />
11
+ </head>
12
+
13
+ <body>
14
+
15
+ <%= content %>
16
+
17
+ </body>
18
+
19
+ </html>
File without changes
@@ -0,0 +1,42 @@
1
+ class Sassic::Generator
2
+ DIRECTORIES = ['images', 'sass', 'javascripts', 'templates']
3
+
4
+ FILES = {
5
+ 'layout.html.erb' => 'templates/layout.html.erb',
6
+ 'index.html.erb' => 'templates/index.html.erb',
7
+ 'screen.sass' => 'sass/screen.sass'
8
+ }
9
+
10
+ attr_reader :target
11
+
12
+ def initialize(target)
13
+ @target = target
14
+ end
15
+
16
+ def run!
17
+ create_directories!
18
+ create_files!
19
+ end
20
+
21
+ private
22
+ def create_directories!
23
+ create_directory(target)
24
+ DIRECTORIES.each { |d| create_directory(File.join(target, d)) }
25
+ end
26
+
27
+ def create_files!
28
+ FILES.each do |src, dest|
29
+ create_file(File.join(File.dirname(__FILE__), 'files', src), File.join(target, dest))
30
+ end
31
+ end
32
+
33
+ def create_directory(d)
34
+ FileUtils.mkdir_p(d)
35
+ puts "Created directory #{d}"
36
+ end
37
+
38
+ def create_file(src, dest)
39
+ FileUtils.cp src, dest
40
+ puts "Created file #{dest}"
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ require 'erb'
2
+
3
+ class Sassic::Page
4
+ def initialize(template)
5
+ @template = template
6
+ @template_content = File.read(template_path(template)) if exists?
7
+ @layout_content = File.read(layout_path)
8
+
9
+ # Preparse template content
10
+ content
11
+ end
12
+
13
+ def template_path(template)
14
+ Dir::pwd + '/templates/' + template + '.html.erb'
15
+ end
16
+
17
+ def layout_path
18
+ Dir::pwd + '/templates/layout.html.erb'
19
+ end
20
+
21
+ def exists?
22
+ File.exists?(template_path(@template))
23
+ end
24
+
25
+ def content
26
+ ERB.new(@template_content).result(binding)
27
+ end
28
+
29
+ def page
30
+ @template
31
+ end
32
+
33
+ def partial(template)
34
+ ERB.new(File.read(template_path("_#{template}"))).result(binding)
35
+ end
36
+
37
+ def title(new_title=nil)
38
+ @title = new_title if new_title
39
+ @title
40
+ end
41
+
42
+ def body_classes(new_classes=nil)
43
+ @body_classes = new_classes if new_classes
44
+ @body_classes
45
+ end
46
+
47
+ def render
48
+ ERB.new(@layout_content).result(binding)
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ require 'webrick'
2
+
3
+ require File.dirname(__FILE__) + '/servlets'
4
+
5
+ class Sassic::Server
6
+ include WEBrick
7
+
8
+ def initialize(port=2000)
9
+ @port = port
10
+ end
11
+
12
+ def start
13
+ s = HTTPServer.new(:Port => @port)
14
+ setup_mount_points(s)
15
+ trap("INT") { s.shutdown }
16
+ s.start
17
+ end
18
+
19
+ private
20
+ def setup_mount_points(s)
21
+ setup_static_mount_point(s, '/javascripts')
22
+ setup_static_mount_point(s, '/images')
23
+ s.mount("/stylesheets", Sassic::Servlets::SassServlet)
24
+ s.mount("/", Sassic::Servlets::LayoutServlet)
25
+ end
26
+
27
+ def setup_static_mount_point(s, folder)
28
+ s.mount(folder, HTTPServlet::FileHandler, Dir::pwd + folder)
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ module Sassic::Servlets; end
2
+
3
+ require File.dirname(__FILE__) + '/servlets/layout'
4
+ require File.dirname(__FILE__) + '/servlets/sass'
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../page'
2
+
3
+ class Sassic::Servlets::LayoutServlet < WEBrick::HTTPServlet::AbstractServlet
4
+ def do_GET(req, res)
5
+ page = Sassic::Page.new(req.path == '/' ? 'index' : req.path.sub(/^\//, ''))
6
+
7
+ if page.exists?
8
+ res.body = page.render
9
+ else
10
+ res.status = 404
11
+ res.body = "<h1>404 Not Available</h1>"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ require 'sass'
2
+
3
+ class Sassic::Servlets::SassServlet < WEBrick::HTTPServlet::AbstractServlet
4
+ def do_GET(req, res)
5
+ sass = Dir::pwd + req.path.sub('/stylesheets', '/sass').sub(/\.css$/, '.sass')
6
+
7
+ if File.exists?(sass)
8
+ res.body = sassify(sass)
9
+ else
10
+ res.status = 404
11
+ end
12
+ end
13
+
14
+ def sassify(file)
15
+ Sass::Engine.new(File.read(file), :style => :expanded, :load_paths => Dir::pwd + '/sass').render
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module Sassic::Tasks
2
+ def self.generate(target)
3
+ Sassic::Generator.new(target).run!
4
+ end
5
+
6
+ def self.build(options={})
7
+ port = options[:port] || 2000
8
+
9
+ # TODO: Start a new server
10
+
11
+ # Run builder
12
+ Sassic::Builder.new("http://localhost:#{port}", options).run!
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sassic"
3
+ s.version = "0.1"
4
+ s.date = "2008-09-11"
5
+
6
+ s.authors = [ "Sam Pohlenz" ]
7
+ s.email = "sam@sampohlenz.com"
8
+ s.homepage = "http://github.com/spohlenz/sassic"
9
+
10
+ s.summary = "Simply Ruby web framework with SASS support"
11
+ s.description = "Sassic is a simple framework for creating static web sites using SASS stylesheets."
12
+
13
+ s.has_rdoc = false
14
+
15
+ s.files = [ "README",
16
+ "sassic.gemspec",
17
+ "lib/sassic/exec.rb",
18
+ "lib/sassic/files/index.html.erb",
19
+ "lib/sassic/files/layout.html.erb",
20
+ "lib/sassic/files/screen.sass",
21
+ "lib/sassic/generator.rb",
22
+ "lib/sassic/page.rb",
23
+ "lib/sassic/server.rb",
24
+ "lib/sassic/servlets/layout.rb",
25
+ "lib/sassic/servlets/sass.rb",
26
+ "lib/sassic/servlets.rb",
27
+ "lib/sassic/builder.rb",
28
+ "lib/sassic/tasks.rb",
29
+ "lib/sassic.rb",
30
+ "bin/sassic" ]
31
+
32
+ s.executables = ['sassic']
33
+
34
+ #s.add_dependency("diff-lcs", ["> 0.0.0"])
35
+ #s.add_dependency("mime-types", ["> 0.0.0"])
36
+ #s.add_dependency("open4", ["> 0.0.0"])
37
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spohlenz-sassic
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Sam Pohlenz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Sassic is a simple framework for creating static web sites using SASS stylesheets.
17
+ email: sam@sampohlenz.com
18
+ executables:
19
+ - sassic
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - sassic.gemspec
27
+ - lib/sassic/exec.rb
28
+ - lib/sassic/files/index.html.erb
29
+ - lib/sassic/files/layout.html.erb
30
+ - lib/sassic/files/screen.sass
31
+ - lib/sassic/generator.rb
32
+ - lib/sassic/page.rb
33
+ - lib/sassic/server.rb
34
+ - lib/sassic/servlets/layout.rb
35
+ - lib/sassic/servlets/sass.rb
36
+ - lib/sassic/servlets.rb
37
+ - lib/sassic/builder.rb
38
+ - lib/sassic/tasks.rb
39
+ - lib/sassic.rb
40
+ - bin/sassic
41
+ has_rdoc: false
42
+ homepage: http://github.com/spohlenz/sassic
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Simply Ruby web framework with SASS support
67
+ test_files: []
68
+