-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopher_item.ts
76 lines (66 loc) · 2.61 KB
/
gopher_item.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {CRLF} from './gopher_common.ts';
import {ItemType, UnknownType} from './gopher_types.ts';
/** Contains all of the Gopher+ attributes for a GopherItem. */
export class ItemAttributes {
/** The name of the block, e.g. `INFO`, `VIEWS` etc. No leading `+`. */
name: string = '';
/** The attribute block dedscriptor - e.g. `+INFO: <descriptor goes here>`. */
descriptor!: string;
/**
* The lines of the attribute block. This is typically treated as key values,
* but may just be strings, so we have both the raw string block and a map
* for convenience.
*/
rawLines: string = '';
lines: Map<string, string> = new Map<string, string>();
}
/** Represents an item in a Gopher menu. */
export abstract class GopherItem {
/** The type of this menu item - e.g. 'i' or '1' etc. */
type: ItemType = new UnknownType('?');
/**
* The name or description of this item. Typically this is the user-visible
* part of the menu item that is shown in Gopher clients.
*/
name: string = '';
/** Menu selector itself, e.g. `/foo` */
selector: string = 'fake';
/** Hostname */
hostname: string = '';
/** Port number. */
port: number = 0;
/** Flag inidicating if this item supports Gopher+ */
gopherPlus: boolean = false;
/** Original raw string for this item. */
original: string = '';
/** Gopher+ Attribute map for this item. May not be populated. */
attributes: Map<string, ItemAttributes> = new Map<string, ItemAttributes>();
/** Parses a string and converts it to attributes. */
parseAttributes(rawAttributesString:string) {
// Spec does not say if attributes should be CRLF or just LF?
let lines = rawAttributesString.split(`\n`);
// Shift off the first line (the Gopher+ status) since we don't care.
lines.shift();
if (!lines) return;
let lastAttribute = '';
for (const line of lines) {
const separator = line.indexOf(':');
if (line.startsWith('+')) {
const attributeName = line.substring(1, separator);
const descriptor = line.substring(separator + 1).trim();
const attr = new ItemAttributes();
attr.name = attributeName;
attr.descriptor = descriptor;
this.attributes.set(attributeName, attr);
lastAttribute = attributeName;
} else {
if (!lastAttribute) continue;
this.attributes.get(lastAttribute)!.rawLines += `${line}${CRLF}`;
const key = line.substring(0, separator).trim();
const value = line.substring(separator + 1).trim();
if (!key || !value) continue;
this.attributes.get(lastAttribute)!.lines.set(key, value);
}
}
}
}