Allow string input for Kss::Parser #47

Merged
merged 4 commits into from Jul 1, 2013

Conversation

Projects
None yet
4 participants
Contributor

goodtouch commented Oct 31, 2012

Allowing string input (with Kss::Parser.load(string)) allows dynamic use cases in live web applications

Example:

buttons =<<-'EOS'
  /*
  Your standard form button.

  :hover    - Highlights when hovering.
  :disabled - Dims the button when disabled.

  Styleguide 1.1
  */
  button {
    padding: 5px 15px;
    line-height: normal;
    font-family: "Helvetica Neue", Helvetica;
    font-size: 12px;
    font-weight: bold;
    color: #666;
    text-shadow: 0 1px rgba(255, 255, 255, 0.9);
    border-radius: 3px;
    border: 1px solid #ddd;
    border-bottom-color: #bbb;
    background: #f5f5f5;
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='$start', endColorstr='$end');
    background: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#e5e5e5));
    background: -moz-linear-gradient(top, #f5f5f5, #e5e5e5);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
    cursor: pointer;
  }
  button:disabled {
    opacity: 0.5;
  }
EOS
@styleguide = Kss::Parser.load(buttons)

We can then imagine a web app for prototyping designs or html components that would provide live kss documentation & demo rendering without relying on static files.

Owner

kneath commented Nov 5, 2012

Love this idea, I'll take a look at the code and see if I can't cut a release here soon to include it.

@kneath kneath commented on an outdated diff Nov 5, 2012

lib/kss/comment_parser.rb
@@ -54,6 +54,24 @@ def self.parse_multi_line(line)
cleaned = cleaned.sub(/\*\//, '')
cleaned.rstrip
end
+
+ # Public: Initializes a new comment parser object from a string
+ # string - The String to parse.
+ # options - Optional options hash.
+ # :preserve_whitespace - Preserve the whitespace before/after comment
+ # markers (default:false).
+ #
+ def self.load(string, options={})
@kneath

kneath Nov 5, 2012

Owner

I wonder if we could have a different name for this. Maybe like from_string or something more descriptive? Load is very generic. I'm realizing now it's kind of a bummer I used new in the way I did — it'd be nice to pass a path or a string of CSS and get a result. We could also add some magic to new to determine newlines, etc etc but that strikes me as too much magic.

@kneath kneath commented on an outdated diff Nov 5, 2012

README.md
@@ -55,7 +55,38 @@ styleguide.section('2.1.1').modifiers.first.class_name
styleguide.section('2.1.1').modifiers.first.description
# => 'Subtle hover highlight'
+```
+
+Note that you can also initialize the `Kss::Parser` with a string CSS by using `Kss::Parser.load(string)`
@kneath

kneath Nov 5, 2012

Owner

I'd drop the "Note that" from this sentence.

@kneath kneath commented on an outdated diff Nov 5, 2012

lib/kss/comment_parser.rb
- if inside_single_line_block
- current_block += "\n#{parsed}"
- else
- current_block = parsed.to_s
- inside_single_line_block = true
- end
+ if !@file_path.nil?
+ File.open @file_path do |file|
+ parse_blocks_input(file)
+ end
+ else
+ parse_blocks_input(@string_input)
+ end
+ end
+
+ def parse_blocks_input(input)
@kneath

kneath Nov 5, 2012

Owner

Can we document this?

Contributor

goodtouch commented Nov 7, 2012

Glad to ear you like the idea 😄

I updated the pull request with the following :

  • You can now use Kss::Parser.new with path(s) or string(s)
    (I first used load to make it look like YAML.load(string). But using new instead makes sense too and doesn't clutter the code too much)
  • Updated the documentation & example
  • Added some doc about the parse_blocks method

I hope you'll like it !

Contributor

goodtouch commented Dec 19, 2012

Hi there !

Just wanted to know if you had time to review this one and if is ok for you ? 😉

Thanks

Owner

kneath commented Dec 19, 2012

Sorry about that, I haven't reviewed it yet. Failing a bit on being a maintainer. I'm gonna put it on my list to spend some quality time with KSS over the holidays.

Contributor

goodtouch commented Dec 20, 2012

Thanks for the feedback !

I'm building some cool stuff on top of this (that you might like ! But I'll come back to you later with this 🎄 🎁 ), but there is no rush here either.

I'm now feeling guilty to bother with my pull request and would rather know you enjoy some quality free time during the holidays ! 😉

Owner

kneath commented Dec 20, 2012

Don't feel guilty! I don't mind email, and KSS needs some love. Planning on
spending some time with it up in the mountains in between snowboarding for
the holidays.

On Thu, Dec 20, 2012 at 9:11 AM, Jean-Paul Bonnetouche <
notifications@github.com> wrote:

Thanks for the feedback !

I'm building some cool stuff on top of this (that you might like ! But
I'll come back to you later with this [image: 🎄][image:
🎁] ), but there is no rush here either.

I'm now feeling guilty to bother with my pull request and would rather
know you enjoy some quality free time during the holidays ! [image:
😉]


Reply to this email directly or view it on GitHubhttps://github.com/kneath/kss/pull/47#issuecomment-11581802.

Kyle Neath
kneath@gmail.com
http://warpspire.com

Collaborator

benschwarz commented Jun 17, 2013

@neonelectro — So we can close this issue now?

@benschwarz - I'm confused, I certainly haven't solved this issue. I'm just using a workaround in my own project.

Owner

kneath commented Jun 19, 2013

I'm still into this idea 👍 would love to see it get in. I think the references look a bit confusing here since it's a pull request in a fork (not from a fork).

Contributor

goodtouch commented Jun 19, 2013

Don't hesitate to give me some feedback if you want me to rework this in any way (like splitting the fix for the infinite js loop) 😄

I've been using it for (quite) a long time now and it worked pretty well for my projects.
It seems like @neonelectro used it successfully too ?

Anyone else tried it ?

( @kneath: I hope you had fun snowboarding btw 😉 )

Collaborator

benschwarz commented Jun 19, 2013

@goodtouch It sounds to me like everyone is happy with this idea && the implementation looks pretty good.
It'd be great if you could remove the js changes that you made (or rebase again from master perhaps).

Then we'll get a nice clean merge :-)

Yeah, I merged it into my own fork and it worked very well.

goodtouch added some commits Oct 31, 2012

@goodtouch @goodtouch goodtouch Allow string input for Kss::Parser
Allowing string input (with `Kss::Parser.new(string)`) allows dynamic use cases in live web applications

Example:

```ruby
buttons =<<-'EOS'
  /*
  Your standard form button.

  :hover    - Highlights when hovering.
  :disabled - Dims the button when disabled.

  Styleguide 1.1
  */
  button {
    padding: 5px 15px;
    line-height: normal;
    font-family: "Helvetica Neue", Helvetica;
    font-size: 12px;
    font-weight: bold;
    color: #666;
    text-shadow: 0 1px rgba(255, 255, 255, 0.9);
    border-radius: 3px;
    border: 1px solid #ddd;
    border-bottom-color: #bbb;
    background: #f5f5f5;
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='$start', endColorstr='$end');
    background: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#e5e5e5));
    background: -moz-linear-gradient(top, #f5f5f5, #e5e5e5);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
    cursor: pointer;
  }
  button:disabled {
    opacity: 0.5;
  }
EOS
@styleguide = Kss::Parser.new(buttons)
```

We can then imagine a web app for prototyping designs or html components that would provide live kss documentation & demo rendering without relying on static files.
73cdd12
@goodtouch @goodtouch goodtouch Add string use case in the example app 3db1c85
@goodtouch @goodtouch goodtouch Add string use case in the README.md 7eb1cbe
@goodtouch @goodtouch goodtouch Document the CommentParser#parse_blocks method c912899
Contributor

goodtouch commented Jun 20, 2013

Here is the rebased version without the js fix

@benschwarz benschwarz merged commit c912899 into kneath:master Jul 1, 2013

1 check passed

default The Travis CI build passed
Details
Collaborator

benschwarz commented Jul 1, 2013

Merged. You rule!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment