stylize 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $LOAD_PATH << File.dirname(__FILE__) + '/../lib'
4
+
5
+ require 'stylize'
6
+
7
+ Stylize.from_cli(ARGV)
@@ -0,0 +1,79 @@
1
+ require 'optparse'
2
+ require 'rubygems'
3
+ require 'nokogiri'
4
+ require 'stylize/injector'
5
+
6
+ module Stylize
7
+ VERSION = '0.1.1'
8
+
9
+ VALID_FORMATS = [
10
+ :style, # concatenates CSS files and injects inside <style> tag
11
+ :link # links to each CSS file with <link> tag
12
+ ]
13
+
14
+ DEFAULT_OPTIONS = {
15
+ :format => :style
16
+ }
17
+
18
+ def self.stylize(html, css_files, opts={})
19
+ doc = Nokogiri::HTML(html)
20
+
21
+ head = (doc.root/:head).first || doc.root.add_child('<head></head>').first
22
+
23
+ Stylize::Injector.for_format(opts[:format]).inject(head, css_files, opts)
24
+
25
+ doc.to_s
26
+ end
27
+
28
+ def self.from_cli(argv)
29
+ options = DEFAULT_OPTIONS.dup
30
+
31
+ opt = OptionParser.new do |opts|
32
+ opts.banner = "Usage: stylize [options] [page.html | -] styles.css [...]"
33
+
34
+ opts.separator 'If reading from stdin, use a - (dash) in place of the html filename'
35
+ opts.separator ''
36
+
37
+ opts.on '-f FORMAT', '--format FORMAT', VALID_FORMATS, 'Select injection format' do |fmt|
38
+ options[:format] = fmt
39
+ end
40
+
41
+ opts.on_tail '-h', '--help', 'Show this message' do
42
+ puts opts
43
+ exit
44
+ end
45
+
46
+ opts.on_tail '-v', '--version', 'Display version information' do
47
+ puts "stylize #{Stylize::VERSION}"
48
+ exit
49
+ end
50
+ end
51
+
52
+ begin
53
+ opt.parse! argv
54
+ rescue OptionParser::InvalidOption => e
55
+ $stderr.puts e
56
+ $stderr.puts opt
57
+ abort
58
+ end
59
+
60
+ if argv.length <= 1
61
+ $stderr.puts "at least one input stylesheet is required"
62
+ $stderr.puts opt
63
+ abort
64
+ end
65
+
66
+ file = argv.shift
67
+
68
+ if file == '-'
69
+ input = $stdin.read
70
+ else
71
+ input = File.read(file)
72
+ end
73
+
74
+ print self.stylize(input, argv, options)
75
+ end
76
+ end
77
+
78
+ require 'stylize/link_injector.rb'
79
+ require 'stylize/style_injector.rb'
@@ -0,0 +1,16 @@
1
+ module Stylize
2
+ class Injector
3
+ @@injectors = {}
4
+ def self.register(format, klass)
5
+ @@injectors[format] = klass
6
+ end
7
+
8
+ def self.for_format(format)
9
+ @@injectors[format].new
10
+ end
11
+
12
+ def inject(elem, files, opts={})
13
+ raise NotImplementedError.new("Must subclass Injector and override inject()")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'stylize/injector'
2
+
3
+ module Stylize
4
+ class LinkInjector < Stylize::Injector
5
+ register :link, self
6
+
7
+ def inject(elem, files, opts={})
8
+ files.each do |f|
9
+ link = elem.add_element('link')
10
+ link.add_attribute('type','text/css')
11
+ link.add_attribute('rel','stylesheet')
12
+ link.add_attribute('href',f)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require 'stylize/injector'
2
+
3
+ module Stylize
4
+ class StyleInjector < Stylize::Injector
5
+ register :style, self
6
+
7
+ def inject(elem, files, opts={})
8
+ elem.add_child %{<style type="text/css">
9
+ #{files.map{|f| "/* #{f} */\n#{File.read(f)}"}.join("\n\n")}
10
+ </style>}
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stylize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Austin Hyde
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &70183750446920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70183750446920
25
+ description: Stylize is a command-line utility that injects CSS into HTML documents
26
+ email: austin109@gmail.com
27
+ executables:
28
+ - stylize
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/stylize.rb
33
+ - lib/stylize/injector.rb
34
+ - lib/stylize/link_injector.rb
35
+ - lib/stylize/style_injector.rb
36
+ - bin/stylize
37
+ homepage: http://bitbucket.org/austinhyde/stylize
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.16
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Injects CSS into existing HTML files
61
+ test_files: []