From 37551200969bd30b2cf4f0c973cc7aba67c18b0b Mon Sep 17 00:00:00 2001 From: Wei Ting <59229084+hoonweiting@users.noreply.github.com> Date: Fri, 22 Oct 2021 01:46:08 +0800 Subject: [PATCH] Document standard tokens and provide examples (#3104) --- extending.html | 4 +- faq.html | 7 +- tokens.html | 437 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 443 insertions(+), 5 deletions(-) create mode 100644 tokens.html diff --git a/extending.html b/extending.html index eb6a561d26..606c2816fb 100644 --- a/extending.html +++ b/extending.html @@ -60,8 +60,8 @@

Language definitions

alias
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 standard + token, 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 latex-equation is not supported by any theme, but it will be highlighted the same as a string.
{
diff --git a/faq.html b/faq.html
index c060119167..8dd2a57338 100644
--- a/faq.html
+++ b/faq.html
@@ -21,7 +21,7 @@
 
 
 
-
+
 
 
@@ -95,14 +95,15 @@

How do I know which tokens I can style for every language?

Language:

+ +

Additionally, you can find a list of standard tokens on this page.

How can I use different highlighting for tokens with the same name in different languages?

Just use a descendant selector, that includes the language class. The default prism.css 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: -


-.token.string {
+	
.token.string {
 	color: #690;
 }
 
diff --git a/tokens.html b/tokens.html
new file mode 100644
index 0000000000..0fa13cce4f
--- /dev/null
+++ b/tokens.html
@@ -0,0 +1,437 @@
+
+
+
+
+
+
+Prism tokens ▲ Prism
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +

Prism tokens

+

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.

+
+ +
+

Standard tokens

+ +

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.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
General purpose
keyword + Pre-defined and reserved words. + +
for (const foo of bar) {
+	if (foo === 'foobar') break;
+	await foo;
+}
+
builtin + Functions/Methods/Classes/Types that are available out of the box. + +
pi = round(float('3.14159'), 2)
+
type SearchFunc = (source: string, subStr: string) => boolean;
+
class-name + The name of a class, interface, trait, or type. + +
class Rectangle extends Square { /* ... */ }
+
public class CameraController : MonoBehaviour { /* ... */ }
+
function + The name of a function or method. + +
function isEven(number) {
+	return Number(number) % 2 === 0;
+}
+const isOdd = (number) => !isEven(number);
+
boolean + True and false, and pairs with similar concepts (e.g. yes and no). + +
console.log(true === false); // prints false
+console.log(true === !false); // prints true
+
number + A numerical value, regardless of base and order, and no matter real or imaginary. + +
print(3.14159 * 42)
+print(1e100 + .001j)
+return foo & 0xdeadbeef
+
string + Literal text, including numbers and symbols and maybe even more special characters. + +
let greeting = 'Hello World!';
+
char + A string that can comprise only a single character, enforced by the language. + +
['A', 'z', '0', '-', '\t', '\u{2728}']
+
symbol + A primitive data type found in some languages, can be thought of as an identifier. + +
#myFirstSymbol "#myFirstSymbol is a symbol in Smalltalk"
+
regex + A regular expression. + +
let entity = /&#x?[\da-f]{1,8};/;
+
url + A link to another page or resource. + +
body {
+	background: url(foo.png);
+}
+
[Prism](https://prismjs.com) is a cool syntax highlighter.
+
operator + A symbol that represents an action or process, whether it's a mathematical operation, logical operation, and so on. + +
x += (y + 4 >> -z === w) ? b ** c : ~a;
+
variable + 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). + +
@nice-blue: #5B83AD;
+@light-blue: lighten(@nice-blue, 20%);
+
echo $STRING
+args=("$@")
+echo ${args[0]} ${args[1]} ${args[2]}
+
constant + The name of a constant. + +
const PI = 3.14159;
+
const THING: u32 = 0xABAD1DEA;
+
fprintf(stdout, "hello world\n");
+
property + An attribute/characteristic or object/map key. + +
body {
+	color: red;
+	line-height: normal;
+}
+
{
+	"data": { "labels": ["foo", "bar"], },
+	"error": null,
+	"status": "Ok"
+}
+
punctuation + Punctuation such as brackets, parentheses, commas, and more. + +
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
+
important + Anything that is important and needs special highlighting. + +
body {
+	color: red !important;
+}
+
# This is a heading. Headings are important.
+
comment + Code comments. + +
<!-- Here's a comment -->
+<style>
+	/* Here's another comment */
+</style>
+<script>
+// Here's yet another comment
+</script>
+
Markup languages
tag + A markup tag (e.g. HTML and XML tags). + +
<p>Hello World!</p>
+
attr-name, attr-value + Kind of like a property of a markup tag and its value/argument respectively. + +
<p id="greeting">Hello World!</p>
+<video width="1280" height="720" allowfullscreen controls>
+	<source src="hello_world.mp4" type="video/mp4" />
+</video>
+
namespace + 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. + +
<html:p foo:bar="baz" foo:weee></html:p>
+
class Foo extends foo.bar.Foo {
+	java.util.List<foo.bar.Foo.Bar> bar(foo.bar.Baz bat) {
+		throw new java.lang.UnsupportedOperationException();
+	}
+}
+
use std::sync::Arc;
+
prolog + The first part of an XML document. + +
<?xml version="1.0" encoding="utf-8"?>
+<svg></svg>
+
doctype + Document type declaration, specific to markup languages. + +
<!DOCTYPE html>
+<html></html>
+
cdata + Character data, specific to markup languages. + +
<ns1:description><![CDATA[
+  CDATA is <not> magical.
+]]></ns1:description>
+
entity + Code used to display reserved characters in markup languages. + +
&amp; &#x2665; &#160; &#x152;
+
Document-markup languages
bold + Bolded text. Mostly found in document-markup languages. + +
**I am bolded text!**
+
italic + Italicised text. Mostly found in document-markup languages. + +
*I am italicised text!*
+
Stylesheets
atrule + Literally @ rules (statements) in stylesheets. + +
@font-family {
+	font-family: Questrial;
+	src: url(questrial.otf);
+}
+@media screen and (min-width: 768px) { /* ... */ }
+
selector + Code that identifies or picks something out of a group to operate on, such as the names of HTML elements in stylesheets. + +
section h1,
+#features li strong,
+header h2,
+footer p { /* ... */ }
+
Diff
inserted, deleted + Added or modified line and deleted line respectively, mainly for diffs. In general, also the idea of something being increased and decreased/removed respectively. + +
--- 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"
+
+
+ +
+

Embedded languages

+ +

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 language-xxxx corresponding to the embedded language.

+ +

Open your browser's developer tools and check out the example below to see it in action!

+ +
<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="utf-8" />
+	<title>I can haz embedded CSS and JS</title>
+	<style>
+		@media print {
+			p { color: red !important; }
+		}
+	</style>
+</head>
+<body>
+	<h1>I can haz embedded CSS and JS</h1>
+	<script>
+	if (true) {
+		console.log('foo');
+	}
+	</script>
+
+</body>
+</html>
+
+ +
+

Non-standard tokens

+ +

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 function-defintion. Since function-definition is not a standard token, you might want to alias it with a standard token such as function, which is semantically similar, and will ensure that Prism's themes will highlight it. Here's an example:

+ +
fn main() {
+	println!("Hello World");
+	another_function();
+}
+
+ +
+ + + + + + + + + + + +