Dear list, I have and \xmlraw command that gives some text with percent signs. ConTeXt parses them as comments (so no output). Using Lua gsub(), I need to replace something like: string.gsub([[\xmlraw{#1}{.}]], "%", "\\letterpercent") How do I need to invoke % to get the character found? Many thanks for your help, Pablo -- http://www.ousia.tk
Hi,
On 27 Aug 2020, at 17:11, Pablo Rodriguez
wrote: Dear list,
I have and \xmlraw command that gives some text with percent signs. ConTeXt parses them as comments (so no output).
Using Lua gsub(), I need to replace something like:
string.gsub([[\xmlraw{#1}{.}]], "%", "\\letterpercent")
Double it: %% See also https://www.lua.org/manual/5.3/manual.html#6.4.1 , but the mention of %% is a bit hidden. Best wishes, Taco
On 8/27/20 5:30 PM, Taco Hoekwater wrote:
[...] Using Lua gsub(), I need to replace something like:
string.gsub([[\xmlraw{#1}{.}]], "%", "\\letterpercent")
Double it: %%
See also https://www.lua.org/manual/5.3/manual.html#6.4.1 , but the mention of %% is a bit hidden.
Many thanks for your replies, Taco and Hans. I’m afraid this doesn’t work (and I need \xmlraw there): \startbuffer[demo] <html> <body> <div id="First"> <p>The <span class="special">% best</span> paragraph.</p> </div> </body> </html> \stopbuffer \startxmlsetups xml:initialize \xmlsetsetup{#1}{html}{xml:gen} \stopxmlsetups \xmlregistersetup{xml:initialize} \startxmlsetups xml:gen \startitemize \xmlfilter{#1}{/**/span[@class='special']/command(xml:special)} \stopitemize \stopxmlsetups \startxmlsetups xml:special \startitem \cldcontext{string.gsub([[\xmlraw{#1}{.}]], "%%", "\\letterpercent")} \stopitem \stopxmlsetups \starttext \xmlprocessbuffer{main}{demo}{} \stoptext What am I missing or doing wrong there? Many thanks for your help, Pablo -- http://www.ousia.tk
Pablo Rodriguez schrieb am 27.08.2020 um 17:56:
On 8/27/20 5:30 PM, Taco Hoekwater wrote:
[...] Using Lua gsub(), I need to replace something like:
string.gsub([[\xmlraw{#1}{.}]], "%", "\\letterpercent")
Double it: %%
See also https://www.lua.org/manual/5.3/manual.html#6.4.1 , but the mention of %% is a bit hidden.
Many thanks for your replies, Taco and Hans.
I’m afraid this doesn’t work (and I need \xmlraw there):
\startbuffer[demo] <html> <body> <div id="First"> <p>The <span class="special">% best</span> paragraph.</p> </div> </body> </html> \stopbuffer
\startxmlsetups xml:initialize \xmlsetsetup{#1}{html}{xml:gen} \stopxmlsetups
\xmlregistersetup{xml:initialize}
\startxmlsetups xml:gen \startitemize \xmlfilter{#1}{/**/span[@class='special']/command(xml:special)} \stopitemize \stopxmlsetups
\startxmlsetups xml:special \startitem \cldcontext{string.gsub([[\xmlraw{#1}{.}]], "%%", "\\letterpercent")} \stopitem \stopxmlsetups
This works fine with your example \startxmlsetups xml:special \startitem \xmlflush{#1} \stopitem \stopxmlsetups and to escape special characters you can use context.escape() \startxmlsetups xml:special \startitem \cldcontext{context.escape([[\xmlraw{#1}{.}]])} \stopitem \stopxmlsetups Wolfgang
On 8/27/20 6:16 PM, Wolfgang Schuster wrote:
Pablo Rodriguez schrieb am 27.08.2020 um 17:56:
[...] \startxmlsetups xml:special \startitem \cldcontext{string.gsub([[\xmlraw{#1}{.}]], "%%", "\\letterpercent")} \stopitem \stopxmlsetups [...] and to escape special characters you can use context.escape()
Many thanks for your reply, Wolfgang. I’m afraid that I need to keep $ in order to get some formulas: \cldcontext{(string.gsub([[\xmlraw{#1}{.}]], '<span class="'..'math inline'..'">', '$'):gsub("</span>", "\\ifmmode $\\fi"))}} Actually this is way more complex, but I need to catch % and have to substitution above. Many thanks for your help, Pablo -- http://www.ousia.tk
Pablo Rodriguez schrieb am 27.08.2020 um 19:16:
On 8/27/20 6:16 PM, Wolfgang Schuster wrote:
Pablo Rodriguez schrieb am 27.08.2020 um 17:56:
[...] \startxmlsetups xml:special \startitem \cldcontext{string.gsub([[\xmlraw{#1}{.}]], "%%", "\\letterpercent")} \stopitem \stopxmlsetups [...] and to escape special characters you can use context.escape() Many thanks for your reply, Wolfgang.
I’m afraid that I need to keep $ in order to get some formulas:
\cldcontext{(string.gsub([[\xmlraw{#1}{.}]], '<span class="'..'math inline'..'">', '$'):gsub("</span>", "\\ifmmode $\\fi"))}}
Actually this is way more complex, but I need to catch % and have to substitution above.
You have to replace %% with \letterpercent\letterpercent because you're still playing with TeX rules. \startxmlsetups xml:special \startitem \cldcontext{string.gsub("\xmlraw{#1}{.}","\letterpercent\letterpercent","\\letterpercent{}")} \stopitem \stopxmlsetups To avoid these limitations create a new Lua function and call only this function in the setup: \startluacode moduledata = moduledata or { } function moduledata.special(str) return string.gsub(str,"%%","\\letterpercent{}") end \stopluacode \startxmlsetups xml:special \startitem \cldcontext{moduledata.special([[\xmlraw{#1}{.}]])} \stopitem \stopxmlsetups Wolfgang
On 8/27/20 7:35 PM, Wolfgang Schuster wrote:
[...] You have to replace %% with \letterpercent\letterpercent because you're still playing with TeX rules.
\startxmlsetups xml:special \startitem \cldcontext{string.gsub("\xmlraw{#1}{.}","\letterpercent\letterpercent","\\letterpercent{}")} \stopitem \stopxmlsetups
Many thanks for your reply, Wolfgang. Now I understand what was going on.
To avoid these limitations create a new Lua function and call only this function in the setup:
\startluacode
moduledata = moduledata or { }
function moduledata.special(str) return string.gsub(str,"%%","\\letterpercent{}") end
\stopluacode
\startxmlsetups xml:special \startitem \cldcontext{moduledata.special([[\xmlraw{#1}{.}]])} \stopitem \stopxmlsetups
Now I see that I have a lot to learn. Many thanks again for your help, Pablo -- http://www.ousia.tk
In Lua patterns % is a socalled magic character like ^$ etc. These are esacped with a %. Thus %% is what you need in the first string. dr. Hans van der Meer
On 27 Aug 2020, at 17:11, Pablo Rodriguez
wrote: Dear list,
I have and \xmlraw command that gives some text with percent signs. ConTeXt parses them as comments (so no output).
Using Lua gsub(), I need to replace something like:
string.gsub([[\xmlraw{#1}{.}]], "%", "\\letterpercent")
How do I need to invoke % to get the character found?
Many thanks for your help,
Pablo -- http://www.ousia.tk ___________________________________________________________________________________ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
participants (4)
-
Hans van der Meer
-
Pablo Rodriguez
-
Taco Hoekwater
-
Wolfgang Schuster