Mercurial > kallithea
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 "1.0-ml"; | |
48 (: this is | |
49 : a | |
50 "comment" :) | |
51 let $let := <x attr="value">"test"<func>function() $var {function()} {$var}</func></x> | |
52 let $joe:=1 | |
53 return element element { | |
54 attribute attribute { 1 }, | |
55 element test { 'a' }, | |
56 attribute foo { "bar" }, | |
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 := <let>let $let := "let"</let> | |
67 return element element { | |
68 attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } }, | |
69 attribute fn:doc { "bar" castable as xs:string }, | |
70 element text { text { "text" } }, | |
71 fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ], | |
72 //fn:doc | |
73 } | |
74 | |
75 | |
76 | |
77 xquery version "1.0-ml"; | |
78 | |
79 (: Copyright 2006-2010 Mark Logic Corporation. :) | |
80 | |
81 (: | |
82 : Licensed under the Apache License, Version 2.0 (the "License"); | |
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 "AS IS" 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 = "http://marklogic.com/json"; | |
96 declare default function namespace "http://www.w3.org/2005/xpath-functions"; | |
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, "\\", "\\\\") | |
101 let $s := replace($s, """", "\\""") | |
102 let $s := replace($s, codepoints-to-string((13, 10)), "\\n") | |
103 let $s := replace($s, codepoints-to-string(13), "\\n") | |
104 let $s := replace($s, codepoints-to-string(10), "\\n") | |
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 = "number") 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("Not a number: ", xdmp:describe($x))) | |
117 else if ($x/@type = "boolean") then | |
118 let $castable := $x castable as xs:boolean | |
119 return | |
120 if ($castable) then xs:string(xs:boolean($x)) | |
121 else error(concat("Not a boolean: ", xdmp:describe($x))) | |
122 else concat('"', json:escape($x), '"') | |
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 = "true") then | |
130 concat('"', json:escape(xdmp:quote($x/node())), '"') | |
131 else | |
132 string-join(('{', | |
133 string-join(for $i in $x/* return json:print-name-value($i), ","), | |
134 '}'), "") | |
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]) > 0 or $x/@array = "true") | |
143 let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) > 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(('"', json:escape($name), '":[', | |
150 string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), ","), | |
151 ']'), "") | |
152 else | |
153 string-join(('"', json:escape($name), '":', json:print-value($x)), "") | |
154 }; | |
155 | |
156 (:~ | |
157 Transforms an XML element into a JSON string representation. See http://json.org. | |
158 <p/> | |
159 Sample usage: | |
160 <pre> | |
161 xquery version "1.0-ml"; | |
162 import module namespace json="http://marklogic.com/json" at "json.xqy"; | |
163 json:serialize(&lt;foo&gt;&lt;bar&gt;kid&lt;/bar&gt;&lt;/foo&gt;) | |
164 </pre> | |
165 Sample transformations: | |
166 <pre> | |
167 &lt;e/&gt; becomes {"e":null} | |
168 &lt;e&gt;text&lt;/e&gt; becomes {"e":"text"} | |
169 &lt;e&gt;quote " escaping&lt;/e&gt; becomes {"e":"quote \" escaping"} | |
170 &lt;e&gt;backslash \ escaping&lt;/e&gt; becomes {"e":"backslash \\ escaping"} | |
171 &lt;e&gt;&lt;a&gt;text1&lt;/a&gt;&lt;b&gt;text2&lt;/b&gt;&lt;/e&gt; becomes {"e":{"a":"text1","b":"text2"}} | |
172 &lt;e&gt;&lt;a&gt;text1&lt;/a&gt;&lt;a&gt;text2&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1","text2"]}} | |
173 &lt;e&gt;&lt;a array="true"&gt;text1&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1"]}} | |
174 &lt;e&gt;&lt;a type="boolean"&gt;false&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":false}} | |
175 &lt;e&gt;&lt;a type="number"&gt;123.5&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":123.5}} | |
176 &lt;e quote="true"&gt;&lt;div attrib="value"/&gt;&lt;/e&gt; becomes {"e":"&lt;div attrib=\"value\"/&gt;"} | |
177 </pre> | |
178 <p/> | |
179 Namespace URIs are ignored. Namespace prefixes are included in the JSON name. | |
180 <p/> | |
181 Attributes are ignored, except for the special attribute @array="true" 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 "boolean" or "number" 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 <p/> | |
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), '}'), "") | |
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> |