-
Notifications
You must be signed in to change notification settings - Fork 2
/
root_url.ts
53 lines (45 loc) · 1.46 KB
/
root_url.ts
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
import * as Colors from "https://deno.land/[email protected]/fmt/colors.ts";
import {
Context,
Expression,
Invocation,
is_expression,
new_macro,
} from "./tsgen.ts";
import { out_file_absolute, write_file_absolute } from "./out.ts";
/*
General principle of creating rss feeds: use the `rss_add_item` function to add an item to some feed identified by a title (this system supports multiple feeds generated in the same build process).
Later, use the `build_rss_feeds` macro (exactly once) to aggregate all items into one xml document per feed (title).
*/
const statekey = Symbol("RootUrl");
interface RootUrlState {
url: string;
}
export function set_root_url(url: string): Expression {
const macro = new_macro(
(_args, ctx) => {
const state = ctx.state.get(statekey);
if (state) {
ctx.error("Tried to set root url multiple times.");
ctx.halt();
return "";
} else {
ctx.state.set(statekey, <RootUrlState> {
url,
});
return "";
}
}
);
return new Invocation(macro, []);
}
export function get_root_url(ctx: Context): string {
const state = <RootUrlState> ctx.state.get(statekey);
if (state) {
return state.url;
} else {
ctx.error("Tried to access root url without setting it first.");
ctx.halt();
return "root url was not set";
}
}