(:
: Copyright (c) 2004 raff@aromatic.org
: original source: xfaqtor (see following copyright)
:
:
: Copyright (c) 2004 Mark Logic Corporation
:
: Licensed under the Apache License, Version 2.0 (the "License");
: you may not use this file except in compliance with the License.
: You may obtain a copy of the License at
:
: http://www.apache.org/licenses/LICENSE-2.0
:
: Unless required by applicable law or agreed to in writing, software
: distributed under the License is distributed on an "AS IS" BASIS,
: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
: See the License for the specific language governing permissions and
: limitations under the License.
:
: The use of the Apache License does not indicate that this project is
: affiliated with the Apache Software Foundation.
:)
module "http://www.w3.org/2003/05/xpath-functions"
import module "http://www.w3.org/2003/05/xpath-functions" at "xqlog-lib.xqy"
define variable $app-title { "xqlog" }
define function get-title() as xs:string
{
$app-title
}
define function print-user() as element()
{
if (is-login())
then
login:
{get-user()}
else
Login
}
define function print-go-home() as element()
{
}
(: Takes element(log) and element(reply) :)
define function is-new($item as element()) as xs:boolean
{
let $threshold := xdt:dayTimeDuration("PT12H")
let $threshold-moment := current-dateTime() - $threshold
return xs:dateTime($item/date) > $threshold-moment
}
define function print-entry($entry as element(entry)) as element()
{
let $user := get-user()
let $log := $entry/log
let $live-replies := $entry/reply[state="live" or author=$user]
return
{if (is-new($log)) then New! else ()}
{print-preserving($log/title/text())}
{print-preserving($log/content/text())}
{
for $reply in $live-replies
return
{if (is-new($reply)) then New! else ()}
{print-preserving($reply/text/text())}
else
()
}
}
define function print-state-select-all($name as xs:string,
$starter as xs:string) as element(select)
{
let $sel := /@selected
return
}
define function print-state-select($name as xs:string,
$starter as xs:string) as element(select)
{
let $sel := /@selected
return
}
define function limit-string($str as xs:string, $max as xs:integer) as xs:string
{
if (string-length($str) > $max) then
concat(substring($str, 0, $max), "...")
else
$str
}
define function print-admin-entry($entry as element(entry),
$states as xs:string*)
as element()
{
(: print the log regardless of state :)
{ print-admin-log($entry/log) }
{
for $reply in $entry//reply[state = $states]
return
}
define function print-admin-log($log as element(log))
as element()
{
let $id := data($log/@id)
let $title-str := limit-string(string($log/title/text()), 80)
let $content-str := limit-string(string($log/content/text()), 80)
return
}
define function print-admin-reply($reply as element(reply))
as element()
{
let $id := data($reply/@id)
let $str := limit-string(string($reply/text/text()), 80)
return
}
define function print-preserving($texts as text()*) as item()*
{
print-preserve-code(string-join($texts, ""))
}
define function print-preserve-code($str as xs:string) as item()*
{
let $before-begin := substring-before($str, "")
let $after-begin := substring-after($str, "")
return
if ($before-begin = "" and $after-begin = "")
then preserve-newlines($str)
else
let $middle := substring-before($after-begin, "")
let $middle := if ($middle = "") then $after-begin else $middle
let $after-end := substring-after($after-begin, "")
return (preserve-newlines($before-begin),
{$middle}
,
print-preserve-code($after-end))
}
define function preserve-newlines($str as xs:string) as node()*
{
for $line in tokenize($str, "\n")
return ($line, )
}