kss 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tom Preston-Werner, Kyle Neath
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ Software), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ # Knyle Style Sheets
2
+
3
+ This is a ruby library for parsing KSS documented stylesheets and generating styleguides.
4
+
5
+ Inspired by [TomDoc](http://tomdoc.org), KSS attempts to provide a methodology for writing maintainable, documented CSS within a team. Specifically, KSS is a CSS structure, documentation specification, and styleguide format. It is **not** a preprocessor, CSS framework, naming convention, or specificity guideline.
6
+
7
+ ## Spec
8
+
9
+ If you would like to learn more about the methodology and ideas behind Knyle Style Sheets, you should read SPEC.md. It contains the documenting syntax and styleguide guidelines.
10
+
11
+ ## Ruby Library
12
+
13
+ This repository includes a ruby library suitable for parsing SASS, SCSS, and CSS documented with KSS guidelines.
14
+
15
+ The library is also fully TomDoc'd, completing the circle of life.
16
+
17
+ ## Development
18
+
19
+ To hack on KSS, you'll need to install dependencies with `bundle install`. Run tests with `rake`.
20
+
21
+ To make your life easier, I suggest `bundle install --binstubs` and adding `bin/` to your `$PATH`. If you don't understand this, just blindly add `bundle exec` in front of everything you'd normally do, like `bundle exec rake`.
22
+
23
+ I apologize on behalf of the Ruby community for this, it's embarrassing and disappointing that dependency management is still so clumsy.
@@ -0,0 +1,56 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ def command?(command)
6
+ system("type #{command} > /dev/null 2>&1")
7
+ end
8
+
9
+ #
10
+ # Tests
11
+ #
12
+
13
+ if command? :turn
14
+ desc "Run tests"
15
+ task :test do
16
+ suffix = "-n #{ENV['TEST']}" if ENV['TEST']
17
+ sh "turn -Ilib:. test/*.rb #{suffix}"
18
+ end
19
+ else
20
+ Rake::TestTask.new do |t|
21
+ t.libs << 'lib'
22
+ t.libs << '.'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+ end
27
+
28
+ #
29
+ # Development
30
+ #
31
+
32
+ desc "Drop to irb."
33
+ task :console do
34
+ exec "irb -I lib -rkss"
35
+ end
36
+
37
+ #
38
+ # Gems
39
+ #
40
+
41
+ begin
42
+ require 'mg'
43
+ MG.new("kss.gemspec")
44
+ rescue LoadError
45
+ warn "mg not available."
46
+ warn "Install it with: gem install mg"
47
+ end
48
+
49
+ desc "Push a new version to Gemcutter and publish docs."
50
+ task :publish => "gem:publish" do
51
+ require File.dirname(__FILE__) + '/lib/kss/version'
52
+
53
+ sh "git tag v#{Kss::VERSION}"
54
+ sh "git push origin master --tags"
55
+ sh "git clean -fd"
56
+ end
@@ -0,0 +1,9 @@
1
+ require 'sass'
2
+
3
+ require 'kss/modifier'
4
+ require 'kss/parser'
5
+ require 'kss/section'
6
+ require 'kss/version'
7
+
8
+ module Kss
9
+ end
@@ -0,0 +1,36 @@
1
+ module Kss
2
+ # Public: Represents a style modifier. Usually a class name or a
3
+ # pseudo-class such as :hover. See the spec on The Modifiers Section for
4
+ # more information.
5
+ class Modifier
6
+
7
+ # Public: Returns the modifier name String.
8
+ attr_accessor :name
9
+
10
+ # Public: Returns the description String for a Modifier.
11
+ attr_accessor :description
12
+
13
+ # Public: Initialize a new Modifier.
14
+ #
15
+ # name - The name String of the modifier.
16
+ # description - The description String of the modifier.
17
+ def initialize(name, description=nil)
18
+ @name = name.to_s
19
+ @description = description
20
+ end
21
+
22
+ # Public: The modifier name as a CSS class. For pseudo-classes, a
23
+ # generated class name is returned. Useful for generating styleguides.
24
+ #
25
+ # Examples
26
+ #
27
+ # :hover => "pseudo-class-hover"
28
+ # sexy-button => "sexy-button"
29
+ #
30
+ # Returns a CSS class String.
31
+ def class_name
32
+ name.sub('.', ' ').sub(':', ' pseudo-class-').strip
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,68 @@
1
+ module Kss
2
+ # Public: The main KSS parser. Takes a directory full of SASS / SCSS / CSS
3
+ # files and parses the KSS within them.
4
+ class Parser
5
+
6
+ # Public: Initializes a new parser based on a directory of files. Scans
7
+ # within the directory recursively for any comment blocks that look like
8
+ # KSS.
9
+ #
10
+ # base_path - The path String where style files are located.
11
+ def initialize(base_path)
12
+ @sections = {}
13
+
14
+ Dir["#{base_path}/**/*.*"].each do |filename|
15
+ root_node = Sass::SCSS::Parser.new(File.read(filename), filename).parse
16
+ root_node.children.each do |node|
17
+ next unless node.is_a? Sass::Tree::CommentNode
18
+ comment_text = self.class.clean_comments node.value[0]
19
+
20
+ if self.class.kss_block? comment_text
21
+ base_name = File.basename(filename)
22
+ section = Section.new(comment_text, base_name)
23
+ @sections[section.section] = section
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ # Public: Takes a cleaned (no comment syntax like // or /* */) comment
30
+ # block and determines whether it is a KSS documentation block.
31
+ #
32
+ # Returns a boolean indicating whether the block conforms to KSS.
33
+ def self.kss_block?(cleaned_comment)
34
+ return false unless cleaned_comment.is_a? String
35
+
36
+ possible_reference = cleaned_comment.split("\n\n").last
37
+ possible_reference =~ /Styleguide \d/i
38
+ end
39
+
40
+ # Takes the raw comment text including comment syntax and strips all
41
+ # comment syntax and normalizes the indention and whitespace to generate
42
+ # a clean comment block.
43
+ #
44
+ # Returns a claned comment String.
45
+ def self.clean_comments(text)
46
+ text.strip!
47
+
48
+ # SASS generated comment syntax
49
+ text.gsub!(/(\/\* )?( \*\/)?/, '') # [/* + space] or [space + */]
50
+ text.gsub!(/\n\s\* ?/, "\n") # * in front of every line
51
+
52
+ # Manual generated comment syntax
53
+ text.gsub!(/^\/\*/, '') # starting block
54
+ text.gsub!(/\*\/$/, '') # ending block
55
+
56
+ text.strip!
57
+ text
58
+ end
59
+
60
+ # Finds the Section for a given styleguide reference
61
+ #
62
+ # Returns a Section for a reference, or a blank Section if none found.
63
+ def section(reference)
64
+ @sections[reference] || Section.new
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,80 @@
1
+ module Kss
2
+ # Public: Represents a styleguide section. Each section describes one UI
3
+ # element. A Section can be thought of as the collection of the description,
4
+ # modifiers, and styleguide reference.
5
+ class Section
6
+
7
+ # Returns the raw comment text for the section, not including comment
8
+ # syntax (such as // or /* */).
9
+ attr_reader :raw
10
+
11
+ # Public: Returns the filename where this section is found.
12
+ attr_reader :filename
13
+
14
+ # Public: Initialize a new Section
15
+ #
16
+ # comment_text - The raw comment String, minus any comment syntax.
17
+ # filename - The filename as a String.
18
+ def initialize(comment_text=nil, filename=nil)
19
+ @raw = comment_text
20
+ @filename = filename
21
+ end
22
+
23
+ # Splits up the raw comment text into comment sections that represent
24
+ # description, modifiers, etc.
25
+ #
26
+ # Returns an Array of comment Strings.
27
+ def comment_sections
28
+ @comment_sections ||= raw ? raw.split("\n\n") : []
29
+ end
30
+
31
+ # Public: The styleguide section for which this comment block references.
32
+ #
33
+ # Returns the section reference String (ex: "2.1.8").
34
+ def section
35
+ return @section unless @section.nil?
36
+
37
+ comment_sections.each do |text|
38
+ if text =~ /Styleguide \d/i
39
+ cleaned = text.strip.sub!(/\.$/, '') # Kill trailing period
40
+ @section = cleaned.match(/Styleguide (.+)/)[1]
41
+ end
42
+ end
43
+
44
+ @section
45
+ end
46
+
47
+ # Public: The description section of a styleguide comment block.
48
+ #
49
+ # Returns the description String.
50
+ def description
51
+ comment_sections.first
52
+ end
53
+
54
+ # Public: The modifiers section of a styleguide comment block.
55
+ #
56
+ # Returns an Array of Modifiers.
57
+ def modifiers
58
+ last_indent = nil
59
+ modifiers = []
60
+ return modifiers unless comment_sections[1]
61
+
62
+ comment_sections[1].split("\n").each do |line|
63
+ next if line.strip.empty?
64
+ indent = line.scan(/^\s*/)[0].to_s.size
65
+
66
+ if last_indent && indent > last_indent
67
+ modifiers.last.description += line.squeeze(" ")
68
+ else
69
+ modifier, desc = line.split(" - ")
70
+ modifiers << Modifier.new(modifier.strip, desc.strip) if modifier && desc
71
+ end
72
+
73
+ last_indent = indent
74
+ end
75
+
76
+ modifiers
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Kss
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ /*
2
+ Your standard form button.
3
+
4
+ :hover - Highlights when hovering.
5
+ :disabled - Dims the button when disabled.
6
+ .primary - Indicates button is the primary action.
7
+ .smaller - A little bit smaller now.
8
+
9
+ Styleguide 2.1.1.
10
+ */
11
+ button {
12
+ padding: 5px 15px; }
13
+ button.primary, button.primary:hover {
14
+ color: #fff; }
15
+ button.smaller {
16
+ font-size: 11px; }
17
+ button:hover {
18
+ color: #337797; }
19
+ button:disabled {
20
+ opacity: 0.5; }
21
+
22
+
23
+ /*
24
+ A button suitable for giving stars to someone.
25
+
26
+ .star-given - A highlight indicating you've already given a star.
27
+ .disabled - Dims the button to indicate it cannot be used.
28
+
29
+ Styleguide 2.2.1.
30
+ */
31
+ a.button.star {
32
+ display: inline-block; }
33
+ a.button.star .star {
34
+ font-size: 10px; }
35
+ a.button.star.star-given {
36
+ color: #ae7e00; }
37
+ a.button.star.disabled {
38
+ opacity: 0.5; }
@@ -0,0 +1,47 @@
1
+ // Your standard form button.
2
+ //
3
+ // :hover - Highlights when hovering.
4
+ // :disabled - Dims the button when disabled.
5
+ // .primary - Indicates button is the primary action.
6
+ // .smaller - A little bit smaller now.
7
+ //
8
+ // Styleguide 2.1.1.
9
+ button{
10
+ padding:5px 15px;
11
+
12
+ &.primary, &.primary:hover{
13
+ color:#fff;
14
+ }
15
+
16
+ &.smaller{
17
+ font-size:11px;
18
+ }
19
+
20
+ &:hover{
21
+ color:#337797;
22
+ }
23
+
24
+ &:disabled{
25
+ opacity:0.5;
26
+ }
27
+ }
28
+
29
+ // A button suitable for giving stars to someone.
30
+ //
31
+ // .star-given - A highlight indicating you've already given a star.
32
+ // .disabled - Dims the button to indicate it cannot be used.
33
+ //
34
+ // Styleguide 2.2.1.
35
+ a.button.star{
36
+ display:inline-block;
37
+
38
+ .star{ font-size:10px; }
39
+
40
+ &.star-given{
41
+ color:#ae7e00;
42
+ }
43
+
44
+ &.disabled{
45
+ opacity:0.5;
46
+ }
47
+ }
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+
3
+ require 'kss'
4
+
5
+ module Kss
6
+ class Test < ::Test::Unit::TestCase
7
+ def self.test(name, &block)
8
+ define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
9
+ end
10
+
11
+ def default_test
12
+ end
13
+
14
+ def fixture(name)
15
+ @fixtures ||= {}
16
+ @fixtures[name] ||= File.read("test/fixtures/#{name}")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,58 @@
1
+ require 'test/helper'
2
+
3
+ class ParserTest < Kss::Test
4
+
5
+ def setup
6
+ @scss_parsed = Kss::Parser.new('test/fixtures/scss')
7
+ @css_parsed = Kss::Parser.new('test/fixtures/css')
8
+
9
+ @css_comment = <<comment
10
+ /*
11
+ A button suitable for giving stars to someone.
12
+
13
+ .star-given - A highlight indicating you've already given a star.
14
+ .disabled - Dims the button to indicate it cannot be used.
15
+
16
+ Styleguide 2.2.1.
17
+ */
18
+ comment
19
+
20
+ @starred_css_comment = <<comment
21
+ /* A button suitable for giving stars to someone.
22
+ *
23
+ * .star-given - A highlight indicating you've already given a star.
24
+ * .disabled - Dims the button to indicate it cannot be used.
25
+ *
26
+ * Styleguide 2.2.1. */
27
+ comment
28
+
29
+ @cleaned_css_comment = <<comment
30
+ A button suitable for giving stars to someone.
31
+
32
+ .star-given - A highlight indicating you've already given a star.
33
+ .disabled - Dims the button to indicate it cannot be used.
34
+
35
+ Styleguide 2.2.1.
36
+ comment
37
+ @cleaned_css_comment.rstrip!
38
+
39
+ end
40
+
41
+ test "parses KSS comments in SCSS" do
42
+ assert_equal 'Your standard form button.',
43
+ @scss_parsed.section('2.1.1').description
44
+ end
45
+
46
+ test "parses KSS comments in CSS" do
47
+ assert_equal 'Your standard form button.',
48
+ @css_parsed.section('2.1.1').description
49
+ end
50
+
51
+ test "cleans css comments" do
52
+ assert_equal @cleaned_css_comment,
53
+ Kss::Parser.clean_comments(@css_comment)
54
+ assert_equal @cleaned_css_comment,
55
+ Kss::Parser.clean_comments(@starred_css_comment)
56
+ end
57
+
58
+ end
@@ -0,0 +1,40 @@
1
+ require 'test/helper'
2
+
3
+ class SectionTest < Kss::Test
4
+
5
+ def setup
6
+ @comment_text = <<comment
7
+ Your standard form button.
8
+
9
+ :hover - Highlights when hovering.
10
+ :disabled - Dims the button when disabled.
11
+ .primary - Indicates button is the primary action.
12
+ .smaller - A smaller button
13
+
14
+ Styleguide 2.1.1.
15
+ comment
16
+
17
+ @section = Kss::Section.new(@comment_text, 'example.css')
18
+ end
19
+
20
+ test "parses the description" do
21
+ assert_equal "Your standard form button.", @section.description
22
+ end
23
+
24
+ test "parses the modifiers" do
25
+ assert_equal 4, @section.modifiers.size
26
+ end
27
+
28
+ test "parses a modifier's names" do
29
+ assert_equal ':hover', @section.modifiers.first.name
30
+ end
31
+
32
+ test "parses a modifier's description" do
33
+ assert_equal 'Highlights when hovering.', @section.modifiers.first.description
34
+ end
35
+
36
+ test "parses the styleguide reference" do
37
+ assert_equal '2.1.1', @section.section
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kss
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kyle Neath
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-02 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: sass
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 21
30
+ segments:
31
+ - 3
32
+ - 1
33
+ - 11
34
+ version: 3.1.11
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: " Inspired by TomDoc, KSS attempts to provide a methodology for writing\n maintainable, documented CSS within a team. Specifically, KSS is a CSS\n structure, documentation specification, and styleguide format.\n\n This is a ruby library for parsing KSS documented CSS and generating\n styleguides.\n"
38
+ email: kneath@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - LICENSE
49
+ - lib/kss/modifier.rb
50
+ - lib/kss/parser.rb
51
+ - lib/kss/section.rb
52
+ - lib/kss/version.rb
53
+ - lib/kss.rb
54
+ - test/fixtures/css/buttons.css
55
+ - test/fixtures/scss/buttons.scss
56
+ - test/helper.rb
57
+ - test/parser_test.rb
58
+ - test/section_test.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/kneath/kss
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.6.2
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: A library for parsing KSS documented stylesheets and generating styleguides
93
+ test_files: []
94
+