broadway 0.0.3 → 0.0.3.2

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/Rakefile CHANGED
@@ -14,7 +14,7 @@ RAILS_ROOT = File.dirname(__FILE__)
14
14
  spec = Gem::Specification.new do |s|
15
15
  s.name = "broadway"
16
16
  s.author = "Lance Pollard"
17
- s.version = "0.0.3"
17
+ s.version = "0.0.3.2"
18
18
  s.date = "Mon Mar 22 20:12:47 -0700 2010"
19
19
  s.summary = "Write Posts in Textile and Markdown, built it into a Rails or Sinatra Blog"
20
20
  s.homepage = "http://github.com/viatropos/broadway"
data/lib/broadway/base.rb CHANGED
@@ -29,6 +29,7 @@ require "redcloth"
29
29
  $:.push(File.dirname(__FILE__))
30
30
  require "core_ext"
31
31
  require "convertible"
32
+ require "resource"
32
33
  require "site"
33
34
  require "asset"
34
35
  require "page"
@@ -24,10 +24,9 @@ module Broadway
24
24
  end
25
25
  self.content = self.content[($1.size + $2.size)..-1]
26
26
  end
27
- self.render(site.layouts, site.site_payload)
28
- output = self.output
27
+ result = self.render(site.layouts, site.site_payload)
29
28
  self.content = self.output = nil
30
- output
29
+ result
31
30
  end
32
31
 
33
32
  # Read the YAML frontmatter
@@ -45,6 +44,8 @@ module Broadway
45
44
 
46
45
  self.data.merge!(YAML.load($1))
47
46
  end
47
+
48
+ self.data ||= {}
48
49
  end
49
50
 
50
51
  # Transform the contents based on the file extension.
@@ -82,16 +83,9 @@ module Broadway
82
83
  # Returns nothing
83
84
  def do_layout(payload, layouts)
84
85
  info = { :filters => [], :registers => { :site => self.site } }
85
-
86
86
  # render and transform content (this becomes the final content of the object)
87
87
  payload["content_type"] = self.content_type
88
- self.content = Liquid::Template.parse(self.content).render(payload, info)
89
-
90
- # TODO FIX
91
- # self.transform
92
-
93
- # output keeps track of what will finally be written
94
- self.output = self.content
88
+ self.output = Liquid::Template.parse(self.content).render(payload, info)
95
89
  end
96
90
  end
97
91
  end
data/lib/broadway/page.rb CHANGED
@@ -1,11 +1,9 @@
1
1
  module Broadway
2
2
 
3
3
  class Page
4
+ include Comparable
4
5
  include Convertible
5
-
6
- attr_accessor :site, :position # order
7
- attr_accessor :name, :ext, :basename, :dir, :path, :slug
8
- attr_accessor :data, :content, :output, :categories, :tags, :children
6
+ include Resource
9
7
 
10
8
  # Initialize a new Page.
11
9
  # +site+ is the Site
@@ -19,66 +17,16 @@ module Broadway
19
17
  self.path = options[:path] if options.has_key?(:path)
20
18
  self.data = {}
21
19
  self.dir = path =~ /\// ? File.dirname(path.gsub(/#{site.config[:source]}\/?/, "")).gsub(/^\//, "") : path
22
- base = self.dir == "." ? "" : File.basename(self.dir)
23
- self.name = base
20
+ self.slug = self.dir == "." ? "" : File.basename(self.dir)
24
21
  self.ext = File.extname(path)
25
22
  self.basename = File.basename(path).split('.')[0..-2].first
26
23
 
27
24
  self.categories ||= []
28
- self.categories.concat base.split('/').reject { |x| x.empty? }
25
+ self.categories.concat self.slug.split('/').reject { |x| x.empty? }
29
26
  self.children ||= []
30
27
  process(options) unless options.has_key?(:process) and options[:process] == false
31
28
  end
32
29
 
33
- def children
34
- @children = site.find_pages_by_category(self.categories.last) + site.find_posts_by_category(self.categories.last)
35
- @children.delete_if {|i| i.url == self.url}
36
- end
37
-
38
- # The UID for this post (useful in feeds)
39
- # e.g. /2008/11/05/my-awesome-post
40
- #
41
- # Returns <String>
42
- def id
43
- File.join(self.dir, self.name)
44
- end
45
-
46
- def menu_title
47
- self.data && self.data['menu_title']
48
- end
49
-
50
- def tooltip
51
- self.data && self.data["tooltip"]
52
- end
53
-
54
- def permalink
55
- self.data && self.data['permalink']
56
- end
57
-
58
- def show_children?
59
- return true if self.data.nil?
60
- if self.data.has_key?("show_children")
61
- return self.data["show_children"] == "true" || self.data["show_children"] == true
62
- end
63
- return true
64
- end
65
-
66
- def title
67
- self.data && (self.data['title'] || self.name)
68
- end
69
-
70
- def excerpt
71
- self.data && self.data['excerpt']
72
- end
73
-
74
- def parent
75
- url ? site.find_page_by_url(url[1..-1].split("/").first) : nil
76
- end
77
-
78
- def layout
79
- self.data and self.data.has_key?("layout") ? self.data["layout"] : self.categories.first
80
- end
81
-
82
30
  # The generated relative url of this page
83
31
  # e.g. /about
84
32
  #
@@ -86,7 +34,7 @@ module Broadway
86
34
  def url
87
35
  return permalink if permalink
88
36
  @url ||= {
89
- "name" => CGI.escape(name),
37
+ "slug" => CGI.escape(slug),
90
38
  "categories" => categories[0..-2].join('/')
91
39
  }.inject(template) { |result, token|
92
40
  result.gsub(/:#{token.first}/, token.last)
@@ -95,60 +43,28 @@ module Broadway
95
43
 
96
44
  def template
97
45
  if self.site.permalink_style == :pretty
98
- "/:categories/:name"
46
+ "/:categories/:slug"
99
47
  else
100
- "/:categories/:name.html"
48
+ "/:categories/:slug.html"
101
49
  end
102
50
  end
103
51
 
104
52
  def <=>(other)
105
53
  return self.position <=> other.position
106
54
  end
107
-
108
- # Extract information from the page filename
109
- # +name+ is the String filename of the page file
110
- #
111
- # Returns nothing
112
- def process(options = {})
113
- self.read_yaml(path)
114
-
115
- self.tags = self.data.pluralized_array("tag", "tags")
116
- end
117
55
 
118
56
  # Add any necessary layouts to this post
119
- # +layouts+ is a Hash of {"name" => "layout"}
57
+ # +layouts+ is a Hash of {"slug" => "layout"}
120
58
  # +site_payload+ is the site payload hash
121
59
  #
122
60
  # Returns nothing
123
61
  def render(layouts, site_payload)
124
- payload = {"page" => self.data}.deep_merge(site_payload)
62
+ payload = {"page" => self.data, "site" => {}}.deep_merge(site_payload)
125
63
  do_layout(payload, layouts)
126
64
  end
127
65
 
128
- # Write the generated page file to the destination directory.
129
- # +dest_prefix+ is the String path to the destination dir
130
- # +dest_suffix+ is a suffix path to the destination dir
131
- #
132
- # Returns nothing
133
- def write(dest_prefix, dest_suffix = nil)
134
- dest = File.join(dest_prefix, @dir)
135
- dest = File.join(dest, dest_suffix) if dest_suffix
136
- FileUtils.mkdir_p(dest)
137
-
138
- # The url needs to be unescaped in order to preserve the correct filename
139
- path = File.join(dest, CGI.unescape(self.url))
140
- if self.ext == '.html' && self.url[/\.html$/].nil?
141
- FileUtils.mkdir_p(path)
142
- path = File.join(path, "index.html")
143
- end
144
-
145
- File.open(path, 'w') do |f|
146
- f.write(self.output)
147
- end
148
- end
149
-
150
66
  def inspect
151
- "#<Broadway:Page @url=#{self.url.inspect} @name=#{self.name.inspect} @categories=#{self.categories.inspect} @tags=#{self.tags.inspect} @data=#{self.data.inspect}>"
67
+ "#<Broadway:Page @url=#{self.url.inspect} @slug=#{self.slug.inspect} @categories=#{self.categories.inspect} @tags=#{self.tags.inspect} @data=#{self.data.inspect}>"
152
68
  end
153
69
  end
154
70
 
data/lib/broadway/post.rb CHANGED
@@ -3,10 +3,11 @@ module Broadway
3
3
  class Post
4
4
  include Comparable
5
5
  include Convertible
6
+ include Resource
6
7
 
7
8
  SRC_MATCHER = /^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)(\.[^.]+)$/
8
9
  URL_MATCHER = /^(.+\/)*(.*)$/
9
-
10
+
10
11
  # Post name validator. Post filenames must be like:
11
12
  # 2008-11-05-my-awesome-post.textile
12
13
  #
@@ -15,12 +16,6 @@ module Broadway
15
16
  site.config[:posts_include].include?(File.extname(name))
16
17
  end
17
18
 
18
- attr_accessor :site
19
- # where the file is
20
- attr_accessor :path, :dir, :name, :basename, :ext
21
- attr_accessor :data, :content, :output, :position # order
22
- attr_accessor :date, :slug, :published, :tags, :categories, :asset
23
-
24
19
  # Initialize this Post instance.
25
20
  # +site+ is the Site
26
21
  # +base+ is the String path to the dir containing the post file
@@ -40,38 +35,11 @@ module Broadway
40
35
  self.ext = ext
41
36
  self.dir = options.has_key?(:dir) ? options[:dir] : File.dirname(path.gsub(/#{site.config[:source]}\/?/, ""))
42
37
  base = self.dir == "." ? "" : File.basename(self.dir)
43
- self.name = base
44
38
  self.categories = base.split('/').reject { |x| x.empty? }
45
39
 
46
- process(options) unless options.has_key?(:process) and options[:process] == false
40
+ process(options)
47
41
  end
48
42
 
49
- # Extract information from the post filename
50
- # +name+ is the String filename of the post file
51
- #
52
- # Returns nothing
53
- def process(options = {})
54
- self.read_yaml(path)
55
-
56
- # If we've added a date and time to the yaml, use that instead of the filename date
57
- # Means we'll sort correctly.
58
- self.date = Time.parse(self.data["date"].to_s) if self.data.has_key?('date')
59
-
60
- if self.data.has_key?('published') && self.data['published'] == false
61
- self.published = false
62
- else
63
- self.published = true
64
- end
65
-
66
- if self.data.has_key?("asset")
67
- data["asset"]["title"] ||= self.title
68
- self.asset = Asset.new(data["asset"])
69
- end
70
-
71
- self.tags = self.data.pluralized_array("tag", "tags")
72
- self.tags ||= []
73
- end
74
-
75
43
  # Spaceship is based on Post#date, slug
76
44
  #
77
45
  # Returns -1, 0, 1
@@ -83,39 +51,6 @@ module Broadway
83
51
  end
84
52
  end
85
53
 
86
- # The UID for this post (useful in feeds)
87
- # e.g. /2008/11/05/my-awesome-post
88
- #
89
- # Returns <String>
90
- def id
91
- File.join(self.dir, self.slug)
92
- end
93
-
94
- def parent
95
- url ? site.find_page_by_url(url[1..-1].split("/").first) : nil
96
- end
97
-
98
- def layout
99
- self.data and self.data.has_key?("layout") ? self.data["layout"] : self.categories.first
100
- end
101
-
102
- # The full path and filename of the post.
103
- # Defined in the YAML of the post body
104
- # (Optional)
105
- #
106
- # Returns <String>
107
- def permalink
108
- self.data && self.data['permalink']
109
- end
110
-
111
- def title
112
- self.data && (self.data['title'] || self.slug)
113
- end
114
-
115
- def excerpt
116
- self.data && self.data["excerpt"]
117
- end
118
-
119
54
  def template
120
55
  case self.site.permalink_style
121
56
  when :pretty
@@ -152,42 +87,15 @@ module Broadway
152
87
  #
153
88
  # Returns nothing
154
89
  def render(layouts, site_payload)
155
- # construct payload
156
- payload =
157
- {
158
- "site" => {},
159
- "page" => self.to_liquid
160
- }
161
- payload = payload.deep_merge(site_payload)
162
-
90
+ payload = {"site" => {}, "page" => self.to_liquid}.deep_merge(site_payload)
163
91
  do_layout(payload, layouts)
164
92
  end
165
93
 
166
- # Write the generated post file to the destination directory.
167
- # +dest+ is the String path to the destination dir
168
- #
169
- # Returns nothing
170
- def write(dest)
171
- FileUtils.mkdir_p(File.join(dest, dir))
172
-
173
- # The url needs to be unescaped in order to preserve the correct filename
174
- path = File.join(dest, CGI.unescape(self.url))
175
-
176
- if template[/\.html$/].nil?
177
- FileUtils.mkdir_p(path)
178
- path = File.join(path, "index.html")
179
- end
180
-
181
- File.open(path, 'w') do |f|
182
- f.write(self.output)
183
- end
184
- end
185
-
186
94
  # Convert this post into a Hash for use in Liquid templates.
187
95
  #
188
96
  # Returns <Hash>
189
97
  def to_liquid
190
- { "title" => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
98
+ { "title" => self.title || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
191
99
  "url" => self.url,
192
100
  "date" => self.date,
193
101
  "id" => self.id,
@@ -0,0 +1,121 @@
1
+ module Broadway::Resource
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include InstanceMethods
6
+ end
7
+ end
8
+
9
+ module InstanceMethods
10
+ attr_accessor :site, :position # order
11
+ # where the file is
12
+ attr_accessor :path, :dir, :basename, :ext, :title
13
+ attr_accessor :data, :content, :output
14
+ attr_accessor :date, :slug, :published, :categories, :asset
15
+
16
+ # Extract information from the post filename
17
+ # +name+ is the String filename of the post file
18
+ #
19
+ # Returns nothing
20
+ def process(options = {})
21
+ return if options.has_key?(:process) and options[:process] == false
22
+
23
+ self.read_yaml(path)
24
+
25
+ self.title = data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' ')
26
+
27
+ # If we've added a date and time to the yaml, use that instead of the filename date
28
+ # Means we'll sort correctly.
29
+ self.date = Time.parse(self.data["date"].to_s) if self.data.has_key?('date')
30
+
31
+ if self.data.has_key?('published') && self.data['published'] == false
32
+ self.published = false
33
+ else
34
+ self.published = true
35
+ end
36
+
37
+ if self.data.has_key?("asset")
38
+ data["asset"]["title"] ||= self.title
39
+ self.asset = ::Broadway::Asset.new(data["asset"])
40
+ end
41
+
42
+ self.tags = self.data["tags"]
43
+ self.tags ||= []
44
+ end
45
+
46
+ def children
47
+ @children = site.find_pages_by_category(self.categories.last) + site.find_posts_by_category(self.categories.last)
48
+ @children.delete_if {|i| i.url == self.url}
49
+ end
50
+
51
+ # The UID for this post (useful in feeds)
52
+ # e.g. /2008/11/05/my-awesome-post
53
+ #
54
+ # Returns <String>
55
+ def id
56
+ File.join(self.dir, self.slug)
57
+ end
58
+
59
+ def menu_title
60
+ self.data['menu_title'] || self.title
61
+ end
62
+
63
+ def tooltip
64
+ self.data["tooltip"]
65
+ end
66
+
67
+ def permalink
68
+ self.data['permalink']
69
+ end
70
+
71
+ def excerpt
72
+ self.data['excerpt']
73
+ end
74
+
75
+ def show_children?
76
+ return true if self.data.nil?
77
+ if self.data.has_key?("show_children")
78
+ return self.data["show_children"] == "true" || self.data["show_children"] == true
79
+ end
80
+ return true
81
+ end
82
+
83
+ def parent
84
+ url ? site.find_page_by_url(url[1..-1].split("/").first) : nil
85
+ end
86
+
87
+ def layout
88
+ self.data and self.data.has_key?("layout") ? self.data["layout"] : self.categories.first
89
+ end
90
+
91
+ def tags=(value)
92
+ @tags = case value
93
+ when Hash
94
+ value = value.each { |k, v| value[k] = tag_array(v) }
95
+ else
96
+ {"tags" => tag_array(value)}
97
+ end
98
+ end
99
+
100
+ # tags can be stored under multiple contexts
101
+ # default is "tags" (use "categories" or whatever else you want)
102
+ def tags(context = "tags")
103
+ @tags[context.to_s]
104
+ end
105
+
106
+ private
107
+
108
+ # helper method to convert tags to arrays
109
+ def tag_array(value)
110
+ result = case value
111
+ when String
112
+ value.split(",\s+")
113
+ when Array
114
+ value
115
+ end
116
+ result
117
+ end
118
+
119
+ end
120
+
121
+ end
metadata CHANGED
@@ -6,7 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 0
8
8
  - 3
9
- version: 0.0.3
9
+ - 2
10
+ version: 0.0.3.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Lance Pollard
@@ -76,6 +77,7 @@ files:
76
77
  - lib/broadway/page.rb
77
78
  - lib/broadway/post.rb
78
79
  - lib/broadway/rails.rb
80
+ - lib/broadway/resource.rb
79
81
  - lib/broadway/runner.rb
80
82
  - lib/broadway/sinatra/base.rb
81
83
  - lib/broadway/sinatra/helpers/collection_helper.rb