css_primer 0.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.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ May 17, 2010 - v0.1
2
+
3
+ - initial release
data/README ADDED
@@ -0,0 +1,29 @@
1
+ CSSPrimer takes in a markup file and generates a css file of all the extracted ids and classes.
2
+
3
+ == Terminal usage
4
+ As a ruby gem:
5
+ css_primer -h
6
+
7
+ Or you can just:
8
+ ruby bin/css_primer -h
9
+
10
+ == Using Examples
11
+ ruby main.rb --in ../examples/test.html
12
+ ruby main.rb --in ../examples/test.xml --out some_styles.css
13
+
14
+ == Dependencies
15
+ Core Lib
16
+ - ostruct
17
+ - optparse
18
+ - fileutils
19
+
20
+ == Tests
21
+ You need rspec http://rspec.info/
22
+
23
+ spec tests/css_primer.rspec
24
+
25
+ == Author
26
+ Copyright 2010 Brent Lintner
27
+
28
+ The MIT License
29
+ http://www.opensource.org/licenses/mit-license.php
data/bin/css_primer ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/ruby
2
+ lib = File.join(File.dirname(__FILE__), %w(.. lib))
3
+ $:.unshift(lib) if File.exists?(lib) unless $:.member?(lib)
4
+
5
+ require 'optparse'
6
+ require 'ostruct'
7
+ require 'fileutils'
8
+
9
+ require "#{lib}/modules/GenericApplication"
10
+ require "#{lib}/modules/IOHelper"
11
+ require "#{lib}/classes/CSSPrimer"
12
+
13
+ CSSPrimer.new(ARGV).prime!
14
+
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.platform = Gem::Platform::RUBY
4
+ s.name = 'css_primer'
5
+ s.version = '0.1'
6
+ s.summary = 'CSS Primer for markup files'
7
+ s.description = 'Takes in a markup file (html/xml) and creates a CSS file with a reference to classes/ids.'
8
+
9
+ s.required_ruby_version = '>= 1.8.6'
10
+
11
+ s.author = 'Brent Lintner'
12
+ s.email = 'brent.lintner@gmail.com'
13
+ s.homepage = 'http://github.com/brentlintner/css_primer'
14
+
15
+ s.files = Dir['bind/*', 'css_primer.gemspec', 'CHANGELOG', 'README', 'lib/classes/*', 'lib/modules/*'].to_a
16
+
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = %w( README )
19
+ s.rdoc_options.concat ['--main', 'README']
20
+
21
+ s.executables = ["css_primer"]
22
+ s.require_path = "lib"
23
+
24
+ end
25
+
26
+
27
+
28
+
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/ruby
2
+
3
+ HELP = <<TEXT
4
+
5
+ == CSS::Primer
6
+ Takes in a markup file (html/xml) and creates a CSS file with a reference to classes/ids.
7
+
8
+ == Usage
9
+ css_primer [options] --in FILE_TO_PRIME [--out] CSS_OUTPUT_FILE
10
+
11
+ == Options
12
+ -h, --help Displays help message
13
+ -q, --quiet Output as little as possible, overrides verbose
14
+ -V, --verbose Verbose output
15
+ -i, --in Markup file to parse
16
+ -o, --out CSS file to save out to (optional)
17
+
18
+ TEXT
19
+
20
+ class CSSPrimer
21
+
22
+ include GenericApplication
23
+ include IOHelper
24
+
25
+ class MarkupFileError < StandardError ; end
26
+
27
+ attr_accessor :markup_file, :css_file
28
+
29
+ def initialize(argv)
30
+
31
+ @config = OpenStruct.new
32
+ @config.HELP = HELP
33
+
34
+ @config.DEFAULT_CSS_FILE_OUT = "primed_styles.css"
35
+
36
+ @argv = argv
37
+
38
+ @options = OpenStruct.new
39
+ @options.verbose = false
40
+ @options.quiet = false
41
+
42
+ @my_ids, @my_classes = [], []
43
+
44
+ @markup_file = ""
45
+ @css_file = self.config("DEFAULT_CSS_FILE_OUT")
46
+
47
+ end
48
+
49
+ def config(struct)
50
+ @config.send(struct)
51
+ end
52
+
53
+ def prime!
54
+
55
+ if self.parsed_options?
56
+
57
+ begin
58
+
59
+ self.log("what up! lets do some priming\nopening up #{@markup_file}\n") if @options.verbose
60
+
61
+ buffer = ""
62
+
63
+ # TODO: clean up
64
+ self.read_in_file(@markup_file).scan(/(id="\S+"|class="\S+")/) do |w|
65
+
66
+ w[0].scan(/"\S+"/) do |rule_attribute|
67
+
68
+ css_type = (w[0] =~ /^id=/) ? "#" : "."
69
+
70
+ rule_attribute.gsub!(/"/,"")
71
+
72
+ if css_type == "#"
73
+ @my_ids.push [rule_attribute, css_type]
74
+ else
75
+ @my_classes.push [rule_attribute, css_type]
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ @my_ids.uniq.sort.concat(@my_classes.uniq.sort).each do |arr|
83
+ buffer += "#{arr[1]}#{arr[0]}{\n\n}\n\n"
84
+ end
85
+
86
+ self.save(buffer, @css_file)
87
+
88
+ puts "saved to #{@css_file}\n\nLet's Roll!" if @options.verbose
89
+
90
+ rescue Exception => e
91
+ self.handle_exception(e)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ protected
99
+
100
+ def generate_docs
101
+ begin
102
+ Kernel.exec "rdoc1.8 -U -S -N -o 'rdoc' --main CSSPrimer"
103
+ rescue Exception => e
104
+ self.handle_exception(e)
105
+ ensure
106
+ Kernel.exit!
107
+ end
108
+ end
109
+
110
+ def parsed_options?
111
+
112
+ opts = OptionParser.new
113
+
114
+ opts.on('-v', '--version') { self.log("0.1") ; exit!(0) }
115
+ opts.on('-h', '--help') { self.log(self.config("HELP")) ; exit!(0) }
116
+ opts.on('-V', '--verbose') { @options.verbose = true }
117
+ opts.on('-q', '--quiet') { @options.quiet = true }
118
+ opts.on('-r', '--rdoc') { self.generate_docs }
119
+
120
+ opts.on('-i', '--in MARKUPFILE') do |markup_file|
121
+ @markup_file = markup_file
122
+ end
123
+
124
+ opts.on('-o', '--out [CSSFILE]') do |css_file|
125
+ @css_file = css_file
126
+ end
127
+
128
+ begin
129
+
130
+ opts.parse!(@argv)
131
+
132
+ rescue Exception => e
133
+ self.handle_exception(e)
134
+ exit!(0)
135
+ end
136
+
137
+ self.process_options
138
+
139
+ true
140
+
141
+ end
142
+
143
+ def process_options
144
+ @options.verbose = false if @options.quiet
145
+
146
+ raise MarkupFileError, "No input markup file specified." if !File.exists?(@markup_file)
147
+ end
148
+
149
+ end
@@ -0,0 +1,16 @@
1
+ module GenericApplication
2
+
3
+ def log(msg)
4
+ puts msg.to_s
5
+ end
6
+
7
+ def handle_exception(e, verbose=false)
8
+ msg = e.message
9
+
10
+ msg = e.exception.to_s+" :: "+msg if e.message.to_s != e.exception.to_s
11
+
12
+ self.log "\nHANDLED EXCEPTION --> #{e.class.to_s}\n\nMESSAGE --> #{msg}" if verbose
13
+ self.log "\nBACKTRACE\n\n#{e.backtrace.join("\n")}\n\n" if verbose
14
+ end
15
+
16
+ end
@@ -0,0 +1,39 @@
1
+ module IOHelper
2
+
3
+ def read_in_file(full_path, &block)
4
+ data = ""
5
+
6
+ if !File.directory?(full_path)
7
+ IO.foreach full_path do |line|
8
+ data += block_given? ? yield(line) : line
9
+ end
10
+ end
11
+
12
+ data
13
+ end
14
+
15
+ def copy_full_dir(orig, target)
16
+ FileUtils.cp_r(orig, target)
17
+ end
18
+
19
+ def delete_full_dir(directory)
20
+ FileUtils.rm_r(directory)
21
+ end
22
+
23
+ def delete_full_dir_contents(directory)
24
+ FileUtils.rm_r(Dir.glob("#{directory}*"))
25
+ end
26
+
27
+ def save(lines, file_to_write)
28
+ File.open(file_to_write, "w") do |file|
29
+ file.syswrite lines
30
+ end
31
+ end
32
+
33
+ def append(lines, file_to_append)
34
+ File.open(file_to_append, "a") do |file|
35
+ file.syswrite lines
36
+ end
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css_primer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Brent Lintner
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-06-07 00:00:00 -04:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: Takes in a markup file (html/xml) and creates a CSS file with a reference to classes/ids.
21
+ email: brent.lintner@gmail.com
22
+ executables:
23
+ - css_primer
24
+ extensions: []
25
+
26
+ extra_rdoc_files:
27
+ - README
28
+ files:
29
+ - css_primer.gemspec
30
+ - CHANGELOG
31
+ - README
32
+ - lib/classes/CSSPrimer.rb
33
+ - lib/modules/IOHelper.rb
34
+ - lib/modules/GenericApplication.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/brentlintner/css_primer
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 1
51
+ - 8
52
+ - 6
53
+ version: 1.8.6
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: CSS Primer for markup files
68
+ test_files: []
69
+