Skip to content

Instantly share code, notes, and snippets.

@joewiz
Last active August 8, 2023 15:45
Show Gist options
  • Save joewiz/29d75b014732cdf6857a5bd4fd9708ad to your computer and use it in GitHub Desktop.
Save joewiz/29d75b014732cdf6857a5bd4fd9708ad to your computer and use it in GitHub Desktop.
Convert quotes from straight to curly, with XQuery
xquery version "3.1";
(: This uses the eXist cache module to mimic xsl:accumulator approach described
: by Norm Walsh at https://so.nwalsh.com/2023/08/08-accumulators :)
declare function local:initiate-cache() {
cache:destroy("quotes"),
cache:create("quotes", map{}),
cache:put("quotes", "counter", 1)
};
declare function local:process-text($text as text()) {
if (contains($text, '"')) then
let $fragments := analyze-string($text, '"')/*
let $new-text :=
for $fragment in $fragments
return
if ($fragment instance of element(fn:match)) then
let $quote :=
if (cache:get("quotes", "counter") mod 2 eq 1) then
'“'
else
'”'
let $increment := cache:put("quotes", "counter", cache:get("quotes", "counter") + 1)
return
$quote
else
$fragment/string()
return
text { string-join($new-text) }
else
$text
};
declare function local:iterate-through-nodes($nodes) {
for $node in $nodes
return
typeswitch ($node)
case element() return element { node-name($node) } { $node/@*, local:iterate-through-nodes($node/node()) }
case text() return local:process-text($node)
default return ()
};
declare function local:fix-straight-quotes($node) {
local:initiate-cache(),
local:iterate-through-nodes($node)
};
let $node := <p>What about "<code>markup</code>" like "this".</p>
return
local:fix-straight-quotes($node)
<p>What about “<code>markup</code>” like “this”.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment