[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby CSS parser

Kevin

2/6/2007 12:40:00 AM

Anyone know of a decent ruby CSS parser?

_Kevin

7 Answers

James Gray

2/6/2007 1:57:00 AM

0

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


Keith Fahlgren

2/6/2007 3:55:00 PM

0

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-mzscheme-and-a-portable-packrat-parsing-combinat...

Keith

James Gray

2/6/2007 4:05:00 PM

0

On Feb 6, 2007, at 9:55 AM, Keith Fahlgren wrote:

> 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!

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-mzsch...
> portable-packrat-parsing-combinator-library

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

James Edward Gray II

James Gray

2/6/2007 4:28:00 PM

0

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-mzsch...
>> 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


khaines

2/6/2007 7:36:00 PM

0

Andrew Stewart

2/7/2007 2:48:00 PM

0



>> 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.

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/cssdryer-dry-u...

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://airbladeso...

"Software that doesn't make you cry..."
---------------------------------------



David Johnston

5/14/2012 2:02:00 PM

0

On 5/14/2012 7:04 AM, shawn wrote:
> On Mon, 14 May 2012 06:59:02 -0600, David Johnston<David@block.net>
> wrote:
>
>> On 5/14/2012 6:44 AM, David wrote:
>>> what, if anything, XIII knows. Season one of XIII features 13 episodes
>>> with new episodes premiering Fridays at 9pm ET and at 9pm PT.
>>
>> And by "new" we mean "reruns of a series that already finished in Canada".
>
> Hmm, yet another Canadian series that I had never heard of until it
> aired down here. Does that mean it just didn't do well enough in
> Canada for our resident Canadians to think it worthy of discussing?

I mentioned it a few times on What Did You Watch, but there's no point
in trying to start a thread of it's own for a Canadian show. Nobody
ever responds.