Commit 37551200969bd30b2cf4f0c973cc7aba67c18b0b

Wei Ting 2021-10-22T01:46:08

Document standard tokens and provide examples (#3104)

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
diff --git a/extending.html b/extending.html
index eb6a561..606c281 100644
--- a/extending.html
+++ b/extending.html
@@ -60,8 +60,8 @@ ol.indent {
 
 		<dt>alias</dt>
 		<dd>This option can be used to define one or more aliases for the matched token. The result will be, that
-		the styles of the token and its aliases are combined. This can be useful, to combine the styling of a well known
-		token, which is already supported by most of the themes,  with a semantically correct token name. The option
+		the styles of the token and its aliases are combined. This can be useful, to combine the styling of a <a href="/tokens.html">standard
+		token</a>, which is already supported by most of the themes,  with a semantically correct token name. The option
 		can be set to a string literal or an array of string literals. In the following example the token
 		name <code>latex-equation</code> is not supported by any theme, but it will be highlighted the same as a string.
 		<pre><code class="language-javascript">{
diff --git a/faq.html b/faq.html
index c060119..8dd2a57 100644
--- a/faq.html
+++ b/faq.html
@@ -21,7 +21,7 @@
 <script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
 <script src="https://www.google-analytics.com/ga.js" async></script>
 </head>
-<body>
+<body class="language-none">
 
 <header>
 	<div class="intro" data-src="assets/templates/header-main.html" data-type="text/html"></div>
@@ -95,14 +95,15 @@
 
 	<p>Language: <select id="language-select"></select></p>
 	<pre class="language-none" style="height: 30em"><code id="print-tokens-output"></code></pre>
+
+	<p>Additionally, you can find a list of standard tokens on <a href="/tokens.html">this page</a>.</p>
 </section>
 
 <section>
 	<h1>How can I use different highlighting for tokens with the same name in different languages?</h1>
 	<p>Just use a descendant selector, that includes the language class. The default <code>prism.css</code> does this, to have different colors for
 	JavaScript strings (which are very common) and CSS strings (which are relatively rare). Here’s that code, simplified to illustrate the technique:
-	<pre><code class="language-css">
-.token.string {
+	<pre><code class="language-css">.token.string {
 	color: #690;
 }
 
diff --git a/tokens.html b/tokens.html
new file mode 100644
index 0000000..0fa13cc
--- /dev/null
+++ b/tokens.html
@@ -0,0 +1,437 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+
+<meta charset="utf-8" />
+<link rel="icon" href="assets/favicon.png" />
+<title>Prism tokens ▲ Prism</title>
+<link rel="stylesheet" href="assets/style.css" />
+<link rel="stylesheet" href="themes/prism.css" data-noprefix />
+<link rel="stylesheet" href="plugins/line-highlight/prism-line-highlight.css" data-noprefix />
+<link rel="stylesheet" href="plugins/toolbar/prism-toolbar.css" data-noprefix />
+<script src="assets/vendor/prefixfree.min.js"></script>
+
+<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
+<script src="https://www.google-analytics.com/ga.js" async></script>
+<style>
+table.styled {
+	border: 1px solid #ccc;
+	border-spacing: 0;
+}
+table.styled tr:not(:first-child) > * {
+	border-top: 1px solid #ccc;
+}
+table.styled tr > *:not(:first-child) {
+	border-left: 1px solid #ccc;
+}
+table.styled tr:nth-child(2n + 1) {
+	background-color: #F8F8F8;
+}
+table.styled tr > * {
+	padding: .5em .75em;
+}
+table.styled tr > th {
+	text-align: left;
+}
+</style>
+</head>
+<body class="language-none">
+
+<header>
+	<div class="intro" data-src="assets/templates/header-main.html" data-type="text/html"></div>
+
+	<h2>Prism tokens</h2>
+	<p>Prism identifies tokens in your code, which are in turn styled by CSS to produce the syntax highlighting. This page provides an overview of the standard tokens and corresponding examples.</p>
+</header>
+
+<section id="standard-tokens">
+	<h1>Standard tokens</h1>
+
+	<p>When defining a new language, you will need to provide token names for each 'type' of code, such as keywords and operators, so Prism's themes can assign colors (and other styles) accordingly. Prism's themes (both official and non-official) only guarantee coverage for these standard tokens, so it is recommended to make use of the following standard tokens to ensure that code will be highlighted.</p>
+
+	<table class="styled">
+		<tbody>
+			<tr>
+				<th colspan="2">General purpose</th>
+			</tr>
+			<tr>
+				<td><code>keyword</code></td>
+				<td>
+					Pre-defined and reserved words.
+
+					<pre><code class="language-javascript">for (const foo of bar) {
+	if (foo === 'foobar') break;
+	await foo;
+}</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>builtin</code></td>
+				<td>
+					Functions/Methods/Classes/Types that are available out of the box.
+
+					<pre><code class="language-python">pi = round(float('3.14159'), 2)</code></pre>
+					<pre><code class="language-typescript">type SearchFunc = (source: string, subStr: string) => boolean;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>class-name</code></td>
+				<td>
+					The name of a class, interface, trait, or type.
+
+					<pre><code class="language-javascript">class Rectangle extends Square { /* ... */ }</code></pre>
+					<pre><code class="language-csharp">public class CameraController : MonoBehaviour { /* ... */ }</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>function</code></td>
+				<td>
+					The name of a function or method.
+
+					<pre><code class="language-javascript">function isEven(number) {
+	return Number(number) % 2 === 0;
+}
+const isOdd = (number) => !isEven(number);</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>boolean</code></td>
+				<td>
+					True and false, and pairs with similar concepts (e.g. yes and no).
+
+					<pre><code class="language-javascript">console.log(true === false); // prints false
+console.log(true === !false); // prints true</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>number</code></td>
+				<td>
+					A numerical value, regardless of base and order, and no matter real or imaginary.
+
+					<pre><code class="language-python">print(3.14159 * 42)
+print(1e100 + .001j)
+return foo & 0xdeadbeef</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>string</code></td>
+				<td>
+					Literal text, including numbers and symbols and maybe even more special characters.
+
+					<pre><code class="language-javascript">let greeting = 'Hello World!';</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>char</code></td>
+				<td>
+					A string that can comprise only a single character, enforced by the language.
+
+					<pre><code class="language-elm">['A', 'z', '0', '-', '\t', '\u{2728}']</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>symbol</code></td>
+				<td>
+					A primitive data type found in some languages, can be thought of as an identifier.
+
+					<pre><code class="language-smalltalk">#myFirstSymbol "#myFirstSymbol is a symbol in Smalltalk"</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>regex</code></td>
+				<td>
+					A regular expression.
+
+					<pre><code class="language-javascript">let entity = /&#x?[\da-f]{1,8};/;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>url</code></td>
+				<td>
+					A link to another page or resource.
+
+					<pre><code class="language-css">body {
+	background: url(foo.png);
+}</code></pre>
+					<pre><code class="language-markdown">[Prism](https://prismjs.com) is a cool syntax highlighter.</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>operator</code></td>
+				<td>
+					A symbol that represents an action or process, whether it's a mathematical operation, logical operation, and so on.
+
+					<pre><code class="language-javascript">x += (y + 4 >> -z === w) ? b ** c : ~a;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>variable</code></td>
+				<td>
+					The name of a variable. This token is intended to be used sparingly. It's generally used on special variables (e.g. Less or Bash), not general variables from imperative and procedural programming languages (e.g. C, JavaScript, Python).
+
+					<pre><code class="language-less">@nice-blue: #5B83AD;
+@light-blue: lighten(@nice-blue, 20%);</code></pre>
+					<pre><code class="language-bash">echo $STRING
+args=("$@")
+echo ${args[0]} ${args[1]} ${args[2]}</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>constant</code></td>
+				<td>
+					The name of a constant.
+
+					<pre><code class="language-javascript">const PI = 3.14159;</code></pre>
+					<pre><code class="language-rust">const THING: u32 = 0xABAD1DEA;</code></pre>
+					<pre><code class="language-c">fprintf(stdout, "hello world\n");</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>property</code></td>
+				<td>
+					An attribute/characteristic or object/map key.
+
+					<pre><code class="language-css">body {
+	color: red;
+	line-height: normal;
+}</code></pre>
+					<pre><code class="language-json">{
+	"data": { "labels": ["foo", "bar"], },
+	"error": null,
+	"status": "Ok"
+}</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>punctuation</code></td>
+				<td>
+					Punctuation such as brackets, parentheses, commas, and more.
+
+					<pre><code class="language-python">def median(pool):
+	copy = sorted(pool)
+	size = len(copy)
+	if size % 2 == 1:
+		return copy[(size - 1) / 2]
+	else:
+		return (copy[size/2 - 1] + copy[size/2]) / 2</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>important</code></td>
+				<td>
+					Anything that is important and needs special highlighting.
+
+					<pre><code class="language-css">body {
+	color: red !important;
+}</code></pre>
+					<pre><code class="language-markdown"># This is a heading. Headings are important.</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>comment</code></td>
+				<td>
+					Code comments.
+
+					<pre><code class="language-markup">&lt;!-- Here's a comment --&gt;
+&lt;style&gt;
+	/* Here's another comment */
+&lt;/style&gt;
+&lt;script&gt;
+// Here's yet another comment
+&lt;/script&gt;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<th colspan="2">Markup languages</th>
+			</tr>
+			<tr>
+				<td><code>tag</code></td>
+				<td>
+					A markup tag (e.g. HTML and XML tags).
+
+					<pre><code class="language-markup">&lt;p&gt;Hello World!&lt;/p&gt;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>attr-name</code>, <code>attr-value</code></td>
+				<td>
+					Kind of like a property of a markup tag and its value/argument respectively.
+
+					<pre><code class="language-markup">&lt;p id="greeting"&gt;Hello World!&lt;/p&gt;
+&lt;video width="1280" height="720" allowfullscreen controls&gt;
+	&lt;source src="hello_world.mp4" type="video/mp4" /&gt;
+&lt;/video&gt;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>namespace</code></td>
+				<td>
+					Used to provide uniquely named elements and attributes in XML documents. Outside of markup languages, it is used to tokenize the package/namespace part of identifiers.
+
+					<pre><code class="language-markup">&lt;html:p foo:bar="baz" foo:weee&gt;&lt;/html:p&gt;</code></pre>
+					<pre><code class="language-java">class Foo extends foo.bar.Foo {
+	java.util.List&lt;foo.bar.Foo.Bar&gt; bar(foo.bar.Baz bat) {
+		throw new java.lang.UnsupportedOperationException();
+	}
+}</code></pre>
+					<pre><code class="language-rust">use std::sync::Arc;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>prolog</code></td>
+				<td>
+					The first part of an XML document.
+
+					<pre><code class="language-markup">&lt;?xml version="1.0" encoding="utf-8"?&gt;
+&lt;svg&gt;&lt;/svg&gt;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>doctype</code></td>
+				<td>
+					Document type declaration, specific to markup languages.
+
+					<pre><code class="language-markup">&lt;!DOCTYPE html&gt;
+&lt;html&gt;&lt;/html&gt;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>cdata</code></td>
+				<td>
+					Character data, specific to markup languages.
+
+					<pre><code class="language-markup">&lt;ns1:description&gt;&lt;![CDATA[
+  CDATA is &lt;not&gt; magical.
+]]>&lt;/ns1:description&gt;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>entity</code></td>
+				<td>
+					Code used to display reserved characters in markup languages.
+
+					<pre><code class="language-markup">&amp;amp; &amp;#x2665; &amp;#160; &amp;#x152;</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<th colspan="2">Document-markup languages</th>
+			</tr>
+			<tr>
+				<td><code>bold</code></td>
+				<td>
+					Bolded text. Mostly found in document-markup languages.
+
+					<pre><code class="language-markdown">**I am bolded text!**</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>italic</code></td>
+				<td>
+					Italicised text. Mostly found in document-markup languages.
+
+					<pre><code class="language-markdown">*I am italicised text!*</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<th colspan="2">Stylesheets</th>
+			</tr>
+			<tr>
+				<td><code>atrule</code></td>
+				<td>
+					Literally <code>@</code> rules (statements) in stylesheets.
+
+					<pre><code class="language-css">@font-family {
+	font-family: Questrial;
+	src: url(questrial.otf);
+}
+@media screen and (min-width: 768px) { /* ... */ }</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<td><code>selector</code></td>
+				<td>
+					Code that identifies or picks something out of a group to operate on, such as the names of HTML elements in stylesheets.
+
+					<pre><code class="language-css">section h1,
+#features li strong,
+header h2,
+footer p { /* ... */ }</code></pre>
+				</td>
+			</tr>
+			<tr>
+				<th colspan="2">Diff</th>
+			</tr>
+			<tr>
+				<td><code>inserted</code>, <code>deleted</code></td>
+				<td>
+					Added or modified line and deleted line respectively, mainly for diffs. In general, also the idea of something being increased and decreased/removed respectively.
+
+					<pre><code class="language-diff">--- bar.yml	2014-12-16 11:43:41 +0800
++++ /Users/foo/Desktop/bar.yml	2014-12-31 11:28:08 +0800
+@@ -4,5 +4,5 @@
+project:
+	sources: "src/*.cpp"
+	headers: "src/*.h"
+-    qt: core
++    qt: core gui
+public_headers: "src/*.h"</code></pre>
+				</td>
+			</tr>
+		</tbody>
+	</table>
+</section>
+
+<section id="embedded-languages">
+	<h1>Embedded languages</h1>
+
+	<p>In addition to the standard tokens above, Prism also has a token for languages that are embedded in another language, such as CSS in HTML, JS in HTML, Bash in Shell-session, and CSS in JS, allowing Prism's themes to highlight the tokens in the embedded languages more accurately. All embedded languages are wrapped in their own special token, which includes a CSS class <code>language-xxxx</code> corresponding to the embedded language.</p>
+
+	<p>Open your browser's developer tools and check out the example below to see it in action!</p>
+
+	<pre data-line="6-10,14-18"><code class="language-markup">&lt;!DOCTYPE html&gt;
+&lt;html lang="en"&gt;
+&lt;head&gt;
+	&lt;meta charset="utf-8" /&gt;
+	&lt;title>I can haz embedded CSS and JS&lt;/title&gt;
+	&lt;style&gt;
+		@media print {
+			p { color: red !important; }
+		}
+	&lt;/style&gt;
+&lt;/head&gt;
+&lt;body&gt;
+	&lt;h1&gt;I can haz embedded CSS and JS&lt;/h1&gt;
+	&lt;script&gt;
+	if (true) {
+		console.log('foo');
+	}
+	&lt;/script&gt;
+
+&lt;/body&gt;
+&lt;/html&gt;</code></pre>
+</section>
+
+<section id="non-standard-tokens">
+	<h1>Non-standard tokens</h1>
+
+	<p>Sometimes, a language might use a particular name to refer to certain pieces of code, but which is not one of Prism's standard token names, such as <code>function-defintion</code>. Since <code>function-definition</code> is not a standard token, you might want to alias it with a standard token such as <code>function</code>, which is semantically similar, and will ensure that Prism's themes will highlight it. Here's an example:</p>
+
+	<pre><code class="language-rust">fn main() {
+	println!("Hello World");
+	another_function();
+}</code></pre>
+</section>
+
+<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
+
+<script src="assets/vendor/utopia.js"></script>
+<script src="prism.js"></script>
+<script src="plugins/autoloader/prism-autoloader.js"></script>
+<script src="plugins/line-highlight/prism-line-highlight.js"></script>
+<script src="plugins/toolbar/prism-toolbar.js"></script>
+<script src="plugins/show-language/prism-show-language.js"></script>
+<script src="components.js"></script>
+<script src="assets/code.js"></script>
+
+</body>
+</html>