File: main.bs

package info (click to toggle)
storm-lang 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 51,832 kB
  • sloc: ansic: 261,420; cpp: 138,870; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (187 lines) | stat: -rw-r--r-- 4,373 bytes parent folder | download | duplicates (3)
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
use core:io;
use markdown;
use lang:bs:macro;

// Options when generating documentation.
class Options {
	// Title of the documentation as a whole.
	Str title;

	// Theme to use for the generated HTML.
	Str theme;

	// Root of the input markdown tree.
	Url root;

	// Output location of the generated HTML.
	Url output;

	// Remove numbers from the file names in the final output?
	Bool stripNumbers;

	// Remove the output directory before generating documentation?
	Bool clearOutput;

	// No progress output?
	Bool noProgress;

	// Additional options passed to the theme.
	Str->Str themeOptions;

	// Create an empty options-set, indented to be filled in later.
	init() {
		init {}
	}

	// Create, specify the most common parameters.
	init(Str title, Str theme, Url root, Url output) {
		init {
			title = title;
			theme = theme;
			root = root;
			output = output;
			stripNumbers = true;
			clearOutput = false;
			noProgress = false;
		}
	}

	// Apply an option from the command-line.
	void applyOption(Str option) {
		var eq = option.find('=');
		Str key = option.cut(option.begin, eq);
		Str val = option.cut(eq + 1);

		if (key == "--clear") {
			clearOutput = true;
		} else if (key == "-q" | key == "--quiet") {
			noProgress = true;
		} else if (key == "-n" | key == "--numbers") {
			stripNumbers = false;
		} else if (key.startsWith("--")) {
			themeOptions.put(key.cut(key.begin + 2), val);
		}
	}

	// Apply options from the array. Returns the first non-processed index.
	Nat applyOptions(Str[] options, Nat start) {
		while (start < options.count) {
			if (!options[start].startsWith("-"))
				break;
			applyOption(options[start]);
			start++;
		}
		return start;
	}
}

// Generate documentation programmatically. The `options` object stores all options for the
// generation.
void generate(Options options) {
	Theme theme = loadTheme(options.theme);

	{
		Str->Str themeOptions = options.themeOptions.clone;
		theme.options(themeOptions);
		for (k, v in themeOptions) {
			print("WARNING: Unknown option: --${k}");
		}
	}


	if (!options.noProgress)
		print("Loading documentation from ${options.root}...");

	Moment loadStart;

	Tree tree(options.title, options.root);
	tree.config.stripNumbers = options.stripNumbers;
	tree.load();

	Moment loadEnd;

	if (options.clearOutput) {
		if (!options.noProgress)
			print("Removing contents of ${options.output}...");
		deleteTree(options.output);
	}

	if (!options.noProgress)
		print("Generating output to ${options.output}...");

	Moment outStart;

	tree.output(theme, options.output);

	Moment outEnd;

	if (!options.noProgress) {
		print("Done! Loaded sources in: ${loadEnd - loadStart}, outputted HTML in: ${outEnd - outStart}");
	}
}

// Generate documentation programmatically. Wrapper for the overload that accepts an `Options`
// parameter with explicit parameters. This overload does, however, not allow specifying additional
// options to the documentation.
void generate(Str title, Str theme, Url root, Url output) {
	generate(Options(title, theme, root, output));
}

// Print help.
private void printHelp() {
	print("Usage: <title> <theme> <input> <output>");
	print("--quiet, -q - no output");
	print("--clear     - clear <output> before generating new documentation");
	print("--numbers   - keep numbers in output file names");
}

// Main entry point from the command-line.
Int generate() {
	Options options;
	var args = argv;
	Nat pos = options.applyOptions(args, 0);
	if (pos >= args.count) {
		printHelp();
		return 1;
	}
	options.title = args[pos++];

	pos = options.applyOptions(args, pos);
	if (pos >= args.count) {
		printHelp();
		return 1;
	}
	options.theme = args[pos++];

	pos = options.applyOptions(args, pos);
	if (pos >= args.count) {
		printHelp();
		return 1;
	}
	options.root = parsePath(args[pos++]).makeAbsolute(cwdUrl);

	pos = options.applyOptions(args, pos);
	if (pos >= args.count) {
		printHelp();
		return 1;
	}
	options.output = parsePath(args[pos++]).makeAbsolute(cwdUrl);

	pos = options.applyOptions(args, pos);

	if (pos != args.count) {
		printHelp();
		return 1;
	}

	generate(options);
	return 0;
}

// Convenience function to generate the Storm documentation.
Int generateStormDoc() {
	print("On Debian systems, the HTML version is available");
	print("in /usr/share/doc/storm-lang/html, if the package");
	print("storm-lang-doc is installed.");
	return 1;
}