lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Dec 26, 2010 8:55 PM, "임창진(callin)" <callin2@gmail.com> wrote:
>
> Hi all lua lovers!
>
> few weeks ago I need lib which behave like jQuery over lua table.
> I search it but cannot find. then I coded it for me.
> And I guess it may useful.
> below is sample.
> It uses Lpeg(0.9)
> anybody can use/improve it freely. feedback is welcome.
> --------------------------------
>
> require "lquery"
> require "base" -- this is optional for table pretty print
>
> -- table for test
> local root = {tag='a',
>        {tag='c', 1,2,3},
>        {tag='a',b='xxx',
>                {tag='c',id='qq',age=33, class='a b c'},
>                {tag='c',id='qq123',age=34, class='a b'},
>        },
> }
>
> local lqroot = lquery.new(root)
> assert( lqroot:get(1) == root , 'it must be equal')
>
> --[[
>        '='     :       equal
>        '^='    :       startwith
>        '$='    :       endWith
>        '~='    :       contains
>
>
>
>        '/a/c'          ===> '/[tag="a"]/[tag="c"]'
>        'c'             ===> '*[tag="c"]'
>        '#xxx'          ===> '*[id="xxx"]'
>        '.xxx'          ===> '*[class~="xxx"]'
>
> ]]
>
>
> local fr = lqroot:find('c')
>
> for t in fr:each() do
>        print('t1',t)
> end
>
> print( fr:get(3))  --  {age=34,name=qq123,tag=c}
>
> fr = lqroot:find('/a/c')
>
> for t in fr:each() do
>        print('t2',t)
> end
>
> fr = lqroot:find('/[tag="a"]/[tag="c"]')
>
> for t in fr:each() do
>        print('t3',t)
> end
>
> fr = lqroot:find('/a/a c')
>
> for t in fr:each() do
>        print('t4',t)
> end
>
> fr = lqroot:find('#qq')
>
> for t in fr:each() do
>        print('t5',t)
> end
>
> for t in lqroot:find('.b'):each() do
>        print('t6',t)
> end
>
> fr = lqroot:find('.c')
>
> for t in fr:each() do
>        print('t7',t)
> end
>
>
> -----------------------------------
>
>
> --
> 임창진(rim.chang.jin)
Awesome, will have to give this a try sometime.  I recommend that you try putting this on the web somewhere, such as Github and luaforge, so that changes can be tracked and a better authoritative src/name can be preserved. It would also be important to apply a license so it can reasonably be used in real projects.  I recommend MIT and/or public domain.