source: Dev/trunk/src/client/dojox/highlight/tests/test_highlight.html

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 11.4 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2        "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5       
6        <title>dojox.highlight - syntax highlighting | The Dojo Toolkit</title>
7
8        <style type="text/css">
9                @import "../../../dijit/tests/css/dijitTests.css";
10
11                /* a sample set of definitions to use as a foundation to color your code */
12                @import "../resources/highlight.css";
13
14                /* additional styling for this test case */
15                pre.item code[class]:after {
16                  content: 'highlight: ' attr(class);
17                  display: block; text-align: right;
18                  font-size: smaller;
19                  color: #CCC; background: white;
20                  border-top: solid 1px;
21                  padding-top: 0.5em;
22                }
23               
24                pre code {
25                  display: block;
26                }
27               
28                code {
29                  background: #F0F0F0;
30                }
31               
32                pre code,
33                .ruby .subst {
34                  color: black;
35                }
36        </style>
37
38        <script type="text/javascript" data-dojo-config="parseOnLoad: false, isDebug: true" src="../../../dojo/dojo.js"></script>
39
40        <script type="text/javascript">
41                // initHighlightOnLoad is deprecated.
42                // if parseOnLoad==true, the onLoad init stuff is run.
43                // if parseOnLoad==false, you can call dojox.highlight.init(domNode)
44                //
45                // utilizing the dojo build system, the dojox.highlight engine
46                // will "do it's best" based on the language maps available
47                // at the time of construction.
48                //
49               
50                require(["dojo/parser", "dojox/highlight", "dojox/highlight/languages/_all", "dojox/highlight/widget/Code"], function(){
51                   
52                        dojo.query("code").forEach(dojox.highlight.init);
53                        dojo.xhrGet({
54                                url: "../../../dojo/aspect.js",
55                                load: function(data){
56                                        var n = dojo.byId("foobar"),
57                                                c = document.createElement('div'),
58                                                e = n.parentNode.parentNode;
59                                        c.innerHTML = '<pre class="item"><code class="javascript">' + data.replace(/\</gi,"&lt;") + '</code></pre>';
60                                        e.replaceChild(c.firstChild, n.parentNode);
61                                        dojo.query("pre > code", e).forEach(dojox.highlight.init);
62                                }
63                        });
64                       
65                        dojo.ready(function(){
66                                dojo.parser.parse();
67                        })
68
69                });
70
71        </script>
72</head>
73<body>
74
75        <h1 class="testTitle">dojox.highlight</h1>
76
77        <p>client-side syntax highlighting for a number of languages.</p>
78
79        <p><em>NOTE:</em> All languages listed here have working language definitions, though
80        not all exist in the release or dojo subversion. The missing packs are not publically available.
81        <span style="display:none">based on</span>
82        </p>
83
84        <h2>Examples:</h2>
85
86        <p>Some Python code:</p>
87
88<pre class="item"><code>@requires_authorization
89def somefunc(param1, param2):
90  '''A docstring'''
91  if param1 &gt; param2: # interesting
92    print 'Gre\'ater'
93    print ''
94  return param2 - param1 + 1
95 
96class SomeClass:<br>    pass
97</code></pre>
98
99        <p>A chunk of PHP: </p>
100
101<pre class="item"><code class="php">
102$opAr = array (    "-a|--append", // a or append toggle, nothing extra
103        "-i|--input:", // i or input with next input being needed
104        "-l|--list:",           // l with input needed
105        //"--foo",     // broken
106        "-f:",           // f with no input
107        "--wot:"      // wot with input, no short
108        );
109
110
111$op = bgetop($opAr);
112if (is_array($op)) { print_r($op); }
113
114/* here is the code: */
115
116function bgetop($opAr=array(),$unknown=true) {
117
118$argv = $_SERVER['argv'];
119$argc = $_SERVER['argc'];
120$argPos = 1; // zero is program running
121
122// foreach arg
123while ($argPos&lt;$argc) {
124    $arg = $argv[$argPos];
125    if ($arg{0}=="-") {
126        if ($arg{1}=="-") {
127        $var = substr($arg,2,strlen($arg));
128        } else { $var = $arg{1}; }
129        foreach ($opAr as $opk =&gt; $opv) {
130            if (!isset($return[$var])) {
131            if (strpos($opv,$arg) !== FALSE) {
132                // this is where the -f -foo fix needs to be,
133                // the partial string exists in this record,
134                // but we need to determine if it's accurate
135                // somehow (i'm thinking: eregi?)
136                if ($accurate=1) {
137                    // we foudn the key
138                    if (strpos($opv,':') !== FALSE) {
139                        // next value is the one to use,
140                        // then skip it in the parser.
141                        if (isset($argv[$argPos+1])) {
142                            $return[$var] = $argv[++$argPos];
143                            } else {
144                            $return[$var] = FALSE;     
145                            }
146                        } else {
147                        // just set the toggle
148                        $return[$var] = TRUE;
149                        }
150                    // don't check this opAr value again
151                    unset($opAr[$opk]);
152                    }
153                } // if accurate
154            } // !isset already
155            } // foreach opAr   
156    } else { // we weren't expecting a non-hyphened argument, possibly just a filename, or whatnot
157        if ($unknown) { $return['unknown'][]=$arg; }
158    }
159    $argPos++;
160} // while argPos < argc
161
162if (is_array($return)) {
163return $return;
164} else { return 0; }
165
166} // end function bgetop
167
168</code></pre>
169
170        <p>A custom XML document:</p>
171
172<pre class="item"><code>&lt;?xml version="1.0"?&gt;
173&lt;response value="ok"&gt;
174  &lt;text&gt;Ok&lt;/text&gt;
175  &lt;comment/&gt;
176  &lt;ns:description&gt;&lt;![CDATA[
177  CDATA is &lt;not&gt; magical.
178  ]]&gt;&lt;/ns:description&gt;
179&lt;/response&gt;
180</code></pre>
181
182        <p>Some HTML code:</p>
183
184<pre class="item"><code>&lt;head&gt;
185  &lt;title&gt;Title&lt;/title&gt;
186&lt;body&gt;
187  &lt;p class="something"&gt;Something&lt;/p&gt;
188  &lt;p class=something&gt;Something&lt;/p&gt;
189  &lt;!-- comment --&gt;
190  &lt;p class&gt;Something&lt;/p&gt;
191  &lt;p class="something" title="p"&gt;Something&lt;/p&gt;
192&lt;/body&gt;
193</code></pre>
194
195        <p>HTML with Django templates:</p>
196
197<pre class="item"><code>{% if articles|length %}
198{% for article in articles %}
199
200{# Striped table #}
201&lt;tr class="{% cycle odd,even %}"&gt;
202  &lt;td&gt;{{ article|default:"Hi... "|escape }}&lt;/td&gt;
203  &lt;td&gt;{{ article.date|date:"d.m.Y" }}&lt;/td&gt;
204&lt;/tr&gt;
205 
206{% endfor %}
207{% endif %}
208
209{% comment %}
210Comments may be long and
211multiline.
212{% endcomment %}
213</code></pre>
214
215        <p>Some CSS code:</p>
216
217<pre class="item"><code>body,
218html {
219  font: Tahoma, Arial, san-serif;
220}
221
222#content {
223  width: 100%; /* css comment */
224  height: 100%
225}
226
227p[lang=ru] {
228  color: red;
229}
230</code></pre>
231
232        <p>Explicit Python highlight:</p>
233
234<pre class="item"><code class="python">for x in [1, 2, 3]:
235  count(x)
236</code></pre>
237
238        <p>Disabled highlighting:</p>
239
240<pre class="item"><code class="no-highlight">&lt;div id="contents"&gt;
241  &lt;p&gt;Hello, World!
242&lt;/div&gt;
243</code></pre>
244
245        <p>Normal dojo-looking code</p>
246
247<pre class="item"><code>
248dojo.provide("some.object");
249dojo.declare("some.object",null,{
250        param: "value",
251        _myMethod: function(/* Event */e){
252                this.inherited(arguments);
253        },
254        // comments
255        _another: function(){
256                dojo.addClass("foo","hovered");
257        }
258});
259dojo.addOnLoad(function(){
260        //
261        // comments with &lt;HTML&gt; inline
262        var d = dojo;
263        d.mixin(d,{
264                foo: function(e){
265                        d.bar(e);
266                },
267                bar: function(e){
268                        alert(e);
269                }
270        });
271});
272</code></pre>
273
274        <p>Lazy, xhr'd code:</p>
275
276<pre class="item"><code id="foobar"></code></pre>
277
278<hr>
279
280        <p>Text with inlined JavaScript code: <code class="javascript">dojo.forEach(a, function(x){ console.log(x); });</code> &mdash; that was the inlined sample.</p>
281
282<hr>
283
284        <p>Markuped code (python), no language was specified:</p>
285
286<div data-dojo-type="dojox.highlight.widget.Code">@requires_authorization
287def somefunc(param1, param2):
288  '''A docstring'''
289  if param1 &gt; param2: # interesting
290    print 'Gre\'ater'
291    print ''
292  return param2 - param1 + 1
293 
294class SomeClass:<br>    pass
295</div>
296
297        <p>Markuped code, "python" was specified:</p>
298
299<div data-dojo-type="dojox.highlight.widget.Code" class="python">
300@requires_authorization
301def somefunc(param1, param2):
302  '''A docstring'''
303  if param1 &gt; param2: # interesting
304    print 'Gre\'ater'
305    print ''
306  return param2 - param1 + 1
307 
308class SomeClass:<br>    pass
309</div>
310
311        <p>Some XQuery code:</p>
312
313<pre class="item"><code class="xquery">
314declare variable $my:entityName as xs:string external;
315
316declare variable $databaseURI := concat('jdbc://getCreditDefaultSwapsByEntityName?cd&#37;&amp;', $my:entityName);
317declare variable $creditDefaultSwaps := collection($databaseURI);
318
319(: This is a comment :)
320
321(: This is a multi-line
322comment :)
323declare function local:equityRows($root) {
324        for $equity in $root//equity
325        let $referenceEntity := $creditDefaultSwaps//fpml:referenceEntity
326        where $equity/name = $referenceEntity/fpml:entityName
327        return
328                &lt;tr xmlns="http://www.w3.org/1999/xhtml">
329                        &lt;td>{ $equity/*:symbol/text() }&lt;/td>
330                        &lt;td>{ $equity/*:name/text() }&lt;/td>
331                        &lt;td>{ $equity/*:high/text() }&lt;/td>
332                        &lt;td>{ $equity/*:currency/text() }&lt;/td>
333                &lt;/tr>
334};
335
336&lt;table border="1">
337&lt;tr>
338        &lt;th>Ticker Symbol&lt;/th>
339        &lt;th>Company Name&lt;/th>
340        &lt;th>High&lt;/th>
341        &lt;th>Currency&lt;/th>
342&lt;/tr>
343{ local:equityRows(/) }
344&lt;/table>
345</code></pre>
346
347        <p>Some Java code:</p>
348
349<pre class="item"><code class="java">
350import java.io.*;
351public final class DOHRobot extends Applet{
352        // The last reported mouse x,y.
353        // If this is different from the real one, something's up.
354        private int lastMouseX;
355        private int lastMouseY;
356        JSObject dohrobot = null;
357
358        final private class onvisible extends ComponentAdapter{
359                public void componentShown(ComponentEvent evt){
360                        /* sets the security manager to fix a bug *
361                         * in liveconnect in Safari on Mac        */
362                        if(key != -1){ return; }
363                        Thread thread = new Thread(){
364                                public void run(){
365                                        window = (JSObject) JSObject.getWindow(applet());   
366                                        AccessController.doPrivileged(new PrivilegedAction(){
367                                                public Object run(){
368                                                        log("> init Robot");
369                                                        try{
370                                                                SecurityManager oldsecurity = System.getSecurityManager();
371                                                                boolean needsSecurityManager = applet().getParameter("needsSecurityManager").equals("true");
372                                                                log("Socket connections managed? "+needsSecurityManager);
373                                                                try{
374                                                                        securitymanager.checkTopLevelWindow(null);
375                                                                        // xdomain
376                                                                        if(charMap == null){
377                                                                                if(!confirm("DOH has detected that the current Web page is attempting to access DOH,\n"+
378                                                                                                        "but belongs to a different domain than the one you agreed to let DOH automate.")){
379                                                                                        return null;
380                                                                                }
381                                                                        }
382                                                                }catch(Exception e){
383                                                                        e.printStackTrace();
384                                                                        securitymanager = new RobotSecurityManager(needsSecurityManager,
385                                                                                        oldsecurity);
386                                                                        System.setSecurityManager(securitymanager);
387                                                                }
388                                                                // instantiate the Robot
389                                                                robot = new Robot();
390                                                                robot.setAutoWaitForIdle(true);
391                                                        }catch(Exception e){
392                                                                key = -2;
393                                                                e.printStackTrace();
394                                                        }
395                                                        return null;
396                                                }
397                                        });
398                                        if(key == -2){
399                                                // applet not trusted
400                                                // start the test without it
401                                                window.eval("doh.robot._appletDead=true;doh.run();");
402                                        }
403                                }
404                        };
405                        thread.start();
406                }
407        }
408}
409</code></pre>
410
411        <p>A Groovy fragment:</p>
412
413<pre class="item"><code class="groovy">
414/*
415* A comment test block
416*
417* ?debug
418* ?nodebug
419*
420*/
421
422def settings = [
423        debug: false,
424        compress: true,
425        console: false,
426        ping: false,
427        testing: false,
428        profile: false
429];
430
431// function calls
432init(settings);
433render(settings);
434
435def render(settings) {
436        request.header = settings;
437        render();
438}
439
440def init(settings) {
441
442        // Default parameter handling.
443        settings.each { key, value ->
444                def onkey = "${key}";
445                def offkey = "no${key}";
446                if (foo("/request/params/${onkey}",null) != null) {
447                        settings[key] = true;
448                }
449                else if (foo("/request/params/${offkey}",null) != null) {
450                        settings[key] = false;
451                }
452        }
453
454        return settings;
455}
456</code></pre>
457
458</body></html>
Note: See TracBrowser for help on using the repository browser.