fnando-glue 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -26,7 +26,9 @@ Just run `glue /some/path` to generate your site skeleton.
26
26
  TODO
27
27
  ----
28
28
 
29
- Write tests
29
+ * Write documentation
30
+ * Automatically generate sitemap
31
+ * Generate feed
30
32
 
31
33
  MAINTAINER
32
34
  ----------
data/Rakefile CHANGED
@@ -19,6 +19,7 @@ spec = Gem::Specification.new do |s|
19
19
  s.add_dependency "RedCloth"
20
20
  s.add_dependency "rdiscount"
21
21
  s.add_dependency "haml"
22
+ s.add_dependency "builder"
22
23
  end
23
24
 
24
25
  namespace :gem do
@@ -72,4 +73,4 @@ namespace :gem do
72
73
  system "sudo gem install #{spec.instance_variable_get('@name')}"
73
74
  system "rm *.gem"
74
75
  end
75
- end
76
+ end
data/glue.gemspec CHANGED
@@ -14,14 +14,14 @@ Gem::Specification.new do |s|
14
14
  s.add_dependency "RedCloth", ">= 0"
15
15
  s.add_dependency "rdiscount", ">= 0"
16
16
  s.add_dependency "haml", ">= 0"
17
- s.version = "0.0.2"
17
+ s.add_dependency "builder", ">= 0"
18
+ s.version = "0.0.3"
18
19
  s.require_paths = ["lib"]
19
20
  s.files = ["Rakefile",
20
21
  "glue.gemspec",
21
22
  "README.markdown",
22
23
  "bin/glue",
23
24
  "lib/glue",
24
- "lib/glue/capistrano.rb",
25
25
  "lib/glue/generator.rb",
26
26
  "lib/glue/helper.rb",
27
27
  "lib/glue/rake.rb",
@@ -29,16 +29,21 @@ Gem::Specification.new do |s|
29
29
  "lib/glue/template/helper.rb",
30
30
  "lib/glue/template/meta.rb",
31
31
  "lib/glue/template/parser.rb",
32
+ "lib/glue/template/sitemap.rb",
32
33
  "lib/glue/template.rb",
33
34
  "lib/glue.rb",
34
35
  "lib/vendor",
35
36
  "lib/vendor/colorize-0.0.1",
36
37
  "lib/vendor/colorize-0.0.1/colorize.rb",
38
+ "templates/404.haml",
37
39
  "templates/_footer.haml",
40
+ "templates/glue.yml",
38
41
  "templates/helper.rb",
42
+ "templates/htaccess",
39
43
  "templates/index.haml",
40
44
  "templates/main.haml",
41
45
  "templates/Rakefile",
46
+ "templates/robots.txt",
42
47
  "templates/style.sass"]
43
48
  s.authors = ["Nando Vieira"]
44
49
  end
@@ -12,9 +12,9 @@ module Glue
12
12
  FileUtils.mkdir_p(output)
13
13
  end
14
14
 
15
- # create views directory
16
- run %(creating #{Colorize.yellow("views/layouts")} directory) do
17
- FileUtils.mkdir_p File.join(output, "views/layouts")
15
+ # create public directory
16
+ run %(creating #{Colorize.yellow("public")} directory) do
17
+ # FileUtils.mkdir_p File.join(output, "public")
18
18
  end
19
19
 
20
20
  # create javascripts directory
@@ -22,22 +22,32 @@ module Glue
22
22
  FileUtils.mkdir_p File.join(output, "public/javascripts")
23
23
  end
24
24
 
25
+ # create images directory
26
+ run %(creating #{Colorize.yellow("public/images")} directory) do
27
+ FileUtils.mkdir_p File.join(output, "public/images")
28
+ end
29
+
30
+ # create views directory
31
+ run %(creating #{Colorize.yellow("views/layouts")} directory) do
32
+ FileUtils.mkdir_p File.join(output, "views/layouts")
33
+ end
34
+
25
35
  # create config directory
26
36
  run %(creating #{Colorize.yellow("config")} directory) do
27
37
  FileUtils.mkdir_p File.join(output, "config")
28
38
  end
29
39
 
40
+ # copy rake file
41
+ run %( copying #{Colorize.yellow("config/glue.yml")}) do
42
+ FileUtils.cp File.join(templates, "glue.yml"), File.join(output, "config/glue.yml")
43
+ end
44
+
30
45
  # copy stylesheet file
31
46
  run %( copying #{Colorize.yellow("views/stylesheets/style.sass")}) do
32
47
  FileUtils.mkdir_p File.join(output, "views/stylesheets")
33
48
  FileUtils.cp File.join(templates, "style.sass"), File.join(output, "views/stylesheets/style.sass")
34
49
  end
35
50
 
36
- # create images directory
37
- run %(creating #{Colorize.yellow("public/images")} directory) do
38
- FileUtils.mkdir_p File.join(output, "public/images")
39
- end
40
-
41
51
  # copy rake file
42
52
  run %( copying #{Colorize.yellow("Rakefile")}) do
43
53
  FileUtils.cp File.join(templates, "Rakefile"), File.join(output, "Rakefile")
@@ -58,10 +68,25 @@ module Glue
58
68
  FileUtils.cp File.join(templates, "index.haml"), File.join(output, "views/index.haml")
59
69
  end
60
70
 
71
+ # copy 404.haml
72
+ run %( copying #{Colorize.yellow("views/404.haml")}) do
73
+ FileUtils.cp File.join(templates, "404.haml"), File.join(output, "views/404.haml")
74
+ end
75
+
61
76
  # copy _footer.haml
62
77
  run %( copying #{Colorize.yellow("views/_footer.haml")}) do
63
78
  FileUtils.cp File.join(templates, "_footer.haml"), File.join(output, "views/_footer.haml")
64
79
  end
80
+
81
+ # copy .htaccess
82
+ run %( copying #{Colorize.yellow("public/.htaccess")}) do
83
+ FileUtils.cp File.join(templates, "htaccess"), File.join(output, "public/.htaccess")
84
+ end
85
+
86
+ # copy robots.txt
87
+ run %( copying #{Colorize.yellow("public/robots.txt")}) do
88
+ FileUtils.cp File.join(templates, "robots.txt"), File.join(output, "public/robots.txt")
89
+ end
65
90
  end
66
91
  end
67
92
  end
data/lib/glue/rake.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require File.dirname(__FILE__) + "/../glue"
2
2
 
3
+ task :default => "glue:auto"
4
+
3
5
  namespace :glue do
4
6
  task :default => :generate
5
7
 
@@ -7,4 +9,43 @@ namespace :glue do
7
9
  task :generate do
8
10
  Glue::Template.generate!
9
11
  end
12
+
13
+ desc "Auto-generate pages"
14
+ task :auto do
15
+ thread = Thread.new do
16
+ latest_mtime = 0
17
+
18
+ trap 'INT' do
19
+ puts
20
+ thread.exit
21
+ end
22
+
23
+ loop do
24
+ files = Dir["views/**/*.*"] + Dir["config/**/*.*"]
25
+ changes = []
26
+
27
+ mtime = files.collect do |file|
28
+ mt = File.mtime(file).to_i
29
+ changes << file if latest_mtime > 0 && mt > latest_mtime
30
+ mt
31
+ end.max
32
+
33
+ if latest_mtime < mtime
34
+ if changes.size > 0
35
+ puts Colorize.yellow("\n\n#{changes.size} file(s) changed") + " - #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
36
+ changes.each do |file|
37
+ puts " - #{Colorize.blue(file, :options => :highlight)}"
38
+ end unless latest_mtime == 0
39
+ end
40
+ latest_mtime = mtime
41
+ Glue::Template.generate!
42
+ puts
43
+ end
44
+
45
+ sleep ENV["WAIT"] || 2
46
+ end
47
+ end
48
+
49
+ thread.join
50
+ end
10
51
  end
@@ -10,7 +10,7 @@ module Glue
10
10
  end
11
11
 
12
12
  def haml(text)
13
- Haml::Engine.new(text).to_html(self)
13
+ Haml::Engine.new(text).to_html(self, $meta.data)
14
14
  end
15
15
 
16
16
  def render(path)
@@ -1,19 +1,19 @@
1
1
  module Glue
2
2
  module Template
3
3
  class Meta
4
- attr_accessor :metadata
4
+ attr_accessor :data
5
5
  attr_accessor :body
6
6
  attr_accessor :content
7
7
 
8
8
  def initialize(path)
9
- self.metadata = {}
9
+ self.data = {}
10
10
  self.body = File.read(path)
11
11
  process!
12
12
  end
13
13
 
14
14
  def process!
15
15
  self.content = body.gsub /^\/ *(.*?): *(.*?) *$/sm do |m|
16
- metadata[$1] = $2
16
+ data[$1.to_sym] = $2
17
17
  ""
18
18
  end
19
19
  end
@@ -2,17 +2,23 @@ module Glue
2
2
  module Template
3
3
  class Parser
4
4
  attr_accessor :root
5
+ attr_accessor :sitemap
6
+ attr_accessor :config
5
7
 
6
8
  include Glue::Helper
7
9
 
8
10
  def initialize(root)
9
11
  @root = root
12
+ @config = YAML.load_file("#{root}/config/glue.yml")
13
+
14
+ @sitemap = Glue::Template::Sitemap.new
15
+ @sitemap.base_url = config["base_url"]
10
16
  end
11
17
 
12
18
  def run!
13
19
  # load user helper file if exist
14
- if File.exists?("#{root}/helper.rb")
15
- require "#{root}/helper.rb"
20
+ if File.exists?("#{root}/config/helper.rb")
21
+ require "#{root}/config/helper.rb"
16
22
  Glue::Template::Helper.__send__ :include, ::Helper
17
23
  end
18
24
 
@@ -29,36 +35,65 @@ module Glue
29
35
  next if path =~ /#{Regexp.escape("#{root}/views/layouts/")}/
30
36
 
31
37
  # partial files should be skipped
32
- next if path =~ /_.*?\.haml$/
38
+ next if File.basename(path) =~ /_.*?\.haml$/
33
39
 
34
40
  run "processing #{Colorize.yellow(path)}" do
35
41
  render_haml(path)
36
42
  end
37
43
  end
44
+
45
+ # save sitemap
46
+ run "generating #{Colorize.yellow("public/sitemap.xml")}..." do
47
+ sitemap.save File.join(root, "public", "sitemap.xml")
48
+ end
38
49
  end
39
50
 
40
51
  def render_haml(path)
41
52
  # extract metadata from file
42
- meta = Glue::Template::Meta.new(path)
53
+ $meta = Glue::Template::Meta.new(path)
43
54
 
44
55
  # instantiate template helper
45
56
  helper = Glue::Template::Helper.new
46
57
 
47
58
  # instantiate and process haml for page
48
- haml = Haml::Engine.new(meta.content)
49
- content_for_layout = haml.to_html(helper)
59
+ haml = Haml::Engine.new($meta.content)
60
+ content_for_layout = haml.to_html(helper, $meta.data)
50
61
 
51
62
  # instantiate haml for layout
52
- layout = File.read("#{root}/views/layouts/#{meta.metadata["layout"]}.haml")
63
+ layout = File.read("#{root}/views/layouts/#{$meta.data[:layout]}.haml")
53
64
  haml = Haml::Engine.new(layout)
54
- vars = meta.metadata.merge({
65
+
66
+ # set haml variables
67
+ vars = {}
68
+ vars.merge!($meta.data)
69
+ vars.merge!({
55
70
  :content_for_layout => content_for_layout,
56
- :glue_version => Glue::VERSION
71
+ :generator => Glue::VERSION,
72
+ :last_update => File.mtime(path).xmlschema
57
73
  })
58
74
 
59
75
  # retrieve relative file path and set target
60
- relative_path = path.sub("#{root}/views/", "").gsub(/\.haml$/, ".html")
61
- target = File.join(root, "public", relative_path)
76
+ relative_path = path.sub("#{root}/views/", "").gsub(/\.haml$/, "")
77
+ target = File.join(root, "public", relative_path + ".html")
78
+
79
+ # sitemap data
80
+ if config["use_extension"]
81
+ path_for_sitemap = relative_path + ".html"
82
+ else
83
+ path_for_sitemap = relative_path.gsub(/\/?index$/, "")
84
+ end
85
+
86
+ mapping = {
87
+ :path => path_for_sitemap,
88
+ :changed_at => File.mtime(path),
89
+ :priority => $meta.data[:priority] || "0.5",
90
+ :frequency => $meta.data[:frequency] || "daily"
91
+ }
92
+
93
+ unless $meta.data[:sitemap] == "no"
94
+ self.sitemap << mapping
95
+ self.sitemap << mapping.merge(:path => "/") if $meta.data[:index] == "yes"
96
+ end
62
97
 
63
98
  # create directories if necessary
64
99
  FileUtils.mkdir_p File.dirname(target)
@@ -72,7 +107,7 @@ module Glue
72
107
  end
73
108
 
74
109
  # save as index page
75
- if meta.metadata["index"] == "true"
110
+ if $meta.data[:index] == "yes"
76
111
  index_path = File.join(root, "public", "index.html")
77
112
 
78
113
  File.open(index_path, "w+") do |f|
@@ -0,0 +1,36 @@
1
+ module Glue
2
+ module Template
3
+ class Sitemap
4
+ attr_accessor :urls
5
+ attr_accessor :base_url
6
+
7
+ def initialize
8
+ self.urls = []
9
+ end
10
+
11
+ def <<(options)
12
+ self.urls << options
13
+ end
14
+
15
+ def save(path)
16
+ file = File.open(path, "w+")
17
+ xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
18
+ xml.instruct! :xml, :version => "1.1", :encoding => "UTF-8"
19
+ xml.comment! "Generated at #{Time.now.inspect}"
20
+
21
+ xml.urlset :xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" do
22
+ urls.each do |options|
23
+ xml.url do
24
+ xml.loc File.join(base_url, options[:path])
25
+ xml.lastmod options[:changed_at].xmlschema if options[:changed_at]
26
+ xml.changefreq options[:frequency] if options[:frequency]
27
+ xml.priority options[:priority] if options[:priority]
28
+ end
29
+ end
30
+ end
31
+
32
+ file.close
33
+ end
34
+ end
35
+ end
36
+ end
data/lib/glue.rb CHANGED
@@ -1,22 +1,25 @@
1
1
  module Glue
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
4
4
 
5
5
  # add glue lib to load path
6
6
  $LOAD_PATH.unshift File.dirname(__FILE__)
7
7
 
8
8
  require "FileUtils" unless defined?(FileUtils)
9
- require "ostruct"
10
9
  require "open-uri"
11
10
  require "RedCloth"
12
11
  require "rdiscount"
13
12
  require "haml"
14
13
  require "sass"
15
14
  require "vendor/colorize-0.0.1/colorize"
15
+ require "builder"
16
+ require "uri"
17
+ require "yaml"
16
18
 
17
19
  require "glue/helper"
18
20
  require "glue/generator"
19
21
  require "glue/template"
20
22
  require "glue/template/parser"
21
23
  require "glue/template/meta"
22
- require "glue/template/helper"
24
+ require "glue/template/helper"
25
+ require "glue/template/sitemap"
@@ -0,0 +1,16 @@
1
+ / title: Page not found
2
+ / description: The page you're looking for could not be found
3
+ / keywords:
4
+ / layout: main
5
+ / id: not-found
6
+ / sitemap: no
7
+
8
+ %h2 Page not found
9
+
10
+ %p
11
+ %strong Sorry!
12
+ The page you're looking for could not be found.
13
+
14
+ %p
15
+ Go back to the
16
+ %a{"href" => "/"} home page
@@ -0,0 +1,10 @@
1
+ # Set your base url.
2
+ # Include domain and path (if needed).
3
+ # Domain is required for sitemap generation.
4
+ base_url: http://example.com/some/path
5
+
6
+ # Use extension when generating sitemap.
7
+ # If you're using Apache you can enable `MultiViews`
8
+ # and access your URL like http://example.com/about
9
+ # instead of http://example.com/about.html
10
+ use_extension: false
@@ -0,0 +1,20 @@
1
+ # DEFLATE compresses files
2
+ <IfModule mod_deflate.c>
3
+ <FilesMatch "\.(js|css)$">
4
+ SetOutputFilter DEFLATE
5
+ </FilesMatch>
6
+ </IfModule>
7
+
8
+ # Multiviews allows to access URLs like http://example.com/some/page.html
9
+ # without the extension; so http://example.com/some/page will be valid as
10
+ # well.
11
+ Options +MultiViews
12
+
13
+ # Set 404 page
14
+ ErrorDocument 404 /404.html
15
+
16
+ # Force PDF/ZIP download
17
+ <FilesMatch "\.(?i:pdf|zip)$">
18
+ ForceType application/octet-stream
19
+ Header set Content-Disposition attachment
20
+ </FilesMatch>
data/templates/main.haml CHANGED
@@ -5,8 +5,9 @@
5
5
  %title= title
6
6
  %meta{"name" => "description", "content" => description}
7
7
  %meta{"name" => "keywords", "content" => keywords}
8
- %meta{"name" => "generator", "content" => "Glue #{glue_version}"}
8
+ %meta{"name" => "generator", "content" => "Glue #{generator}"}
9
9
  %meta{"http-equiv" => "content-type", "content" => "text/html; charset=UTF-8"}
10
+ %meta{"http-equiv" => "last-update", "content" => last_update}
10
11
  %link{"rel" => "shortcut icon", "href" => "/favicon.ico", "type" => "image/x-icon"}
11
12
  %link{"rel" => "stylesheet", "href" => "/stylesheets/style.css", "type" => "text/css"}
12
13
 
@@ -0,0 +1,4 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ # User-agent: *
3
+
4
+ SITEMAP: /sitemap.xml
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fnando-glue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
@@ -52,6 +52,16 @@ dependencies:
52
52
  - !ruby/object:Gem::Version
53
53
  version: "0"
54
54
  version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: builder
57
+ type: :runtime
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
55
65
  description:
56
66
  email:
57
67
  - fnando.vieira@gmail.com
@@ -67,7 +77,6 @@ files:
67
77
  - README.markdown
68
78
  - bin/glue
69
79
  - lib/glue
70
- - lib/glue/capistrano.rb
71
80
  - lib/glue/generator.rb
72
81
  - lib/glue/helper.rb
73
82
  - lib/glue/rake.rb
@@ -75,16 +84,21 @@ files:
75
84
  - lib/glue/template/helper.rb
76
85
  - lib/glue/template/meta.rb
77
86
  - lib/glue/template/parser.rb
87
+ - lib/glue/template/sitemap.rb
78
88
  - lib/glue/template.rb
79
89
  - lib/glue.rb
80
90
  - lib/vendor
81
91
  - lib/vendor/colorize-0.0.1
82
92
  - lib/vendor/colorize-0.0.1/colorize.rb
93
+ - templates/404.haml
83
94
  - templates/_footer.haml
95
+ - templates/glue.yml
84
96
  - templates/helper.rb
97
+ - templates/htaccess
85
98
  - templates/index.haml
86
99
  - templates/main.haml
87
100
  - templates/Rakefile
101
+ - templates/robots.txt
88
102
  - templates/style.sass
89
103
  has_rdoc: true
90
104
  homepage: http://github.com/fnando/glue
@@ -1,5 +0,0 @@
1
- module Glue
2
- module Capistrano
3
-
4
- end
5
- end