From: mf <massifr@fastwebnet.it>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: changing an XML node and reprocess it (or xmlflushing a string)
Date: Mon, 7 Oct 2019 18:26:50 +0200 [thread overview]
Message-ID: <f769cbd2-00f5-64e7-5b12-01ab3ff93032@fastwebnet.it> (raw)
Hello,
i'm using XML and i find useful specifying a fraction made of text this way:
<span class="fraction">text for numerator/text for denominator</span>
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:
<span class="fraction">text for <i>numerator</i>/text for
<b>denominator</b></span>
You should split the span node into two elements and the xmlflush them.
This is my M(not)WE:
\startbuffer[text]
<p>A paragraph with a fraction made of text with styles:
<span class="fraction">a <i>fraction</i> made of text/with
<red>styles</red> inside</span>.</p>
\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
___________________________________________________________________________________
next reply other threads:[~2019-10-07 16:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-07 16:26 mf [this message]
2019-10-08 9:17 ` mf
2019-10-08 10:30 ` Hans Hagen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f769cbd2-00f5-64e7-5b12-01ab3ff93032@fastwebnet.it \
--to=massifr@fastwebnet.it \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).