Permalink
Browse files

Added support for loadCSS and async javascripts

  • Loading branch information...
1 parent d895c2b commit 7575ac4ac0a0321b3335adca9b7e3cfecc160b30 @imathis imathis committed May 3, 2015
View
@@ -0,0 +1,52 @@
+/*!
+loadCSS: load a CSS file asynchronously.
+[c]2014 @scottjehl, Filament Group, Inc.
+Licensed MIT
+*/
+
+/* exported loadCSS */
+function loadCSS( href, before, media, callback ){
+ "use strict";
+ // Arguments explained:
+ // `href` is the URL for your CSS file.
+ // `before` optionally defines the element we'll use as a reference for injecting our <link>
+ // By default, `before` uses the first <script> element in the page.
+ // However, since the order in which stylesheets are referenced matters, you might need a more specific location in your document.
+ // If so, pass a different reference element to the `before` argument and it'll insert before that instead
+ // note: `insertBefore` is used instead of `appendChild`, for safety re: http://www.paulirish.com/2011/surefire-dom-element-insertion/
+ var ss = window.document.createElement( "link" );
+ var ref = before || window.document.getElementsByTagName( "script" )[ 0 ];
+ var sheets = window.document.styleSheets;
+ ss.rel = "stylesheet";
+ ss.href = href;
+ // temporarily, set media to something non-matching to ensure it'll fetch without blocking render
+ ss.media = "only x";
+ // DEPRECATED
+ if( callback ) {
+ ss.onload = callback;
+ }
+
+ // inject link
+ ref.parentNode.insertBefore( ss, ref );
+ // This function sets the link's media back to `all` so that the stylesheet applies once it loads
+ // It is designed to poll until document.styleSheets includes the new sheet.
+ ss.onloadcssdefined = function( cb ){
+ var defined;
+ for( var i = 0; i < sheets.length; i++ ){
+ if( sheets[ i ].href && sheets[ i ].href.indexOf( href ) > -1 ){
+ defined = true;
+ }
+ }
+ if( defined ){
+ cb();
+ } else {
+ setTimeout(function() {
+ ss.onloadcssdefined( cb );
+ });
+ }
+ };
+ ss.onloadcssdefined(function() {
+ ss.media = media || "all";
+ });
+ return ss;
+}
@@ -13,6 +13,9 @@
require 'octopress-ink/jekyll/hooks'
require 'octopress-ink/tags/set_lang'
require 'octopress-ink/cache'
+begin
+ require 'octopress-debugger'
+rescue; end
module Octopress
module Ink
@@ -54,7 +57,7 @@ def payload(lang=nil)
'plugins' => config['plugins'],
'theme' => config['theme'],
'octopress' => {
- 'version' => version
+ 'version' => version,
}
}
@@ -2,8 +2,9 @@ module Octopress
module Ink
module Assets
class Coffeescript < Javascript
- def tag
- %{<script src="#{Filters.expand_url(File.join(dir, File.basename(file, '.*') << '.js'))}"></script>}
+
+ def tag_path
+ Filters.expand_url(File.join(dir, File.basename(file, '.*') << '.js'))
end
def add
@@ -3,7 +3,11 @@ module Ink
module Assets
class Javascript < Asset
def tag
- %Q{<script src="#{Filters.expand_url(File.join(dir, file))}"></script>}
+ %Q{<script src="#{tag_path}"></script>}
+ end
+
+ def tag_path
+ Filters.expand_url(File.join(dir, file))
end
def add
@@ -27,7 +27,11 @@ def disabled?
end
def tag
- %Q{<link href="#{Filters.expand_url(File.join(dir, output_file_name))}" media="#{media}" rel="stylesheet" type="text/css">}
+ %Q{<link href="#{tag_path}" media="#{media}" rel="stylesheet" type="text/css">}
+ end
+
+ def tag_path
+ Filters.expand_url(File.join(dir, output_file_name))
end
def add
@@ -9,6 +9,8 @@ module Ink
'combine_js' => true,
'compress_js' => true,
'uglifier' => {},
+ 'async_css' => false,
+ 'async_js' => true
},
'date_format' => 'ordinal',
@@ -10,9 +10,9 @@ def reset
@javascripts = nil
@uglify_settings = nil
@combined_js = ''
- @combined_stylesheets = nil
@stylesheet_fingerprint = {}
@javascript_fingerprint = nil
+ @uglify_settings ||= Jekyll::Utils.symbolize_hash_keys(Ink.configuration['asset_pipeline']['uglifier'])
end
# Compile CSS to take advantage of Sass's compression settings
@@ -55,20 +55,60 @@ def sass_converter
# Return a link tag, for all plugins' stylesheets
#
+ def stylesheet_tag
+ if Ink.configuration['asset_pipeline']['async_css']
+ async_stylesheet_tag
+ else
+ combined_stylesheet_tag
+ end
+ end
+
def combined_stylesheet_tag
tags = ''
- combine_stylesheets.keys.each do |media|
- tags.concat "<link href='#{Filters.expand_url(combined_stylesheet_path(media))}' media='#{media}' rel='stylesheet' type='text/css'>"
+ combined_stylesheet_urls.each do |media, url|
+ tags.concat "<link href='#{url}' media='#{media}' rel='stylesheet' type='text/css'>"
end
tags
end
- def combined_javascript_tag
+ def async_stylesheet_tag
+ js = uglify(File.read(Ink.gem_dir('assets','js','loadCSS.js')))
+ %Q{<script>#{js}\n#{load_css}\n</script>\n<noscript>#{combined_stylesheet_tag}</noscript>}
+ end
+
+ def load_css
+ script = ''
+ combined_stylesheet_urls.each do |media, url|
+ script << %Q{loadCSS("#{url}", null, "#{media}")\n}
+ end
+ script
+ end
+
+ def combined_stylesheet_urls
+ urls = {}
+ combine_stylesheets.keys.each do |media|
+ urls[media] = combined_stylesheet_url(media)
+ end
+ urls
+ end
+
+ def combined_stylesheet_url(media)
+ Filters.expand_url(combined_stylesheet_path(media))
+ end
+
+ def javascript_tag
unless @combined_js == ''
- "<script src='#{Filters.expand_url(combined_javascript_path)}'></script>"
+ if Ink.configuration['asset_pipeline']['async_js']
+ "<script async src='#{combined_javascript_url}'></script>"
+ else
+ "<script src='#{combined_javascript_url}'></script>"
+ end
end
end
+ def combined_javascript_url
+ Filters.expand_url(combined_javascript_path)
+ end
# Combine stylesheets from each plugin
#
@@ -80,6 +120,7 @@ def combine_stylesheets
@combined_stylesheets ||= begin
combined = {}
+
stylesheets.clone.each do |media,files|
files.each do |file|
header = "/* #{file.plugin.type.capitalize}: #{file.plugin.name} */"
@@ -148,12 +189,10 @@ def write_combined_javascript
javascripts.each do |file|
js = if compress_js?(file)
- @uglify_settings ||= Jekyll::Utils.symbolize_hash_keys(Ink.configuration['asset_pipeline']['uglifier'])
-
if cached = Cache.read_cache(file, @uglify_settings)
cached
else
- js = Uglifier.new(@uglify_settings).compile(file.content)
+ js = uglify(file.content)
Cache.write_to_cache(file, js, @uglify_settings)
end
else
@@ -172,6 +211,10 @@ def write_combined_javascript
end
end
+ def uglify(js)
+ Uglifier.new(@uglify_settings).compile(js)
+ end
+
def compress_js?(file)
Ink.configuration['asset_pipeline']['compress_js'] && !file.path.end_with?('.min.js')
end
@@ -1,7 +1,7 @@
module Octopress
module Ink
module Plugins
- attr_reader :registered
+ attr_reader :registered, :css_asset_paths, :js_asset_paths
extend self
@registered = false
@@ -150,15 +150,15 @@ def add_javascripts
def css_tags
if Ink.configuration['asset_pipeline']['combine_css']
- PluginAssetPipeline.combined_stylesheet_tag
+ PluginAssetPipeline.stylesheet_tag
else
@css_tags.join('')
end
end
def js_tags
if Ink.configuration['asset_pipeline']['combine_js']
- PluginAssetPipeline.combined_javascript_tag
+ PluginAssetPipeline.javascript_tag
else
@js_tags.join('')
end
@@ -1,2 +1,2 @@
<link href='/stylesheets/all-.css' media='all' rel='stylesheet' type='text/css'><link href='/stylesheets/print-.css' media='print' rel='stylesheet' type='text/css'>
-<script src='/javascripts/all-.js'></script>
+<script async src='/javascripts/all-.js'></script>
@@ -1,2 +1,2 @@
<link href='/stylesheets/print-.css' media='print' rel='stylesheet' type='text/css'><link href='/stylesheets/all-.css' media='all' rel='stylesheet' type='text/css'>
-<script src='/javascripts/all-.js'></script>
+<script async src='/javascripts/all-.js'></script>
@@ -0,0 +1,13 @@
+# Contributor Code of Conduct
+
+As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
+
+We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
+
+Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
+
+Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
+
+Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
+
+This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 TODO: Write your name
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -1,12 +1,11 @@
require 'test-theme/version'
require 'octopress-ink'
-Octopress::Ink.add_plugin({
+Octopress::Ink.add_theme({
name: "Test Theme",
slug: "theme",
gem: "test-theme",
path: File.expand_path(File.join(File.dirname(__FILE__), "..")),
- type: "theme",
version: TestTheme::VERSION,
description: "", # What does your theme/plugin do?
source_url: "https://github.com/user/project", # <- Update info
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
+ spec.license = "MIT"
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.

0 comments on commit 7575ac4

Please sign in to comment.