-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfig.odin
180 lines (147 loc) · 5.06 KB
/
config.odin
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
package odin_html_docs
import "core:encoding/json"
import "core:fmt"
import "core:log"
import "core:os"
import "core:path/filepath"
import "core:slice"
import "core:strings"
import doc "core:odin/doc-format"
Config :: struct {
hide_base: bool,
hide_core: bool,
_collections: map[string]Collection `json:"collections"`,
url_prefix: string,
// -- Start non configurable --
header: ^doc.Header,
files: []doc.File,
pkgs: []doc.Pkg,
entities: []doc.Entity,
types: []doc.Type,
// Maps are unordered, we want an order to places where it matters, like the homepage.
// Why is 'collections' not a slice? Because the JSON package overwrites the full slice when
// you unmarshal into it, with a map, when unmarshalling into it, the entries are added to existing ones.
collections: [dynamic]^Collection,
pkg_to_collection: map[^doc.Pkg]^Collection,
pkg_to_header: map[^doc.Pkg]^doc.Header,
handled_packages: map[string]struct{},
pkgs_line_docs: map[string]string,
}
Collection :: struct {
name: string,
source_url: string,
base_url: string,
root_path: string,
license: Collection_License,
home: Collection_Home,
// Hides the collection from navigation but can still be
// linked to.
hidden: bool,
// -- Start non configurable --
root: ^Dir_Node,
pkgs: map[string]^doc.Pkg,
pkg_to_path: map[^doc.Pkg]string,
pkg_entries_map: map[^doc.Pkg]Pkg_Entries,
}
Collection_License :: struct {
text: string,
url: string,
}
Collection_Home :: struct {
title: Maybe(string),
description: Maybe(string),
embed_readme: Maybe(string),
}
Collection_Error :: string
collection_validate :: proc(c: ^Collection) -> Maybe(Collection_Error) {
if c.name == "" {
return "collection requires the key \"name\" to be set to the name of the collection, example: \"core\""
}
if c.source_url == "" {
return "collection requires the key \"source_url\" to be set to a URL that points to the root of collection on a website like GitHub, example: \"https://github.com/odin-lang/Odin/tree/master/core\""
}
if c.base_url == "" {
return "collection requires the key \"base_url\" to be set to the relative URL to your collection, example: \"/core\""
}
if c.license.text == "" {
return "collection requires the key \"license.text\" to be set to the name of the license of your collection, example: \"BSD-3-Clause\""
}
if c.license.url == "" {
return "collection requires the key \"license.url\" to be set to a URL that points to the license of your collection, example: \"https://github.com/odin-lang/Odin/tree/master/LICENSE\""
}
if c.root_path == "" {
return "collection requires the key \"root_path\" to be set to part of the path of all packages in the collection that should be removed, you can use $ODIN_ROOT, or $PWD as variables"
}
if strings.contains_rune(c.name, '/') {
return "collection name should not contain slashes"
}
if !strings.has_prefix(c.base_url, "/") {
return "collection base_url should start with a slash"
}
c.base_url = strings.trim_suffix(c.base_url, "/")
c.source_url = strings.trim_suffix(c.source_url, "/")
new_root_path := config_do_replacements(c.root_path)
delete(c.root_path)
c.root_path = new_root_path
if rm, ok := c.home.embed_readme.?; ok {
new_rm := config_do_replacements(rm)
delete(rm)
c.home.embed_readme = new_rm
}
return nil
}
config_default :: proc() -> (c: Config) {
err := json.unmarshal(#load("resources/odin-doc.json"), &c)
fmt.assertf(err == nil, "Unable to load default config: %v", err)
config_sort_collections(&c)
return
}
config_merge_from_file :: proc(c: ^Config, file: string) -> (file_ok: bool, err: json.Unmarshal_Error) {
data: []byte
data, file_ok = os.read_entire_file_from_filename(file)
if !file_ok do return
err = json.unmarshal(data, c)
for _, &collection in c._collections {
if c.hide_core && (collection.name == "core" || collection.name == "vendor") {
collection.hidden = true
}
if c.hide_base && (collection.name == "base") {
collection.hidden = true
}
if c.url_prefix != "" {
new_base_url := strings.concatenate({c.url_prefix, collection.base_url})
delete(collection.base_url)
collection.base_url = new_base_url
}
}
config_sort_collections(c)
return
}
config_sort_collections :: proc(c: ^Config) {
clear(&c.collections)
for _, &collection in c._collections {
append(&c.collections, &collection)
}
slice.sort_by(
c.collections[:],
proc(a, b: ^Collection) -> bool { return a.name < b.name },
)
}
// Replaces $ODIN_ROOT with ODIN_ROOT, and turns it into an absolute path.
config_do_replacements :: proc(path: string) -> string {
res, allocated := strings.replace(path, "$ODIN_ROOT", ODIN_ROOT, 1)
abs, ok := filepath.abs(res)
if !ok {
log.warnf("Could not resolve absolute path from %q", res)
return res
}
if allocated do delete(res)
// The `odin doc` spits paths out the other way.
when ODIN_OS == .Windows {
replaced, replace_was_alloc := strings.replace_all(abs, "\\", "/")
if replace_was_alloc do delete(abs)
return replaced
} else {
return abs
}
}