fnando-glue 0.0.3 → 0.0.4

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.markdown CHANGED
@@ -27,8 +27,6 @@ TODO
27
27
  ----
28
28
 
29
29
  * Write documentation
30
- * Automatically generate sitemap
31
- * Generate feed
32
30
 
33
31
  MAINTAINER
34
32
  ----------
data/glue.gemspec CHANGED
@@ -2,12 +2,9 @@
2
2
  # RUN : 'rake gem:update_gemspec'
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.required_rubygems_version = ">= 0"
6
- s.has_rdoc = true
7
- s.email = ["fnando.vieira@gmail.com"]
8
- s.name = "glue"
9
5
  s.homepage = "http://github.com/fnando/glue"
10
6
  s.bindir = "bin"
7
+ s.name = "glue"
11
8
  s.executables = ["glue"]
12
9
  s.summary = "Glue is a simple and dumb static site generator."
13
10
  s.add_dependency "main", ">= 0"
@@ -15,17 +12,20 @@ Gem::Specification.new do |s|
15
12
  s.add_dependency "rdiscount", ">= 0"
16
13
  s.add_dependency "haml", ">= 0"
17
14
  s.add_dependency "builder", ">= 0"
18
- s.version = "0.0.3"
19
15
  s.require_paths = ["lib"]
20
16
  s.files = ["Rakefile",
21
17
  "glue.gemspec",
22
18
  "README.markdown",
23
19
  "bin/glue",
24
20
  "lib/glue",
21
+ "lib/glue/config.rb",
22
+ "lib/glue/core_ext.rb",
25
23
  "lib/glue/generator.rb",
26
24
  "lib/glue/helper.rb",
27
25
  "lib/glue/rake.rb",
28
26
  "lib/glue/template",
27
+ "lib/glue/template/base.rb",
28
+ "lib/glue/template/feed.rb",
29
29
  "lib/glue/template/helper.rb",
30
30
  "lib/glue/template/meta.rb",
31
31
  "lib/glue/template/parser.rb",
@@ -46,4 +46,8 @@ Gem::Specification.new do |s|
46
46
  "templates/robots.txt",
47
47
  "templates/style.sass"]
48
48
  s.authors = ["Nando Vieira"]
49
+ s.required_rubygems_version = ">= 0"
50
+ s.version = "0.0.4"
51
+ s.has_rdoc = true
52
+ s.email = ["fnando.vieira@gmail.com"]
49
53
  end
data/lib/glue.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Glue
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
4
4
 
5
5
  # add glue lib to load path
@@ -16,10 +16,14 @@ require "builder"
16
16
  require "uri"
17
17
  require "yaml"
18
18
 
19
+ require "glue/core_ext"
20
+ require "glue/config"
19
21
  require "glue/helper"
20
22
  require "glue/generator"
21
23
  require "glue/template"
22
- require "glue/template/parser"
23
- require "glue/template/meta"
24
+ require "glue/template/base"
24
25
  require "glue/template/helper"
26
+ require "glue/template/meta"
27
+ require "glue/template/parser"
28
+ require "glue/template/feed"
25
29
  require "glue/template/sitemap"
@@ -0,0 +1,8 @@
1
+ module Glue
2
+ class Config < Hash
3
+ def initialize
4
+ path = File.join(GLUE_ROOT, "config", "glue.yml")
5
+ self.replace YAML.load_file(path).symbolize_keys
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ class Hash
2
+ def symbolize_keys
3
+ inject({}) do |options, (key, value)|
4
+ value = value.symbolize_keys if value.respond_to?(:symbolize_keys)
5
+ options[(key.to_sym rescue key) || key] = value
6
+ options
7
+ end
8
+ end
9
+
10
+ def symbolize_keys!
11
+ self.replace(self.symbolize_keys)
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ module Glue
2
+ module Template
3
+ class Base
4
+ attr_accessor :items
5
+ attr_accessor :config
6
+ attr_accessor :filters
7
+
8
+ def initialize(config)
9
+ self.config = config
10
+ self.items = []
11
+ self.filters = []
12
+ end
13
+
14
+ def <<(options)
15
+ self.items << options
16
+ end
17
+
18
+ def apply_filters
19
+ filtered_items = []
20
+
21
+ items.each do |options|
22
+ filters.collect do |f|
23
+ r = Regexp.new(f, Regexp::MULTILINE|Regexp::EXTENDED|Regexp::IGNORECASE)
24
+ filtered_items << options and break if options[:path] =~ r
25
+ end
26
+ end
27
+
28
+ filtered_items
29
+ end
30
+
31
+ def save(path)
32
+ raise "This is an abstract method; override it!"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,71 @@
1
+ module Glue
2
+ module Template
3
+ class Feed < Base
4
+ RSS_TIME_FORMAT = "%a, %d %b %Y %H:%M:%S %z"
5
+
6
+ def save(path)
7
+ # create a file handler
8
+ file = File.open(path, "w+")
9
+
10
+ # sort and keep latest 10 items
11
+ self.filters = config[:feed][:filters]
12
+
13
+ filtered_items = apply_filters
14
+ filtered_items = filtered_items.sort_by {|options| options[:updated_at].to_i }
15
+ filtered_items.reverse!
16
+ filtered_items = filtered_items[0,10]
17
+
18
+ # instantiate builder
19
+ xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
20
+ xml.instruct! :xml, :version => "1.1", :encoding => "UTF-8"
21
+
22
+ feed_opts = {
23
+ "xmlns" => "http://www.w3.org/2005/Atom",
24
+ "xml:lang" => config[:site][:language],
25
+ "xml:base" => "http://www.example.org"
26
+ }
27
+
28
+ xml.feed(feed_opts) do
29
+ # set feed id to url
30
+ xml.id config[:site][:base_url]
31
+
32
+ # set title
33
+ xml.title config[:site][:title]
34
+
35
+ # set description
36
+ xml.subtitle config[:site][:description]
37
+
38
+ # set link to site
39
+ xml.link(:href => config[:site][:base_url])
40
+
41
+ # set link to feed
42
+ xml.link(:href => File.join(config[:site][:base_url], "feed.xml"), :rel => "self")
43
+
44
+ # add update date as the saving time
45
+ xml.updated Time.now.xmlschema
46
+
47
+ # add generator info
48
+ xml.generator("Glue", :uri => "http://github.com/fnando/glue", :version => Glue::VERSION)
49
+
50
+ # set language
51
+ xml.language config[:site][:language]
52
+
53
+ filtered_items.each do |options|
54
+ url = File.join(config[:site][:base_url], options[:path])
55
+
56
+ xml.entry do
57
+ xml.title options[:title]
58
+ xml.summary options[:description]
59
+ xml.updated options[:updated_at].xmlschema
60
+ xml.link :rel => "alternate", :type => "text/html", :href => url
61
+ xml.id url
62
+ end
63
+ end
64
+ end
65
+
66
+ # close handler
67
+ file.close
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,6 +1,19 @@
1
1
  module Glue
2
2
  module Template
3
3
  class Helper
4
+ def config
5
+ @config ||= YAML.load_file(GLUE_ROOT + "/config/glue.yml")
6
+ end
7
+
8
+ def url_for(path)
9
+ uri = URI.parse(config["base_url"])
10
+ base_path = uri.path.chomp("/")
11
+
12
+ path = File.join(base_path, path)
13
+ path << ".html" if config["use_extension"] && base_path !~ /\.html$/
14
+ path
15
+ end
16
+
4
17
  def markdown(text)
5
18
  RDiscount.new(text).to_html
6
19
  end
@@ -13,10 +13,12 @@ module Glue
13
13
 
14
14
  def process!
15
15
  self.content = body.gsub /^\/ *(.*?): *(.*?) *$/sm do |m|
16
- data[$1.to_sym] = $2
16
+ data[$1] = $2
17
17
  ""
18
18
  end
19
+
20
+ self.data.symbolize_keys!
19
21
  end
20
22
  end
21
23
  end
22
- end
24
+ end
@@ -3,16 +3,16 @@ module Glue
3
3
  class Parser
4
4
  attr_accessor :root
5
5
  attr_accessor :sitemap
6
+ attr_accessor :feed
6
7
  attr_accessor :config
7
8
 
8
9
  include Glue::Helper
9
10
 
10
11
  def initialize(root)
11
12
  @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"]
13
+ @config = Glue::Config.new
14
+ @sitemap = Glue::Template::Sitemap.new(@config)
15
+ @feed = Glue::Template::Feed.new(@config)
16
16
  end
17
17
 
18
18
  def run!
@@ -43,8 +43,17 @@ module Glue
43
43
  end
44
44
 
45
45
  # save sitemap
46
- run "generating #{Colorize.yellow("public/sitemap.xml")}..." do
47
- sitemap.save File.join(root, "public", "sitemap.xml")
46
+ if config[:sitemap][:enabled]
47
+ run "generating #{Colorize.yellow("public/sitemap.xml")}..." do
48
+ sitemap.save File.join(root, "public", "sitemap.xml")
49
+ end
50
+ end
51
+
52
+ # save feed
53
+ if config[:feed][:enabled]
54
+ run "generating #{Colorize.yellow("public/feed.xml")}..." do
55
+ feed.save File.join(root, "public", "feed.xml")
56
+ end
48
57
  end
49
58
  end
50
59
 
@@ -77,24 +86,27 @@ module Glue
77
86
  target = File.join(root, "public", relative_path + ".html")
78
87
 
79
88
  # sitemap data
80
- if config["use_extension"]
81
- path_for_sitemap = relative_path + ".html"
82
- else
89
+ if config[:site][:friendly_url]
83
90
  path_for_sitemap = relative_path.gsub(/\/?index$/, "")
91
+ else
92
+ path_for_sitemap = relative_path + ".html"
84
93
  end
85
94
 
86
- mapping = {
95
+ # merge meta data with some template info
96
+ mapping = $meta.data.merge({
87
97
  :path => path_for_sitemap,
88
- :changed_at => File.mtime(path),
89
- :priority => $meta.data[:priority] || "0.5",
90
- :frequency => $meta.data[:frequency] || "daily"
91
- }
98
+ :updated_at => File.mtime(path)
99
+ })
92
100
 
93
101
  unless $meta.data[:sitemap] == "no"
94
102
  self.sitemap << mapping
95
103
  self.sitemap << mapping.merge(:path => "/") if $meta.data[:index] == "yes"
96
104
  end
97
105
 
106
+ unless $meta.data[:feed] == "no"
107
+ self.feed << mapping
108
+ end
109
+
98
110
  # create directories if necessary
99
111
  FileUtils.mkdir_p File.dirname(target)
100
112
 
@@ -1,28 +1,20 @@
1
1
  module Glue
2
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
-
3
+ class Sitemap < Base
15
4
  def save(path)
16
5
  file = File.open(path, "w+")
17
6
  xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
18
7
  xml.instruct! :xml, :version => "1.1", :encoding => "UTF-8"
19
8
  xml.comment! "Generated at #{Time.now.inspect}"
20
9
 
10
+ self.filters = config[:sitemap][:filters]
11
+ filtered_items = apply_filters
12
+
21
13
  xml.urlset :xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" do
22
- urls.each do |options|
14
+ filtered_items.each do |options|
23
15
  xml.url do
24
- xml.loc File.join(base_url, options[:path])
25
- xml.lastmod options[:changed_at].xmlschema if options[:changed_at]
16
+ xml.loc File.join(config[:site][:base_url], options[:path])
17
+ xml.lastmod options[:updated_at].xmlschema if options[:updated_at]
26
18
  xml.changefreq options[:frequency] if options[:frequency]
27
19
  xml.priority options[:priority] if options[:priority]
28
20
  end
data/templates/404.haml CHANGED
@@ -4,6 +4,7 @@
4
4
  / layout: main
5
5
  / id: not-found
6
6
  / sitemap: no
7
+ / feed: no
7
8
 
8
9
  %h2 Page not found
9
10
 
data/templates/glue.yml CHANGED
@@ -1,10 +1,40 @@
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
1
+ site:
2
+ # Set default page title.
3
+ title: "Glue is awesome!"
4
+
5
+ # Set default page description
6
+ description: "This awesome page has been generated using Glue <http://github.com/fnando/glue>!"
7
+
8
+ # Set default keywords
9
+ keywords: "glue, ruby, haml, sass"
10
+
11
+ # Set your base url.
12
+ # Include domain and path (if needed).
13
+ # Domain is required for sitemap generation.
14
+ base_url: http://example.com/some/path
15
+
16
+ # If you're using Apache you can enable `MultiViews`
17
+ # or mod_rewrite and access your URL like
18
+ # http://example.com/about instead of http://example.com/about.html
19
+ friendly_url: true
20
+
21
+ # Set your language for feeds and meta tags
22
+ language: en-us
5
23
 
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
24
+ sitemap:
25
+ # Do generate sitemap
26
+ enabled: true
27
+
28
+ # Filters will be applied against full filename.
29
+ # Use .*? to match all. Use regular expressions.
30
+ filters:
31
+ - .*?
32
+
33
+ feed:
34
+ # Do generate feed
35
+ enabled: true
36
+
37
+ # Filters will be applied against full filename.
38
+ # Use .*? to match all. Use regular expressions.
39
+ filters:
40
+ - .*?
data/templates/htaccess CHANGED
@@ -5,11 +5,6 @@
5
5
  </FilesMatch>
6
6
  </IfModule>
7
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
8
  # Set 404 page
14
9
  ErrorDocument 404 /404.html
15
10
 
@@ -18,3 +13,17 @@ ErrorDocument 404 /404.html
18
13
  ForceType application/octet-stream
19
14
  Header set Content-Disposition attachment
20
15
  </FilesMatch>
16
+
17
+ # Enable Multiviews by default. Use mod rewrite
18
+ # rules if you prefer.
19
+ Options +MultiViews
20
+
21
+ <IfModule mod_rewrite.c>
22
+ RewriteEngine Off
23
+
24
+ # handle existing file /some/path.html as /some/path
25
+ RewriteCond %{REQUEST_FILENAME} !-f
26
+ RewriteCond %{REQUEST_URI} !-d
27
+ RewriteCond %{REQUEST_FILENAME}.html -f
28
+ RewriteRule (.*) /$1.html
29
+ </IfModule>
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
@@ -77,10 +77,14 @@ files:
77
77
  - README.markdown
78
78
  - bin/glue
79
79
  - lib/glue
80
+ - lib/glue/config.rb
81
+ - lib/glue/core_ext.rb
80
82
  - lib/glue/generator.rb
81
83
  - lib/glue/helper.rb
82
84
  - lib/glue/rake.rb
83
85
  - lib/glue/template
86
+ - lib/glue/template/base.rb
87
+ - lib/glue/template/feed.rb
84
88
  - lib/glue/template/helper.rb
85
89
  - lib/glue/template/meta.rb
86
90
  - lib/glue/template/parser.rb