-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcourse.html
More file actions
1378 lines (1184 loc) · 53.7 KB
/
course.html
File metadata and controls
1378 lines (1184 loc) · 53.7 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>
<meta charset="utf-8">
<!--============================================================================================-->
<title>code.berlin Summer School Python Course</title>
<header>code.berlin Summer School Python Course</header>
<footer>Fabio Fracassi, code.berlin</footer>
<section>
<h1>Welcome!</h1>
</section>
<section>
<h1>Python Course</h1>
<p>code.berlin Summer School</p>
</section>
<section><h2>Goals</h2>
<ul>
<li>Teach you the basics of Programming</li>
<li>Enable you to ...
<ul>
<li>... learn more on your own</li>
<li>... do simple programming tasks</li>
<li>... fill the SE role in the Orientation semester</li>
<li>... understand the progamming way of thinking</li>
<li>... follow more advanced talks</li>
</ul></li>
</ul>
</section>
<section><h2>Non Goals</h2>
<ul>
<li>Test you</li>
<li>Teach you <b>all</b> the basics of Programming</li>
<li>Get through a fixed amount of material
<ul><li>it is more important not to loose you</li></ul>
</li>
</ul>
</section>
<section><h2>Psychology</h2>
<ul>
<li>Programming is in essence a way of thinking</li>
<li>Learning a new way of thinking is not easy</li>
<li>Being told that your way of thinking is wrong can hurt
<ul><li>Perspective: this is not an attack of your personality</li></ul>
</li>
<li>Computers will persistently point out even tiny Errors
<ul><li><b>Extremly frustrating</b></li>
<li>Perspective: there is no malice involved</li>
</ul>
</li>
</ul>
</section>
<section><h2>Why Python</h2>
<ul>
<li>One of the most popular languages<br/>
<div class="reveal">Favourite Language for many scientific and industry fields</div>
<ul><li>Machine Learning</li>
<li>Data Visualisation</li>
</ul>
</li>
<li>Immediately useful in "the real world"
<ul><li>scripting</li>
<li>task automation</li>
<li>helpers/tools</li>
</ul>
</li>
<li>well suited for beginners
<ul><li>distraction free</li>
<li>fairly consistent</li>
<li>modern paradigms/ideoms</li>
<li>clean syntax, principled semantics</li>
</ul>
</li>
</ul>
</section>
<section><h2>Why not:</h2>
<ul>
<li>Javascript
<ul><li>You will learn it anyway</li></ul></li>
<li>Ruby
<ul><li>very similar to python, but not used as much</li></ul></li>
<li>C++
<ul><li>easier once you know a bit about programming</li></ul></li>
<li>C, Pascal, Basic
<ul><li>Outdated</li></ul></li>
<li>Java, C#</li>
<li>Rust, Swift</li>
<li>Haskell, Elexir, Scala, Clojure, ...</li>
<li>...</li>
</ul>
</section>
<section><h1>How:</h1>
Udacity Course "Intro to Computer Science" (CS101)<br/>
https://classroom.udacity.com/courses/cs101/
</section>
<section><h2>Why</h2>
<ul>
<li>Everyone can learn at their own pace</li>
<li>Interesting exercises</li>
<li>Explains a few important concepts besides programming</li>
</ul>
</section>
<section><h2>A few notes</h2>
<ul>
<li>No "Magic" approach</li>
<li>You'll implement a lot of stuff manually
<ul><li>It is all about <b>understanding</b> how programming works</li>
<li>Not about getting the thing done</li>
<li>google will probably give you a solution that you could copy
<br/><b>Please don't do that!</b>
</li>
<li>there is probably a solution in the pyton standard library</li>
</ul>
</li>
</ul>
</section>
<section><h2>Caveat:</h2>
<ul>
<li>The Udacity material still uses Python 2</li>
<li>On your machines you have Python 3</li>
</ul>
<div class='reveal'><pre title="Python 2"><code class='python'><xmp>
a = 10 / 4 # a will be 2
b = 8 / 4 # b will be 2
print "The course will show you the Python 2 way", a, b
num = 3
print num
s = "The course will show this"
print s.find("show", num)
</xmp></code></pre></div>
<div class='reveal'><pre title="Python 3"><code class='python'><xmp>
a = 10 / 4 # a will be 2.5
b = 8 / 4 # b will be 2
print ( "But you should prefer the Python 3 way!", a, b )
num = 3
print (num)
s = "The course will show this"
print (s.find("show", num))
</xmp></code></pre></div>
</section>
<section><h2>What to do when Errors occur</h2>
<ul>
<li>Don't Panic!</li>
<li>Do not randomly change things in your program until it (seems to) work.</li>
<li>try to reason about what went wrong.</li>
<li>using the reason above, what would you have to do to correct that?</li>
<li>try your hypothesis.</li>
<li>If it works -> Yay!</li>
<ul><li>Do you understand why it works now?</li></ul>
<li>If it does not: </li>
<ul><li>Start again from 'Don't Panic!'</li></ul>
</ul>
</section>
<section><h2>How:</h2>
<ul>
<li>Sign in to Udacity, enroll to the CS101 course</li>
<li>Watch the Video Lessons
<ul><li>(and/or) read the transcripts</li>
<li><b>You are welcome to form groups</b></li></ul></li>
<li>Do the exercises/quizes!
<ul><li><b>Groups are ok, but make sure to engage and understand</b></li></ul></li>
<li><b>Ask us for explanations, comments, help at ANY TIME!</b>
<ul><li>That is why we are here!</li></ul></li>
<li>We meet every 2ish hours, to syncronize, discuss, and have a break</li>
<li>There will be a bit of lessons for material the course does not cover</li>
</ul>
</section>
<section><h2>What exactly?</h2>
<table style="width:100%">
<tr><th>Lesson #</th><th>Unit/Chapter</th><th>Action</th></tr>
<tr><td>1</td><td>1</td><td>watch</td></tr>
<tr><td>2</td><td>1</td><td>do exercise</td></tr>
<tr><td>3</td><td>1</td><td>optional</td></tr>
<tr><td>3.5</td><td>1</td><td>Attention answers are differnt in Python 3</td></tr>
<tr><td>4</td><td>1</td><td>watch</td></tr>
<tr><td>---</td><td>---</td><td>Let's Meet</td></tr>
<tr><td>5</td><td>2</td><td>watch</td></tr>
<tr><td>6</td><td>2</td><td>do exercise</td></tr>
<tr><td>7</td><td>2</td><td>optional</td></tr>
<tr><td>8</td><td>2</td><td>optional</td></tr>
<tr><td>9</td><td>2</td><td>watch</td></tr>
<tr><td>---</td><td>---</td><td>Let's Meet</td></tr>
<tr><td>10</td><td>-</td><td>watch</td></tr>
<tr><td>11</td><td>3</td><td>do exercise</td></tr>
<tr><td>12</td><td>3</td><td>optional</td></tr>
<tr><td>13</td><td>3</td><td>optional</td></tr>
<tr><td>14</td><td>3</td><td>watch</td></tr>
<tr><td>---</td><td>---</td><td>Let's Meet</td></tr>
<tr><td>15.1-6</td><td>4</td><td>watch</td></tr>
<tr><td>15.8-21</td><td>4</td><td>skip</td></tr>
<tr><td>16</td><td>4</td><td>do exercise</td></tr>
<tr><td>17</td><td>4</td><td>optional</td></tr>
<tr><td>---</td><td>---</td><td>Let's Meet</td></tr>
<tr><td>18</td><td>5</td><td>watch</td></tr>
<tr><td>19-21</td><td>5</td><td>optional</td></tr>
<tr><td>---</td><td>---</td><td>Let's Meet</td></tr>
</table>
</section>
<section><h2>Notes:</h2>
<ul>
<li>Do larger exercises locally on your computer
<ul><li>Fork https://github.com/code-uni-workshops/python-intro</li></ul>
</li>
<li>There is reading material in the sidebar</li>
<li>Have a Break!</li>
</ul>
</section>
<section>
<h1>Lesson: Scopes, Functions & Classes</h1>
</section>
<section><h2>Recap</h2>
<div class='reveal'><pre><code class='python'><xmp>
def move_to(x, y, z): // (*)
print( "moved to ", x, y, z)
move_to(1, 2, 3) # moved to 1, 2, 3
move_to(0, 0, 0) # moved to 0, 0, 0
move_to(1, 2, 0) # moved to 1, 2, 0
</xmp></code></pre></div>
</section>
<section><h2>Default parameters</h2>
<div class='reveal'><pre><code class='python'><xmp>
def move_to(x = 0, y = 0, z = 0): // (*)
print( "moved to ", x, y, z)
move_to(1, 2, 3) # moved to 1, 2, 3 // (*)
move_to(0, 0, 0) # moved to 0, 0, 0 // (*)
move_to(0, 0) # moved to 0, 0, 0
move_to(0) # moved to 0, 0, 0
move_to(1, 2, 0) # moved to 1, 2, 0 // (*)
move_to(1, 2) # moved to 1, 2, 0
</xmp></code></pre></div>
</section>
<section><h2>Default parameters</h2>
<div class='reveal'><pre><code class='python'><xmp>
s = "all zip files are zipped"
print (s.find("zip")) // (*)
print (s.find("zip", 0)) // (*)
print (s.find("zip", 6))
</xmp></code></pre></div>
</section>
<section><h2>Named parameters</h2>
<div class='reveal'><pre><code class='python'><xmp>
def move_to(x = 0, y = 0, z = 0): // (*)
print( "moved to ", x, y, z)
move_to(1, 2, 3) # moved to 1, 2, 3
move_to(x = 1, z = 3) # moved to 1, 0, 3 // (*)
move_to(z = 1, y = 3) # moved to 0, 3, 1
</xmp></code></pre></div>
</section>
<section><h2>Scopes</h2>
<p class="quote" data-source="wikipedia"><b>Scope:</b>
The region of a computer program where a name-binding is valid</p>
<div class='reveal'><pre><code class='python'><xmp>
# File starts here
# print(a) // (*)
a = 42 // (*)
print("a =", a) // (*)
</xmp></code></pre>
<p class="code-note"></p>
<p class="code-note">Can't do that, <var>a</var> is not in scope</p>
<p class="code-note">Now we defined <var>a</var></p>
<p class="code-note"><var>a</var> is in scope</p>
</div>
</section>
<section><h2>Scopes</h2>
<div class='reveal'><pre class='donot'><code class='python'><xmp>
a = # some value we do not know
if(a < 10):
b = 100
print ( "To b or not to b?", b ) // (*)
</xmp></code></pre>
<p class="code-note">Good idea?
<div class="reveal"> No, <var>b</var> might or might not be in scope</div></p>
</div>
</section>
<section><h2>A question of style</h2>
<div class='side-by-side'>
<div><pre class='do' title="--- A ---"><code class='python'><xmp>
a = 1
if (b == 42):
a = 2
</xmp></code></pre></div>
<div class='reveal'><pre class='do' title="--- B ---"><code class='python'><xmp>
if (b == 42):
a = 2
else:
a = 1
</xmp></code></pre></div>
</div>
<p class="reveal">It depends:</p>
<div class='side-by-side'>
<div class='reveal'><pre class='do' title="default with exceptions"><code class='python'><xmp>
race = "Human"
if (name == "The Doctor"):
race = "Time Lord"
</xmp></code></pre></div>
<div class='reveal'><pre class='do' title="equaly likely"><code class='python'><xmp>
if (wants_to_start):
color = "White"
else:
color = "Black"
</xmp></code></pre></div>
</div>
</section>
<section><h2>Local Scopes</h2>
<div><pre><code class='python'><xmp>
a = 5
def a_function():
b = 10
print(a) // (*)
print(b) // (*)
a_function()
print(a) // (*)
#print(b) // (*)
</xmp></code></pre></div>
</section>
<section><h2>Local Scopes</h2>
<div class='reveal'><pre><code class='python'><xmp>
a = 5 // (*)
def a_function():
a = 10 // (*)
print(a) // (*)
a_function()
print(a) // (*)
</xmp></code></pre></div>
</section>
<section><h2>mixing scopes</h2>
<div class='reveal'><pre class='donot'><code class='python'><xmp>
a = 5
def a_function():
print(a) // (*)
a = 10
print(a)
a_function()
print(a)
</xmp></code></pre></div>
</section>
<section><h2>Global Scope</h2>
<div class='reveal'><pre title='Yes - No - Maybe - I dont know'><code class='python'><xmp>
a = 5
def a_function():
global a // (*)
print(a) // (*)
a = 10
print(a)
a_function()
print(a) // (*)
</xmp></code></pre></div>
</section>
<section><h2>File Scope</h2>
<div class='reveal'><pre title='file.py'><code class='python'><xmp>
a = 10
def function():
print(a)
function()
print(a)
</xmp></code></pre></div>
<div class='reveal'><pre title='main.py'><code class='python'><xmp>
import file
a = 5
file.function()
print(a)
</xmp></code></pre></div>
</section>
<section><h2>Class scope</h2>
<div class='reveal'><pre><code class='python'><xmp>
class date:
year = 1900
month = 1
day = 1
def as_iso(self):
return str(self.year) + "-" + str(self.month) + "-" + str(self.day)
def is_valid(self):
# ...
return false
today = date()
today.year, today.month, today.day = 2018, 8, 13
print (today.as_iso())
</xmp></code></pre></div>
</section>
<section><h2>Initializer</h2>
<div class='reveal'><pre><code class='python'><xmp>
class date:
def __init__(self, y=1900, m=1, d=1):
self.year = y
self.month = m
self.day = d
def as_iso(self):
return str(self.year) + "-" + str(self.month) + "-" + str(self.day)
def is_valid(self):
# ...
return False
today = date(2018, 8, 13)
print(today.as_iso())
</xmp></code></pre></div>
</section>
<section><h1>Unit Tests</h1>
</section>
<section><h2>get the code</h2>
<p>https://help.github.com/articles/syncing-a-fork/</p>
</section>
<section>
<h1>Thank You!</h1>
<p>Fabio Fracassi
<br/>fabio.fracassi@code.berlin</p>
</section>
<!--============================================================================================-->
<style type="text/css">
/*-----------------------------------------------------------------------------------------------*/
/* Color Theme */
:root { --text-color: #1a2a2a;
--text-color-sub: #5a6a6a;
/*--background: linear-gradient(#c8c4c8, #e8e4e8);*/
--background: linear-gradient(#e8e4e8, #f2e8f2);
--highlight-color: rgba(133, 45, 133, 0.5);
--title-background: linear-gradient(rgba(112, 21, 112, 1.0), #250025);
--title-text-color: white;
--code-text-color: #536466;
--code-keyword-color: #533436;
--code-string-color: #43a456;
--code-comment-color: #93a4a6;
--code-background: #fdf6e3;
--code-background2: #ede6d3;
--border-text-color: white;
--border-background: black;
--normal-font: 'Roboto', Verdana, sans-serif;
--code-font: 'Monoid', 'Courier New', monospace;
/*--load-font: "Roboto:300italic,300,400italic,400";*/
}
/*-----------------------------------------------------------------------------------------------*/
/*
CodeSlides (c) 2017,2018 Fabio Fracassi
*/
/*-----------------------------------------------------------------------------------------------*/
/* Style */
* { box-sizing: border-box; }
html { margin: 0; padding: 0; overflow: hidden;
font-family: var(--normal-font);
font-weight: 300;
}
body { background: var(--background); color: var(--text-color);
margin: 0; padding: 0; overflow: hidden;
display: flex; flex-direction: column;
}
header { background: var(--border-background); color: var(--border-text-color);
width: 100%; order: 1; padding-left: 10px; padding-right: 10px;
}
#canvas { order:2; width: 100vw; height: 100vh;
overflow-y: scroll; overflow-x: hidden; padding: 0%; margin: 0; font-size: 2em;
}
footer { background: var(--border-background); color: var(--border-text-color);
position: absolute; bottom: 0; width: 100%; padding-left: 10px; padding-right: 10px;
flex-direction: row; display: flex;
}
#__internal_footer_text
{ text-align: left; order: 1; flex-grow: 4; }
#__internal_time
{ text-align: right; order: 2; flex-grow: 1; }
#__internal_page_number
{ text-align: right; order: 3; flex-grow: 1; }
/*************************************************************************************************/
h1 { background: var(--title-background); color: var(--title-text-color);
height: 45%; padding: 2%; margin: 0; margin-bottom: 1%;
display: flex; justify-content: flex-end; align-items: flex-end;
font-size: 3em;
}
h2 { background: var(--title-background); color: var(--title-text-color);
height: 20%; padding: 2%; margin: 0; margin-bottom: 1%;
display: flex; justify-content: flex-start; align-items: flex-end;
font-size: 2em;
}
h3, h4 { background: var(--title-background); color: var(--title-text-color); margin: 0; }
/*************************************************************************************************/
ul, ol { list-style-position: inside; padding-left: 3%; }
li.hidden { visibility: hidden; }
li.shadow { color: var(--text-color-sub); }
li.current { color: var(--text-color); }
/* fix for chrome */
ul { list-style-type: none; }
ul > li { position: relative; padding-left: 1.5em; }
ul > li:before { content: "•"; position: absolute; left: 0; }
/* end fix for chrome */
p { padding-left: 2%; width: 100%; }
p.code-note { margin: 2%; margin-top: 0; width: 96%;
background-color: white; border-radius: 5px; z-index: 2;
}
p.small { margin: 0; width: 100%; }
span { width: 100%; }
.quote { background-color: #f6f6f6; border-radius: 5px; }
img { margin: 2%; border-radius: 5px; width: 94% }
div.reveal { margin: 0; padding: 0; width: 100%; }
div.fp > p.code-note
{ margin: 2%; margin-top: -3%; width: 96%;
background-color: white; border-radius: 5px; z-index: 2;
}
table { width: 96%; }
th { background: var(--title-background); color: var(--title-text-color);
}
td { background-color: white;
}
/*************************************************************************************************/
span.__ch_ln { font-size: 0.65em;
margin: -10px 5px 0 -15px;
padding: 0; padding-right: 15px;
background-color: var(--code-background2);
border-color: var(--code-background2);
color: var(--code-comment-color);
border-style: solid;
border-width: 10px 0 25px 25px;
position: relative;
user-select: none;
cursor: default;
}
[data-chln]::after {
content: attr(data-chln);
}
span.__ch_comment
{ color: var(--code-comment-color);
}
span.__ch_string
{ color: var(--code-string-color);
}
span.__ch_keyword
{ color: var(--code-keyword-color);
font-weight: bold;
}
span.__ch_preproc
{ color: #533436;
}
span.__ch_preproc_param
{ color: #333436;
}
span.__ch_inc_file
{ color: var(--code-string-color);
}
/*************************************************************************************************/
legend { visibility: hidden; position: fixed; }
.__internal_code
{ margin-left: 0px; margin-right:3px; margin-bottom: 3px;
background-color: var(--code-background); color: var(--code-text-color);
border-color: var(--text-color); border-style: solid;
border-width: 1px; border-radius: 5px;
/*font-family: var(--code-font);*/
height: 93%; /* I don't know why this works but the correct calc(100% - 1.5em -3px) doesn't */
overflow:auto;
}
pre { font-size: 0.5em;
margin-top: 0px; margin-left: 2%; margin-right:2%;
padding-top: 3px; padding-left: 5px; padding-bottom: 3px; padding-right: 5px;
border-radius: 5px; /*border-color: #206020; border-style: solid; border-width: 1px;*/
/*padding: 2px; */
transition: all 0.5s ease;
}
pre > * { font-size: 2em; padding:5px; }
pre#zoomed { position: absolute; left:3%; top:3%; width: 94%; height: 94%;
font-size: 0.75em;
transition: all 0.5s ease;
}
pre code {}
var { font-family: monospace; font-style: normal; }
xmp { margin: 0; }
.side-by-side { display: flex; flex-direction: row; margin-left: 1%; margin-right: 1%; width: 94vw; }
.side-by-side > * { flex-grow: 1; flex-shrink: 1; flex-basis: 40%; width: 50%;}
.do { background-color: #ddf6dd;
margin-top: 0; margin-bottom: 0; margin-left: 1%; margin-right: 1%;
border-color: #206020; color: #206020; border-style: solid; border-width: 1px;
}
.donot { background-color: #f6dddd;
margin-top: 0; margin-bottom: 0; margin-left: 1%; margin-right: 1%;
border-color: #602020; color: #602020; border-style: solid; border-width: 1px;
}
.__internal_normal_code
{ background-color: #c2c2c2; border-color: var(--text-color); color: var(--text-color);
border-style: solid; border-width: 1px;
}
#__internal_code_highlight_line
{ position: absolute; z-index: 1; height: 1em;
background-color: var(--highlight-color); border-radius: 5px;
}
/*************************************************************************************************/
#__internal_ruler
{ position: fixed; visibility: hidden; height: auto; width: auto; white-space: nowrap;
}
</style>
<!--============================================================================================-->
<noscript>
<h1>CodeSlides needs Javascript to work!</h1>
</noscript>
<script type="text/javascript">
//--- Compatibility bullshit ------------------------------------------------------------------
function isFullScreen() {
return ( document.fullScreenElement && document.fullScreenElement !== null)
|| document.mozFullScreen || document.webkitIsFullScreen;
}
function requestFullScreen(element) {
if (element.requestFullscreen) element.requestFullscreen();
else if (element.msRequestFullscreen) element.msRequestFullscreen();
else if (element.mozRequestFullScreen) element.mozRequestFullScreen();
else if (element.webkitRequestFullscreen) element.webkitRequestFullscreen();
}
function exitFullScreen() {
if (document.exitFullscreen) document.exitFullscreen();
else if (document.msExitFullscreen) document.msExitFullscreen();
else if (document.mozCancelFullScreen) document.mozCancelFullScreen();
else if (document.webkitExitFullscreen) document.webkitExitFullscreen();
}
//---------------------------------------------------------------------------------------------
// html helpers
function toggleFullScreen(element) {
if (isFullScreen()) { exitFullScreen(); }
else { requestFullScreen(element || document.documentElement); }
}
function createDocument(html, title) {
var doc = document.implementation.createHTMLDocument(title);
doc.body.innerHTML = html;
return doc;
}
function addElement(to, id, type='div') {
var e = document.createElement(type);
if(id) { e.id = id; }
to.appendChild(e);
return e;
}
function addNewElement(to, id, type='div') {
let e = document.getElementById(id);
if(!e) { e = addElement(to, id, type); }
return e;
}
function escapeHTML(string) {
var pre = document.createElement('pre');
var text = document.createTextNode(string);
pre.appendChild(text);
return pre.innerHTML;
}
function escapeCode(string) {
let s = string
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
//console.log("orig = \n", string, "\nesc = \n", s);
return s;
}
function getAttrib(from, name) {
if(from && from.hasAttributes()) {
var attrs = from.attributes;
for(var i = 0; i < attrs.length; ++i) {
if(attrs[i].name == name) { return attrs[i].value; }
}
}
return null;
}
function findParentTag(s, tn) {
let p = s.parentNode;
while(p.tagName != tn.toUpperCase()) { p = p.parentNode; }
return p;
}
function clamp(min, v, max) {
return Math.min(Math.max(min, v), max);
}
function rjust(i, n, f = ' ') {
console.assert(f.length == 1, "fill char has to be exactly one character");
let s = i.toString();
return f.repeat(Math.max(0, n - s.length)) + s;
}
//---------------------------------------------------------------------------------------------
// app specific helpers
function isEmptyLine(line) { return (!line || /^\s*$/.test(line)); }
//---------------------------------------------------------------------------------------------
const _Reveal_ListItem = 1;
const _Highlight_Code = 2;
const _Reveal_CustomTag = 3;
const _Reveal_CodePart = 4;
function parseCode(code) {
function lineNo(show, no) {
if(!show) { return; }
return "<span class='__ch_ln'>" + rjust(no + 1, 2) + "</span>";
}
function tokenize(input, lang) {
// this does not do real tokenizing, it just does a rough splitting and a first pass
// of identifying what kind of token we have.
// it is also probably the worst way to tokenize
function make_token(content, type) { return { content: content, type: type }; }
function split(s, at) {
let p = s.search(at);
return { head: s.substring(0, p), tail: s.substring(p) };
}
let r = [];
let p = { head: '', tail: input };
let tokenCount = 0; // safety, prevent us from running forever in case
let tokenLimit = input.length+1; // we can't handle the input correctly
while(p.tail != '' && tokenCount < tokenLimit) {
++tokenCount;
p = split(p.tail, /\S|$/);
p.head .split(/(\n)/)
.filter( function(t) { return t != '';} )
.forEach( function(t) { r.push(make_token(t, /\n/.test(t)
? 'newline' : 'whitespace'));
}
)
;
// Todo: Fix comments/string that do not have whitespace before their init token!
if (p.tail.startsWith('//')) {
p.tail = p.tail.substring(2);
p = split(p.tail, /\n|\/\//);
p.head = '//' + p.head;
r.push(make_token(p.head, 'comment'));
continue;
}
// Todo: multi-line comments, strings and recursive comments
if (p.tail.startsWith('/*')) {
p = split(p.tail, /\*\//);
p.tail = p.tail.substring(2);
r.push(make_token(p.head + '*/', 'comment'));
continue;
}
if (p.tail.startsWith('"')) {
p.tail = p.tail.substring(1);
p = split(p.tail, /"/);
p.head = '"' + p.head + '"';
p.tail = p.tail.substring(1);
r.push(make_token(p.head, 'string'));
continue;
}
p = split(p.tail, /\s|$/);
p.head .split(/([^A-Za-z0-9_]+)/)
.filter( function(t) { return t != '';} )
.forEach( function(t) { r.push(make_token(t, /[^A-Za-z0-9_]+/.test(t)
? 'special' : 'name'));
}
)
;
}
console.assert(tokenCount < tokenLimit, "tokenizer failed to yield sensible results");
return r;
}
function indexOf(m, v, arr, start) {
for (let i = start; i < arr.length; ++i) { if (arr[i][m] === v) { return i; } }
return -1;
}
function adjustIndent(tok) {
function relevantWhitespaceLength(tp) {
if (tok[tp].type == 'whitespace') {
if (tp < tok.length - 1 && tok[tp + 1].type != 'newline') { return tok[tp].content.length; }
return Number.MAX_SAFE_INTEGER;
}
if (tok[tp].type != 'newline') { return 0; }
return Number.MAX_SAFE_INTEGER;
}
let tp = 0;
let min_indent = Number.MAX_SAFE_INTEGER;
do {
tp = indexOf('type', 'newline', tok, tp);
if (tp == -1 || tp >= tok.length - 1) { break; } ++tp;
min_indent = Math.min(min_indent, relevantWhitespaceLength(tp));
} while (tp != -1 || t >= tok.length);
tp = 0;
do {
tp = indexOf('type', 'newline', tok, tp);
if (tp == -1 || tp >= tok.length - 1) { break; } ++tp;
if (tok[tp].type == 'whitespace') {
tok[tp].content = tok[tp].content.substring(min_indent);
}
} while (tp != -1 || t >= tok.length);
return tok;
}
function isWS(tok) { return tok.type == 'whitespace'; }
function isNl(tok) { return tok.type == 'newline'; }
function isWsOrNl(tok) { return isWS(tok) || isNl(tok); }
function trimEmptyLines(tok) {
while(isWsOrNl(tok[tok.length-1])) { tok.pop(); }
while(isWsOrNl(tok[0]) && isWsOrNl(tok[1])) { tok.shift(); }
}
function styleCode(tok, lang, showLineNos = false) {
function processCpp(tok) {
const keywords = ["alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", "compl", "concept", "const", "constexpr", "const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", "goto", "if", "import", "inline", "int", "long", "module", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", " wchar_t", " while", " xor", " xor_eq" ];
console.log("Processing cpp code", tok);
for (let i = 0; i < tok.length; ++i) {
function next(x) { return Math.min(x + 1, tok.length - 1); }
if(tok[i].type == 'special' && tok[i].content == '#') {
let j = next(i);
if(tok[j].type == 'whitespace') { j = next(j); }
if(tok[j].type == 'name') {
for(let k = i; k <= j; ++k) {
tok[k].type = 'preproc';
}
i = j;
j = next(i);
let inc = tok[i].content == 'include';
console.log("i,j = ", i, j, tok[i], tok[j]);
while(j < tok.length && tok[j].type != 'newline' && tok[j].type != 'comment') { // TODO: multiline macro
if(tok[j].type != 'whitespace') { tok[j].type = inc ? 'inc_file' : 'preproc_param'; }
++j;
}
}
}
if(tok[i].type == 'name' && keywords.includes(tok[i].content)) { tok[i].type = 'keyword'; }
}
return tok;
}
if(lang == 'c++') { tok = processCpp(tok); }
let ln = 2; // we remove leading newlines, so we need to manually add the first line number
if(showLineNos) { tok.unshift({type: 'dummy', hlPre: "<span class='__ch_ln' data-chln=' 1'>", hlPost: "</span>", alt: " "}); }
let openRevealBlock = false;
for (let i = 0; i < tok.length; ++i) {
if (tok[i].type == 'newline') {
if(showLineNos) {
tok[i].hlPre = "<span class='__ch_ln' data-chln='" + rjust(ln, 2) + "'>"; tok[i].hlPost = "</span>";
tok[i].alt = " "; // if we leave this empty the formatting goes haywire
}
++ln;
}
const simpleTok = ['comment', 'string', 'keyword', 'preproc', 'preproc_param', 'inc_file'];
simpleTok.forEach(function(type) {
if (tok[i].type == type) {
tok[i].hlPre = "<span class='__ch_" + type + "'>"; tok[i].hlPost = "</span>";
}
});
if(tok[i].type == 'reveal') {
if(openRevealBlock) { tok[i].hlPre = "</span>"; }
tok[i].hlPost = "<span class='__c_reveal'>";
openRevealBlock = true;
}
}
if (openRevealBlock) {
if(tok[tok.length-1].hlPost) { tok[tok.length-1].hlPost += "</span>"; }
else { tok[tok.length-1].hlPost = "</span>"; }
}
return tok;
}
function calcHighlights(tok, ci) {
let r = { tok: tok, hl: [] };
let cln = 0;
let ao = 0;
for(let i = 0; i < tok.length; /* empty because sometimes we remove items */ ) {
if(tok[i].type == 'newline') { ++cln; }
if(tok[i].type == 'comment') {
let hlind = /\((\-+\*|\*|\d+)\)/.exec(tok[i].content);
if(hlind) {
if(hlind[1] == '----*') {
r.hl.push({"box": ci, "type": _Reveal_CodePart });
tok[i] = { type: 'reveal' };
} else {
if(hlind[1] != '*') { ao = hlind[1]; }
r.hl.push({"box": ci, "line": cln, "type": _Highlight_Code, "order": ao++ });
tok.splice(i, 1);
continue;
}
}
}
++i;
}
function compare(lhs, rhs) { return lhs < rhs ? -1 : lhs > rhs ? 1 : 0}
r.hl.sort(function(lhs,rhs) { return compare(lhs.order, rhs.order); });
return r;
}
let r = { hl: [],
processed: ""
};
console.log("-----------------------------");
if(code) {
if (code.length != 0) { r.hl.push({"box": 0, "line": "none"}); }
for(let ci = 0; ci < code.length; ++ci) {
console.log("Parsing code view #", ci);
let cic = code.item(ci);
let lang = getAttrib(cic, 'class');
let showLineNos = lang == 'c++';
let xmp = cic.getElementsByTagName('xmp').item(0);
let codeText = (xmp ? xmp : cic).innerHTML.toString();
console.log(codeText);