-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-sty.ts
More file actions
91 lines (77 loc) · 1.91 KB
/
generate-sty.ts
File metadata and controls
91 lines (77 loc) · 1.91 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
import * as Licenses from "../includes/licenses";
/**
* Type for LaTeX source
*/
type LaTeX = string;
/**
* String matching /^[a-z]+$/
*/
type BasicString = string;
/**
* A map between a LaTeX package option and what it should do
*/
export interface Rule {
/**
* The callable option name
*
* Used like `\usepackage[<name>]{avimehra.sty}`.
*/
name: BasicString;
/**
* LaTeX source to be called if the option is provided
*/
if?: LaTeX;
/**
* LaTeX source to be called if the option is missing
*/
else?: LaTeX;
/**
* Commented out documentation
*
* Will be included in the generated sty file above the rule logic.
*/
description?: `%${string}`;
}
/**
* Generate a sty file for a LaTeX package with given rules
* @param rules The options handled by the package
* @param packageName The name of the package that is provided
* @param license The license for the file as a LaTeX comment
* @param header Any text in the source code above the license, usually as a LaTeX comment
* @return The sty file contents
*/
const generateSty = (
rules: Rule[],
packageName = "avimehra",
license = Licenses.MIT,
header = "% avimehra.sty"
): LaTeX => {
const options = rules.map(
({
name,
}) => `\\newif\\ifavimehragenerated${name}\\avimehragenerated${name}false
\\DeclareOption{${name}}{\\avimehragenerated${name}true}`
);
// cannot indent entire blocks because they may internally depend on whitespace (e.g. verbatim)
const cases = rules.map(
({ name, if: ifCommand = "", else: elseCommand = "", description }) => `${
description
? `%%%%%%%%%%%%%%%%
${description}
%%%%%%%%%%%%%%%%
`
: ""
}\\ifavimehragenerated${name}
${ifCommand}
\\else
${elseCommand}
\\fi`
);
return `${header}
${license}
\\ProvidesPackage{${packageName}}
${options.join("\n\n")}
\\ProcessOptions\\relax
${cases.join("\n\n")}`;
};
export default generateSty;