comparison rhodecode/public/js/mode/xquery/index.html @ 4026:a60a0e9092c6

added codemirror edit mode with autodetection
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 20 Jun 2013 23:53:18 +0200
parents
children c9bcfe2d2ade
comparison
equal deleted inserted replaced
4025:cd23cc2c9961 4026:a60a0e9092c6
1 <!doctype html>
2 <html>
3 <!--
4 /*
5 Copyright (C) 2011 by MarkLogic Corporation
6 Author: Mike Brevoort <mike@brevoort.com>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 */
26 -->
27 <head>
28 <meta charset="utf-8">
29 <title>CodeMirror: XQuery mode</title>
30 <link rel="stylesheet" href="../../lib/codemirror.css">
31 <script src="../../lib/codemirror.js"></script>
32 <script src="xquery.js"></script>
33 <link rel="stylesheet" href="../../doc/docs.css">
34 <link rel="stylesheet" href="../../theme/xq-dark.css">
35 <style type="text/css">
36 .CodeMirror {
37 border-top: 1px solid black; border-bottom: 1px solid black;
38 height:400px;
39 }
40 </style>
41 </head>
42 <body>
43 <h1>CodeMirror: XQuery mode</h1>
44
45 <div class="cm-s-default">
46 <textarea id="code" name="code">
47 xquery version &quot;1.0-ml&quot;;
48 (: this is
49 : a
50 "comment" :)
51 let $let := &lt;x attr=&quot;value&quot;&gt;&quot;test&quot;&lt;func&gt;function() $var {function()} {$var}&lt;/func&gt;&lt;/x&gt;
52 let $joe:=1
53 return element element {
54 attribute attribute { 1 },
55 element test { &#39;a&#39; },
56 attribute foo { &quot;bar&quot; },
57 fn:doc()[ foo/@bar eq $let ],
58 //x }
59
60 (: a more 'evil' test :)
61 (: Modified Blakeley example (: with nested comment :) ... :)
62 declare private function local:declare() {()};
63 declare private function local:private() {()};
64 declare private function local:function() {()};
65 declare private function local:local() {()};
66 let $let := &lt;let&gt;let $let := &quot;let&quot;&lt;/let&gt;
67 return element element {
68 attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } },
69 attribute fn:doc { &quot;bar&quot; castable as xs:string },
70 element text { text { &quot;text&quot; } },
71 fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ],
72 //fn:doc
73 }
74
75
76
77 xquery version &quot;1.0-ml&quot;;
78
79 (: Copyright 2006-2010 Mark Logic Corporation. :)
80
81 (:
82 : Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
83 : you may not use this file except in compliance with the License.
84 : You may obtain a copy of the License at
85 :
86 : http://www.apache.org/licenses/LICENSE-2.0
87 :
88 : Unless required by applicable law or agreed to in writing, software
89 : distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
90 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
91 : See the License for the specific language governing permissions and
92 : limitations under the License.
93 :)
94
95 module namespace json = &quot;http://marklogic.com/json&quot;;
96 declare default function namespace &quot;http://www.w3.org/2005/xpath-functions&quot;;
97
98 (: Need to backslash escape any double quotes, backslashes, and newlines :)
99 declare function json:escape($s as xs:string) as xs:string {
100 let $s := replace($s, &quot;\\&quot;, &quot;\\\\&quot;)
101 let $s := replace($s, &quot;&quot;&quot;&quot;, &quot;\\&quot;&quot;&quot;)
102 let $s := replace($s, codepoints-to-string((13, 10)), &quot;\\n&quot;)
103 let $s := replace($s, codepoints-to-string(13), &quot;\\n&quot;)
104 let $s := replace($s, codepoints-to-string(10), &quot;\\n&quot;)
105 return $s
106 };
107
108 declare function json:atomize($x as element()) as xs:string {
109 if (count($x/node()) = 0) then 'null'
110 else if ($x/@type = &quot;number&quot;) then
111 let $castable := $x castable as xs:float or
112 $x castable as xs:double or
113 $x castable as xs:decimal
114 return
115 if ($castable) then xs:string($x)
116 else error(concat(&quot;Not a number: &quot;, xdmp:describe($x)))
117 else if ($x/@type = &quot;boolean&quot;) then
118 let $castable := $x castable as xs:boolean
119 return
120 if ($castable) then xs:string(xs:boolean($x))
121 else error(concat(&quot;Not a boolean: &quot;, xdmp:describe($x)))
122 else concat('&quot;', json:escape($x), '&quot;')
123 };
124
125 (: Print the thing that comes after the colon :)
126 declare function json:print-value($x as element()) as xs:string {
127 if (count($x/*) = 0) then
128 json:atomize($x)
129 else if ($x/@quote = &quot;true&quot;) then
130 concat('&quot;', json:escape(xdmp:quote($x/node())), '&quot;')
131 else
132 string-join(('{',
133 string-join(for $i in $x/* return json:print-name-value($i), &quot;,&quot;),
134 '}'), &quot;&quot;)
135 };
136
137 (: Print the name and value both :)
138 declare function json:print-name-value($x as element()) as xs:string? {
139 let $name := name($x)
140 let $first-in-array :=
141 count($x/preceding-sibling::*[name(.) = $name]) = 0 and
142 (count($x/following-sibling::*[name(.) = $name]) &gt; 0 or $x/@array = &quot;true&quot;)
143 let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) &gt; 0
144 return
145
146 if ($later-in-array) then
147 () (: I was handled previously :)
148 else if ($first-in-array) then
149 string-join(('&quot;', json:escape($name), '&quot;:[',
150 string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), &quot;,&quot;),
151 ']'), &quot;&quot;)
152 else
153 string-join(('&quot;', json:escape($name), '&quot;:', json:print-value($x)), &quot;&quot;)
154 };
155
156 (:~
157 Transforms an XML element into a JSON string representation. See http://json.org.
158 &lt;p/&gt;
159 Sample usage:
160 &lt;pre&gt;
161 xquery version &quot;1.0-ml&quot;;
162 import module namespace json=&quot;http://marklogic.com/json&quot; at &quot;json.xqy&quot;;
163 json:serialize(&amp;lt;foo&amp;gt;&amp;lt;bar&amp;gt;kid&amp;lt;/bar&amp;gt;&amp;lt;/foo&amp;gt;)
164 &lt;/pre&gt;
165 Sample transformations:
166 &lt;pre&gt;
167 &amp;lt;e/&amp;gt; becomes {&quot;e&quot;:null}
168 &amp;lt;e&amp;gt;text&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;text&quot;}
169 &amp;lt;e&amp;gt;quote &quot; escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;quote \&quot; escaping&quot;}
170 &amp;lt;e&amp;gt;backslash \ escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;backslash \\ escaping&quot;}
171 &amp;lt;e&amp;gt;&amp;lt;a&amp;gt;text1&amp;lt;/a&amp;gt;&amp;lt;b&amp;gt;text2&amp;lt;/b&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:&quot;text1&quot;,&quot;b&quot;:&quot;text2&quot;}}
172 &amp;lt;e&amp;gt;&amp;lt;a&amp;gt;text1&amp;lt;/a&amp;gt;&amp;lt;a&amp;gt;text2&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:[&quot;text1&quot;,&quot;text2&quot;]}}
173 &amp;lt;e&amp;gt;&amp;lt;a array=&quot;true&quot;&amp;gt;text1&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:[&quot;text1&quot;]}}
174 &amp;lt;e&amp;gt;&amp;lt;a type=&quot;boolean&quot;&amp;gt;false&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:false}}
175 &amp;lt;e&amp;gt;&amp;lt;a type=&quot;number&quot;&amp;gt;123.5&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:123.5}}
176 &amp;lt;e quote=&quot;true&quot;&amp;gt;&amp;lt;div attrib=&quot;value&quot;/&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;&amp;lt;div attrib=\&quot;value\&quot;/&amp;gt;&quot;}
177 &lt;/pre&gt;
178 &lt;p/&gt;
179 Namespace URIs are ignored. Namespace prefixes are included in the JSON name.
180 &lt;p/&gt;
181 Attributes are ignored, except for the special attribute @array=&quot;true&quot; that
182 indicates the JSON serialization should write the node, even if single, as an
183 array, and the attribute @type that can be set to &quot;boolean&quot; or &quot;number&quot; to
184 dictate the value should be written as that type (unquoted). There's also
185 an @quote attribute that when set to true writes the inner content as text
186 rather than as structured JSON, useful for sending some XHTML over the
187 wire.
188 &lt;p/&gt;
189 Text nodes within mixed content are ignored.
190
191 @param $x Element node to convert
192 @return String holding JSON serialized representation of $x
193
194 @author Jason Hunter
195 @version 1.0.1
196
197 Ported to xquery 1.0-ml; double escaped backslashes in json:escape
198 :)
199 declare function json:serialize($x as element()) as xs:string {
200 string-join(('{', json:print-name-value($x), '}'), &quot;&quot;)
201 };
202 </textarea>
203 </div>
204
205 <script>
206 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
207 lineNumbers: true,
208 matchBrackets: true,
209 theme: "xq-dark"
210 });
211 </script>
212
213 <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p>
214
215 <p>Development of the CodeMirror XQuery mode was sponsored by
216 <a href="http://marklogic.com">MarkLogic</a> and developed by
217 <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>.
218 </p>
219
220 </body>
221 </html>