Skip to content
This repository

Add stylesheet and javascript liquid tags that include mtimes #551

Closed
wants to merge 5 commits into from

4 participants

Kyle Burton Parker Moore Aleksey V. Zapparov rn-superg
Kyle Burton

This is a patch that implements:

stylesheet tag render with mtime

and additionally adds a javascript tag. I re-created the patch on top of the current master.

Please let me know if this is ok or if there are changes that should be made.

Best Regards,

Kyle Burton

Kyle Burton

Testing this further, it appears that it needs some more work in locating the files. I'll open another pull req when I have worked those issues out.

Kyle Burton kyleburton closed this May 08, 2012
Kyle Burton kyleburton reopened this May 08, 2012
Kyle Burton

Sorry for all the back and forth. After using it a bit more, I enhanced it to additionally look for the included file relative to the current page, and to support a page property: js_includes so a list of includes can be specified in the yaml front matter.

I think this is ready to be tried out. I'd love to see this feature get into the released gem - please let me know if there are any changes, fixes, cleanup, etc that will help get this considered for inclusion.

Best Regards,

Kyle

Parker Moore
Owner

Ref: #44.

Parker Moore parkr closed this March 17, 2013
Parker Moore
Owner
parkr commented March 17, 2013

This is pretty out-dated. I'd like to hear @mojombo and @qrush's thoughts on this one - the last comment from either of them was 4 years ago.

Aleksey V. Zapparov
ixti commented March 17, 2013

This (and much more to work with assets) can be achieved with existing plugins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  jekyll.gemspec
@@ -90,6 +90,8 @@ Gem::Specification.new do |s|
90 90
     lib/jekyll/static_file.rb
91 91
     lib/jekyll/tags/highlight.rb
92 92
     lib/jekyll/tags/include.rb
  93
+    lib/jekyll/tags/stylesheet_link.rb
  94
+    lib/jekyll/tags/javascript.rb
93 95
     test/helper.rb
94 96
     test/source/.htaccess
95 97
     test/source/_includes/sig.markdown
71  lib/jekyll/tags/javascript.rb
... ...
@@ -0,0 +1,71 @@
  1
+module Jekyll
  2
+
  3
+  class Javascript < Liquid::Tag
  4
+    def initialize(tag_name, file, tokens)
  5
+      super
  6
+      @file = file
  7
+    end
  8
+
  9
+    def find_javascript(original_file,context)
  10
+      file = original_file
  11
+      file.strip!
  12
+      file.gsub! /^["']/, ""
  13
+      file.gsub! /["']$/, ""
  14
+      if ! file =~ /.[a-z]+$/
  15
+        file = "#{file}.js"
  16
+      end
  17
+
  18
+      source_dir = context.registers[:site].source
  19
+      page_url = context.environments.first['page']['url']
  20
+      files = [File.join(source_dir, file),
  21
+               # try with stripping a leading slash
  22
+               File.join(source_dir, file[1..-1]),
  23
+               # try relative to the current page
  24
+               File.join(source_dir, File.dirname(page_url), file)]
  25
+
  26
+      if context['site']['javascript']
  27
+        context['site']['javascript']['search_paths'].each do |path|
  28
+          files << File.join(path, file)
  29
+          files << File.join(path, file[1..-1])
  30
+          files << File.join(path, File.dirname(page_url), file)
  31
+        end
  32
+      end
  33
+
  34
+      files.each {|file|
  35
+        if File.exists? file
  36
+          return file
  37
+        end
  38
+      }
  39
+      warn "JAVASCRIPT FILE: '#{original_file}' NOT FOUND"
  40
+      return original_file
  41
+    end
  42
+
  43
+    def render(context, *stuff)
  44
+      files = context.environments.first['page']['js_includes']
  45
+      if !@file.nil? && !@file.empty?
  46
+        files = [@file]
  47
+      end
  48
+
  49
+      result = []
  50
+      files.each do |file|
  51
+        next if file.nil? || file.empty?
  52
+        mfile = find_javascript(file,context)
  53
+
  54
+        mtime = nil
  55
+        if ! File.exists?(mfile)
  56
+          mtime = Time.now.to_i
  57
+        else
  58
+          mtime = File.mtime(mfile).to_i
  59
+        end
  60
+
  61
+        result << %Q{<script src="#{file}?#{mtime}" type="text/javascript" ></script>}
  62
+      end
  63
+
  64
+      result.join("")
  65
+    end
  66
+  end
  67
+
  68
+end
  69
+
  70
+Liquid::Template.register_tag('javascript', Jekyll::Javascript)
  71
+
91  lib/jekyll/tags/stylesheet_link.rb
... ...
@@ -0,0 +1,91 @@
41  test/test_tags.rb
@@ -198,4 +198,45 @@ def fill_post(code, override = {})
198 198
       assert_match %r{/2008/11/21/complex/}, @result
199 199
     end
200 200
   end
  201
+
  202
+  context "test stylesheet_link" do
  203
+    setup do
  204
+      content = <<-CONTENT
  205
+---
  206
+title: test stylesheet link
  207
+---
  208
+
  209
+{% stylesheet "/path/to/some/css.file" %}
  210
+
  211
+      CONTENT
  212
+      create_post(content, {}, Jekyll::TextileConverter)
  213
+    end
  214
+
  215
+    should "have stylesheet link with time based querystring" do
  216
+      assert_match %r{stylesheet}, @result
  217
+      assert_match %r{href=}, @result
  218
+      assert_match %r{\?\d+}, @result
  219
+    end
  220
+  end
  221
+
  222
+  context "test javascript" do
  223
+    setup do
  224
+      content = <<-CONTENT
  225
+---
  226
+title: test javascript link
  227
+---
  228
+
  229
+{% javascript "/path/to/some/file.js" %}
  230
+
  231
+      CONTENT
  232
+      create_post(content, {}, Jekyll::TextileConverter)
  233
+    end
  234
+
  235
+    should "have javascript link with time based querystring" do
  236
+      assert_match %r{script}, @result
  237
+      assert_match %r{src=}, @result
  238
+      assert_match %r{\?\d+}, @result
  239
+      puts @result
  240
+    end
  241
+  end
201 242
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.