* XML expression to read HTML classes
@ 2015-08-25 15:05 massifr
2015-08-26 13:16 ` massifr
2015-08-27 20:57 ` Hans Hagen
0 siblings, 2 replies; 4+ messages in thread
From: massifr @ 2015-08-25 15:05 UTC (permalink / raw)
To: ntg-context
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") ?
Thanks in advance,
Massi
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: XML expression to read HTML classes
2015-08-25 15:05 XML expression to read HTML classes massifr
@ 2015-08-26 13:16 ` massifr
2015-08-27 20:57 ` Hans Hagen
1 sibling, 0 replies; 4+ messages in thread
From: massifr @ 2015-08-26 13:16 UTC (permalink / raw)
To: ntg-context
A working example to clarify my request:
\startluacode
-- splits the attribute class using spaces as classes' separator
-- returns an array of strings
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
\stopluacode
\startbuffer[test]
<text>
<p>Generic paragraph (no class).</p>
<p class="myClass1">Emphasized paragraph (<tt>class="myClass1"</tt>).</p>
<p class="otherClass myClass1">Another emphasized paragraph
(but <tt>myClass1</tt> is not the only class specified).
</p>
<p class="myClass12">Another not emphasized paragraph
(<tt>class="myClass12"</tt>) showing why <br/>
<tt>p[hasClass(@class, "myClass1")]</tt><br/>
is better than <br/>
<tt>p[@class="myClass1"]</tt><br/>
or <br/>
<tt>p[contains(@class, "myClass1")]</tt>.
</p>
<p><tt>p[hasClass("myClass1")]</tt> would be better
since it's obvious that the classes are specified
in the <tt>class</tt> attribute, but this means that
the lua expression needs to access the current selected element
(<tt>ll</tt>?).
</p>
</text>
\stopbuffer
\startxmlsetups xml:somesetups
\xmlsetsetup{#1}{text}{xml:text}
\xmlsetsetup{#1}{p}{xml:p}
\xmlsetsetup{#1}{p[hasClass(@class, 'myClass1')]}{xml:p:myClass1}
\xmlsetsetup{#1}{tt}{xml:tt}
\xmlsetsetup{#1}{br}{xml:br}
\stopxmlsetups
\xmlregistersetup{xml:somesetups}
\startxmlsetups xml:text
\xmlflush{#1}
\stopxmlsetups
\startxmlsetups xml:p
\xmlflush{#1}\par\blank[line]
\stopxmlsetups
\startxmlsetups xml:p:myClass1
{\em\xmlflush{#1}}\par\blank[line]
\stopxmlsetups
\startxmlsetups xml:br
\\
\stopxmlsetups
\startxmlsetups xml:tt
\dontleavehmode{\tt\xmlflush{#1}}
\stopxmlsetups
\starttext
\xmlprocessbuffer{main}{test}{}
\stoptext
Thanks in advance for any help,
Massi
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: XML expression to read HTML classes
2015-08-25 15:05 XML expression to read HTML classes massifr
2015-08-26 13:16 ` massifr
@ 2015-08-27 20:57 ` Hans Hagen
2015-08-27 21:16 ` Hans Hagen
1 sibling, 1 reply; 4+ messages in thread
From: Hans Hagen @ 2015-08-27 20:57 UTC (permalink / raw)
To: ntg-context
On 8/25/2015 5:05 PM, massifr@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
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: XML expression to read HTML classes
2015-08-27 20:57 ` Hans Hagen
@ 2015-08-27 21:16 ` Hans Hagen
0 siblings, 0 replies; 4+ messages in thread
From: Hans Hagen @ 2015-08-27 21:16 UTC (permalink / raw)
To: ntg-context
On 8/27/2015 10:57 PM, Hans Hagen wrote:
> On 8/25/2015 5:05 PM, massifr@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
alternative:
function xml.functions.hasclass(e,c,class)
class = class and e.at[class] or e.at.class
if class and class ~= "" then
if class == c then
return true
else
for s in string.gmatch(class,"%S+") do
if s == c then
return true
end
end
end
end
return false
end
.. hasclass("foo")
--
-----------------------------------------------------------------
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
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-08-27 21:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-25 15:05 XML expression to read HTML classes massifr
2015-08-26 13:16 ` massifr
2015-08-27 20:57 ` Hans Hagen
2015-08-27 21:16 ` Hans Hagen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).