Re: [NTG-context] changing an XML node and reprocess it (or xmlflushing a string)
On 10/8/2019 11:17 AM, mf wrote: Solved with a dirty hack: \startbuffer[text] A paragraph with a fraction made of text with styles: a fraction made of text/with nested styles inside. \stopbuffer \startluacode local gsub = string.gsub local sfind = string.find local xmlflush = lxml.flush local startTextFraction = "$\\frac{\\text{" local stopTextFraction = "}}$" function xml.functions.textfraction( t ) local dt = t.dt inspect( dt ) local wrapper local slashfound = false context( startTextFraction ) local index, child ^^ Not needed, they are local to the for loop for index, child in ipairs( dt ) do if type( child ) == "string" then if not slashfound and sfind( child, "/" ) then context( gsub( child, "/", "}}{\\text{" ) ) slashfound = true else context( child ) end elseif type( child ) == "table" then wrapper = { __parent__ = child.__parent__, at = {}, dt = child } child.__parent__ = wrapper xmlflush( wrapper ) end end context( stopTextFraction ) end \stopluacode or, to get rid of the wrapper: \startluacode local gsub, find = string.gsub, string.find local xmlprint = xml.sprint local context = context local startTextFraction = "$\\frac{\\text{" local stopTextFraction = "}}$" function xml.functions.textfraction(t) context(startTextFraction) local slashfound = false local elements = t.dt for i=1,#elements do local child = elements[i] if type(child) == "table" then -- lxml.all(child,".") xmlprint(child,t) elseif not slashfound and find(child,"/") then context((gsub(child,"/","}}{\\text{"))) slashfound = true else context(child) end end context(stopTextFraction) end \stopluacode i'll add the example to the "todo" for a "very dirty tricks" chapter some day \startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{p|i|b|red}{xml:*} \xmlsetsetup{#1}{{span.fraction}}{xml:fraction} \stopxmlsetups \xmlregistersetup{xml:textsetups} \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \startxmlsetups xml:i {\it \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:red {\red \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:b {\bf \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:fraction \xmlfunction{#1}{textfraction} \stopxmlsetups \starttext \xmlprocessbuffer{main}{text}{} \stoptext Il 07/10/19 18:26, mf ha scritto: Hello, i'm using XML and i find useful specifying a fraction made of text this way: text for numerator/text for denominator With some lua, i can transform it into \frac{\text{text for numerator}}{\text{text for denominator}} which typesets something like this: text for numerator text for denominator Suppose you want to add some styling to the texts, like this: text for numerator/text for denominator You should split the span node into two elements and the xmlflush them. This is my M(not)WE: \startbuffer[text] A paragraph with a fraction made of text with styles: a fraction made of text/with styles inside. \stopbuffer \startluacode local sub = string.sub local sfind = string.find local xmltext = xml.text local xmlconvert = xml.convert local function numeratorDenominator( text ) local before, after = sfind( text, "[^<]/[^>]" ) local num, den if before and after then num = sub( text, 1, before ) den = sub( text, after ) end return num, den end function xml.functions.textfraction( t ) local text = xmltext( t, '' ) local num, den = numeratorDenominator( text ) if num and den then local fontstyle = tokens.getters.macro( "fontstyle" ) local xml_num = xmlconvert( num ) local xml_den = xmlconvert( den ) -- context( "$\\frac{\\text{\\" ..fontstyle .. " " .. num .. "}}{\\text{\\" ..fontstyle .. " " .. den .. "}}$" ) context( "$\\frac{\\text{\\" ..fontstyle .. " " ) context( num ) -- context.xmlprocessstring( xml_num ) context( "}}{\\text{\\" ..fontstyle .. " " ) context( den ) -- context.xmlprocessstring( xml_den ) context( "}}$" ) else context.xmlflush( t ) end end \stopluacode \startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{p|i|red}{xml:*} \xmlsetsetup{#1}{{span.fraction}}{xml:fraction} \stopxmlsetups \xmlregistersetup{xml:textsetups} \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \startxmlsetups xml:i {\it \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:red {\red \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:fraction \xmlfunction{#1}{textfraction} \stopxmlsetups \starttext \xmlprocessbuffer{main}{text}{} \stoptext Massi ___ If your question is of interest to others as well, please add an entry to the
Re: [NTG-context] changing an XML node and reprocess it (or xmlflushing a string)
Solved with a dirty hack: \startbuffer[text] A paragraph with a fraction made of text with styles: a fraction made of text/with nested styles inside. \stopbuffer \startluacode local gsub = string.gsub local sfind = string.find local xmlflush = lxml.flush local startTextFraction = "$\\frac{\\text{" local stopTextFraction = "}}$" function xml.functions.textfraction( t ) local dt = t.dt inspect( dt ) local wrapper local slashfound = false context( startTextFraction ) local index, child for index, child in ipairs( dt ) do if type( child ) == "string" then if not slashfound and sfind( child, "/" ) then context( gsub( child, "/", "}}{\\text{" ) ) slashfound = true else context( child ) end elseif type( child ) == "table" then wrapper = { __parent__ = child.__parent__, at = {}, dt = child } child.__parent__ = wrapper xmlflush( wrapper ) end end context( stopTextFraction ) end \stopluacode \startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{p|i|b|red}{xml:*} \xmlsetsetup{#1}{{span.fraction}}{xml:fraction} \stopxmlsetups \xmlregistersetup{xml:textsetups} \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \startxmlsetups xml:i {\it \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:red {\red \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:b {\bf \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:fraction \xmlfunction{#1}{textfraction} \stopxmlsetups \starttext \xmlprocessbuffer{main}{text}{} \stoptext Il 07/10/19 18:26, mf ha scritto: Hello, i'm using XML and i find useful specifying a fraction made of text this way: text for numerator/text for denominator With some lua, i can transform it into \frac{\text{text for numerator}}{\text{text for denominator}} which typesets something like this: text for numerator text for denominator Suppose you want to add some styling to the texts, like this: text for numerator/text for denominator You should split the span node into two elements and the xmlflush them. This is my M(not)WE: \startbuffer[text] A paragraph with a fraction made of text with styles: a fraction made of text/with styles inside. \stopbuffer \startluacode local sub = string.sub local sfind = string.find local xmltext = xml.text local xmlconvert = xml.convert local function numeratorDenominator( text ) local before, after = sfind( text, "[^<]/[^>]" ) local num, den if before and after then num = sub( text, 1, before ) den = sub( text, after ) end return num, den end function xml.functions.textfraction( t ) local text = xmltext( t, '' ) local num, den = numeratorDenominator( text ) if num and den then local fontstyle = tokens.getters.macro( "fontstyle" ) local xml_num = xmlconvert( num ) local xml_den = xmlconvert( den ) -- context( "$\\frac{\\text{\\" ..fontstyle .. " " .. num .. "}}{\\text{\\" ..fontstyle .. " " .. den .. "}}$" ) context( "$\\frac{\\text{\\" ..fontstyle .. " " ) context( num ) -- context.xmlprocessstring( xml_num ) context( "}}{\\text{\\" ..fontstyle .. " " ) context( den ) -- context.xmlprocessstring( xml_den ) context( "}}$" ) else context.xmlflush( t ) end end \stopluacode \startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{p|i|red}{xml:*} \xmlsetsetup{#1}{{span.fraction}}{xml:fraction} \stopxmlsetups \xmlregistersetup{xml:textsetups} \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \startxmlsetups xml:i {\it \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:red {\red \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:fraction \xmlfunction{#1}{textfraction} \stopxmlsetups \starttext \xmlprocessbuffer{main}{text}{} \stoptext 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
[NTG-context] changing an XML node and reprocess it (or xmlflushing a string)
Hello, i'm using XML and i find useful specifying a fraction made of text this way: text for numerator/text for denominator With some lua, i can transform it into \frac{\text{text for numerator}}{\text{text for denominator}} which typesets something like this: text for numerator text for denominator Suppose you want to add some styling to the texts, like this: text for numerator/text for denominator You should split the span node into two elements and the xmlflush them. This is my M(not)WE: \startbuffer[text] A paragraph with a fraction made of text with styles: a fraction made of text/with styles inside. \stopbuffer \startluacode local sub = string.sub local sfind = string.find local xmltext = xml.text local xmlconvert = xml.convert local function numeratorDenominator( text ) local before, after = sfind( text, "[^<]/[^>]" ) local num, den if before and after then num = sub( text, 1, before ) den = sub( text, after ) end return num, den end function xml.functions.textfraction( t ) local text = xmltext( t, '' ) local num, den = numeratorDenominator( text ) if num and den then local fontstyle = tokens.getters.macro( "fontstyle" ) local xml_num = xmlconvert( num ) local xml_den = xmlconvert( den ) --context( "$\\frac{\\text{\\" ..fontstyle .. " " .. num .. "}}{\\text{\\" ..fontstyle .. " " .. den .. "}}$" ) context( "$\\frac{\\text{\\" ..fontstyle .. " " ) context( num ) -- context.xmlprocessstring( xml_num ) context( "}}{\\text{\\" ..fontstyle .. " " ) context( den ) -- context.xmlprocessstring( xml_den ) context( "}}$" ) else context.xmlflush( t ) end end \stopluacode \startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{p|i|red}{xml:*} \xmlsetsetup{#1}{{span.fraction}}{xml:fraction} \stopxmlsetups \xmlregistersetup{xml:textsetups} \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \startxmlsetups xml:i {\it \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:red {\red \xmlflush{#1}} \stopxmlsetups \startxmlsetups xml:fraction \xmlfunction{#1}{textfraction} \stopxmlsetups \starttext \xmlprocessbuffer{main}{text}{} \stoptext 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___