-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjavascript.html
231 lines (227 loc) · 12.5 KB
/
javascript.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Codebraid with JavaScript</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; }
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.at { color: #7d9029; }
code span.bn { color: #40a070; }
code span.bu { color: #008000; }
code span.cf { color: #007020; font-weight: bold; }
code span.ch { color: #4070a0; }
code span.cn { color: #880000; }
code span.co { color: #60a0b0; font-style: italic; }
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.do { color: #ba2121; font-style: italic; }
code span.dt { color: #902000; }
code span.dv { color: #40a070; }
code span.er { color: #ff0000; font-weight: bold; }
code span.ex { }
code span.fl { color: #40a070; }
code span.fu { color: #06287e; }
code span.im { color: #008000; font-weight: bold; }
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.kw { color: #007020; font-weight: bold; }
code span.op { color: #666666; }
code span.ot { color: #007020; }
code span.pp { color: #bc7a00; }
code span.sc { color: #4070a0; }
code span.ss { color: #bb6688; }
code span.st { color: #4070a0; }
code span.va { color: #19177c; }
code span.vs { color: #4070a0; }
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
</style>
<style type="text/css">body {display: block;text-align: left;max-width: 40rem;padding-left: 0.5rem;padding-right: 0.5rem;margin: auto;}pre.numberSource a.sourceLine {position: relative;left: -3em;}pre.numberSource a.sourceLine::before{content: attr(title);position: relative;left: -1em;text-align: right;vertical-align: baseline;border: none;pointer-events: all;display: inline-block;-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;padding: 0 4px;width: 3em;color: #aaaaaa;}pre.numberSource {margin-left: 2em;border-left: 1px solid #aaaaaa;padding-left: 4px;}div.exampleMarkup:before{content: "Markdown";font-family: monospace;display: block;height: 2em;font-size: 150%;}div.exampleMarkup {padding-top: 0.5em;padding-left: 0.5em;padding-right: 0.5em;margin-top: 2em;margin-bottom: 1em;background-color: honeydew;border-style: solid;border-width: 1px;border-color: limegreen;border-radius: 0.2em;}div.exampleOutput:before{content: "Output";font-family: monospace;display: block;font-size: 150%;}div.exampleOutput {color: black;padding-top: 0.5em;padding-left: 0.5em;padding-right: 0.5em;margin-top: 1em;margin-bottom: 2em;background-color: whitesmoke;border-style: solid;border-width: 1px;border-color: lightgray;border-radius: 0.2em;}div.sourceCode {padding: 0.5em;background-color: aliceblue;border-style: solid;border-width: 1px;border-color: lightskyblue;border-radius: 0.2em;}div.exampleOutput div.sourceCode:before{content: "code";font-family: monospace;display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.stdout:before{content: "stdout";display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.stdout {color: black;padding: 0.5em;background-color: azure;border-style: solid;border-width: 1px;border-color: turquoise;border-radius: 0.2em;padding-left: 0.5em;}code.stdout {color: black;background-color: azure;border-style: solid;border-width: 1px;border-color: turquoise;}pre.stderr:before{content: "stderr";display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.stderr {color: red;padding: 0.5em;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;border-radius: 0.2em;padding-left: 0.5em;}code.stderr {color: red;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;}pre.error:before{content: "Error";display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.error {color: red;padding: 0.5em;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;border-radius: 0.2em;padding-left: 0.5em;}code.error {color: red;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;}</style>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title">Codebraid with JavaScript</h1>
</header>
<h2 id="inline-code">Inline code</h2>
<h3 id="run">Run</h3>
<p>Inline code with <code>.cb-run</code> gives raw stdout. While the
language can be specified with <code>.javascript</code>,
<code>.js</code> works as well.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`console.log(1 + 2);`{.js .cb-run}</code></pre>
</div>
<div class="exampleOutput">
<p>3</p>
</div>
</div>
<h3 id="expression-and-inline-notebook">Expression and inline
notebook</h3>
<p>Inline code with <code>.cb-expr</code> evaluates an expression and
then inserts the raw output into the document, where it is interpreted
as Markdown. Inline code with <code>.cb-nb</code> (<code>nb</code> is
short for <code>notebook</code>) is similar, except output is shown
verbatim.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`` `\$2^8 = ${2**8}\$` ``{.js .cb-expr}</code></pre>
</div>
<div class="exampleOutput">
<p><span class="math inline">2<sup>8</sup> = 256</span></p>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>`4*16`{.js .cb-nb}</code></pre>
</div>
<div class="exampleOutput">
<p><code class="expr">64</code></p>
</div>
</div>
<h3 id="stderr">Stderr</h3>
<p>In the event of an error, inline code automatically shows stderr by
default. This code is executed in its own session,
<code>inline_error</code>, so that it does not impact other
examples.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`console.logs(1 + 2);`{.js .cb-run session=inline_error}</code></pre>
</div>
<div class="exampleOutput">
<p><code class="stderr error"><string>:1 console.logs(1 + 2); ^ TypeError: console.logs is not a function at Object.<anonymous> (<string>:1:9) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 Node.js v18.15.0</code></p>
</div>
</div>
<h2 id="block-code">Block code</h2>
<h3 id="run-1">Run</h3>
<p>Code blocks with <code>.cb-run</code> give raw stdout. There is
continuity between code blocks so long as they are in the same session;
variables persist.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.js .cb-run}
message = "Hello from *JavaScript!*";
```</code></pre>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.js .cb-run}
console.log(message);
```</code></pre>
</div>
<div class="exampleOutput">
<p>Hello from <em>JavaScript!</em></p>
</div>
</div>
<h3 id="notebook">Notebook</h3>
<p>Code blocks with <code>.cb-nb</code> show the code and also the
verbatim stdout.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.js .cb-nb session=notebook}
function pows_of_two(start, end) {
n = 2;
x = start;
while (x < end) {
process.stdout.write(String(2**x) + ", ");
x += 1;
}
console.log(2**end);
}
pows_of_two(1, 9);
pows_of_two(1, 15);
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb8" data-startFrom="1"><pre class="sourceCode numberSource js numberLines"><code class="sourceCode javascript"><span id="cb8-1"><a href="#cb8-1"></a><span class="kw">function</span> <span class="fu">pows_of_two</span>(start<span class="op">,</span> end) {</span>
<span id="cb8-2"><a href="#cb8-2"></a> n <span class="op">=</span> <span class="dv">2</span><span class="op">;</span></span>
<span id="cb8-3"><a href="#cb8-3"></a> x <span class="op">=</span> start<span class="op">;</span></span>
<span id="cb8-4"><a href="#cb8-4"></a> <span class="cf">while</span> (x <span class="op"><</span> end) {</span>
<span id="cb8-5"><a href="#cb8-5"></a> <span class="bu">process</span><span class="op">.</span><span class="at">stdout</span><span class="op">.</span><span class="fu">write</span>(<span class="bu">String</span>(<span class="dv">2</span><span class="op">**</span>x) <span class="op">+</span> <span class="st">", "</span>)<span class="op">;</span></span>
<span id="cb8-6"><a href="#cb8-6"></a> x <span class="op">+=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb8-7"><a href="#cb8-7"></a> }</span>
<span id="cb8-8"><a href="#cb8-8"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="dv">2</span><span class="op">**</span>end)<span class="op">;</span></span>
<span id="cb8-9"><a href="#cb8-9"></a>}</span>
<span id="cb8-10"><a href="#cb8-10"></a><span class="fu">pows_of_two</span>(<span class="dv">1</span><span class="op">,</span> <span class="dv">9</span>)<span class="op">;</span></span>
<span id="cb8-11"><a href="#cb8-11"></a><span class="fu">pows_of_two</span>(<span class="dv">1</span><span class="op">,</span> <span class="dv">15</span>)<span class="op">;</span></span></code></pre></div>
<pre class="stdout"><code>2, 4, 8, 16, 32, 64, 128, 256, 512
2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768</code></pre>
</div>
</div>
<h3 id="stderr-1">Stderr</h3>
<p>Code blocks show stderr automatically by default.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.js .cb-nb session=block_error}
x = 1 + ;
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb11" data-startFrom="1"><pre class="sourceCode numberSource js numberLines"><code class="sourceCode javascript"><span id="cb11-1"><a href="#cb11-1"></a>x <span class="op">=</span> <span class="dv">1</span> <span class="op">+</span> <span class="op">;</span></span></code></pre></div>
<pre class="stderr error"><code>source.js:1
x = 1 + ;
^
SyntaxError: Unexpected token ';'
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Node.js v18.15.0</code></pre>
</div>
</div>
</body>
</html>