[NTG-context] XML expression to read HTML classes

Hans Hagen pragma at wxs.nl
Thu Aug 27 22:57:04 CEST 2015


On 8/25/2015 5:05 PM, massifr at fastwebnet.it wrote:
> Hello list,
> I'm working on HTML typesetting with ConTeXt.
> I wrote a custom expression to test whether an element has a class:
>
> function xml.functions.classes(classAttr)
>    local classes = {}
>    if classAttr then
>      for c in string.gmatch(classAttr, "%S+") do
>        if (string.len(c) > 0) then classes[c] = true end
>      end
>    end
>    return classes
> end
>
> function xml.expressions.hasClass(classAttr, className)
>    if classAttr and className then
>      return xml.functions.classes(classAttr)[className] ~= nil
>    end
>    return false
> end
>
> This way I can write this in my xmlsetup:
> \xmlsetsetup{#1}{p[hasClass(@class,'myclass1')]}{html:p:myclass1}
>
> That is better than
> - p[@class='myclass1']   (it fails when you have more classes)
> - p[contains(@class, 'myclass1')]    (it gives you false positives when you have class="myclass12")
>
> That works, but I would like to write:
> \xmlsetsetup{#1}{p[hasClass('myclass1')]}{html:p:myclass1}
>
> omitting the @class argument, because it's obvious.
> To omit it I need to access the current collected element.
>
> At page 25 of "Dealing with XML in ConTeXt MkIV" it is said
> that the predefined variables "list","l","ll" (I think I need that one)
> and "order" are available, but I did not manage to access them.
>
> How can I get the current element in Lua to write
> local classAttr = xml.attribute(currentElement, "", "class")   ?

function xml.functions.classes(e)
     local class = e.at.class
     if class then
         local classes = { }
         for c in string.gmatch(class,"%S+") do
             classes[c] = true
         end
         return classes
     else
         return { }
     end
end



-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list