-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
2111 lines (1511 loc) · 93.1 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Computational Nonsense]]></title>
<link href="https://superkerokero.github.io/atom.xml" rel="self"/>
<link href="https://superkerokero.github.io/"/>
<updated>2018-01-08T00:19:45+09:00</updated>
<id>https://superkerokero.github.io/</id>
<author>
<name><![CDATA[]]></name>
</author>
<generator uri="http://www.mweb.im/">MWeb</generator>
<entry>
<title type="html"><![CDATA[Notes on numerically solving diffusion equations.]]></title>
<link href="https://superkerokero.github.io/15153040186127.html"/>
<updated>2018-01-07T14:46:58+09:00</updated>
<id>https://superkerokero.github.io/15153040186127.html</id>
<content type="html"><![CDATA[
<blockquote>
<p>Many phenomena in various science fields are mathematically expressed by using the well-known evolution equations. The diffusion equation is one of them, and mathematically corresponds to the Markov process in relation to the normal distribution rule.<br/>
<div style="text-align: right"> <cite> **—<a href="https://en.wikipedia.org/wiki/Diffusion_equation">Wikipedia: Diffusion equation</a></cite> </div></p>
</blockquote>
<span id="more"></span><!-- more -->
<h2 id="toc_0">A. Standard diffusion equation</h2>
<p>Standard diffusion equation is a simplistic second order partial differential equation written as:</p>
<p>\[<br/>
\begin{equation}<br/>
\frac{\partial \rho(x, t)}{\partial t}<br/>
= D \frac{\partial^2 \rho(x, t)}{\partial x^2}<br/>
\end{equation}<br/>
\]</p>
<h3 id="toc_1">A1. Finite difference method for time and space</h3>
<p>Finite difference of time:</p>
<p>\begin{equation}<br/>
\frac{\partial\rho}{\partial t} (x_i,t_j) = \frac{\rho(x_i, t_j + \Delta t) - \rho(x_i, t_j)}{\Delta t} + \mathcal{O}(\Delta t)<br/>
\end{equation}</p>
<p>Finite difference of space:</p>
<p>\[<br/>
\begin{equation}<br/>
\frac{\partial^2\rho}{\partial x^2} (x_i,t_j) = \frac{\rho(x_i+\Delta x,t_j) - 2\rho(x_i,t_j) + \rho(x_i-\Delta x,t_j)}{dx^2} + \mathcal{O}(\Delta x^2)<br/>
\end{equation}<br/>
\]</p>
<h3 id="toc_2">A2. Explicit scheme (forward time)</h3>
<p>Apply equation(2) and (3) to equation(1), and let \(\rho_{i,j} = \rho(x_i,t_j)\), we get the expression of forward time evolution</p>
<p>\[<br/>
\begin{align}<br/>
&\ \frac{\rho_{i,j+1} - \rho_{i,j}}{\Delta t} = D \frac{\rho_{i+1,j} - 2\rho_{i,j} + \rho_{i-1,j}}{\Delta x^2} \\<br/>
&\Rightarrow \rho_{i,j+1} = (1 - 2\lambda) \rho_{i,j} + \lambda (\rho_{i+1,j} + \rho_{i-1, j})<br/>
\end{align}<br/>
\]</p>
<p>where \[\lambda=\frac{D \cdot \Delta t}{\Delta x^2}\].<br/>
Rewrite this in matrix form, </p>
<p>\[<br/>
\begin{equation}<br/>
\vec{\rho}_{i, j+1} = A \vec{\rho}_{i, j} + R(\text{accounts for boundary conditions})<br/>
\end{equation}<br/>
\]</p>
<p>Supplemented by initial condition: \( \rho_{i,0} = \rho(x_i)\),<br/>
and Boundary conditions: \( \rho_{0,j} = \rho_0\), \( \rho_{m,j} = \rho_m\), we can solve it like this:</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.04.40.png" alt="Screenshot 2018-01-07 15.04.40"/></p>
<p>*Sovling an example system with initial value of \(\rho_{center}=1.0\) at center and boundary conditions with \(\rho_L = \rho_R = 0\).</p>
<p><img src="media/15153040186127/forward.gif" alt="forward" title="Solving standard diffusion equation using forward method"/></p>
<h3 id="toc_3">A3. Implicit scheme (backward time)</h3>
<p>In implicit scheme, time evolves backwards.</p>
<p>\[<br/>
\begin{align}<br/>
&\ \frac{\rho_{i,j} - \rho_{i,j-1}}{\Delta t} = D \frac{\rho_{i+1,j} - 2\rho_{i,j} + \rho_{i-1,j}}{\Delta x^2} \\<br/>
&\Rightarrow (1 + 2\lambda) \rho_{i,j} - \lambda (\rho_{i+1,j} + \rho_{i-1, j}) = \rho_{i,j-1}<br/>
\end{align}<br/>
\]</p>
<p>where \[\lambda=\frac{D \cdot \Delta t}{\Delta x^2}.\]<br/>
Rewrite this in matrix form, </p>
<p>\[<br/>
\begin{equation}<br/>
A \vec{\rho}_{i, j} - R = \vec{\rho}_{i, j - 1}<br/>
\end{equation}<br/>
\]</p>
<p>Supplemented by initial condition: \( \rho_{i,0} = \rho(x_i)\),<br/>
and Boundary conditions: \( \rho_{0,j} = \rho_0\), \( \rho_{m,j} = \rho_m\), we can solve it like this:</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.18.52.png" alt="Screenshot 2018-01-07 15.18.52"/></p>
<p>*Sovling an example system with initial value of \(\rho_{center}=1.0\) at center and boundary conditions with \(\rho_L = \rho_R = 0\).</p>
<p><img src="media/15153040186127/backward.gif" alt="backward" title="Solving standard diffusion equation using backward method"/></p>
<h3 id="toc_4">A4. Crank-Nicolson scheme</h3>
<p>In Crank-Nicolson scheme, we take the average of explicit scheme and implicit scheme.</p>
<p>\[<br/>
\begin{align}<br/>
&\ \frac{\rho_{i,j + 1} - \rho_{i,j}}{\Delta t} = \frac{D}{2} ( \frac{\rho_{i+1,j} - 2\rho_{i,j} + \rho_{i-1,j}}{\Delta x^2} + \frac{\rho_{i + 1,j + 1} - 2\rho_{i,j + 1} + \rho_{i - 1,j + 1}}{\Delta x^2} ) \\<br/>
&\Rightarrow (2 + 2\lambda) \rho_{i,j + 1} - \lambda (\rho_{i+1,j + 1} + \rho_{i-1, j + 1}) = (2 - 2\lambda) \rho_{i,j} + \lambda (\rho_{i+1,j} + \rho_{i-1, j})<br/>
\end{align}<br/>
\]</p>
<p>where \[\lambda=\frac{D \cdot \Delta t}{\Delta x^2}\].<br/>
Rewrite this in matrix form, </p>
<p>\[<br/>
\begin{equation}<br/>
A \vec{\rho}_{i, j + 1} - R = B \vec{\rho}_{i, j} + R<br/>
\end{equation}<br/>
\]</p>
<p>Supplemented by initial condition: \( \rho_{i,0} = \rho(x_i)\),<br/>
and Boundary conditions: \( \rho_{0,j} = \rho_0\), \( \rho_{m,j} = \rho_m\), we can solve it like this:</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.25.12.png" alt="Screenshot 2018-01-07 15.25.12"/></p>
<p>Solvlng an example system with initial value of \(\rho_{center}=1.0\) at center and boundary conditions with \(\rho_L = \rho_R = 0\).</p>
<p><img src="media/15153040186127/crank-nicolson.gif" alt="crank-nicolson"/></p>
<h2 id="toc_5">B. Solving diffusion equation with a potential profile.</h2>
<p>\[<br/>
\begin{equation}<br/>
\frac{\partial \rho(x, t)}{\partial t}<br/>
= D \frac{\partial}{\partial x}[ \frac{\partial \rho(x, t)}{\partial x} + \frac{1}{k_BT} \frac{\partial U(x)}{\partial x}\rho(x,t)] = D \frac{\partial^2 \rho(x, t)}{\partial x^2} + \frac{D}{k_BT} \cdot U^{\prime}(x) \cdot \frac{\partial \rho(x, t)}{\partial x} + \frac{D}{k_BT} U^{\prime \prime}(x) \rho(x, t)<br/>
\end{equation}<br/>
\]</p>
<p>The finite difference in Crank-Nicolson scheme for the above equation is:</p>
<p>\[<br/>
\begin{align}<br/>
&\ \frac{\rho_{i,j + 1} - \rho_{i,j}}{\Delta t} = \frac{D}{2} ( \frac{\rho_{i+1,j} - 2\rho_{i,j} + \rho_{i-1,j}}{\Delta x^2} + \frac{\rho_{i + 1,j + 1} - 2\rho_{i,j + 1} + \rho_{i - 1,j + 1}}{\Delta x^2} ) + \frac{D \cdot U^{\prime}(x_i)}{2 k_BT} (\frac{\rho_{i + 1,j + 1} - \rho_{i - 1,j + 1}}{2 \Delta x} + \frac{\rho_{i + 1,j} - \rho_{i - 1, j}}{2 \Delta x}) + \frac{D \cdot U^{\prime \prime}(x_i)}{2 k_BT} (\rho_{i,j + 1} + \rho_{i,j}) \\<br/>
&\Rightarrow (2 + 2\lambda + \tau_i) \rho_{i,j + 1} + (\eta_i - \lambda)\rho_{i+1,j + 1} - (\lambda + \eta_i) \rho_{i-1, j + 1} = (2 - 2\lambda - \tau_i) \rho_{i,j} + (\lambda - \eta_i) \rho_{i+1,j} + (\lambda + \eta_i) \rho_{i-1, j}<br/>
\end{align}<br/>
\]</p>
<p>where \(\lambda=\frac{D \cdot \Delta t}{\Delta x^2}\), \(\eta_i = - \frac{D \cdot \Delta t}{2 k_BT \Delta x} \cdot U^{\prime}(x_i)\), \(\tau_i = - \frac{D \cdot \Delta t}{k_BT} \cdot U^{\prime \prime}(x_i)\).</p>
<p>Rewrite this in matrix form, </p>
<p>\[<br/>
\begin{equation}<br/>
A \vec{\rho}_{i, j + 1} - R = B \vec{\rho}_{i, j} + R<br/>
\end{equation}<br/>
\]</p>
<p>Supplemented by initial condition: \( \rho_{i,0} = \rho(x_i)\),<br/>
and Boundary conditions: \( \rho_{0,j} = \rho_0\), \( \rho_{m,j} = \rho_m\), we can solve it like this:</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.28.00.png" alt="Screenshot 2018-01-07 15.28.00"/></p>
<p>Solving an example system with initial value of \(\rho=1.0\) at \(x=30 \unicode{x212B}\) and boundary conditions with \(\rho_L = \rho_R = 0\) using the potential profile of \(n_w = 0\).</p>
<p><img src="media/15153040186127/potential-crank-nicolson.gif" alt="potential-crank-nicolson"/></p>
<p>Potential used in this example and its derivatives are as follows:</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.33.08.png" alt="Screenshot 2018-01-07 15.33.08"/><br/>
<img src="media/15153040186127/Screenshot%202018-01-07%2015.33.23.png" alt="Screenshot 2018-01-07 15.33.23"/><br/>
<img src="media/15153040186127/Screenshot%202018-01-07%2015.33.34.png" alt="Screenshot 2018-01-07 15.33.34"/></p>
<h2 id="toc_6">C. Solving coupled diffusion equations with potential profiles and reactions.</h2>
<p>When reaction terms are added to the equation(16), equations corresponding to different \(n_w\) become coupled equations: </p>
<p>\[<br/>
\begin{equation}<br/>
\begin{cases}<br/>
\frac{\partial \rho_0(x, t)}{\partial t}<br/>
= D \frac{\partial}{\partial x}[ \frac{\partial \rho_0(x, t)}{\partial x} + \frac{1}{k_BT} \frac{\partial U_0(x)}{\partial x}\rho_0(x, t)] - [k_{0 \to 1} + k_0^{wf}(x)]\rho_0(x, t) + k_{1 \to 0}\rho_1(x, t) \\<br/>
\frac{\partial \rho_1(x, t)}{\partial t}<br/>
= D \frac{\partial}{\partial x}[ \frac{\partial \rho_1(x, t)}{\partial x} + \frac{1}{k_BT} \frac{\partial U_1(x)}{\partial x}\rho_1(x, t)] - [k_{1 \to 2} + k_{1 \to 0} + k_1^{wf}(x)]\rho_1(x, t) + k_{2 \to 1}\rho_2(x, t) + k_{0 \to 1}\rho_0(x, t) \\<br/>
\frac{\partial \rho_2(x, t)}{\partial t}<br/>
= D \frac{\partial}{\partial x}[ \frac{\partial \rho_2(x, t)}{\partial x} + \frac{1}{k_BT} \frac{\partial U_2(x)}{\partial x}\rho_2(x, t)] - [k_{2 \to 3} + k_{2 \to 1} + k_2^{wf}(x)]\rho_2(x, t) + k_{3 \to 2}\rho_3(x, t) + k_{1 \to 2}\rho_1(x, t) \\<br/>
\vdots \\<br/>
\vdots \\<br/>
\frac{\partial \rho_{n-1}(x, t)}{\partial t}<br/>
= D \frac{\partial}{\partial x}[ \frac{\partial \rho_{n-1}(x, t)}{\partial x} + \frac{1}{k_BT} \frac{\partial U_{n-1}(x)}{\partial x}\rho_{n-1}(x, t)] - [k_{n-1 \to n} + k_{n-1 \to n-2} + k_{n-1}^{wf}(x)]\rho_{n-1}(x, t) + k_{n \to n-1}\rho_n(x, t) + k_{n-2 \to n-1}\rho_{n-2}(x, t) \\<br/>
\frac{\partial \rho_n(x, t)}{\partial t}<br/>
= D \frac{\partial}{\partial x}[ \frac{\partial \rho_n(x, t)}{\partial x} + \frac{1}{k_BT} \frac{\partial U_n(x)}{\partial x}\rho_n(x, t)] - [k_{n \to n-1} + k_n^{wf}(x)]\rho_n(x, t) + k_{n-1 \to n}\rho_{n-1}(x, t)<br/>
\end{cases}<br/>
\end{equation}<br/>
\]</p>
<p>Let \(\rho_{i,j}^k = \rho_k(x_i,t_j)\), the finite difference in Crank-Nicolson scheme for \(n_w = k\) is:</p>
<p>\[<br/>
\begin{align}<br/>
&\ \frac{\rho_{i,j + 1}^k - \rho_{i,j}^k}{\Delta t} = \frac{D}{2} ( \frac{\rho_{i+1,j}^k - 2\rho_{i,j}^k + \rho_{i-1,j}}{\Delta x^2} + \frac{\rho_{i + 1,j + 1} - 2\rho_{i,j + 1} + \rho_{i - 1,j + 1}}{\Delta x^2} ) + \frac{D \cdot U_k^{\prime}(x_i)}{2 k_BT} (\frac{\rho_{i + 1,j + 1}^k - \rho_{i - 1,j + 1}^k}{2 \Delta x} + \frac{\rho_{i + 1,j}^k - \rho_{i - 1, j}^k}{2 \Delta x}) + \frac{D \cdot U_k^{\prime \prime}(x_i)}{2 k_BT} (\rho_{i,j + 1}^k + \rho_{i,j}^k) - \frac{k_{k \to k-1} + k_{k \to k+1} + k_k^{wf}(x_i)}{2} (\rho_{i,j+1}^k + \rho_{i,j}^k) + \frac{k_{k-1 \to k}}{2} (\rho_{i,j+1}^{k-1} + \rho_{i,j}^{k-1}) + \frac{k_{k+1 \to k}}{2} (\rho_{i,j+1}^{k+1} + \rho_{i,j}^{k+1})\\<br/>
&\Rightarrow (2 + 2\lambda + \tau_i^k) \rho_{i,j + 1}^k + (\eta_i - \lambda)\rho_{i+1,j + 1}^k - (\lambda + \eta_i^k) \rho_{i-1, j + 1}^k + \xi_i^k \rho_{i,j + 1}^k - \zeta^k \rho_{i,j + 1}^{k+1} - \kappa^k \rho_{i,j + 1}^{k-1} = (2 - 2\lambda - \tau_i^k) \rho_{i,j}^k + (\lambda - \eta_i^k) \rho_{i+1,j}^k + (\lambda + \eta_i^k) \rho_{i-1, j}^k - \xi_i^k \rho_{i,j + 1}^k + \zeta^k \rho_{i,j + 1}^{k+1} + \kappa^k \rho_{i,j + 1}^{k-1}<br/>
\end{align}<br/>
\]</p>
<p>where </p>
<p>\[<br/>
\begin{align}<br/>
&\lambda=\frac{D \cdot \Delta t}{\Delta x^2} \\<br/>
&\eta_i^k = - \frac{D \cdot \Delta t}{2 k_BT \Delta x} \cdot U_k^{\prime}(x_i) \\<br/>
&\tau_i^k = - \frac{D \cdot \Delta t}{k_BT} \cdot U_k^{\prime \prime}(x_i) \\<br/>
&\xi_i^k = (k_{k \to k+1} + k_{k \to k-1} + k_k^{wf}(x_i)) \Delta t \\<br/>
&\zeta^k = k_{k+1 \to k} \Delta t \\<br/>
&\kappa^k = k_{k-1 \to k} \Delta t<br/>
\end{align}<br/>
\]</p>
<p>We further define following \((m-1) \times (m-1)\) matrices:</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.38.20.png" alt="Screenshot 2018-01-07 15.38.20"/></p>
<p>Rewrite in matrix form,</p>
<p><img src="media/15153040186127/Screenshot%202018-01-07%2015.38.29.png" alt="Screenshot 2018-01-07 15.38.29"/></p>
<p>Solving an example system with initial value of \(\rho=1.0\) at \(x=40 \unicode{x212B}\) for all \(n_w=0:9\) and boundary conditions with \(\rho_L = \rho_R = 0\) using the potential profile of \(n_w = 0\).<br/>
<img src="media/15153040186127/coupled.gif" alt="coupled"/></p>
<p>The above example calculations are produced using Julia 6.0 with matplotlib. You can find the example jupyter notebook file <a href="http://nbviewer.jupyter.org/gist/superkerokero/e5340ecacefe20d11815010a22ee2799">HERE</a>.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Notes on reverse SSH tunnel: Connect to servers behind NATs and firewalls through publicly available middle server]]></title>
<link href="https://superkerokero.github.io/15152601961161.html"/>
<updated>2018-01-07T02:36:36+09:00</updated>
<id>https://superkerokero.github.io/15152601961161.html</id>
<content type="html"><![CDATA[
<p>In some cases, the servers you use in office/school may be behind some NAT or firewall so that you can not access these servers from the external internet. This can be inconvenient sometimes. A simple walkaround of this problem is reverse SSH tunneling. For all Unix like systems(e.g. Linux, MacOS), this just works fine.</p>
<span id="more"></span><!-- more -->
<h2 id="toc_0">How to establish reverse SSH tunnel</h2>
<p>The whole connection route will look like this:</p>
<p><strong>Destination Server <- |NAT| <- Public Server <- Your PC</strong></p>
<p>In order to establish a reverse SSH tunnel, first you need to forward an unused port(e.g. 19999) from the destination server to the public server:</p>
<pre><code class="language-bash">ssh -R 19999:localhost:22 user@pubic_server
</code></pre>
<p>Then from your PC, SSH to the public server first:</p>
<pre><code class="language-bash">ssh user@public_server
</code></pre>
<p>Then from the public server, do the following to SSH to the destination server:</p>
<pre><code class="language-bash">ssh localhost -p 19999
</code></pre>
<p>Now you have successfully gain access to the destination server from your PC(from external Internet)!</p>
<h2 id="toc_1">Make the reverse SSH tunnel stable</h2>
<p>This tunnel can be unstable for long terms(some times the port forwarding can fail). In order to make it more reliable, we make use of linux system's cron utility.</p>
<p>First we make a configuration for the public server by adding the following to the "~/.ssh/config" file:</p>
<pre><code class="language-bash">Host public_server
ServerAliveInterval 60
ExitOnForwardFailure yes
TCPKeepAlive no
</code></pre>
<p>Then restart sshd service to make the configuration effective:</p>
<pre><code class="language-bash">sudo service sshd restart
</code></pre>
<p>Now make a script file like this(make sure you can SSH to public_server without password using ssh keys):</p>
<pre><code class="language-bash"># reverse_ssh_tunnel.sh
#!/bin/sh
COMMAND="ssh -N -f -R 19999:localhost:22 user@public_server"
pgrep -f -x "$COMMAND" > /dev/null 2>&1 || $COMMAND
</code></pre>
<p>Then add a new crontab by the following command:</p>
<pre><code class="language-bash">crontab -e
</code></pre>
<p>Add the following:</p>
<pre><code class="language-bash">*/1 * * * * /bin/sh [path to your script file]
</code></pre>
<p>This will check if the forwarding is alive or not every 1 minute. If it is not,cron will rerun the SSH command.</p>
<h2 id="toc_2">Using SFTP and etc. through reverse SSH tunnel</h2>
<p>You might want to be able to use SFTP or access the jupyter notebook on the destination server as well. This can be achieved by adding a regular tunnel from any unused port on your PC to port 19999 on public_server:</p>
<pre><code class="language-bash">ssh -L 19998:localhost:19999 user@public_server
</code></pre>
<p>Now you can access the destination server by using the 19998 port on your PC(localhost). Works like a magic!</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Slack webhooks for dummies: a beginer's guide to making your own slack bots using Python]]></title>
<link href="https://superkerokero.github.io/15152599330196.html"/>
<updated>2018-01-07T02:32:13+09:00</updated>
<id>https://superkerokero.github.io/15152599330196.html</id>
<content type="html"><![CDATA[
<h2 id="toc_0">What is slack</h2>
<p>Slack is a cloud-based set of team collaboration tools and services, founded by Stewart Butterfield. Slack began as an internal tool used by their company, Tiny Speck, in the development of Glitch, a now defunct online game. The name is an acronym for "Searchable Log of All Conversation and Knowledge".<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>
<span id="more"></span><!-- more -->
<h2 id="toc_1">What is webhook</h2>
<p>A webhook in web development is a method of augmenting or altering the behavior of a web page, or web application, with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application. The term "webhook" was coined by Jeff Lindsay in 2007 from the computer programming term hook.<sup id="fnref2"><a href="#fn2" rel="footnote">2</a></sup></p>
<h2 id="toc_2">Slack's webhook</h2>
<p>Slack offers two kinds of webhooks to allow the communication with the slack channels. </p>
<h3 id="toc_3">1. Incoming Webhooks</h3>
<p>Incoming Webhooks are a simple way to post messages from external sources into Slack. They make use of normal HTTP requests with a JSON payload that includes the message text and some options. Message Attachments can also be used in Incoming Webhooks to display richly-formatted messages that stand out from regular chat messages.</p>
<h3 id="toc_4">2. Outgoing webhooks</h3>
<p>Outgoing webhooks will send an HTTP POST request to your specified URL when a message matches one or both of the following conditions:</p>
<ul>
<li>The message is in the specified channel</li>
<li>The message begins with one of the defined trigger word(s)</li>
</ul>
<p>This is useful for providing automated responses to messages your team members post. For example, you might have a service that posts the status of a server. This service could receive the HTTP POST from Slack and automatically respond with a status update.</p>
<p>The outgoing webhook integration is only available in public channels. If you would like to get data out of private groups and DMs in real-time, try a slash command.</p>
<p>To summarize, incoming webhooks allows you to send messages to a slack channel; while outgoing webhooks allows you to get some message from a slack channel and send responses to it.</p>
<h2 id="toc_5">How a slack robot works using webhooks</h2>
<p>A slack robot is basically a piece of software that </p>
<ul>
<li>takes some informaton from either external source(<em>e.g.</em> a web crawler) of from a slack channel(using the outgoing webhook),</li>
<li>process the information and send some information back to the slack channel(using either the incoming webhook or the response function of the outgoing webhook).</li>
</ul>
<p>Now let's build a simple slack bot that looks at <a href="https://stackoverflow.com/questions">stackoverflow's question page</a> every minute, grab the current number of total questions, and return this information to our slack channel using incoming webhook.<br/>
<img src="http://i.imgur.com/aOqzsmu.png" alt="stackoverflow question page"/></p>
<h3 id="toc_6">1. Add incoming webhooks integration in slack</h3>
<p>Go to slacks' <a href="https://my.slack.com/services/new/incoming-webhook/">incoming webhook integration page</a> to choose a slack channel and click the <strong>Add Incoming WebHooks Integration</strong> button to add a new incoming webhook of your own. <br/>
<img src="http://i.imgur.com/R8UPaoe.png" alt="Incoming WebHooks"/><br/>
Copy the generated <strong>WebHook URL</strong> and store it somewhere, you will need it later. You can also change the name/icon/description of your slack bot here.</p>
<h3 id="toc_7">2. Create a simple web crawler in python</h3>
<p>Now we will create a python program to do the web crawling and send the message. For html parsing, we will be using BeautifulSoup4 and lxml here. Install it using the following command if you don't already them installed:</p>
<pre><code class="language-python">pip install beautifulsoup4 lxml
</code></pre>
<p>Now create a new python script file, <strong>scraper.py</strong> like this:<br/>
First import all needed libraries.</p>
<pre><code class="language-python"># Import all needed libraries
from bs4 import BeautifulSoup as bs
import urllib.request
import threading
import requests
import json
import time
</code></pre>
<p>Then add a function called <strong>checkStatus</strong> to do web crawling.</p>
<pre><code class="language-python">def checkStatus():
# Function that checks a specific webpage and returns the information as string.
url = 'https://stackoverflow.com/questions' # The URL of sltackoverflow.
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",
} # Add browser header to your http request to avoid blocking.
req = urllib.request.Request(url=url, headers=headers)
try:
page = urllib.request.urlopen(req)
except:
return '503: service unavailable, try again later.'
soup = bs(page, "lxml")
count = soup.find("div", class_='summarycount al')
return count.string
</code></pre>
<p>Then add a function called <strong>notify</strong> to send data to your slack channel using incoming webhook.</p>
<pre><code class="language-python">def notify(data):
# Function that send data in a string to your slack channel using incoming webhook
slack_url = '[PASTE YOUR GENERATED INCOMING WEBHOOK URL HERE]'
requests.post(slack_url, data = json.dumps({
'text': data,
'username': 'Tornado-webhook-notify',
'icon_emoji': ':bulb:',
'link_names': 1,
}))
</code></pre>
<p>Create a python thread class to be able to run some function at some specified time interval.</p>
<pre><code class="language-python"># Create a recursive thread to run the checkup every 1 minute.
class RecursiveThread(threading.Thread):
def __init__(self, event):
threading.Thread.__init__(self)
self.stopped = event
def run(self):
# Set the number to 60.0 to run every 1 minute.
while not self.stopped.wait(60.0):
status = checkStatus()
notify('The total question number is: ' + status)
</code></pre>
<p>Then at last call these functions and classes to check the web site for 3 minutes.</p>
<pre><code class="language-python">if __name__ == '__main__':
# Create the event used to stop the thread.
stopFlag = threading.Event()
# Instantialize the thread.
thread = RecursiveThread(stopFlag)
# Start the thread.
thread.start()
# Wait for some time(3 minutes).
time.sleep(180.0)
# Use the flag to stop the thread.
stopFlag.set()
</code></pre>
<p>If successful, you shoud be able to see the message telling you the current stackoverflow question number 3 times in your slack channel.</p>
<p><img src="http://i.imgur.com/Usg0YdZ.png" alt="slack message"/></p>
<h2 id="toc_8">Add more control using outgoing webhooks</h2>
<p>The above slack bot is already quite practically useful(you can do a bunch of things like auto checking the price of a apecific item in an online store and such folloing the same logic), but to add more control of the bot behavior, we need the outgoing webhooks. In order to use outgoing webhook, you need a server machine that can be accessed from the internet. Once you have the server setup, you only need to add a server logic that listens to the POST request from your slack channel and responses to it. Python frameworks like <a href="http://www.tornadoweb.org/en/stable/">Tornado</a> and <a href="https://www.djangoproject.com/">Django</a> can help you do this relatively easily.<br/>
Setting up a server to handle RESTful APIs is a rather broad topic and I won't do it here. It also seems that slack is deprecating the outgoing webhooks as well.<br/>
<img src="http://i.imgur.com/Mmk6B6d.png" alt="deprecating outgoing webhook"/><br/>
Maybe I will write about building slack apps using their latest APIs in the future. Also I think it is noteworthy that there are alternatives to slack like <a href="https://rocket.chat/">Rocket.Chat</a> that allows the user to store their messages and information in their own servers for credential reasons(this is impossible with slack). So maybe it's better to these alternatives instead.</p>
<div class="footnotes">
<hr/>
<ol>
<li id="fn1">
<p><a href="https://en.wikipedia.org/wiki/Slack_(software)">Slack (software) from Wikipedia</a> <a href="#fnref1" rev="footnote">↩</a></p>
</li>
<li id="fn2">
<p><a href="https://en.wikipedia.org/wiki/Webhook">Webhook from Wikipedia</a> <a href="#fnref2" rev="footnote">↩</a></p>
</li>
</ol>
</div>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Some notes on the use of "sed" command for text processing automation]]></title>
<link href="https://superkerokero.github.io/15152598378572.html"/>
<updated>2018-01-07T02:30:37+09:00</updated>
<id>https://superkerokero.github.io/15152598378572.html</id>
<content type="html"><![CDATA[
<p><strong>sed</strong>, short for "stream editor", allows you to filter and transform text. A stream editor is used to perform basic text transformations on an input stream (a file, or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), <strong>sed</strong> works by making only one pass over the input(s), and is consequently more efficient. But it is <strong>sed</strong>'s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup></p>
<span id="more"></span><!-- more -->
<h2 id="toc_0">"In-place" mode</h2>
<table>
<thead>
<tr>
<th>Options</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>-n,--quiet, --silent</td>
<td>Suppress automatic printing of pattern space.</td>
</tr>
<tr>
<td>-e script, --expression=script</td>
<td>Add the script script to the commands to be executed.</td>
</tr>
<tr>
<td>-i[SUFFIX], --in-place[=SUFFIX]</td>
<td>Edit files in place (this makes a backup with file extension SUFFIX, if SUFFIX is supplied).</td>
</tr>
</tbody>
</table>
<p>The "-i" option is used to make modifications to files immediately. You can also make backups if you offer "SUFFIX" specifications as well. The commonly used "-e" option doesn't modify the files.</p>
<h2 id="toc_1">Use inside bash scripts</h2>
<p>Using <strong>sed</strong> inside some bash script is an elegant way to exploit its true power. But here are some pitfalls you might come across along the way. If you like to <strong>echo</strong> the commands you want to excute before the actual excution, you might get the error " :sed: -e expression #1, char 1: unknown command: ' " ' ".<br/>
Below is a typical case:</p>
<pre><code class="language-bash">inFile="$WORK/${SimName}.in"
replaceCmd="sed 's/sim.dat/${SimName}.dat/g' $inFile"
echo $replaceCmd
$replaceCmd
</code></pre>
<p>It seems that the single quotes inside the string "replaceCmd" weren't recongized by the <strong>sed</strong> command. </p>
<p>One way to get across this problem is to use <strong>eval</strong> command.<br/>
Instead of using </p>
<pre><code class="language-bash">$replaceCmd
</code></pre>
<p>, you can use </p>
<pre><code class="language-bash">eval "$replaceCmd"
</code></pre>
<p>This will make the <strong>sed</strong> command work properly. <br/>
But using <strong>eval</strong> is in general not recommended due to security reasons.<sup id="fnref2"><a href="#fn2" rel="footnote">2</a></sup></p>
<p>The recommended method is to wrap the <strong>sed</strong> command inside a function:</p>
<pre><code class="language-bash">replaceCmd(){
sed "s/sim.dat/$1.dat/g" "$2"
}
inFile="$work/$SimName.in"
replaceCmd "$SimName" "$inFile"
</code></pre>
<p>This method is found in LinuxQustions.org forum.<sup id="fnref3"><a href="#fn3" rel="footnote">3</a></sup></p>
<blockquote>
<p>Oh, hells no! Never, and I mean NEVER, use eval for something as trivial as this. Indeed, you should only very rarely need it at all. It can be quite a security risk, and is almost never really necessary in general scripting.</p>
<p>In addition, variables are for storing data, not code. The spaces, quotes, and other special characters cease to be special when in a variable (unless the expansion is unquoted, then some of them still have an effect, but likely not the one you want; see my next point), and in any case leads to unclear, obfuscated code flow. If you need repeated use of the same command, don't put it inside a variable, use a function.</p>
<p>A second problem here is that if the sed command is surrounded by single quotes, the variable inside them does not get expanded. No quotes is even worse, as the shell attempts to perform word-splitting and glob expansion after the substitution.</p>
</blockquote>
<div class="footnotes">
<hr/>
<ol>
<li id="fn1">
<p><a href="https://www.computerhope.com/unix/used.htm">Computer Hope: Linux sed command</a> <a href="#fnref1" rev="footnote">↩</a></p>
</li>
<li id="fn2">
<p><a href="http://mywiki.wooledge.org/BashFAQ/048">Eval command and security issues</a> <a href="#fnref2" rev="footnote">↩</a></p>
</li>
<li id="fn3">
<p><a href="http://www.linuxquestions.org/questions/linux-newbie-8/sed-gives-sed-e-expression-1-char-1-unknown-command-%60%27%27-947056/">[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `''</a> <a href="#fnref3" rev="footnote">↩</a></p>
</li>
</ol>
</div>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Markdown syntax guide full version]]></title>
<link href="https://superkerokero.github.io/15152585359421.html"/>
<updated>2018-01-07T02:08:55+09:00</updated>
<id>https://superkerokero.github.io/15152585359421.html</id>
<content type="html"><![CDATA[
<ul>
<li><a href="#overview">Overview</a>
<ul>
<li><a href="#philosophy">Philosophy</a></li>
<li><a href="#html">Inline HTML</a></li>
<li><a href="#autoescape">Automatic Escaping for Special Characters</a></li>
</ul></li>
<li><a href="#block">Block Elements</a>
<ul>
<li><a href="#p">Paragraphs and Line Breaks</a></li>
<li><a href="#header">Headers</a></li>
<li><a href="#blockquote">Blockquotes</a></li>
<li><a href="#list">Lists</a></li>
<li><a href="#precode">Code Blocks</a></li>
<li><a href="#hr">Horizontal Rules</a></li>
</ul></li>
<li><a href="#span">Span Elements</a>
<ul>
<li><a href="#link">Links</a></li>
<li><a href="#em">Emphasis</a></li>
<li><a href="#code">Code</a></li>
<li><a href="#img">Images</a></li>
</ul></li>
<li><a href="#misc">Miscellaneous</a>
<ul>
<li><a href="#backslash">Backslash Escapes</a></li>
<li><a href="#autolink">Automatic Links</a></li>
</ul></li>
</ul>
<span id="more"></span><!-- more -->
<p><strong>Note:</strong> This document is itself written using Markdown; you<br/>
can <a href="/projects/markdown/syntax.text">see the source for it by adding '.text' to the URL</a>.</p>
<hr/>
<h2 id="toc_0">Overview</h2>
<p><a id="overview"></a></p>
<h3 id="toc_1">Philosophy</h3>
<p><a id="philosophy"></a></p>
<p>Markdown is intended to be as easy-to-read and easy-to-write as is feasible.</p>
<p>Readability, however, is emphasized above all else. A Markdown-formatted<br/>
document should be publishable as-is, as plain text, without looking<br/>
like it's been marked up with tags or formatting instructions. While<br/>
Markdown's syntax has been influenced by several existing text-to-HTML<br/>
filters -- including <a href="http://docutils.sourceforge.net/mirror/setext.html">Setext</a>, <a href="http://www.aaronsw.com/2002/atx/">atx</a>, <a href="http://textism.com/tools/textile/">Textile</a>, <a href="http://docutils.sourceforge.net/rst.html">reStructuredText</a>,<br/>
<a href="http://www.triptico.com/software/grutatxt.html">Grutatext</a>, and <a href="http://ettext.taint.org/doc/">EtText</a> -- the single biggest source of<br/>
inspiration for Markdown's syntax is the format of plain text email.</p>
<p>To this end, Markdown's syntax is comprised entirely of punctuation<br/>
characters, which punctuation characters have been carefully chosen so<br/>
as to look like what they mean. E.g., asterisks around a word actually<br/>
look like *emphasis*. Markdown lists look like, well, lists. Even<br/>
blockquotes look like quoted passages of text, assuming you've ever<br/>
used email.</p>
<h3 id="toc_2">Inline HTML</h3>
<p><a id="html"></a></p>
<p>Markdown's syntax is intended for one purpose: to be used as a<br/>
format for <em>writing</em> for the web.</p>
<p>Markdown is not a replacement for HTML, or even close to it. Its<br/>
syntax is very small, corresponding only to a very small subset of<br/>
HTML tags. The idea is <em>not</em> to create a syntax that makes it easier<br/>
to insert HTML tags. In my opinion, HTML tags are already easy to<br/>
insert. The idea for Markdown is to make it easy to read, write, and<br/>
edit prose. HTML is a <em>publishing</em> format; Markdown is a <em>writing</em><br/>
format. Thus, Markdown's formatting syntax only addresses issues that<br/>
can be conveyed in plain text.</p>
<p>For any markup that is not covered by Markdown's syntax, you simply<br/>
use HTML itself. There's no need to preface it or delimit it to<br/>
indicate that you're switching from Markdown to HTML; you just use<br/>
the tags.</p>
<p>The only restrictions are that block-level HTML elements -- e.g. <code><div></code>,<br/>
<code><table></code>, <code><pre></code>, <code><p></code>, etc. -- must be separated from surrounding<br/>
content by blank lines, and the start and end tags of the block should<br/>
not be indented with tabs or spaces. Markdown is smart enough not<br/>
to add extra (unwanted) <code><p></code> tags around HTML block-level tags.</p>
<p>For example, to add an HTML table to a Markdown article:</p>
<pre><code>This is a regular paragraph.
<table>
<tr>
<td>Foo</td>
</tr>
</table>
This is another regular paragraph.
</code></pre>
<p>Note that Markdown formatting syntax is not processed within block-level<br/>
HTML tags. E.g., you can't use Markdown-style <code>*emphasis*</code> inside an<br/>
HTML block.</p>
<p>Span-level HTML tags -- e.g. <code><span></code>, <code><cite></code>, or <code><del></code> -- can be<br/>
used anywhere in a Markdown paragraph, list item, or header. If you<br/>
want, you can even use HTML tags instead of Markdown formatting; e.g. if<br/>
you'd prefer to use HTML <code><a></code> or <code><img></code> tags instead of Markdown's<br/>
link or image syntax, go right ahead.</p>
<p>Unlike block-level HTML tags, Markdown syntax <em>is</em> processed within<br/>
span-level tags.</p>
<h3 id="toc_3">Automatic Escaping for Special Characters</h3>
<p><a id="autoescape"></a></p>
<p>In HTML, there are two characters that demand special treatment: <code><</code><br/>
and <code>&</code>. Left angle brackets are used to start tags; ampersands are<br/>
used to denote HTML entities. If you want to use them as literal<br/>
characters, you must escape them as entities, e.g. <code>&lt;</code>, and<br/>
<code>&amp;</code>.</p>
<p>Ampersands in particular are bedeviling for web writers. If you want to<br/>
write about 'AT&T', you need to write '<code>AT&amp;T</code>'. You even need to<br/>
escape ampersands within URLs. Thus, if you want to link to:</p>
<pre><code>http://images.google.com/images?num=30&q=larry+bird
</code></pre>
<p>you need to encode the URL as:</p>
<pre><code>http://images.google.com/images?num=30&amp;q=larry+bird
</code></pre>
<p>in your anchor tag <code>href</code> attribute. Needless to say, this is easy to<br/>
forget, and is probably the single most common source of HTML validation<br/>
errors in otherwise well-marked-up web sites.</p>
<p>Markdown allows you to use these characters naturally, taking care of<br/>
all the necessary escaping for you. If you use an ampersand as part of<br/>
an HTML entity, it remains unchanged; otherwise it will be translated<br/>
into <code>&amp;</code>.</p>
<p>So, if you want to include a copyright symbol in your article, you can write:</p>
<pre><code>&copy;
</code></pre>
<p>and Markdown will leave it alone. But if you write:</p>
<pre><code>AT&T
</code></pre>
<p>Markdown will translate it to:</p>
<pre><code>AT&amp;T
</code></pre>
<p>Similarly, because Markdown supports <a href="#html">inline HTML</a>, if you use<br/>
angle brackets as delimiters for HTML tags, Markdown will treat them as<br/>
such. But if you write:</p>
<pre><code>4 < 5
</code></pre>
<p>Markdown will translate it to:</p>
<pre><code>4 &lt; 5
</code></pre>
<p>However, inside Markdown code spans and blocks, angle brackets and<br/>
ampersands are <em>always</em> encoded automatically. This makes it easy to use<br/>
Markdown to write about HTML code. (As opposed to raw HTML, which is a<br/>
terrible format for writing about HTML syntax, because every single <code><</code><br/>
and <code>&</code> in your example code needs to be escaped.)</p>
<hr/>
<h2 id="toc_4">Block Elements</h2>
<p><a id="block"></a></p>
<h3 id="toc_5">Paragraphs and Line Breaks</h3>
<p><a id="p"></a></p>
<p>A paragraph is simply one or more consecutive lines of text, separated<br/>
by one or more blank lines. (A blank line is any line that looks like a<br/>
blank line -- a line containing nothing but spaces or tabs is considered<br/>
blank.) Normal paragraphs should not be indented with spaces or tabs.</p>
<p>The implication of the "one or more consecutive lines of text" rule is<br/>
that Markdown supports "hard-wrapped" text paragraphs. This differs<br/>
significantly from most other text-to-HTML formatters (including Movable<br/>
Type's "Convert Line Breaks" option) which translate every line break<br/>
character in a paragraph into a <code><br /></code> tag.</p>
<p>When you <em>do</em> want to insert a <code><br /></code> break tag using Markdown, you<br/>
end a line with two or more spaces, then type return.</p>
<p>Yes, this takes a tad more effort to create a <code><br /></code>, but a simplistic<br/>
"every line break is a <code><br /></code>" rule wouldn't work for Markdown.<br/>
Markdown's email-style <a href="#blockquote">blockquoting</a> and multi-paragraph <a href="#list">list items</a><br/>
work best -- and look better -- when you format them with hard breaks.</p>
<h3 id="toc_6">Headers</h3>
<p><a id="header"></a></p>
<p>Markdown supports two styles of headers, <a href="http://docutils.sourceforge.net/mirror/setext.html">Setext</a> and <a href="http://www.aaronsw.com/2002/atx/">atx</a>.</p>
<p>Setext-style headers are "underlined" using equal signs (for first-level<br/>
headers) and dashes (for second-level headers). For example:</p>
<pre><code>This is an H1
=============
This is an H2
-------------
</code></pre>
<p>Any number of underlining <code>=</code>'s or <code>-</code>'s will work.</p>
<p>Atx-style headers use 1-6 hash characters at the start of the line,<br/>
corresponding to header levels 1-6. For example:</p>
<pre><code># This is an H1
## This is an H2
###### This is an H6
</code></pre>
<p>Optionally, you may "close" atx-style headers. This is purely<br/>
cosmetic -- you can use this if you think it looks better. The<br/>
closing hashes don't even need to match the number of hashes<br/>
used to open the header. (The number of opening hashes<br/>
determines the header level.) :</p>
<pre><code># This is an H1 #
## This is an H2 ##
### This is an H3 ######
</code></pre>
<h3 id="toc_7">Blockquotes</h3>
<p><a id="blockquote"></a></p>
<p>Markdown uses email-style <code>></code> characters for blockquoting. If you're<br/>
familiar with quoting passages of text in an email message, then you<br/>
know how to create a blockquote in Markdown. It looks best if you hard<br/>
wrap the text and put a <code>></code> before every line:</p>
<pre><code>> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
>
> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
> id sem consectetuer libero luctus adipiscing.
</code></pre>
<p>Markdown allows you to be lazy and only put the <code>></code> before the first<br/>
line of a hard-wrapped paragraph:</p>
<pre><code>> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
id sem consectetuer libero luctus adipiscing.
</code></pre>
<p>Blockquotes can be nested (i.e. a blockquote-in-a-blockquote) by<br/>
adding additional levels of <code>></code>:</p>
<pre><code>> This is the first level of quoting.
>
> > This is nested blockquote.
>
> Back to the first level.
</code></pre>
<p>Blockquotes can contain other Markdown elements, including headers, lists,<br/>
and code blocks:</p>
<pre><code>> ## This is a header.
>
> 1. This is the first list item.
> 2. This is the second list item.
>
> Here's some example code:
>
> return shell_exec("echo $input | $markdown_script");
</code></pre>
<p>Any decent text editor should make email-style quoting easy. For<br/>
example, with BBEdit, you can make a selection and choose Increase<br/>
Quote Level from the Text menu.</p>
<h3 id="toc_8">Lists</h3>
<p><a id="list"></a></p>
<p>Markdown supports ordered (numbered) and unordered (bulleted) lists.</p>
<p>Unordered lists use asterisks, pluses, and hyphens -- interchangably<br/>
-- as list markers:</p>
<pre><code>* Red
* Green
* Blue
</code></pre>
<p>is equivalent to:</p>
<pre><code>+ Red
+ Green
+ Blue
</code></pre>
<p>and:</p>
<pre><code>- Red
- Green
- Blue
</code></pre>
<p>Ordered lists use numbers followed by periods:</p>
<pre><code>1. Bird
2. McHale
3. Parish
</code></pre>
<p>It's important to note that the actual numbers you use to mark the<br/>
list have no effect on the HTML output Markdown produces. The HTML<br/>
Markdown produces from the above list is:</p>
<pre><code><ol>
<li>Bird</li>
<li>McHale</li>
<li>Parish</li>
</ol>
</code></pre>
<p>If you instead wrote the list in Markdown like this:</p>
<pre><code>1. Bird
1. McHale
1. Parish
</code></pre>
<p>or even:</p>
<pre><code>3. Bird
1. McHale
8. Parish
</code></pre>
<p>you'd get the exact same HTML output. The point is, if you want to,<br/>
you can use ordinal numbers in your ordered Markdown lists, so that<br/>
the numbers in your source match the numbers in your published HTML.<br/>
But if you want to be lazy, you don't have to.</p>
<p>If you do use lazy list numbering, however, you should still start the<br/>
list with the number 1. At some point in the future, Markdown may support<br/>
starting ordered lists at an arbitrary number.</p>
<p>List markers typically start at the left margin, but may be indented by<br/>
up to three spaces. List markers must be followed by one or more spaces<br/>
or a tab.</p>
<p>To make lists look nice, you can wrap items with hanging indents:</p>
<pre><code>* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.
* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing.
</code></pre>
<p>But if you want to be lazy, you don't have to:</p>
<pre><code>* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.