File modules/editor/codemirror/mode/xquery/index.html
Last commit: Sun Dec 17 01:14:09 2017 +0100 Jan Dankert Integration eines weiteren Code-Editors: Codemirror. Demnächst müssen wir hier mal aufräumen und andere Editoren rauswerfen.
1 <!doctype html> 2 3 <title>CodeMirror: XQuery mode</title> 4 <meta charset="utf-8"/> 5 <link rel=stylesheet href="../../doc/docs.css"> 6 7 <link rel="stylesheet" href="../../lib/codemirror.css"> 8 <link rel="stylesheet" href="../../theme/xq-dark.css"> 9 <script src="../../lib/codemirror.js"></script> 10 <script src="xquery.js"></script> 11 <style type="text/css"> 12 .CodeMirror { 13 border-top: 1px solid black; border-bottom: 1px solid black; 14 height:400px; 15 } 16 </style> 17 <div id=nav> 18 <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> 19 20 <ul> 21 <li><a href="../../index.html">Home</a> 22 <li><a href="../../doc/manual.html">Manual</a> 23 <li><a href="https://github.com/codemirror/codemirror">Code</a> 24 </ul> 25 <ul> 26 <li><a href="../index.html">Language modes</a> 27 <li><a class=active href="#">XQuery</a> 28 </ul> 29 </div> 30 31 <article> 32 <h2>XQuery mode</h2> 33 34 35 <div class="cm-s-default"> 36 <textarea id="code" name="code"> 37 xquery version "1.0-ml"; 38 (: this is 39 : a 40 "comment" :) 41 let $let := <x attr="value">"test"<func>function() $var {function()} {$var}</func></x> 42 let $joe:=1 43 return element element { 44 attribute attribute { 1 }, 45 element test { 'a' }, 46 attribute foo { "bar" }, 47 fn:doc()[ foo/@bar eq $let ], 48 //x } 49 50 (: a more 'evil' test :) 51 (: Modified Blakeley example (: with nested comment :) ... :) 52 declare private function local:declare() {()}; 53 declare private function local:private() {()}; 54 declare private function local:function() {()}; 55 declare private function local:local() {()}; 56 let $let := <let>let $let := "let"</let> 57 return element element { 58 attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } }, 59 attribute fn:doc { "bar" castable as xs:string }, 60 element text { text { "text" } }, 61 fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ], 62 //fn:doc 63 } 64 65 66 67 xquery version "1.0-ml"; 68 69 (: Copyright 2006-2010 Mark Logic Corporation. :) 70 71 (: 72 : Licensed under the Apache License, Version 2.0 (the "License"); 73 : you may not use this file except in compliance with the License. 74 : You may obtain a copy of the License at 75 : 76 : http://www.apache.org/licenses/LICENSE-2.0 77 : 78 : Unless required by applicable law or agreed to in writing, software 79 : distributed under the License is distributed on an "AS IS" BASIS, 80 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 81 : See the License for the specific language governing permissions and 82 : limitations under the License. 83 :) 84 85 module namespace json = "http://marklogic.com/json"; 86 declare default function namespace "http://www.w3.org/2005/xpath-functions"; 87 88 (: Need to backslash escape any double quotes, backslashes, and newlines :) 89 declare function json:escape($s as xs:string) as xs:string { 90 let $s := replace($s, "\\", "\\\\") 91 let $s := replace($s, """", "\\""") 92 let $s := replace($s, codepoints-to-string((13, 10)), "\\n") 93 let $s := replace($s, codepoints-to-string(13), "\\n") 94 let $s := replace($s, codepoints-to-string(10), "\\n") 95 return $s 96 }; 97 98 declare function json:atomize($x as element()) as xs:string { 99 if (count($x/node()) = 0) then 'null' 100 else if ($x/@type = "number") then 101 let $castable := $x castable as xs:float or 102 $x castable as xs:double or 103 $x castable as xs:decimal 104 return 105 if ($castable) then xs:string($x) 106 else error(concat("Not a number: ", xdmp:describe($x))) 107 else if ($x/@type = "boolean") then 108 let $castable := $x castable as xs:boolean 109 return 110 if ($castable) then xs:string(xs:boolean($x)) 111 else error(concat("Not a boolean: ", xdmp:describe($x))) 112 else concat('"', json:escape($x), '"') 113 }; 114 115 (: Print the thing that comes after the colon :) 116 declare function json:print-value($x as element()) as xs:string { 117 if (count($x/*) = 0) then 118 json:atomize($x) 119 else if ($x/@quote = "true") then 120 concat('"', json:escape(xdmp:quote($x/node())), '"') 121 else 122 string-join(('{', 123 string-join(for $i in $x/* return json:print-name-value($i), ","), 124 '}'), "") 125 }; 126 127 (: Print the name and value both :) 128 declare function json:print-name-value($x as element()) as xs:string? { 129 let $name := name($x) 130 let $first-in-array := 131 count($x/preceding-sibling::*[name(.) = $name]) = 0 and 132 (count($x/following-sibling::*[name(.) = $name]) > 0 or $x/@array = "true") 133 let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) > 0 134 return 135 136 if ($later-in-array) then 137 () (: I was handled previously :) 138 else if ($first-in-array) then 139 string-join(('"', json:escape($name), '":[', 140 string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), ","), 141 ']'), "") 142 else 143 string-join(('"', json:escape($name), '":', json:print-value($x)), "") 144 }; 145 146 (:~ 147 Transforms an XML element into a JSON string representation. See http://json.org. 148 <p/> 149 Sample usage: 150 <pre> 151 xquery version "1.0-ml"; 152 import module namespace json="http://marklogic.com/json" at "json.xqy"; 153 json:serialize(&lt;foo&gt;&lt;bar&gt;kid&lt;/bar&gt;&lt;/foo&gt;) 154 </pre> 155 Sample transformations: 156 <pre> 157 &lt;e/&gt; becomes {"e":null} 158 &lt;e&gt;text&lt;/e&gt; becomes {"e":"text"} 159 &lt;e&gt;quote " escaping&lt;/e&gt; becomes {"e":"quote \" escaping"} 160 &lt;e&gt;backslash \ escaping&lt;/e&gt; becomes {"e":"backslash \\ escaping"} 161 &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"}} 162 &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"]}} 163 &lt;e&gt;&lt;a array="true"&gt;text1&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1"]}} 164 &lt;e&gt;&lt;a type="boolean"&gt;false&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":false}} 165 &lt;e&gt;&lt;a type="number"&gt;123.5&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":123.5}} 166 &lt;e quote="true"&gt;&lt;div attrib="value"/&gt;&lt;/e&gt; becomes {"e":"&lt;div attrib=\"value\"/&gt;"} 167 </pre> 168 <p/> 169 Namespace URIs are ignored. Namespace prefixes are included in the JSON name. 170 <p/> 171 Attributes are ignored, except for the special attribute @array="true" that 172 indicates the JSON serialization should write the node, even if single, as an 173 array, and the attribute @type that can be set to "boolean" or "number" to 174 dictate the value should be written as that type (unquoted). There's also 175 an @quote attribute that when set to true writes the inner content as text 176 rather than as structured JSON, useful for sending some XHTML over the 177 wire. 178 <p/> 179 Text nodes within mixed content are ignored. 180 181 @param $x Element node to convert 182 @return String holding JSON serialized representation of $x 183 184 @author Jason Hunter 185 @version 1.0.1 186 187 Ported to xquery 1.0-ml; double escaped backslashes in json:escape 188 :) 189 declare function json:serialize($x as element()) as xs:string { 190 string-join(('{', json:print-name-value($x), '}'), "") 191 }; 192 </textarea> 193 </div> 194 195 <script> 196 var editor = CodeMirror.fromTextArea(document.getElementById("code"), { 197 lineNumbers: true, 198 matchBrackets: true, 199 theme: "xq-dark" 200 }); 201 </script> 202 203 <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p> 204 205 <p>Development of the CodeMirror XQuery mode was sponsored by 206 <a href="http://marklogic.com">MarkLogic</a> and developed by 207 <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>. 208 </p> 209 210 </article>
Downloadmodules/editor/codemirror/mode/xquery/index.html
History Sun, 17 Dec 2017 01:14:09 +0100 Jan Dankert Integration eines weiteren Code-Editors: Codemirror. Demnächst müssen wir hier mal aufräumen und andere Editoren rauswerfen.