/
hedgeweb.xqm
129 lines (120 loc) · 5.13 KB
/
hedgeweb.xqm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(:~
: trees as svg
: @author andy bunce
: @since sept 2012
:)
module namespace web = 'apb.hedge.web';
declare default function namespace 'apb.hedge.web';
import module namespace tree = 'apb.tree' at "hedgetree/treedraw.xqm";
declare namespace rest = 'http://exquery.org/ns/restxq';
(:~
: main page
:)
declare
%rest:GET %rest:path("hedge")
%output:method("html") %output:version("5.0")
%rest:form-param("hedge","{$hedge}")
%rest:form-param("url","{$url}")
function hedge($hedge,$url) {
let $ehedge:=if($url) then "" else fn:encode-for-uri($hedge)
let $xml:=getxml($hedge,$url)
let $layout:=tree:layout($xml)
let $svg:=tree:svg($layout)
return <html>
<head>
<title>Drawing trees with XQuery and SVG</title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="tree xquery svg" />
<script type="text/javascript"><![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-34544921-1']);
_gaq.push(['_setDomainName', 'rhcloud.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
]]></script>
<style type="text/css">pre {{background-color:#FFFFDD;}}</style>
</head>
<body>
<h1>Drawing trees with XQuery and SVG</h1>
<p>Enter a string representing a tree in the form below; use letters for nodes and () to enclose subnodes. Use {{...}} for multi character names. {{name|href}} will create a link. Examples: <a href="?hedge=a(bcd(ef))">a(bcd(ef))</a>,
<a href="?hedge={{github|https://github.com/apb2006/hedgetree}}(ab({{tree|%23treexml}}))">another </a>
a proper hedge: <a href="?hedge=a(bcd)e(fgh)">a(bcd)e(fgh)</a>.</p>
<p> Or enter a Url to a xml document examples:
<a href="?url=hedgetree/samples/sample1.xml">sample1</a>,
<a href="?url=hedgetree/samples/hedgeweb.xml">hedgeweb</a>
<a href="?url=https://raw.github.com/apb2006/hedgetree/master/src/hedgetree/samples/hedgeweb.xml">remote</a>
</p>
<form method="get" action="./hedge" style="background-color:#EEEEEE;padding:8px;">
<textarea name="hedge" rows="2" cols="80">{$hedge}</textarea>
<p></p>
<p>Or enter the url to a node XML source
<input name="url" value="{$url}" style="width:30em"/></p>
<button type="submit">Redraw</button>
</form >
<h2 id="isvg">Inline SVG</h2>
<div style="width:300px;height:200px">{$svg}</div>
<h2 id="svg">Object referencing <a href="hedge/svg?hedge={$ehedge}&url={$url}">svg</a>,
( <a href="hedge/svg?dl=1&hedge={$ehedge}&url={$url}">download</a> svg)</h2>
<object height="150" width="300" data="hedge/svg?hedge={$ehedge}&url={$url}"
style="border:5px solid red;" type="image/svg+xml">
SVG Here
</object>
<h2 id="svgxml">SVG xml</h2>
<pre>
{fn:serialize($svg)}
</pre>
<h2 id="layout">Layout xml</h2>
<pre>
{fn:serialize($layout)}
</pre>
<h2 id="treexml">Tree xml</h2>
<pre>
{fn:serialize($xml)}
</pre>
<h2>About</h2>
<p> Source: @github:<iframe src="http://ghbtns.com/github-btn.html?user=apb2006&repo=hedgetree&type=watch"
allowtransparency="true" frameborder="0" scrolling="0" width="62px" height="20px"></iframe>, Twitter:
<a href="https://twitter.com/share" class="twitter-share-button" data-via="apb1704" data-count="none">Tweet</a>
<script>!function(d,s,id){{var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){{js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}}}(document,"script","twitter-wjs");</script>.
</p>
</body>
</html>
};
(:~
: @param hedge
: @param url option location of node xml to use instead of hedge
: @param dl if present add header to force browser download
: @return svg for hedge with download option. cors header
:)
declare
%rest:GET %rest:path("hedge/svg")
%rest:form-param("hedge","{$hedge}")
%rest:form-param("url","{$url}")
%rest:form-param("dl","{$dl}")
%output:media-type("image/svg+xml")
function hedge-svg($hedge,$url,$dl) {
let $xml:=getxml($hedge,$url)
let $layout:=tree:layout($xml)
let $svg:=tree:svg($layout)
let $resp:=<rest:response>
<http:response>
<http:header name="Access-Control-Allow-Origin" value="*"/>
{if($dl)
then <http:header name="Content-Disposition" value='attachment;filename="hedge.svg"'/>
else ()}
</http:response>
</rest:response>
return ($resp,$svg)
};
(:~ use hedge or url :)
declare %private function getxml($hedge,$url){
if($url) then
try{fn:doc(fn:resolve-uri($url))/*} catch * { <node label="{$err:description}"/>}
else
try{ tree:hedge2xml($hedge)} catch * { <node label="{$err:description}"/>}
};