-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcode.ts
More file actions
144 lines (137 loc) · 4.22 KB
/
code.ts
File metadata and controls
144 lines (137 loc) · 4.22 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
/** Admonitions to visualise programming codes */
import type Token from "markdown-it/lib/token"
import { Directive, IDirectiveData } from "./main"
import {
class_option,
flag,
int,
optional_int,
unchanged,
unchanged_required
} from "./options"
// TODO add Highlight directive
/** Mark up content of a code block
*
* Adapted from sphinx/directives/patches.py
*/
export class Code extends Directive {
public required_arguments = 0
public optional_arguments = 1
public final_argument_whitespace = false
public has_content = true
public option_spec = {
/** Add line numbers, optionally starting from a particular number. */
"number-lines": optional_int,
/** Ignore minor errors on highlighting */
force: flag,
name: unchanged,
class: class_option
}
run(data: IDirectiveData<keyof Code["option_spec"]>): Token[] {
// TODO handle options
this.assert_has_content(data)
const token = this.createToken("fence", "code", 0, {
// TODO if not specified, the language should come from a central configuration "highlight_language"
info: data.args ? data.args[0] : "",
content: data.body,
map: data.bodyMap
})
if (data.options["number-lines"]) {
token.attrSet("number-lines", data.options["number-lines"])
}
if (data.options.force) {
token.attrSet("force", data.options.force)
}
if (data.options.name) {
token.attrSet("name", data.options.name)
}
if (data.options.class) {
token.attrJoin("class", data.options.class.join(" "))
}
return [token]
}
}
/** Mark up content of a code block, with more settings
*
* Adapted from sphinx/directives/code.py
*/
export class CodeBlock extends Directive {
public required_arguments = 0
public optional_arguments = 1
public final_argument_whitespace = false
public has_content = true
public option_spec = {
/** Add line numbers. */
linenos: flag,
/** Start line numbering from a particular value. */
"lineno-start": int,
/** Strip indentation characters from the code block.
* When number given, leading N characters are removed
*/
dedent: optional_int,
/** Emphasize particular lines (comma-separated numbers) */
"emphasize-lines": unchanged_required,
caption: unchanged_required,
/** Ignore minor errors on highlighting */
force: flag,
name: unchanged,
class: class_option
}
run(data: IDirectiveData<keyof CodeBlock["option_spec"]>): Token[] {
// TODO handle options
this.assert_has_content(data)
const token = this.createToken("fence", "code", 0, {
// TODO if not specified, the language should come from a central configuration "highlight_language"
info: data.args ? data.args[0] : "",
content: data.body,
map: data.bodyMap
})
if (data.options.linenos === null) {
token.attrSet("linenos", "true")
}
if (data.options["lineno-start"]) {
token.attrSet("lineno-start", data.options["lineno-start"])
}
if (data.options.dedent) {
token.attrSet("dedent", data.options.dedent)
}
if (data.options["emphasize-lines"]) {
token.attrSet("emphasize-lines", data.options["emphasize-lines"])
}
if (data.options.caption) {
token.attrSet("caption", data.options.caption)
}
if (data.options.force) {
token.attrSet("force", data.options.force)
}
if (data.options.name) {
token.attrSet("name", data.options.name)
}
if (data.options.class) {
token.attrJoin("class", data.options.class.join(" "))
}
return [token]
}
}
/** A code cell is a special MyST based cell, signifying executable code. */
export class CodeCell extends Directive {
public required_arguments = 0
public optional_arguments = 1
public final_argument_whitespace = false
public has_content = true
public rawOptions = true
run(data: IDirectiveData<keyof CodeCell["option_spec"]>): Token[] {
// TODO store options and the fact that this is a code cell rather than a fence?
const token = this.createToken("fence", "code", 0, {
info: data.args ? data.args[0] : "",
content: data.body,
map: data.bodyMap
})
return [token]
}
}
export const code = {
code: Code,
"code-block": CodeBlock,
"code-cell": CodeCell
}