awestruct 0.4.5 → 0.4.6

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.
@@ -36,7 +36,7 @@ module Awestruct
36
36
  if self.disqus_identifier or site.disqus_generate_id
37
37
  identifier = %Q{ data-disqus-identifier="#{self.resolve_disqus_identifier()}"}
38
38
  end
39
- %Q{ <a href="#{self.url}#disqus_thread"#{identifier}>Comments</a> }
39
+ %Q{ <a href="#{site.base_url}#{self.url}#disqus_thread"#{identifier}>Comments</a> }
40
40
  end
41
41
 
42
42
  def disqus_comments_count()
@@ -1,3 +1,4 @@
1
+ require 'shellwords'
1
2
  require 'fileutils'
2
3
 
3
4
  ##
@@ -96,7 +97,7 @@ module Awestruct
96
97
  end
97
98
  end
98
99
  end
99
- Open3.popen3(cmd) do |stdin, stdout, stderr|
100
+ Open3.popen3(Shellwords.escape(cmd)) do |stdin, stdout, stderr|
100
101
  threads = []
101
102
  threads << Thread.new(stdout) do |o|
102
103
  while ( ! o.eof? )
@@ -124,7 +125,7 @@ module Awestruct
124
125
 
125
126
  def yuicompressor(page, input, type)
126
127
  output = ''
127
- Open3.popen3("yuicompressor --type #{type}") do |stdin, stdout, stderr|
128
+ Open3.popen3("yuicompressor --type #{Shellwords.escape(type)}") do |stdin, stdout, stderr|
128
129
  threads = []
129
130
  threads << Thread.new(stdout) do |o|
130
131
  while ( ! o.eof? )
@@ -152,7 +153,7 @@ module Awestruct
152
153
 
153
154
  def pngcrush(page, input)
154
155
  filename = page.source_path
155
- cmd = "pngcrush #{filename} /tmp/pngcrush"
156
+ cmd = Shellwords.escape("pngcrush #{filename} /tmp/pngcrush")
156
157
  `#{cmd}`
157
158
  if $?.exitstatus != 0
158
159
  raise "Failed to execute pngcrush: #{cmd}"
@@ -2,25 +2,25 @@ module Awestruct
2
2
  module Extensions
3
3
  class Posts
4
4
 
5
- def initialize(path_prefix='', assign_to=:posts)
6
- @path_prefix = path_prefix
7
- @assign_to = assign_to
5
+ attr_accessor :path_prefix, :assign_to, :archive_template, :archive_path
6
+
7
+ def initialize(path_prefix='', assign_to=:posts, archive_template=nil, archive_path=nil)
8
+ @archive_template = archive_template
9
+ @archive_path = archive_path
10
+ @path_prefix = path_prefix
11
+ @assign_to = assign_to
8
12
  end
9
13
 
10
14
  def execute(site)
11
- posts = []
15
+ posts = []
16
+ archive = Archive.new
12
17
 
13
18
  site.pages.each do |page|
14
19
  year, month, day, slug = nil
15
20
 
16
21
  if ( page.relative_source_path =~ /^#{@path_prefix}\// )
17
- if ( page.relative_source_path =~ /^#{@path_prefix}\/(20[01][0-9])-([01][0-9])-([0123][0-9])-([^.]+)\..*$/ )
18
- year = $1
19
- month = $2
20
- day = $3
21
- slug = $4
22
- page.date = Time.utc( year.to_i, month.to_i, day.to_i )
23
- elsif (page.date?)
22
+ # check for a date inside the page first
23
+ if (page.date?)
24
24
  page.relative_source_path =~ /^#{@path_prefix}\/(.*)\..*$/
25
25
  date = page.date;
26
26
  if date.kind_of? String
@@ -29,21 +29,24 @@ module Awestruct
29
29
  year = date.year
30
30
  month = sprintf( "%02d", date.month )
31
31
  day = sprintf( "%02d", date.day )
32
- page.date = Time.utc(year, month, day)
32
+ page.date = date
33
33
  slug = $1
34
+ if ( page.relative_source_path =~ /^#{@path_prefix}\/(20[01][0-9])-([01][0-9])-([0123][0-9])-([^.]+)\..*$/ )
35
+ slug = $4
36
+ end
37
+ elsif ( page.relative_source_path =~ /^#{@path_prefix}\/(20[01][0-9])-([01][0-9])-([0123][0-9])-([^.]+)\..*$/ )
38
+ year = $1
39
+ month = $2
40
+ day = $3
41
+ slug = $4
42
+ page.date = Time.utc( year.to_i, month.to_i, day.to_i )
34
43
  end
35
44
 
36
45
  # if a date was found create a post
37
46
  if( year and month and day)
38
47
  page.slug ||= slug
39
- #context = OpenStruct.new({
40
- #:site=>site,
41
- #:page=>page,
42
- #})
43
48
  context = page.create_context
44
- #page.body = page.render( context )
45
49
  page.output_path = "#{@path_prefix}/#{year}/#{month}/#{day}/#{page.slug}.html"
46
- #page.layout = 'post'
47
50
  posts << page
48
51
  end
49
52
  end
@@ -59,12 +62,46 @@ module Awestruct
59
62
  last.send( "previous_#{singular}=", e )
60
63
  end
61
64
  last = e
65
+ archive << e
62
66
  end
63
-
67
+ site.pages.concat( archive.generate_pages( site.engine, archive_template, archive_path ) ) if (archive_template && archive_path)
64
68
  site.send( "#{@assign_to}=", posts )
69
+ site.send( "#{@assign_to}_archive = ", archive )
65
70
 
66
71
  end
67
72
 
73
+
74
+ class Archive
75
+ attr_accessor :posts
76
+
77
+ def initialize
78
+ @posts = {}
79
+ end
80
+
81
+ def <<( post )
82
+ posts[post.date.year] ||= {}
83
+ posts[post.date.year][post.date.month] ||= {}
84
+ posts[post.date.year][post.date.month][post.date.day] ||= []
85
+ posts[post.date.year][post.date.month][post.date.day] << post
86
+ end
87
+
88
+ def generate_pages( engine, template, output_path )
89
+ pages = []
90
+ posts.keys.sort.each do |year|
91
+ posts[year].keys.sort.each do |month|
92
+ posts[year][month].keys.sort.each do |day|
93
+ archive_page = engine.find_and_load_site_page( template )
94
+ archive_page.send( "archive=", posts[year][month][day] )
95
+ archive_page.output_path = File.join( output_path, year.to_s, month.to_s, day.to_s, File.basename( template ) + ".html" )
96
+ pages << archive_page
97
+ end
98
+ end
99
+ end
100
+ pages
101
+ end
102
+ end
103
+
68
104
  end
69
105
  end
70
106
  end
107
+
@@ -1,17 +1,41 @@
1
1
  require 'open-uri'
2
+ require 'restclient'
2
3
 
3
4
  module Awestruct
4
5
  module Extensions
5
6
  module RemotePartial
6
7
 
7
- def remotePartial(url)
8
+ def remote_partial(url)
9
+ url_tmp = url.sub('http://', '')
10
+ r = 'remote_partial/' + url_tmp[/(.*)\/[^\/].+$/, 1]
11
+ tmp = File.join(tmp(site.tmp_dir, r), File.basename(url_tmp))
12
+ get_or_cache(tmp, url)
13
+ end
8
14
 
9
- page = open(url)
10
- return nil if !page
11
- page.read
15
+ alias_method :remotePartial, :remote_partial
12
16
 
17
+ def get_or_cache(tmp_file, url)
18
+ response_body = ""
19
+ if !File.exist?tmp_file
20
+ puts url
21
+ response_body = RestClient.get(url, :cache => false) { |response, request, result, &block|
22
+ case response.code
23
+ when 404
24
+ response
25
+ else
26
+ response.return!(request, result, &block)
27
+ end
28
+ }.body;
29
+ File.open(tmp_file, 'w') do |out|
30
+ out.write response_body
31
+ end
32
+ else
33
+ response_body = File.read(tmp_file)
34
+ end
35
+ return response_body
13
36
  end
14
37
 
38
+
15
39
  end
16
40
  end
17
41
  end
@@ -8,8 +8,23 @@ module Awestruct
8
8
  module Extensions
9
9
  class Sitemap
10
10
 
11
+ def initialize
12
+ @excluded_files = [ '/.htaccess', '/favicon.ico' ,'/robots.txt', ].to_set
13
+ @excluded_extensions = ['.atom', '.scss', '.css', '.png', '.jpg', '.gif', '.js' ].to_set
14
+ end
15
+
11
16
  def execute( site )
12
17
 
18
+ # Add additional excludes from _config/sitemap.yml
19
+ if site.sitemap
20
+ if site.sitemap["excluded_files"]
21
+ @excluded_files.merge(site.sitemap.excluded_files)
22
+ end
23
+ if site.sitemap["excluded_extensions"]
24
+ @excluded_extensions.merge(site.sitemap.excluded_extensions)
25
+ end
26
+ end
27
+
13
28
  # Go through all of the site's pages and add sitemap metadata
14
29
  sitemap_pages = []
15
30
  entries = site.pages
@@ -67,18 +82,7 @@ module Awestruct
67
82
  end
68
83
 
69
84
  def valid_sitemap_entry( page )
70
- page.output_filename != '.htaccess' &&
71
- page.output_filename != 'screen.css' &&
72
- page.output_filename != 'print.css' &&
73
- page.output_filename != 'ie.css' &&
74
- page.output_filename != 'robots.txt' &&
75
- page.output_extension != '.atom' &&
76
- page.output_extension != '.scss' &&
77
- page.output_extension != '.css' &&
78
- page.output_extension != '.png' &&
79
- page.output_extension != '.jpg' &&
80
- page.output_extension != '.gif' &&
81
- page.output_extension != '.js'
85
+ !@excluded_files.member?(page.output_path) && !@excluded_extensions.member?(page.output_extension)
82
86
  end
83
87
 
84
88
  end
@@ -4,4 +4,4 @@
4
4
  .tag-cloud
5
5
  - for tag in page.tags
6
6
  %span.tag{:class=>"tag-#{tag.group}"}
7
- %a{:href=>tag.primary_page.url}= tag
7
+ %a{:href=>site.base_url + tag.primary_page.url}= tag
@@ -9,7 +9,9 @@ require 'awestruct/handlers/erb_handler'
9
9
  require 'awestruct/handlers/haml_handler'
10
10
  require 'awestruct/handlers/sass_handler'
11
11
  require 'awestruct/handlers/scss_handler'
12
+ require 'awestruct/handlers/javascript_handler'
12
13
  require 'awestruct/handlers/coffeescript_handler'
14
+ require 'awestruct/handlers/redirect_handler'
13
15
 
14
16
  module Awestruct
15
17
 
@@ -25,7 +27,9 @@ module Awestruct
25
27
  Awestruct::Handlers::HamlHandler::CHAIN,
26
28
  Awestruct::Handlers::SassHandler::CHAIN,
27
29
  Awestruct::Handlers::ScssHandler::CHAIN,
30
+ Awestruct::Handlers::JavascriptHandler::CHAIN,
28
31
  Awestruct::Handlers::CoffeescriptHandler::CHAIN,
32
+ Awestruct::Handlers::RedirectHandler::CHAIN,
29
33
  HandlerChain.new( /.*/, Awestruct::Handlers::FileHandler )
30
34
  ]
31
35
 
@@ -1,4 +1,5 @@
1
1
  require 'awestruct/handler_chain'
2
+ require 'shellwords'
2
3
  require 'open3'
3
4
 
4
5
  module Awestruct
@@ -97,8 +98,9 @@ module Awestruct
97
98
  end
98
99
 
99
100
  def execute_shell(command, input=nil)
100
- Open3.popen3(command) do |stdin, stdout, _|
101
+ Open3.popen3(Shellwords.escape( command )) do |stdin, stdout, _|
101
102
  stdin.puts input unless input.nil?
103
+ stdin.close
102
104
  out = stdout.read
103
105
  end
104
106
  rescue Errno::EPIPE
@@ -60,7 +60,7 @@ module Awestruct
60
60
  end
61
61
 
62
62
  def open
63
- input_stream = IO.open(IO.sysopen(@path, "rb"), "rb" )
63
+ input_stream = IO.open(IO.sysopen(@path, "rb"), "rb" )
64
64
  result = input_stream.read
65
65
  input_stream.close
66
66
  return result
@@ -17,13 +17,24 @@ module Awestruct
17
17
 
18
18
  content = content.gsub( /\\/, '\\\\\\\\' )
19
19
  content = content.gsub( /\\\\#/, '\\#' )
20
+ content = content.gsub( Regexp.new('#(?!{)'), '\#' ) if ruby_19?
20
21
  content = content.gsub( '@', '\@' )
21
22
  content = "%@#{content}@"
22
- c = context.instance_eval( content )
23
+ begin
24
+ c = context.instance_eval( content )
25
+ rescue Exception => e # Don't barf all over ourselves if an exception is thrown
26
+ $stderr.puts "Exception thrown interpolating content. #{e.to_s}"
27
+ $stderr.puts e.backtrace
28
+ c = delegate.raw_content
29
+ end
23
30
  c
24
31
 
25
32
  end
26
33
 
34
+ def ruby_19?
35
+ @is_ruby_19 ||= (::Config::CONFIG['ruby_version'] =~ %r(^1\.9))
36
+ end
37
+
27
38
  end
28
39
  end
29
40
  end
@@ -0,0 +1,47 @@
1
+
2
+ require 'awestruct/handler_chain'
3
+ require 'awestruct/handlers/base_handler'
4
+ require 'awestruct/handlers/file_handler'
5
+ require 'awestruct/handlers/front_matter_handler'
6
+ require 'awestruct/handlers/interpolation_handler'
7
+
8
+ module Awestruct
9
+ module Handlers
10
+ class JavascriptHandler < BaseHandler
11
+
12
+
13
+ CHAIN = Awestruct::HandlerChain.new( /\.js$/,
14
+ Awestruct::Handlers::FileHandler,
15
+ Awestruct::Handlers::FrontMatterHandler,
16
+ Awestruct::Handlers::InterpolationHandler,
17
+ Awestruct::Handlers::JavascriptHandler
18
+ )
19
+
20
+ def initialize(site, delegate)
21
+ super( site, delegate )
22
+ end
23
+
24
+ def simple_name
25
+ File.basename( relative_source_path, '.js' )
26
+ end
27
+
28
+ def output_filename
29
+ File.basename( relative_source_path )
30
+ end
31
+
32
+ def output_extension
33
+ '.js'
34
+ end
35
+
36
+ def content_syntax
37
+ :javascript
38
+ end
39
+
40
+ def rendered_content(context, with_layouts=false)
41
+ delegate.rendered_content( context, with_layouts )
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+
@@ -19,6 +19,7 @@ module Awestruct
19
19
  def inherit_front_matter(outer_page)
20
20
  #page.prepare!
21
21
  #page.handler.inherit_front_matter( outer_page )
22
+ @page.handler.inherit_front_matter(outer_page)
22
23
  end
23
24
 
24
25
  def output_path
@@ -0,0 +1,48 @@
1
+ require 'awestruct/handler_chain'
2
+ require 'awestruct/handlers/base_handler'
3
+ require 'awestruct/handlers/file_handler'
4
+ require 'awestruct/handlers/front_matter_handler'
5
+ require 'awestruct/handlers/interpolation_handler'
6
+
7
+ module Awestruct
8
+ module Handlers
9
+ class RedirectHandler < BaseHandler
10
+
11
+
12
+ CHAIN = Awestruct::HandlerChain.new( /\.redirect$/,
13
+ Awestruct::Handlers::FileHandler,
14
+ Awestruct::Handlers::FrontMatterHandler,
15
+ Awestruct::Handlers::InterpolationHandler,
16
+ Awestruct::Handlers::RedirectHandler
17
+ )
18
+
19
+ def initialize(site, delegate)
20
+ super( site, delegate )
21
+ end
22
+
23
+ def simple_name
24
+ File.basename( relative_source_path, '.redirect' )
25
+ end
26
+
27
+ def output_filename
28
+ simple_name + output_extension
29
+ end
30
+
31
+ def output_extension
32
+ '.html'
33
+ end
34
+
35
+ def content_syntax
36
+ :text
37
+ end
38
+
39
+ def rendered_content(context, with_layouts=false)
40
+ url = delegate.rendered_content( context, with_layouts ).strip
41
+ %{<head><meta http-equiv="location" content="URL=#{url}" /></head>}
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+
48
+
@@ -43,6 +43,7 @@ module Awestruct
43
43
 
44
44
  def inherit_front_matter_from(hash)
45
45
  hash.each do |k,v|
46
+ #puts "#{self.output_path} overwrite key: #{k}:#{self[k]} -> #{v}" if ( key?( k ) and !self[k].nil? and !self[k].eql? v)
46
47
  unless ( key?( k ) )
47
48
  self[k.to_sym] = v
48
49
  end
@@ -76,6 +77,9 @@ module Awestruct
76
77
  handler.output_extension
77
78
  end
78
79
 
80
+ def output_filename
81
+ handler.output_filename
82
+ end
79
83
 
80
84
  def source_path
81
85
  handler.path.to_s
@@ -120,9 +124,27 @@ module Awestruct
120
124
  end
121
125
 
122
126
  def rendered_content(context=create_context(), with_layouts=true)
123
- Awestruct::Dependencies.push_page( self ) if context.site.config.track_dependencies
127
+ if context.site.config.track_dependencies
128
+ Awestruct::Dependencies.push_page( self )
129
+ end
124
130
  c = handler.rendered_content( context, with_layouts )
125
- Awestruct::Dependencies.pop_page if context.site.config.track_dependencies
131
+ if context.site.config.track_dependencies
132
+ Awestruct::Dependencies.pop_page
133
+
134
+ # temp disable traqcking when we collect the hash to not dirty the results
135
+ Awestruct::Dependencies.track_dependencies = false
136
+ if with_layouts
137
+ @dependencies.content_hash = Digest::SHA2.hexdigest(c)
138
+
139
+ # create a new Page so we can inherit the updated values not reflected in self
140
+ tmp_page = Awestruct::Page.new @site
141
+ @handler.inherit_front_matter(tmp_page)
142
+ string_to_hash = tmp_page.to_a.each{|x| x[0]=x[0].to_s; x[1]=x[1].to_s; x}.sort.to_s
143
+ hash = Digest::SHA2.hexdigest(string_to_hash)
144
+ @dependencies.key_hash = hash
145
+ end
146
+ Awestruct::Dependencies.track_dependencies = true
147
+ end
126
148
  c
127
149
  end
128
150