UNPKG

2.05 kBJavaScriptView Raw
1// @flow
2import defineFunction from "../defineFunction";
3import buildCommon from "../buildCommon";
4import mathMLTree from "../mathMLTree";
5import ParseError from "../ParseError";
6
7import type {ParseNode} from "../parseNode";
8
9defineFunction({
10 type: "verb",
11 names: ["\\verb"],
12 props: {
13 numArgs: 0,
14 allowedInText: true,
15 },
16 handler(context, args, optArgs) {
17 // \verb and \verb* are dealt with directly in Parser.js.
18 // If we end up here, it's because of a failure to match the two delimiters
19 // in the regex in Lexer.js. LaTeX raises the following error when \verb is
20 // terminated by end of line (or file).
21 throw new ParseError(
22 "\\verb ended by end of line instead of matching delimiter");
23 },
24 htmlBuilder(group, options) {
25 const text = makeVerb(group);
26 const body = [];
27 // \verb enters text mode and therefore is sized like \textstyle
28 const newOptions = options.havingStyle(options.style.text());
29 for (let i = 0; i < text.length; i++) {
30 let c = text[i];
31 if (c === '~') {
32 c = '\\textasciitilde';
33 }
34 body.push(buildCommon.makeSymbol(c, "Typewriter-Regular",
35 group.mode, newOptions, ["mord", "texttt"]));
36 }
37 return buildCommon.makeSpan(
38 ["mord", "text"].concat(newOptions.sizingClasses(options)),
39 buildCommon.tryCombineChars(body),
40 newOptions,
41 );
42 },
43 mathmlBuilder(group, options) {
44 const text = new mathMLTree.TextNode(makeVerb(group));
45 const node = new mathMLTree.MathNode("mtext", [text]);
46 node.setAttribute("mathvariant", "monospace");
47 return node;
48 },
49});
50
51/**
52 * Converts verb group into body string.
53 *
54 * \verb* replaces each space with an open box \u2423
55 * \verb replaces each space with a no-break space \xA0
56 */
57const makeVerb = (group: ParseNode<"verb">): string =>
58 group.body.replace(/ /g, group.star ? '\u2423' : '\xA0');