This repository was archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 886
Expand file tree
/
Copy pathprettify_test_2.html
More file actions
1277 lines (1087 loc) · 28.8 KB
/
prettify_test_2.html
File metadata and controls
1277 lines (1087 loc) · 28.8 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Code Prettifier</title>
<script type="text/javascript" src="prettify_test_2.js"></script>
<script type="text/javascript" src="test_base.js"></script>
<link rel="stylesheet" type="text/css" href="test_styles.css">
<script type="text/javascript">
(function () {
// keep track of when window.onload fires, this is to make sure that we dont
// miss it as it might occur before or after dynamic scripts are loaded
var isReady = false;
window.onload = function () { isReady = true; };
// run tests when page is fully loaded
function onReady() {
runTests(goldens);
}
// JS & CSS prettify files to load
var base = /[&?]loader\b/.test(location.search) ? '../loader/' : '../src/';
var sources = [
'lang-basic.js',
'lang-clj.js',
'lang-css.js',
'lang-dart.js',
'lang-ex.js',
'lang-kotlin.js',
'lang-lisp.js',
'lang-llvm.js',
'lang-matlab.js',
'lang-mumps.js',
'lang-n.js',
'lang-pascal.js',
'lang-r.js',
'lang-tcl.js',
'lang-tex.js',
'lang-verilog.js',
'lang-xq.js'
];
var styles = [
'prettify.css'
];
// load skin(s)
while (styles.length) {
injectCSS(base + styles.shift());
}
// first load prettify.js
injectJS(base + 'prettify.js', function () {
// then load all lang-*.js concurrently
while (sources.length) {
injectJS(base + sources.shift(),
(sources.length) ? undefined : function () {
// then after last one is loaded, run tests when page is ready
// (piggyback on window.onload if it hasn't already fired)
if (isReady) {
onReady();
} else {
window.onload = onReady;
}
});
}
});
})();
</script>
</head>
<body>
<h1>Test Results</h1>
<div id="report"></div>
<hr>
<h1>XQuery</h1>
<p>From <code>http://www.patrick-wied.at/static/xquery/prettify/</code></p>
<pre class="prettyprint lang-xq" id="xquery">
(:
Took some of Mike Brevoort's xquery code samples because they are nice and show common xquery syntax
:)
(:~
: Given a sequence of version URIs, publish all of these versions of each document
: If there is a version of the same document already published, unpublish it 1st
:
: When "publish" is referred to, we mean that it is put into the PUBLISHED collection
: unpublish removes content from this collection
: @param $version_uris - sequence of uris of versions of managed documents to publish
:)
declare function comoms-dls:publish($version_uris as item()*) {
for $uri in $version_uris
let $doc := fn:doc($uri)
let $managed_base_uri := $doc/node()/property::dls:version/dls:document-uri/text()
let $existing := comoms-dls:publishedDoc($managed_base_uri)
let $unpublishExisting := if($existing) then comoms-dls:unpublishVersion((xdmp:node-uri($existing))) else ()
let $addPermissions := dls:document-add-permissions($uri, (xdmp:permission('mkp-anon', 'read')))
return
dls:document-add-collections($uri, ("PUBLISHED"))
};
declare function comoms-dls:publishLatest($uri) {
(: TODO check if it's in the draft collection probably :)
let $latest_version_uri := comoms-dls:latestVersionUri($uri)
let $log:= xdmp:log(fn:concat("latest: ", $latest_version_uri))
let $log:= xdmp:log(fn:concat("uri: ", $uri))
return comoms-dls:publish($latest_version_uri)
};
declare function comoms-dls:latestVersionUri($uri) {
let $latest_version_num :=
(
for $version in dls:document-history($uri)/dls:version
order by fn:number($version//dls:version-id/text()) descending
return $version//dls:version-id/text()
)[1]
return dls:document-version-uri($uri, $latest_version_num)
};
declare function comoms-dls:unpublish($uris as item()*) {
for $uri in $uris
return
let $published_doc := comoms-dls:publishedDoc($uri)
return
if($published_doc) then
let $published_version_uri := xdmp:node-uri($published_doc)
return comoms-dls:unpublishVersion($published_version_uri)
else
()
};
declare function comoms-dls:latestPublishedDocAuthor($uri) {
let $author_id := doc($uri)/property::dls:version/dls:author/text()
return
if($author_id) then
comoms-user:getUsername($author_id)
else
()
};
(:~
: Given a sequence of version URIs, unpublish all of these versions of each document
:)
declare function comoms-dls:unpublishVersion($version_uris as item()*) {
for $uri in $version_uris
return
let $removePermissions := dls:document-remove-permissions($uri, (xdmp:permission('mkp-anon', 'read')))
return dls:document-remove-collections($uri, ("PUBLISHED"))
};
(:~
: Given the base URI of a managed piece of content, return the document of the node
: of the version that is published
:)
declare function comoms-dls:publishedDoc($uri) {
fn:collection("PUBLISHED")[property::dls:version/dls:document-uri = $uri]
};
(:~
: Test if any version of the managed document is published
:)
declare function comoms-dls:isPublished($uri) {
if( comoms-dls:publishedDoc($uri)) then
fn:true()
else
fn:false()
};
declare function comoms-dls:publishedState($uri) {
let $doc := comoms-dls:publishedDoc($uri)
let $published_uri := if($doc) then xdmp:node-uri($doc) else ()
let $latest := comoms-dls:latestVersionUri($uri)
return
if($doc) then
if($latest ne $published_uri) then
"stale"
else
"published"
else
"unpublished"
};
declare function comoms-dls:getManagedDocUri($uri) {
let $doc := fn:doc($uri)
let $managed_uri := $doc/property::dls:version/dls:document-uri/text()
let $managed_uri := if($managed_uri) then $managed_uri else $uri
return $managed_uri
};
(:~
: Given a manage content url (e.g. /content/123456.xml) return the appropriate
: version of the document based on what stage collection is being viewed and
: what's published
:
: @param $uri a manage content url (e.g. /content/123456.xml) - NOT A VERSIONED URI
:)
declare function comoms-dls:doc($uri) {
let $doc := fn:root(comoms-dls:collection()[property::dls:version/dls:document-uri = $uri][1])
return
if($doc) then
$doc
else
let $managedDocInCollection := comoms-dls:collection-name() = xdmp:document-get-collections($uri)
return
if($managedDocInCollection) then
fn:doc($uri)
else
()
};
(:~
: Get the collection to be used when querying for content
: THIS or comoms-dls:collection-name() SHOULD BE USED WHEN BUILDING ANY QUERY FOR MANAGED CONTENT
:)
declare function comoms-dls:collection() {
fn:collection( comoms-dls:collection-name() )
};
(:~
: Get the collection nameto be used when querying for content
: THIS or comoms-dls:collection() SHOULD BE USED WHEN BUILDING ANY QUERY FOR MANAGED CONTENT
:)
declare function comoms-dls:collection-name() as xs:string {
let $default_collection := "PUBLISHED"
return
if(comoms-user:isAdmin()) then
let $pub_stage_collection_cookie := comoms-util:getCookie("COMOMS_COLLECTION")
return
if($pub_stage_collection_cookie) then
$pub_stage_collection_cookie
else
$default_collection
else
$default_collection
};
(:~
: Check if the published collection is being viewed
:)
declare function comoms-dls:isViewingPublished() {
if(comoms-dls:collection-name() = "PUBLISHED") then
fn:true()
else
fn:false()
};
(:~
: Get the best URL for the content URI.
: This is either the default URI based on detail type or should also take
: into account friendly urls and navigation structures to figure out the
: best choice
:)
declare function comoms-dls:contentUrl($uri) {
(: TODO: add friendly URL and nav structure logic 1st :)
let $doc := fn:doc($uri)
let $managedDocUri := $doc/property::dls:version/dls:document-uri
let $uri := if($managedDocUri) then $managedDocUri else $uri
let $type := $doc/node()/fn:name()
let $content_id := fn:tokenize( fn:tokenize($uri, "/")[3], "\.")[1]
return
fn:concat("/", $type, "/", $content_id)
};
(:
:
: gets list of doc versions and uri.
:
:)
declare function comoms-dls:versionHistory($uri) {
let $published_doc := comoms-dls:publishedDoc($uri)
let $published_uri := if($published_doc) then xdmp:node-uri($published_doc) else ()
return
<versions>
{
for $version in dls:document-history($uri)/dls:version
let $version_num := $version/dls:version-id/text()
let $created := $version/dls:created/text()
let $author_id := $version/dls:author/text()
let $author := comoms-user:getUsername($author_id)
let $note := $version/dls:annotation/text()
let $version_uri := xdmp:node-uri(dls:document-version($uri, $version_num))
let $published := $published_uri eq $version_uri
return
<version>
<version-number>{$version_num}</version-number>
<created>{$created}</created>
<author>{$author}</author>
<published>{$published}</published>
<version-uri>{$version_uri}</version-uri>
</version>
}
</versions>
};
(: ########################################################################### :)
(: PRIVATE FUNCTIONS :)
(: ########################################################################### :)
declare function comoms-dls:_import() {
"xquery version '1.0-ml';
import module namespace dls = 'http://marklogic.com/xdmp/dls' at '/MarkLogic/dls.xqy'; "
};
(: ----
---- :)
xquery version '1.0-ml';
declare variable $URI as xs:string external;
declare function local:document-move-forest($uri as xs:string, $forest-ids as xs:unsignedLong*)
{
xdmp:document-insert(
$uri,
fn:doc($uri),
xdmp:document-get-permissions($uri),
xdmp:document-get-collections($uri),
xdmp:document-get-quality($uri),
$forest-ids
)
};
let $xml :=
<xml att="blah" att2="blah">
sdasd<b>asdasd</b>
</xml>
(: -------- :)
for $d in fn:doc("depts.xml")/depts/deptno
let $e := fn:doc("emps.xml")/emps/emp[deptno = $d]
where fn:count($e) >= 10
order by fn:avg($e/salary) descending
return
<big-dept>
{
$d,
<headcount>{fn:count($e)}</headcount>,
<avgsal>{fn:avg($e/salary)}</avgsal>
}
</big-dept>
(: -------- :)
declare function local:depth($e as node()) as xs:integer
{
(: A node with no children has depth 1 :)
(: Otherwise, add 1 to max depth of children :)
if (fn:empty($e/*)) then 1
else fn:max(for $c in $e/* return local:depth($c)) + 1
};
local:depth(fn:doc("partlist.xml"))
(: -------- :)
<html><head/><body>
{
for $act in doc("hamlet.xml")//ACT
let $speakers := distinct-values($act//SPEAKER)
return
<div>{ string($act/TITLE) }</h1>
<ul>
{
for $speaker in $speakers
return <li>{ $speaker }</li>
}
</ul>
</div>
}
</body></html>
(: -------- :)
{
for $book in doc("books.xml")//book
return
if (contains($book/author/text(),"Herbert") or contains($book/author/text(),"Asimov"))
then $book
else $book/text()
let $let := <x>"test"</x>
return element element {
attribute attribute { 1 },
element test { 'a' },
attribute foo { "bar" },
fn:doc()[ foo/@bar eq $let ],
//x }
}
(: -------- :)
<bib>
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
where $b/publisher = "Addison-Wesley" and $b/@year > 1991
return
<book year="{ $b/@year }">
{ $b/title }
</book>
}
</bib>
(: -------- :)
</pre>
<h1>Nemerle</h1>
<pre class="prettyprint lang-nemerle" id="nemerle">
class Set ['a]
{
mutable storage : list ['a] = [];
public Add (e : 'a) : void
{
when (! Contains (e))
storage ::= e;
}
public Contains (e : 'a) : bool
{
storage.Contains (e)
}
}
def s1 = Set ();
s1.Add (3);
s1.Add (42);
assert (s1.Contains (3));
// s1.Add ("foo"); // error here!
def s2 = Set ();
s2.Add ("foo");
assert (s2.Contains ("foo"));
</pre>
<h1>LaTeX</h1>
<pre class="prettyprint lang-tex" id="latex">% resume.tex
% vim:set ft=tex spell:
\documentclass[10pt,letterpaper]{article}
\usepackage[letterpaper,margin=0.8in]{geometry}
\usepackage{mdwlist}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\pagestyle{empty}
\setlength{\tabcolsep}{0em}
</pre>
<h1>Escaped quotes in Bash</h1>
<p><a href="https://github.com/google/code-prettify/issues/144">Issue #144</a></p>
<pre class="prettyprint" id="issue144">
#! /bin/bash
# toascii.sh
for i in $(echo $* | fold -w 1);do
printf "%x " \'$i;
done;
echo
</pre>
<h1>Regular expression in <SCRIPT> body</h1>
<p><a href="https://github.com/google/code-prettify/issues/145">Issue #145</a></p>
<pre class="prettyprint" id="issue145">
<script type="text/javascript">
<!--
var target = $$.css('backgroundImage').replace(/^url[\(\)'"]/g, '');
// nice long chain: wrap img element in span
$$.wrap('<span style="position: relative;"></span>')
-->
</script>
</pre>
<h1>Clojure</h1>
<pre class="prettyprint lang-clj" id="clojure">
; Clojure test comment
(ns test
(:gen-class))
(def foo "bar")
(defn bar [arg1 arg2 & args]
"sample function"
(for [arg args]
(prn arg)))
(bar "foo" "bar" "blah" :baz)
</pre>
<h1>HTML5 language on CODE</h1>
<p>The text is specified to be <em>lisp</em> by the class attribute.
Semicolon is normally a valid punctuation character but
in lisp it is a comment so should be colored as a comment
if the className is being properly parsed.</p>
<code class="prettyprint language-lisp" id="html5conv1">; foo</code>
<h1>HTML5 language on nested CODE element</h1>
<p>The language is attached to a CODE element inside a PRE.</p>
<pre class="prettyprint" id="html5conv2"
><code class="language-lisp">; foo</code></pre>
<h1>HTML5 language on nested CODE element not foiled by space</h1>
<p>The language is attached to a CODE element inside a PRE and there
is space between the PRE element's tags and CODE element's tags.</p>
<pre class="prettyprint" id="html5conv3">
<code class="language-lisp">
; foo
</code>
</pre>
<h1>HTML5 nested CODE element language ignored if not only content</h1>
<p>The below is not treated as lisp despite there being a lisp
language specifier on the contained CODE element, the CODE element
does not wrap all non-space content.</p>
<pre class="prettyprint" id="html5conv4">
before CODE
<code class="language-lisp">; foo</code>
</pre>
<h1>Language using XML processing instrution</h1>
<p>The language is attached to an HTML5 comment that looks like an XML
processing instruction.</p>
<?prettify lang=lisp?>
<pre class="prettyprint" id="procinstr1">; foo</pre>
<h1>Language and line numbers using HTML comment</h1>
<p>The language is attached to a regular HTML5 comment that looks like an XML
processing instruction.</p>
<!--prettify linenums=4 lang=lisp-->
<pre class="prettyprint" id="procinstr2">; foo</pre>
<h1>Language and line numbers using HTML comment</h1>
<p>The language is attached to a regular HTML5 comment that looks like an XML
processing instruction.</p>
<!--prettify linenums=true lang=lisp-->
<pre class="prettyprint" id="procinstr3">; foo</pre>
<h1>Language and line numbers using HTML comment</h1>
<p>The language is attached to a regular HTML5 comment that looks like an XML
processing instruction.</p>
<!--prettify linenums=false lang=lisp-->
<pre id="procinstr4">; foo</pre>
<h1>Don't reprettify prettified content</h1>
<p><a href="https://github.com/google/code-prettify/issues/185">Issue #185</a></p>
<code class="prettyprint prettyprinted" id="issue185"
><span class="str">"No tag backs."</span></code>
<p><a href="https://github.com/google/code-prettify/issues/261">Issue #261</a></p>
<pre class="prettyprint prettyprinted linenums" id="issue261"
><ol class="linenums"><li class="L0"><span class="str">"No tag backs."</span></li></ol></pre>
<h1>C type not full word</h1>
<p><a href="https://github.com/google/code-prettify/issues/201">Issue #201</a></p>
<code class="prettyprint lang-c" id="issue201"
>static Persistent<String> listeners_symbol;</code>
<h1>Pascal</h1>
<pre class="prettyprint lang-pascal" id="pascal_lang">
(* some comment here *)
PROCEDURE TestCase.AssertEquals(msg:String; expect, act:Longint);
VAR ex, ac:String;
BEGIN
IF expect <> act THEN
BEGIN
Str(expect, ex);
Fail(Concat(msg,' expected ',ex,' but was ',ac));
END;
factors := new(ArrayListPtr, Init);
FOR candidate := 2 TO i DO
BEGIN
WHILE i MOD candidate = 0 DO
BEGIN
factors^.Add(candidate);
i := i DIV candidate;
END;
END;
END;
</pre>
<h1>BASIC</h1>
<pre class="prettyprint lang-basic" id="basic_lang">
200 REM ----- method teardown
210 PRINT "green"
220 RETURN
470 IF af=0 THEN GOTO 520
480 FOR j=1 TO af
500 ac=pf(j) : me$=STR$(j)+". factor" : GOSUB 100
510 NEXT
530 RETURN
1000 DATA "one", 1, 0
</pre>
<h1>Dart</h1>
<pre class="prettyprint lang-dart" id="dart">
part of myLib;
part 'something.dart';
import 'dart:math' as test show foo, bar;
class Point {
final num x, y;
Point(this.x, this.y);
Point.zero() : x = 0, y = 0; // Named constructor
// with an initializer list.
num distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
// This is a single-line comment.
/*
This is a
multiline comment.
*/
main() {
Point p = new Point(7, 12);
String thing = 'It\'s awesome!';
String thing2 = '''
This is a test! \'''
This is the end of the test''';
String thing3 = r"""
This is a raw
multiline string!""";
num x = 0x123ABC;
num y = 1.8e-12;
bool flag = false;
String raw = r"This is a raw string, where \n doesn't matter";
}
</pre>
<h1>TCL</h1>
<pre class="prettyprint lang-tcl" id="tcl_lang">#!/bin/tclsh
proc fib {n} {
set a 0
set b 1
while {$n > 0} {
set tmp $a
set a [expr $a + $b]
set b $tmp
incr n -1
}
return $a
}
</pre>
<h1>R, S</h1>
<pre class="prettyprint lang-r" id="r_lang">
### Example R script for syntax highlighting
# This is a comment
## Valid names
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0123456789._a <- NULL
.foo_ <- NULL
._foo <- NULL
## Invalid names
0abc <- NULL
.0abc <- NULL
abc+cde <- NULL
## Reserved Words
NA
NA_integer_
NA_real_
NA_character_
NA_complex_
NULL
NaN
Inf
## Not reserved
NULLa <- NULL
NULL1 <- NULL
NULL. <- NULL
NA_foo_ <- NULL
## Numbers
12345678901
123456.78901
123e3
123E3
1.23e-3
1.23e3
1.23e-3
## integer constants
123L
1.23L
## imaginary numbers
123i
-123i
123e4i
123e-4i
## Hex numbers
0xabcdefABCDEF01234
0xabcp123
0xabcP123
## Not hex
0xg
## Special operators %xyz%
## %xyz%
1 %% 2
diag(2) %*% diag(2)
1 %/% 2
1 %in% 1:10
diag(2) %o% diag(2)
diag(2) %x% diag(2)
`%foo bar%` <- function(x, y) x + y
1 %foo bar% 2
## Control Structures (3.2) and Function
## if, else
if (TRUE) print("foo") else print("bar")
## For, in
for(i in 1:5) {
print(i)
}
## While, break
i <- 1
while (TRUE) {
i <- i + 1
if (i > 3) break
}
## Repeat
repeat {1+1}
## Switch
x <- 3
switch(x, 2+2, mean(1:10), rnorm(5))
## Function, dot-dot-dot, return
foo <- function(...) {
return(sum(...))
}
# Not keywords
functiona <- 2 + 2
function. <- 2 + 2
function1 <- 2 + 2
## Grouping Tokens 10.3.7
## Parentheses
1 + (2 + 3)
## brackets
foo <- function(a) {
a + 1
}
## Indexing 10.3.8
## []
bar <- 1:10
bar[3]
## [[]]
foo <- list(a=1, b=2, c=3)
foo[["a"]]
## $
foo$a
foo$"a"
## Operators
2 - 2
2 + 2
2 ~ 2
! TRUE
?"help"
1:2
2 * 2
2 / 2
2^2
2 < 2
2 > 2
2 == 2
2 >= 2
2 <= 2
2 != 2
TRUE & FALSE
TRUE && FALSE
TRUE | FALSE
TRUE || FALSE
foo <- 2 + 2
foo = 2 + 2
2 + 2 -> foo
foo <<- 2 + 2
2 + 2 ->> foo
base:::sum
base::sum
## Strings
foo <- "hello, world!"
foo <- 'hello, world!'
foo <- "Hello, 'world!"
foo <- 'Hello, "world!'
foo <- 'Hello, \'world!\''
foo <- "Hello, \"world!\""
foo <- "Hello,
world!"
foo <- 'Hello,
world!'
## Backtick strings
`foo123 +!"bar'baz` <- 2 + 2
</pre>
<h1>MUMPS</h1>
<pre class="prettyprint lang-mumps" id="mumps">
HDR ; -- prt/display header
N X,I
I '$D(VALMHDR) X:$G(VALM("HDR"))]"" VALM("HDR")
; -- prt hdr line
W:'$D(VALMPG1) @IOF K VALMPG1
W:VALMCC $C(13)_IOUON_$C(13)_IOINHI_$C(13) ; -- turn on undln/hi
I $E(IOST,1,2)="C-" D IOXY^VALM4(0,0) ; -- position cursor
W $E(VALM("TITLE"),1,30) ; -- prt title
W:VALMCC IOINORM,IOUON ; -- turn off hi
W $J("",30-$L(VALM("TITLE"))) ; -- fill in w/blanks
I $E(IOST,1,2)="C-" W $C(13) D IOXY^VALM4(30,0) ; -- position cursor
W $J("",((VALMWD-80)/2)),$$HTE^XLFDT($H,1),$J("",10+((VALMWD-80)/2)),"Page: ",$J(VALMPGE,4)," of ",$J($$PAGE^VALM4(VALMCNT,VALM("LINES")),4)_$S($D(VALMORE):"+",1:" ") ; -- prt rest of hdr
W:VALMCC IOUOFF I $E(IOST,1,2)="C-" D IOXY^VALM4(0,0) ; -- turn off undln
F I=1:1:VALM("TM")-3 W !,$S('$D(VALMHDR(I)):"",$L(VALMHDR(I))>(VALMWD-1):$$EXTRACT^VALM4($G(VALMHDR(I))),1:VALMHDR(I)) ; -- prt hdr
Q
</pre>
<h1>LLVM</h1>
<p><a href="http://llvm.org/docs/LangRef.html">Hello world module</a></p>
<pre class="prettyprint lang-llvm" id="llvm">
; Declare the string constant as a global constant.
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
; External declaration of the puts function
declare i32 @puts(i8* nocapture) nounwind
; Definition of main function
define i32 @main() { ; i32()*
; Convert [13 x i8]* to i8 *...
%cast210 = getelementptr [13 x i8]* @.str, i64 0, i64 0
; Call puts function to write out the string to stdout.
call i32 @puts(i8* %cast210)
ret i32 0
}
; Named metadata
!1 = metadata !{i32 42}
!foo = !{!1, null}
</pre>
<h1>Regex following '!'</h1>
<p><a href="https://github.com/google/code-prettify/issues/217">Issue #217</a></p>
<pre class="prettyprint lang-js" id="issue217">
if(!/^https?:\/\//i.test(val) && foo == 'bar') {
val = 'http://' + val;
}
</pre>
<h1>MATLAB</h1>
<p><a href="https://github.com/google/code-prettify/issues/32">Issue #32</a></p>
<pre class="prettyprint lang-matlab" id="matlab">
%%%%%%%%%%%%%%%%%% DATA TYPES %%%%%%%%%%%%%%%%%%
v = [1,2,3;4,5,6];
v(v>4) = 0;
s = struct('key',1, 'key2','string');
s.key = 2;
C = cell(1,2);
C{1,1} = 0:9;
double(1)
single(1)
uint8(1)
int8(1)
%%%%%%%%%%%%%%%%%% STRINGS & TRANSPOSE %%%%%%%%%%%%%%%%%%
plot(data');
legend(labels)
str = 'asdasd'; % this is a string
str = 'asdas';
str = 'sdasd''sdasd';
str = ['one' 'two' 'three'];
str = strcat('one', 'two', 'three');
% matrix transpose
M = rand(3,3)';
x = M.';
x = [10 20; 30, 40]';
disp(x')
fprintf('%d\n', x(:)') % with comment
{1,2}' % another comment
%%%%%%%%%%%%%%%%%% LINE CONTINUATION %%%%%%%%%%%%%%%%%%
[1 20; ...
30 4]
['gdgsd'...
'sdfs']
{...
'sdasd' ;
'asdsad'}
%%%%%%%%%%%%%%%%%% SYSTEM COMMANDS %%%%%%%%%%%%%%%%%%
!touch file.txt
%%%%%%%%%%%%%%%%%% COMMAND OUTPUT %%%%%%%%%%%%%%%%%%
>> 1+1
ans =
2
>> 1+1
ans =
2
%%%%%%%%%%%%%%%%%% KEYWORDS %%%%%%%%%%%%%%%%%%
function ret = fcn(in)
ret = sum(in.^2);
end
classdef CC < handle
properties (SetAccess = public)
x = 0;
end
methods
function this = CC(varargin)
this.x = 9;
end
end
end
x = [];
parfor i=1:10
x[i] = i;
end
true ~= false
if x==1
true
elseif
false
else
return
end
while true
continue
break
end
try
error('aa:aa', 'asdasd')
catch ME
warning(ME)
end
switch x
case 1
disp(1)
otherwise
0
end
%%%%%%%%%%%%%%%%%% NUM LITERALS %%%%%%%%%%%%%%%%%%
1
1.
.1
1.0
-1
-1.
-.1
-1.0
+10
+01.
+.1
+1.0
1e1
1e-1
1.e1
1.e-1
1.0e1
1.0e-1
.1e1