Skip to content

Commit

Permalink
[feature] Add a facility to collapse or drop text nodes which only co…
Browse files Browse the repository at this point in the history
…ntain whitespace
  • Loading branch information
adamretter committed Oct 3, 2016
1 parent 051ba1e commit 47718f5
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/org/exist/xquery/lib/kwic.xql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<config
width="character width"
table="yes|no"
link="URL to which the match is linked"/>
link="URL to which the match is linked"
whitespace-text-nodes="collapse|drop|leave"/>
:)
xquery version "3.0";

Expand Down Expand Up @@ -156,6 +157,40 @@ function kwic:string-length($nodes as item()*) as xs:integer {
0
};

declare
%private
function kwic:collapse-whitespace-fn($callback as (function(text(), xs:string) as text()?)?) as (function(text(), xs:string) as text()) {
function($text as text(), $mode as xs:string) as text()? {
let $text :=
if(matches($text, "\s+")) then
text { " " }
else
$text
return
if(exists($callback))then
$callback($text, $mode)
else
$text
}
};

declare
%private
function kwic:drop-whitespace-fn($callback as (function(text(), xs:string) as text()?)?) as (function(text(), xs:string) as text()?) {
function($text as text(), $mode as xs:string) as text()? {
let $text :=
if(matches($text, "\s+")) then
()
else
$text
return
if(exists($text) and exists($callback))then
$callback($text, $mode)
else
$text
}
};

declare function kwic:get-summary($root as node(), $node as element(),
$config as element(config)?) as element() {
kwic:get-summary($root, $node, $config, ())
Expand Down Expand Up @@ -183,6 +218,16 @@ declare function kwic:get-summary($root as node(), $node as element(),
) as element() {
let $chars := xs:integer(($config/@width, $kwic:CHARS_KWIC)[1])
let $table := $config/@table = ('yes', 'true')
let $whitespace-text-nodes := string($config/@whitespace-text-nodes)

let $callback :=
if($whitespace-text-nodes = "collapse") then
kwic:collapse-whitespace-fn($callback)
else if($whitespace-text-nodes = "drop") then
kwic:drop-whitespace-fn($callback)
else
$callback

let $prevTrunc := kwic:truncate-previous($root, $node, (), $chars, 0, $callback)
let $remain :=
if (not($table)) then
Expand Down

0 comments on commit 47718f5

Please sign in to comment.