Forum: Ruby ruby CSS parser

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5f23b06d22b84ec6c902bf390071a79c?d=identicon&s=25 _Kevin (Guest)
on 2007-02-06 01:45
(Received via mailing list)
Anyone know of a decent ruby CSS parser?

_Kevin
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-02-06 02:57
(Received via mailing list)
On Feb 5, 2007, at 6:45 PM, _Kevin wrote:

> Anyone know of a decent ruby CSS parser?

I'm writing a packrat parser library and this would make a fun
example.  I've already built a JSON parser with it, so I'm pretty
sure it's up to the challenge.  I'm currently wrapping the core in a
DSL to make it easier to use, but it's useable.  I'll talk you
through it if you want to work on this.

Of course, I realize that's not what you asked for.  ;)

James Edward Gray II
B7d00e6bd04c81d1fd78554a3d0c38b7?d=identicon&s=25 Keith Fahlgren (Guest)
on 2007-02-06 16:55
(Received via mailing list)
On 2/5/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Feb 5, 2007, at 6:45 PM, _Kevin wrote:
>
> > Anyone know of a decent ruby CSS parser?
>
> I'm writing a packrat parser library and this would make a fun
> example.  I've already built a JSON parser with it, so I'm pretty
> sure it's up to the challenge.  I'm currently wrapping the core in a
> DSL to make it easier to use,

Oooh, packrat parsing! I assume you've seen this stuff:
http://www.lshift.net/blog/2005/08/22/json-for-mzs...

Keith
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-02-06 17:06
(Received via mailing list)
On Feb 6, 2007, at 9:55 AM, Keith Fahlgren wrote:

> Oooh, packrat parsing!
Yeah, I'm having fun playing with it in my spare time.  The project
is Ghost Wheel on RubyForge, if you want to take a peek.  I'll make a
release as soon as I get the Ruby DSL wrapper finished.

> I assume you've seen this stuff:
> http://www.lshift.net/blog/2005/08/22/json-for-mzs...
> portable-packrat-parsing-combinator-library

Interesting.  I hadn't seen that before now.  Thanks for sharing.

James Edward Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-02-06 17:28
(Received via mailing list)
On Feb 6, 2007, at 10:05 AM, James Edward Gray II wrote:

> On Feb 6, 2007, at 9:55 AM, Keith Fahlgren wrote:
>
>> I assume you've seen this stuff:
>> http://www.lshift.net/blog/2005/08/22/json-for-mzs...
>> portable-packrat-parsing-combinator-library
>
> Interesting.  I hadn't seen that before now.  Thanks for sharing.

Looking into this a little further, I find this oddity in the parser:

(define (skip-comment-char results)
   (comment-body (parse-results-next results)))

I'm not familiar with scheme, so I don't know exactly what's going on
here, but the JSON language does not have comments, so this is a
little odd.

For the curious, the current DSL goal in Ghost Wheel is:

JSON_PARSER = GhostWheel::build_parser do
   rule( :keyword,
         alt("true", "false", "null") { |k|
           {"true" => true, "false" => false, "null" => nil}[k]
         } )

   rule( :number,
         /-?(?:0|[1-9]\d*)(?:\.\d+(?:[eE][+-]?\d+)?)?/ ) { |n|
     n.include?(".") ? Float(n) : Integer(n)
   }

   rule( :string_content,
         alt(
           lit(%r{\\["\\/]}) { |e| e[-1, 1] },
           lit(/\\[bfnrt]/) { |e|
             Hash[*%W[b \n f \f n \n r \r t \t]][e[-1, 1]]
           },
           lit(/\\u[0-9a-fA-F]{4}/) { |e| [Integer("0x#{e
[2..-1]}")].pack("U") },
           /[^\\"]+/
         ) )
   rule( :string,
         seq(
           skip('"'),
           zplus(:string_content),
           skip('"')
         ) { |s| s.flatten.join } )

   rule( :array,
         seq(
           skip(/\[\s*/),
           alt(
             seq(
               oplus(seq(:value, skip(/\s*,\s*/))),
               :value
             ) { |a| a[0].inject([]) { |vs, v| vs.push(*v) } + a
[-1..-1] },
             seq(opt(:value))
           ),
           skip(/\s*\]/)
         ) { |a| a.first } )

   rule(:object_pair, seq(:string, /\s*:\s*/, :value) { |kv| {kv[0]
=> kv[-1]} })
   rule( :object,
         seq(
           skip(/\{\s*/),
           alt(
             seq(
               oplus(seq(:object_pair, skip(/\s*;\s*/))),
               :object_pair
             ) { |ps| ps[0][0] + ps[-1..-1] },
             seq(opt(:object_pair))
           ),
           skip(/\}\s*/)
         ) { |ps| ps[0].inject({}) { |h, p| h.merge(p) } } )

   rule(:value_space, opt(skip(/\s+/)))
   rule( :value,
         seq(
           :value_space,
           alt(:object, :array, :string, :number, :keyword),
           :value_space
         ) { |v| v[0] } )

   parser(:json, seq(:value, eof) { |j| j[0] })
end

This is a good bit more compact than the Scheme equivalent at the
above link.

James Edward Gray II
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 unknown (Guest)
on 2007-02-06 20:37
(Received via mailing list)
On Tue, 6 Feb 2007, _Kevin wrote:

> Anyone know of a decent ruby CSS parser?

This is the opposite of what you are looking for, but with the upcoming
IOWA 1.0 release I have a Ruby CSS DSL -- write CSS with Ruby using a
syntax that's about as close as I could get it to actual CSS.  It
supports
everything Ruby does, so variables work as does nesting of selectors
into
other selectors, and storing a selector into a variable that can be
refered to later.  It also provides a smart caching feature so that if,
for instance, one customizes the generated CSS according to a cookie
setting, it can cache that generated CSS and reuse it without having to
execute all of the DSL code for every request.

The goals are to provide much DRYer CSS using Ruby that looks a lot like
CSS, with easy support for dynamic CSS generation, all covered under the
syntax checking of the Ruby parser (which should help one catch simple
errors).  And because it's all Ruby code, it's easy to write tests to
validate dynamically generated CSS.

Here's a quick preview:

Media.screen {
   body {
     font {
       color 'green'
       size 12.px
     }
   }
}

If one wants to refer to something later, one may:

Media.screen {
   body {
     base_font = font {
       color :green
       size 12.px
     }

     footer {
       font {
         size base_font - 2
         color :black
       }
     }
   }
}

All CSS1, CSS2, and CSS3 selectors are supported.


Kirk Haines
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2007-02-07 02:23
(Received via mailing list)
From: khaines@enigo.com [mailto:khaines@enigo.com] :
# IOWA 1.0 release I have a Ruby CSS DSL -- write CSS with Ruby using a
# syntax that's about as close as I could get it to actual CSS.

this is uver cool. is it possible to plug it in to other non*-iowa
frameworks like nitro and rails?
waiting for ruby css...
kind regards -botp
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-02-07 04:29
(Received via mailing list)
On 2/6/07, Peña, Botp <botp@delmonte-phil.com> wrote:
> From: khaines@enigo.com [mailto:khaines@enigo.com] :
> # IOWA 1.0 release I have a Ruby CSS DSL -- write CSS with Ruby using a
> # syntax that's about as close as I could get it to actual CSS.
>
> this is uver cool. is it possible to plug it in to other non*-iowa frameworks like nitro 
and rails?
> waiting for ruby css...

+1, I'd like to see something like this for camping.
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 unknown (Guest)
on 2007-02-07 08:18
(Received via mailing list)
On Wed, 7 Feb 2007, [iso-8859-1] Peña, Botp wrote:

> From: khaines@enigo.com [mailto:khaines@enigo.com] :
> # IOWA 1.0 release I have a Ruby CSS DSL -- write CSS with Ruby using a
> # syntax that's about as close as I could get it to actual CSS.
>
> this is uver cool. is it possible to plug it in to other non*-iowa frameworks like nitro 
and rails?
> waiting for ruby css...

Yes.  It should be possible to use it in Nitro or Rails with no
problems.


Kirk Haines
72ee7b478a235f646c3495f03ebbe676?d=identicon&s=25 Andrew Stewart (Guest)
on 2007-02-07 15:48
(Received via mailing list)
> cache that generated CSS and reuse it without having to execute all
> of the DSL code for every request.
>
> The goals are to provide much DRYer CSS using Ruby that looks a lot
> like CSS, with easy support for dynamic CSS generation, all covered
> under the syntax checking of the Ruby parser (which should help one
> catch simple errors).  And because it's all Ruby code, it's easy to
> write tests to validate dynamically generated CSS.

This sounds similar to my css_dryer plugin for Rails which allows one
to nest selectors and use variables:

http://blog.airbladesoftware.com/2006/12/11/cssdry...

In the code I have to parse DRY CSS stylesheets as input.  At the
time I thought I could either build a proper, robust parser or get by
with regular expressions and a home-made state machine.  Not having
any experience building parsers, I went with the latter approach.  It
works well but would I think be tricky to extend!

Regards,
Andy Stewart


---------------------------------------
AirBlade Software Ltd
http://airbladesoftware.com

"Software that doesn't make you cry..."
This topic is locked and can not be replied to.