odin_html_docs_main.odin 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. package odin_html_docs
  2. import doc "core:odin/doc-format"
  3. import "core:fmt"
  4. import "core:io"
  5. import "core:os"
  6. import "core:strings"
  7. import "core:path/slashpath"
  8. import "core:sort"
  9. import "core:slice"
  10. GITHUB_CORE_URL :: "https://github.com/odin-lang/Odin/tree/master/core"
  11. BASE_CORE_URL :: "/core"
  12. header: ^doc.Header
  13. files: []doc.File
  14. pkgs: []doc.Pkg
  15. entities: []doc.Entity
  16. types: []doc.Type
  17. pkgs_to_use: map[string]^doc.Pkg // trimmed path
  18. pkg_to_path: map[^doc.Pkg]string // trimmed path
  19. array :: proc(a: $A/doc.Array($T)) -> []T {
  20. return doc.from_array(header, a)
  21. }
  22. str :: proc(s: $A/doc.String) -> string {
  23. return doc.from_string(header, s)
  24. }
  25. errorf :: proc(format: string, args: ..any) -> ! {
  26. fmt.eprintf("%s ", os.args[0])
  27. fmt.eprintf(format, ..args)
  28. fmt.eprintln()
  29. os.exit(1)
  30. }
  31. base_type :: proc(t: doc.Type) -> doc.Type {
  32. t := t
  33. for {
  34. if t.kind != .Named {
  35. break
  36. }
  37. t = types[array(t.types)[0]]
  38. }
  39. return t
  40. }
  41. is_type_untyped :: proc(type: doc.Type) -> bool {
  42. if type.kind == .Basic {
  43. flags := transmute(doc.Type_Flags_Basic)type.flags
  44. return .Untyped in flags
  45. }
  46. return false
  47. }
  48. common_prefix :: proc(strs: []string) -> string {
  49. if len(strs) == 0 {
  50. return ""
  51. }
  52. n := max(int)
  53. for str in strs {
  54. n = min(n, len(str))
  55. }
  56. prefix := strs[0][:n]
  57. for str in strs[1:] {
  58. for len(prefix) != 0 && str[:len(prefix)] != prefix {
  59. prefix = prefix[:len(prefix)-1]
  60. }
  61. if len(prefix) == 0 {
  62. break
  63. }
  64. }
  65. return prefix
  66. }
  67. recursive_make_directory :: proc(path: string, prefix := "") {
  68. head, _, tail := strings.partition(path, "/")
  69. path_to_make := head
  70. if prefix != "" {
  71. path_to_make = fmt.tprintf("%s/%s", prefix, head)
  72. }
  73. os.make_directory(path_to_make, 0)
  74. if tail != "" {
  75. recursive_make_directory(tail, path_to_make)
  76. }
  77. }
  78. write_html_header :: proc(w: io.Writer, title: string) {
  79. fmt.wprintf(w, string(#load("header.txt.html")), title)
  80. }
  81. write_html_footer :: proc(w: io.Writer, include_directory_js: bool) {
  82. fmt.wprintf(w, "\n")
  83. io.write(w, #load("footer.txt.html"))
  84. if false && include_directory_js {
  85. io.write_string(w, `
  86. <script type="text/javascript">
  87. (function (win, doc) {
  88. 'use strict';
  89. if (!doc.querySelectorAll || !win.addEventListener) {
  90. // doesn't cut the mustard.
  91. return;
  92. }
  93. let toggles = doc.querySelectorAll('[aria-controls]');
  94. for (let i = 0; i < toggles.length; i = i + 1) {
  95. let toggleID = toggles[i].getAttribute('aria-controls');
  96. if (doc.getElementById(toggleID)) {
  97. let togglecontent = doc.getElementById(toggleID);
  98. togglecontent.setAttribute('aria-hidden', 'true');
  99. togglecontent.setAttribute('tabindex', '-1');
  100. toggles[i].setAttribute('aria-expanded', 'false');
  101. }
  102. }
  103. function toggle(ev) {
  104. ev = ev || win.event;
  105. var target = ev.target || ev.srcElement;
  106. if (target.hasAttribute('data-aria-owns')) {
  107. let toggleIDs = target.getAttribute('data-aria-owns').match(/[^ ]+/g);
  108. toggleIDs.forEach(toggleID => {
  109. if (doc.getElementById(toggleID)) {
  110. ev.preventDefault();
  111. let togglecontent = doc.getElementById(toggleID);
  112. if (togglecontent.getAttribute('aria-hidden') == 'true') {
  113. togglecontent.setAttribute('aria-hidden', 'false');
  114. target.setAttribute('aria-expanded', 'true');
  115. if (target.tagName == 'A') {
  116. togglecontent.focus();
  117. }
  118. } else {
  119. togglecontent.setAttribute('aria-hidden', 'true');
  120. target.setAttribute('aria-expanded', 'false');
  121. }
  122. }
  123. })
  124. }
  125. }
  126. doc.addEventListener('click', toggle, false);
  127. }(this, this.document));
  128. </script>`)
  129. }
  130. fmt.wprintf(w, "</body>\n</html>\n")
  131. }
  132. main :: proc() {
  133. if len(os.args) != 2 {
  134. errorf("expected 1 .odin-doc file")
  135. }
  136. data, ok := os.read_entire_file(os.args[1])
  137. if !ok {
  138. errorf("unable to read file:", os.args[1])
  139. }
  140. err: doc.Reader_Error
  141. header, err = doc.read_from_bytes(data)
  142. switch err {
  143. case .None:
  144. case .Header_Too_Small:
  145. errorf("file is too small for the file format")
  146. case .Invalid_Magic:
  147. errorf("invalid magic for the file format")
  148. case .Data_Too_Small:
  149. errorf("data is too small for the file format")
  150. case .Invalid_Version:
  151. errorf("invalid file format version")
  152. }
  153. files = array(header.files)
  154. pkgs = array(header.pkgs)
  155. entities = array(header.entities)
  156. types = array(header.types)
  157. {
  158. fullpaths: [dynamic]string
  159. defer delete(fullpaths)
  160. for pkg in pkgs[1:] {
  161. append(&fullpaths, str(pkg.fullpath))
  162. }
  163. path_prefix := common_prefix(fullpaths[:])
  164. pkgs_to_use = make(map[string]^doc.Pkg)
  165. fullpath_loop: for fullpath, i in fullpaths {
  166. path := strings.trim_prefix(fullpath, path_prefix)
  167. if !strings.has_prefix(path, "core/") {
  168. continue fullpath_loop
  169. }
  170. pkg := &pkgs[i+1]
  171. if len(array(pkg.entities)) == 0 {
  172. continue fullpath_loop
  173. }
  174. trimmed_path := strings.trim_prefix(path, "core/")
  175. if strings.has_prefix(trimmed_path, "sys") {
  176. continue fullpath_loop
  177. }
  178. pkgs_to_use[trimmed_path] = pkg
  179. }
  180. for path, pkg in pkgs_to_use {
  181. pkg_to_path[pkg] = path
  182. }
  183. }
  184. b := strings.make_builder()
  185. defer strings.destroy_builder(&b)
  186. w := strings.to_writer(&b)
  187. {
  188. strings.reset_builder(&b)
  189. write_html_header(w, "core library - pkg.odin-lang.org")
  190. write_core_directory(w)
  191. write_html_footer(w, true)
  192. os.make_directory("core", 0)
  193. os.write_entire_file("core/index.html", b.buf[:])
  194. }
  195. for path, pkg in pkgs_to_use {
  196. strings.reset_builder(&b)
  197. write_html_header(w, fmt.tprintf("package %s - pkg.odin-lang.org", path))
  198. write_pkg(w, path, pkg)
  199. write_html_footer(w, false)
  200. recursive_make_directory(path, "core")
  201. os.write_entire_file(fmt.tprintf("core/%s/index.html", path), b.buf[:])
  202. }
  203. }
  204. Dir_Node :: struct {
  205. dir: string,
  206. path: string,
  207. name: string,
  208. pkg: ^doc.Pkg,
  209. children: [dynamic]^Dir_Node,
  210. }
  211. generate_directory_tree :: proc() -> (root: ^Dir_Node) {
  212. sort_tree :: proc(node: ^Dir_Node) {
  213. slice.sort_by_key(node.children[:], proc(node: ^Dir_Node) -> string {return node.name})
  214. for child in node.children {
  215. sort_tree(child)
  216. }
  217. }
  218. root = new(Dir_Node)
  219. root.children = make([dynamic]^Dir_Node)
  220. children := make([dynamic]^Dir_Node)
  221. for path, pkg in pkgs_to_use {
  222. dir, _, inner := strings.partition(path, "/")
  223. if inner == "" {
  224. node := new_clone(Dir_Node{
  225. dir = dir,
  226. name = dir,
  227. path = path,
  228. pkg = pkg,
  229. })
  230. append(&root.children, node)
  231. } else {
  232. node := new_clone(Dir_Node{
  233. dir = dir,
  234. name = inner,
  235. path = path,
  236. pkg = pkg,
  237. })
  238. append(&children, node)
  239. }
  240. }
  241. child_loop: for child in children {
  242. dir, _, inner := strings.partition(child.path, "/")
  243. for node in root.children {
  244. if node.dir == dir {
  245. append(&node.children, child)
  246. continue child_loop
  247. }
  248. }
  249. parent := new_clone(Dir_Node{
  250. dir = dir,
  251. name = dir,
  252. path = dir,
  253. pkg = nil,
  254. })
  255. append(&root.children, parent)
  256. append(&parent.children, child)
  257. }
  258. sort_tree(root)
  259. return
  260. }
  261. write_core_directory :: proc(w: io.Writer) {
  262. root := generate_directory_tree()
  263. fmt.wprintln(w, `<div class="row odin-main">`)
  264. defer fmt.wprintln(w, `</div>`)
  265. fmt.wprintln(w, `<article class="col-lg-12 p-4">`)
  266. defer fmt.wprintln(w, `</article>`)
  267. fmt.wprintln(w, "<article>")
  268. fmt.wprintln(w, "<header>")
  269. fmt.wprintln(w, "<h1>Core Library Collection</h1>")
  270. fmt.wprintln(w, "</header>")
  271. fmt.wprintln(w, "</article>")
  272. fmt.wprintln(w, "<div>")
  273. fmt.wprintln(w, "\t<table class=\"doc-directory mt-4 mb-4\">")
  274. fmt.wprintln(w, "\t\t<tbody>")
  275. for dir in root.children {
  276. if len(dir.children) != 0 {
  277. fmt.wprint(w, `<tr aria-controls="`)
  278. for child in dir.children {
  279. fmt.wprintf(w, "pkg-%s ", str(child.pkg.name))
  280. }
  281. fmt.wprint(w, `" class="directory-pkg"><td class="pkg-line pkg-name" data-aria-owns="`)
  282. for child in dir.children {
  283. fmt.wprintf(w, "pkg-%s ", str(child.pkg.name))
  284. }
  285. fmt.wprintf(w, `" id="pkg-%s">`, dir.dir)
  286. } else {
  287. fmt.wprintf(w, `<tr id="pkg-%s" class="directory-pkg"><td class="pkg-name">`, dir.dir)
  288. }
  289. if dir.pkg != nil {
  290. fmt.wprintf(w, `<a href="%s/%s">%s</a>`, BASE_CORE_URL, dir.path, dir.name)
  291. } else {
  292. fmt.wprintf(w, "%s", dir.name)
  293. }
  294. io.write_string(w, `</td>`)
  295. io.write_string(w, `<td class="pkg-line pkg-line-doc">`)
  296. if dir.pkg != nil {
  297. line_doc, _, _ := strings.partition(str(dir.pkg.docs), "\n")
  298. line_doc = strings.trim_space(line_doc)
  299. if line_doc != "" {
  300. write_doc_line(w, line_doc)
  301. }
  302. }
  303. io.write_string(w, `</td>`)
  304. fmt.wprintf(w, "</tr>\n")
  305. for child in dir.children {
  306. assert(child.pkg != nil)
  307. fmt.wprintf(w, `<tr id="pkg-%s" class="directory-pkg directory-child"><td class="pkg-line pkg-name">`, str(child.pkg.name))
  308. fmt.wprintf(w, `<a href="%s/%s/">%s</a>`, BASE_CORE_URL, child.path, child.name)
  309. io.write_string(w, `</td>`)
  310. line_doc, _, _ := strings.partition(str(child.pkg.docs), "\n")
  311. line_doc = strings.trim_space(line_doc)
  312. io.write_string(w, `<td class="pkg-line pkg-line-doc">`)
  313. if line_doc != "" {
  314. write_doc_line(w, line_doc)
  315. }
  316. io.write_string(w, `</td>`)
  317. fmt.wprintf(w, "</td>")
  318. fmt.wprintf(w, "</tr>\n")
  319. }
  320. }
  321. fmt.wprintln(w, "\t\t</tbody>")
  322. fmt.wprintln(w, "\t</table>")
  323. fmt.wprintln(w, "</div>")
  324. }
  325. is_entity_blank :: proc(e: doc.Entity_Index) -> bool {
  326. name := str(entities[e].name)
  327. return name == ""
  328. }
  329. write_where_clauses :: proc(w: io.Writer, where_clauses: []doc.String) {
  330. if len(where_clauses) != 0 {
  331. io.write_string(w, " where ")
  332. for clause, i in where_clauses {
  333. if i > 0 {
  334. io.write_string(w, ", ")
  335. }
  336. io.write_string(w, str(clause))
  337. }
  338. }
  339. }
  340. Write_Type_Flag :: enum {
  341. Is_Results,
  342. Variadic,
  343. Allow_Indent,
  344. Poly_Names,
  345. }
  346. Write_Type_Flags :: distinct bit_set[Write_Type_Flag]
  347. Type_Writer :: struct {
  348. w: io.Writer,
  349. pkg: doc.Pkg_Index,
  350. indent: int,
  351. generic_scope: map[string]bool,
  352. }
  353. write_type :: proc(using writer: ^Type_Writer, type: doc.Type, flags: Write_Type_Flags) {
  354. write_param_entity :: proc(using writer: ^Type_Writer, e, next_entity: ^doc.Entity, flags: Write_Type_Flags, name_width := 0) {
  355. name := str(e.name)
  356. write_padding :: proc(w: io.Writer, name: string, name_width: int) {
  357. for _ in 0..<name_width-len(name) {
  358. io.write_byte(w, ' ')
  359. }
  360. }
  361. if .Param_Using in e.flags { io.write_string(w, "using ") }
  362. if .Param_Const in e.flags { io.write_string(w, "#const ") }
  363. if .Param_Auto_Cast in e.flags { io.write_string(w, "#auto_cast ") }
  364. if .Param_CVararg in e.flags { io.write_string(w, "#c_vararg ") }
  365. if .Param_No_Alias in e.flags { io.write_string(w, "#no_alias ") }
  366. if .Param_Any_Int in e.flags { io.write_string(w, "#any_int ") }
  367. init_string := str(e.init_string)
  368. switch {
  369. case init_string == "#caller_location":
  370. assert(name != "")
  371. io.write_string(w, name)
  372. io.write_string(w, " := ")
  373. fmt.wprintf(w, `<a href="%s/runtime/#Source_Code_Location">`, BASE_CORE_URL)
  374. io.write_string(w, init_string)
  375. io.write_string(w, `</a>`)
  376. case strings.has_prefix(init_string, "context."):
  377. io.write_string(w, name)
  378. io.write_string(w, " := ")
  379. fmt.wprintf(w, `<a href="%s/runtime/#Context">`, BASE_CORE_URL)
  380. io.write_string(w, init_string)
  381. io.write_string(w, `</a>`)
  382. case:
  383. the_type := types[e.type]
  384. type_flags := flags - {.Is_Results}
  385. if .Param_Ellipsis in e.flags {
  386. type_flags += {.Variadic}
  387. }
  388. #partial switch e.kind {
  389. case .Constant:
  390. assert(name != "")
  391. io.write_byte(w, '$')
  392. io.write_string(w, name)
  393. if name != "" && init_string == "" && next_entity != nil && e.field_group_index >= 0 {
  394. if e.field_group_index == next_entity.field_group_index && e.type == next_entity.type {
  395. return
  396. }
  397. }
  398. generic_scope[name] = true
  399. if !is_type_untyped(the_type) {
  400. io.write_string(w, ": ")
  401. write_padding(w, name, name_width)
  402. write_type(writer, the_type, type_flags)
  403. io.write_string(w, " = ")
  404. io.write_string(w, init_string)
  405. } else {
  406. io.write_string(w, " := ")
  407. io.write_string(w, init_string)
  408. }
  409. return
  410. case .Variable:
  411. if name != "" && init_string == "" && next_entity != nil && e.field_group_index >= 0 {
  412. if e.field_group_index == next_entity.field_group_index && e.type == next_entity.type {
  413. io.write_string(w, name)
  414. return
  415. }
  416. }
  417. if name != "" {
  418. io.write_string(w, name)
  419. io.write_string(w, ": ")
  420. write_padding(w, name, name_width)
  421. }
  422. write_type(writer, the_type, type_flags)
  423. case .Type_Name:
  424. io.write_byte(w, '$')
  425. io.write_string(w, name)
  426. generic_scope[name] = true
  427. io.write_string(w, ": ")
  428. write_padding(w, name, name_width)
  429. if the_type.kind == .Generic {
  430. io.write_string(w, "typeid")
  431. if ts := array(the_type.types); len(ts) == 1 {
  432. io.write_byte(w, '/')
  433. write_type(writer, types[ts[0]], type_flags)
  434. }
  435. } else {
  436. write_type(writer, the_type, type_flags)
  437. }
  438. }
  439. if init_string != "" {
  440. io.write_string(w, " = ")
  441. io.write_string(w, init_string)
  442. }
  443. }
  444. }
  445. write_poly_params :: proc(using writer: ^Type_Writer, type: doc.Type, flags: Write_Type_Flags) {
  446. if type.polymorphic_params != 0 {
  447. io.write_byte(w, '(')
  448. write_type(writer, types[type.polymorphic_params], flags+{.Poly_Names})
  449. io.write_byte(w, ')')
  450. }
  451. write_where_clauses(w, array(type.where_clauses))
  452. }
  453. do_indent :: proc(using writer: ^Type_Writer, flags: Write_Type_Flags) {
  454. if .Allow_Indent not_in flags {
  455. return
  456. }
  457. for _ in 0..<indent {
  458. io.write_byte(w, '\t')
  459. }
  460. }
  461. do_newline :: proc(using writer: ^Type_Writer, flags: Write_Type_Flags) {
  462. if .Allow_Indent in flags {
  463. io.write_byte(w, '\n')
  464. }
  465. }
  466. calc_name_width :: proc(type_entites: []doc.Entity_Index) -> (name_width: int) {
  467. for entity_index in type_entites {
  468. e := &entities[entity_index]
  469. name := str(e.name)
  470. name_width = max(len(name), name_width)
  471. }
  472. return
  473. }
  474. type_entites := array(type.entities)
  475. type_types := array(type.types)
  476. switch type.kind {
  477. case .Invalid:
  478. // ignore
  479. case .Basic:
  480. type_flags := transmute(doc.Type_Flags_Basic)type.flags
  481. if is_type_untyped(type) {
  482. io.write_string(w, str(type.name))
  483. } else {
  484. fmt.wprintf(w, `<a href="">%s</a>`, str(type.name))
  485. }
  486. case .Named:
  487. e := entities[type_entites[0]]
  488. name := str(type.name)
  489. tn_pkg := files[e.pos.file].pkg
  490. if tn_pkg != pkg {
  491. fmt.wprintf(w, `%s.`, str(pkgs[tn_pkg].name))
  492. }
  493. if n := strings.contains_rune(name, '('); n >= 0 {
  494. fmt.wprintf(w, `<a class="code-typename" href="{2:s}/{0:s}/#{1:s}">{1:s}</a>`, pkg_to_path[&pkgs[tn_pkg]], name[:n], BASE_CORE_URL)
  495. io.write_string(w, name[n:])
  496. } else {
  497. fmt.wprintf(w, `<a class="code-typename" href="{2:s}/{0:s}/#{1:s}">{1:s}</a>`, pkg_to_path[&pkgs[tn_pkg]], name, BASE_CORE_URL)
  498. }
  499. case .Generic:
  500. name := str(type.name)
  501. if name not_in generic_scope {
  502. io.write_byte(w, '$')
  503. }
  504. io.write_string(w, name)
  505. if name not_in generic_scope && len(array(type.types)) == 1 {
  506. io.write_byte(w, '/')
  507. write_type(writer, types[type_types[0]], flags)
  508. }
  509. case .Pointer:
  510. io.write_byte(w, '^')
  511. write_type(writer, types[type_types[0]], flags)
  512. case .Array:
  513. assert(type.elem_count_len == 1)
  514. io.write_byte(w, '[')
  515. io.write_uint(w, uint(type.elem_counts[0]))
  516. io.write_byte(w, ']')
  517. write_type(writer, types[type_types[0]], flags)
  518. case .Enumerated_Array:
  519. io.write_byte(w, '[')
  520. write_type(writer, types[type_types[0]], flags)
  521. io.write_byte(w, ']')
  522. write_type(writer, types[type_types[1]], flags)
  523. case .Slice:
  524. if .Variadic in flags {
  525. io.write_string(w, "..")
  526. } else {
  527. io.write_string(w, "[]")
  528. }
  529. write_type(writer, types[type_types[0]], flags - {.Variadic})
  530. case .Dynamic_Array:
  531. io.write_string(w, "[dynamic]")
  532. write_type(writer, types[type_types[0]], flags)
  533. case .Map:
  534. io.write_string(w, "map[")
  535. write_type(writer, types[type_types[0]], flags)
  536. io.write_byte(w, ']')
  537. write_type(writer, types[type_types[1]], flags)
  538. case .Struct:
  539. type_flags := transmute(doc.Type_Flags_Struct)type.flags
  540. io.write_string(w, "struct")
  541. write_poly_params(writer, type, flags)
  542. if .Packed in type_flags { io.write_string(w, " #packed") }
  543. if .Raw_Union in type_flags { io.write_string(w, " #raw_union") }
  544. if custom_align := str(type.custom_align); custom_align != "" {
  545. io.write_string(w, " #align")
  546. io.write_string(w, custom_align)
  547. }
  548. io.write_string(w, " {")
  549. if len(type_entites) != 0 {
  550. do_newline(writer, flags)
  551. indent += 1
  552. name_width := calc_name_width(type_entites)
  553. for entity_index, i in type_entites {
  554. e := &entities[entity_index]
  555. next_entity: ^doc.Entity = nil
  556. if i+1 < len(type_entites) {
  557. next_entity = &entities[type_entites[i+1]]
  558. }
  559. do_indent(writer, flags)
  560. write_param_entity(writer, e, next_entity, flags, name_width)
  561. io.write_byte(w, ',')
  562. do_newline(writer, flags)
  563. }
  564. indent -= 1
  565. do_indent(writer, flags)
  566. }
  567. io.write_string(w, "}")
  568. case .Union:
  569. type_flags := transmute(doc.Type_Flags_Union)type.flags
  570. io.write_string(w, "union")
  571. write_poly_params(writer, type, flags)
  572. if .No_Nil in type_flags { io.write_string(w, " #no_nil") }
  573. if .Maybe in type_flags { io.write_string(w, " #maybe") }
  574. if custom_align := str(type.custom_align); custom_align != "" {
  575. io.write_string(w, " #align")
  576. io.write_string(w, custom_align)
  577. }
  578. io.write_string(w, " {")
  579. if len(type_types) > 1 {
  580. do_newline(writer, flags)
  581. indent += 1
  582. for type_index in type_types {
  583. do_indent(writer, flags)
  584. write_type(writer, types[type_index], flags)
  585. io.write_string(w, ", ")
  586. do_newline(writer, flags)
  587. }
  588. indent -= 1
  589. do_indent(writer, flags)
  590. }
  591. io.write_string(w, "}")
  592. case .Enum:
  593. io.write_string(w, "enum")
  594. if len(type_types) != 0 {
  595. io.write_byte(w, ' ')
  596. write_type(writer, types[type_types[0]], flags)
  597. }
  598. io.write_string(w, " {")
  599. do_newline(writer, flags)
  600. indent += 1
  601. name_width := calc_name_width(type_entites)
  602. for entity_index in type_entites {
  603. e := &entities[entity_index]
  604. name := str(e.name)
  605. do_indent(writer, flags)
  606. io.write_string(w, name)
  607. if init_string := str(e.init_string); init_string != "" {
  608. for _ in 0..<name_width-len(name) {
  609. io.write_byte(w, ' ')
  610. }
  611. io.write_string(w, " = ")
  612. io.write_string(w, init_string)
  613. }
  614. io.write_string(w, ", ")
  615. do_newline(writer, flags)
  616. }
  617. indent -= 1
  618. do_indent(writer, flags)
  619. io.write_string(w, "}")
  620. case .Tuple:
  621. if len(type_entites) == 0 {
  622. return
  623. }
  624. require_parens := (.Is_Results in flags) && (len(type_entites) > 1 || !is_entity_blank(type_entites[0]))
  625. if require_parens { io.write_byte(w, '(') }
  626. for entity_index, i in type_entites {
  627. if i > 0 {
  628. io.write_string(w, ", ")
  629. }
  630. e := &entities[entity_index]
  631. next_entity: ^doc.Entity = nil
  632. if i+1 < len(type_entites) {
  633. next_entity = &entities[type_entites[i+1]]
  634. }
  635. write_param_entity(writer, e, next_entity, flags)
  636. }
  637. if require_parens { io.write_byte(w, ')') }
  638. case .Proc:
  639. type_flags := transmute(doc.Type_Flags_Proc)type.flags
  640. io.write_string(w, "proc")
  641. cc := str(type.calling_convention)
  642. if cc != "" {
  643. io.write_byte(w, ' ')
  644. io.write_quoted_string(w, cc)
  645. io.write_byte(w, ' ')
  646. }
  647. params := array(type.types)[0]
  648. results := array(type.types)[1]
  649. io.write_byte(w, '(')
  650. write_type(writer, types[params], flags)
  651. io.write_byte(w, ')')
  652. if results != 0 {
  653. assert(.Diverging not_in type_flags)
  654. io.write_string(w, " -> ")
  655. write_type(writer, types[results], flags+{.Is_Results})
  656. }
  657. if .Diverging in type_flags {
  658. io.write_string(w, " -> !")
  659. }
  660. if .Optional_Ok in type_flags {
  661. io.write_string(w, " #optional_ok")
  662. }
  663. case .Bit_Set:
  664. type_flags := transmute(doc.Type_Flags_Bit_Set)type.flags
  665. io.write_string(w, "bit_set[")
  666. if .Op_Lt in type_flags {
  667. io.write_uint(w, uint(type.elem_counts[0]))
  668. io.write_string(w, "..<")
  669. io.write_uint(w, uint(type.elem_counts[1]))
  670. } else if .Op_Lt_Eq in type_flags {
  671. io.write_uint(w, uint(type.elem_counts[0]))
  672. io.write_string(w, "..=")
  673. io.write_uint(w, uint(type.elem_counts[1]))
  674. } else {
  675. write_type(writer, types[type_types[0]], flags)
  676. }
  677. if .Underlying_Type in type_flags {
  678. write_type(writer, types[type_types[1]], flags)
  679. }
  680. io.write_string(w, "]")
  681. case .Simd_Vector:
  682. io.write_string(w, "#simd[")
  683. io.write_uint(w, uint(type.elem_counts[0]))
  684. io.write_byte(w, ']')
  685. case .SOA_Struct_Fixed:
  686. io.write_string(w, "#soa[")
  687. io.write_uint(w, uint(type.elem_counts[0]))
  688. io.write_byte(w, ']')
  689. case .SOA_Struct_Slice:
  690. io.write_string(w, "#soa[]")
  691. case .SOA_Struct_Dynamic:
  692. io.write_string(w, "#soa[dynamic]")
  693. case .Relative_Pointer:
  694. io.write_string(w, "#relative(")
  695. write_type(writer, types[type_types[1]], flags)
  696. io.write_string(w, ") ")
  697. write_type(writer, types[type_types[0]], flags)
  698. case .Relative_Slice:
  699. io.write_string(w, "#relative(")
  700. write_type(writer, types[type_types[1]], flags)
  701. io.write_string(w, ") ")
  702. write_type(writer, types[type_types[0]], flags)
  703. case .Multi_Pointer:
  704. io.write_string(w, "[^]")
  705. write_type(writer, types[type_types[0]], flags)
  706. case .Matrix:
  707. io.write_string(w, "matrix[")
  708. io.write_uint(w, uint(type.elem_counts[0]))
  709. io.write_string(w, ", ")
  710. io.write_uint(w, uint(type.elem_counts[1]))
  711. io.write_string(w, "]")
  712. write_type(writer, types[type_types[0]], flags)
  713. }
  714. }
  715. write_doc_line :: proc(w: io.Writer, text: string) {
  716. text := text
  717. for len(text) != 0 {
  718. if strings.count(text, "`") >= 2 {
  719. n := strings.index_byte(text, '`')
  720. io.write_string(w, text[:n])
  721. io.write_string(w, "<code class=\"code-inline\">")
  722. remaining := text[n+1:]
  723. m := strings.index_byte(remaining, '`')
  724. io.write_string(w, remaining[:m])
  725. io.write_string(w, "</code>")
  726. text = remaining[m+1:]
  727. } else {
  728. io.write_string(w, text)
  729. return
  730. }
  731. }
  732. }
  733. write_docs :: proc(w: io.Writer, pkg: ^doc.Pkg, docs: string) {
  734. if docs == "" {
  735. return
  736. }
  737. it := docs
  738. was_code := true
  739. was_paragraph := true
  740. for line in strings.split_iterator(&it, "\n") {
  741. if strings.has_prefix(line, "\t") {
  742. if !was_code {
  743. was_code = true;
  744. fmt.wprint(w, `<pre class="doc-code"><code>`)
  745. }
  746. fmt.wprintf(w, "%s\n", strings.trim_prefix(line, "\t"))
  747. continue
  748. } else if was_code {
  749. was_code = false
  750. fmt.wprintln(w, "</code></pre>")
  751. continue
  752. }
  753. text := strings.trim_space(line)
  754. if text == "" {
  755. if was_paragraph {
  756. was_paragraph = false
  757. fmt.wprintln(w, "</p>")
  758. }
  759. continue
  760. }
  761. if !was_paragraph {
  762. fmt.wprintln(w, "<p>")
  763. }
  764. assert(!was_code)
  765. was_paragraph = true
  766. write_doc_line(w, text)
  767. io.write_byte(w, '\n')
  768. }
  769. if was_code {
  770. // assert(!was_paragraph, str(pkg.name))
  771. was_code = false
  772. fmt.wprintln(w, "</code></pre>")
  773. }
  774. if was_paragraph {
  775. fmt.wprintln(w, "</p>")
  776. }
  777. }
  778. write_pkg :: proc(w: io.Writer, path: string, pkg: ^doc.Pkg) {
  779. fmt.wprintln(w, `<div class="row odin-main">`)
  780. defer fmt.wprintln(w, `</div>`)
  781. fmt.wprintln(w, `<article class="col-lg-9 p-4 documentation">`)
  782. { // breadcrumbs
  783. fmt.wprintln(w, `<div class="row">`)
  784. defer fmt.wprintln(w, `</div>`)
  785. fmt.wprintln(w, `<nav aria-label="breadcrumb">`)
  786. defer fmt.wprintln(w, `</nav>`)
  787. io.write_string(w, "<ol class=\"breadcrumb\">\n")
  788. defer io.write_string(w, "</ol>\n")
  789. fmt.wprintf(w, `<li class="breadcrumb-item"><a class="breadcrumb-link" href="%s">core</a></li>`, BASE_CORE_URL)
  790. dirs := strings.split(path, "/")
  791. for dir, i in dirs {
  792. url := strings.join(dirs[:i+1], "/")
  793. short_path := strings.join(dirs[1:i+1], "/")
  794. a_class := "breadcrumb-link"
  795. is_curr := i+1 == len(dirs)
  796. if is_curr {
  797. io.write_string(w, `<li class="breadcrumb-item active" aria-current="page">`)
  798. } else {
  799. io.write_string(w, `<li class="breadcrumb-item">`)
  800. }
  801. if !is_curr && (short_path in pkgs_to_use) {
  802. fmt.wprintf(w, `<a href="%s/%s">%s</a>`, BASE_CORE_URL, url, dir)
  803. } else {
  804. io.write_string(w, dir)
  805. }
  806. io.write_string(w, "</li>\n")
  807. }
  808. }
  809. fmt.wprintf(w, "<h1>package core:%s</h1>\n", path)
  810. overview_docs := strings.trim_space(str(pkg.docs))
  811. if overview_docs != "" {
  812. fmt.wprintln(w, "<h2>Overview</h2>")
  813. fmt.wprintln(w, "<div id=\"pkg-overview\">")
  814. defer fmt.wprintln(w, "</div>")
  815. write_docs(w, pkg, overview_docs)
  816. }
  817. fmt.wprintln(w, `<h2>Index</h2>`)
  818. fmt.wprintln(w, `<section class="doc-index" id="pkg-index">`)
  819. // fmt.wprintln(w, `<a class="btn btn-primary" data-bs-toggle="collapse" href="#pkg-index" role="button" aria-expanded="true" aria-controls="pkg-index"><h2>Index</h2></a>`)
  820. // fmt.wprintln(w, `<section class="doc-index collapse" id="pkg-index">`)
  821. pkg_procs: [dynamic]^doc.Entity
  822. pkg_proc_groups: [dynamic]^doc.Entity
  823. pkg_types: [dynamic]^doc.Entity
  824. pkg_vars: [dynamic]^doc.Entity
  825. pkg_consts: [dynamic]^doc.Entity
  826. for entity_index in array(pkg.entities) {
  827. e := &entities[entity_index]
  828. name := str(e.name)
  829. if name == "" || name[0] == '_' {
  830. continue
  831. }
  832. switch e.kind {
  833. case .Invalid, .Import_Name, .Library_Name:
  834. // ignore
  835. case .Constant: append(&pkg_consts, e)
  836. case .Variable: append(&pkg_vars, e)
  837. case .Type_Name: append(&pkg_types, e)
  838. case .Procedure: append(&pkg_procs, e)
  839. case .Proc_Group: append(&pkg_proc_groups, e)
  840. }
  841. }
  842. entity_key :: proc(e: ^doc.Entity) -> string {
  843. return str(e.name)
  844. }
  845. slice.sort_by_key(pkg_procs[:], entity_key)
  846. slice.sort_by_key(pkg_proc_groups[:], entity_key)
  847. slice.sort_by_key(pkg_types[:], entity_key)
  848. slice.sort_by_key(pkg_vars[:], entity_key)
  849. slice.sort_by_key(pkg_consts[:], entity_key)
  850. write_index :: proc(w: io.Writer, name: string, entities: []^doc.Entity) {
  851. fmt.wprintf(w, "<h3>%s</h3>\n", name)
  852. fmt.wprintln(w, `<section class="doc-index">`)
  853. if len(entities) == 0 {
  854. io.write_string(w, "<p>This section is empty.</p>\n")
  855. } else {
  856. fmt.wprintln(w, "<ul>")
  857. for e in entities {
  858. name := str(e.name)
  859. fmt.wprintf(w, "<li><a href=\"#{0:s}\">{0:s}</a></li>\n", name)
  860. }
  861. fmt.wprintln(w, "</ul>")
  862. }
  863. fmt.wprintln(w, "</section>")
  864. }
  865. entity_ordering := [?]struct{name: string, entities: []^doc.Entity} {
  866. {"Types", pkg_types[:]},
  867. {"Constants", pkg_consts[:]},
  868. {"Variables", pkg_vars[:]},
  869. {"Procedures", pkg_procs[:]},
  870. {"Procedure Groups", pkg_proc_groups[:]},
  871. }
  872. for eo in entity_ordering {
  873. write_index(w, eo.name, eo.entities)
  874. }
  875. fmt.wprintln(w, "</section>")
  876. write_entity :: proc(w: io.Writer, e: ^doc.Entity) {
  877. write_attributes :: proc(w: io.Writer, e: ^doc.Entity) {
  878. for attr in array(e.attributes) {
  879. io.write_string(w, "@(")
  880. name := str(attr.name)
  881. value := str(attr.value)
  882. io.write_string(w, name)
  883. if value != "" {
  884. io.write_string(w, "=")
  885. io.write_string(w, value)
  886. }
  887. io.write_string(w, ")\n")
  888. }
  889. }
  890. pkg_index := files[e.pos.file].pkg
  891. pkg := &pkgs[pkg_index]
  892. writer := &Type_Writer{
  893. w = w,
  894. pkg = pkg_index,
  895. }
  896. defer delete(writer.generic_scope)
  897. name := str(e.name)
  898. path := pkg_to_path[pkg]
  899. filename := slashpath.base(str(files[e.pos.file].name))
  900. fmt.wprintf(w, "<h3 id=\"{0:s}\"><span><a class=\"doc-id-link\" href=\"#{0:s}\">{0:s}", name)
  901. fmt.wprintf(w, "<span class=\"a-hidden\">&nbsp;¶</span></a></span>")
  902. if e.pos.file != 0 && e.pos.line > 0 {
  903. src_url := fmt.tprintf("%s/%s/%s#L%d", GITHUB_CORE_URL, path, filename, e.pos.line)
  904. fmt.wprintf(w, "<div class=\"doc-source\"><a href=\"{0:s}\"><em>Source</em></a></div>", src_url)
  905. }
  906. fmt.wprintf(w, "</h3>\n")
  907. switch e.kind {
  908. case .Invalid, .Import_Name, .Library_Name:
  909. // ignore
  910. case .Constant:
  911. fmt.wprint(w, `<pre class="doc-code">`)
  912. the_type := types[e.type]
  913. init_string := str(e.init_string)
  914. assert(init_string != "")
  915. ignore_type := true
  916. if the_type.kind == .Basic && is_type_untyped(the_type) {
  917. } else {
  918. ignore_type = false
  919. type_name := str(the_type.name)
  920. if type_name != "" && strings.has_prefix(init_string, type_name) {
  921. ignore_type = true
  922. }
  923. }
  924. if ignore_type {
  925. fmt.wprintf(w, "%s :: ", name)
  926. } else {
  927. fmt.wprintf(w, "%s: ", name)
  928. write_type(writer, the_type, {.Allow_Indent})
  929. fmt.wprintf(w, " : ")
  930. }
  931. io.write_string(w, init_string)
  932. fmt.wprintln(w, "</pre>")
  933. case .Variable:
  934. fmt.wprint(w, `<pre class="doc-code">`)
  935. write_attributes(w, e)
  936. fmt.wprintf(w, "%s: ", name)
  937. write_type(writer, types[e.type], {.Allow_Indent})
  938. init_string := str(e.init_string)
  939. if init_string != "" {
  940. io.write_string(w, " = ")
  941. io.write_string(w, init_string)
  942. }
  943. fmt.wprintln(w, "</pre>")
  944. case .Type_Name:
  945. fmt.wprint(w, `<pre class="doc-code">`)
  946. fmt.wprintf(w, "%s :: ", name)
  947. the_type := types[e.type]
  948. type_to_print := the_type
  949. if the_type.kind == .Named && .Type_Alias not_in e.flags {
  950. if e.pos == entities[array(the_type.entities)[0]].pos {
  951. bt := base_type(the_type)
  952. #partial switch bt.kind {
  953. case .Struct, .Union, .Proc, .Enum:
  954. // Okay
  955. case:
  956. io.write_string(w, "distinct ")
  957. }
  958. type_to_print = bt
  959. }
  960. }
  961. write_type(writer, type_to_print, {.Allow_Indent})
  962. fmt.wprintln(w, "</pre>")
  963. case .Procedure:
  964. fmt.wprint(w, `<pre class="doc-code">`)
  965. fmt.wprintf(w, "%s :: ", name)
  966. write_type(writer, types[e.type], nil)
  967. write_where_clauses(w, array(e.where_clauses))
  968. fmt.wprint(w, " {…}")
  969. fmt.wprintln(w, "</pre>")
  970. case .Proc_Group:
  971. fmt.wprint(w, `<pre class="doc-code">`)
  972. fmt.wprintf(w, "%s :: proc{{\n", name)
  973. for entity_index in array(e.grouped_entities) {
  974. this_proc := &entities[entity_index]
  975. this_pkg := files[this_proc.pos.file].pkg
  976. io.write_byte(w, '\t')
  977. if this_pkg != pkg_index {
  978. fmt.wprintf(w, "%s.", str(pkgs[this_pkg].name))
  979. }
  980. name := str(this_proc.name)
  981. fmt.wprintf(w, `<a class="code-procedure" href="{2:s}/{0:s}/#{1:s}">`, pkg_to_path[&pkgs[this_pkg]], name, BASE_CORE_URL)
  982. io.write_string(w, name)
  983. io.write_string(w, `</a>`)
  984. io.write_byte(w, ',')
  985. io.write_byte(w, '\n')
  986. }
  987. fmt.wprintln(w, "}")
  988. fmt.wprintln(w, "</pre>")
  989. }
  990. write_docs(w, pkg, strings.trim_space(str(e.docs)))
  991. }
  992. write_entities :: proc(w: io.Writer, title: string, entities: []^doc.Entity) {
  993. fmt.wprintf(w, "<h2 id=\"pkg-{0:s}\">{0:s}</h2>\n", title)
  994. fmt.wprintln(w, `<section class="documentation">`)
  995. if len(entities) == 0 {
  996. io.write_string(w, "<p>This section is empty.</p>\n")
  997. } else {
  998. for e in entities {
  999. write_entity(w, e)
  1000. }
  1001. }
  1002. fmt.wprintln(w, "</section>")
  1003. }
  1004. for eo in entity_ordering {
  1005. write_entities(w, eo.name, eo.entities)
  1006. }
  1007. fmt.wprintln(w, `<h2 id="pkg-source-files">Source Files</h2>`)
  1008. fmt.wprintln(w, "<ul>")
  1009. any_hidden := false
  1010. source_file_loop: for file_index in array(pkg.files) {
  1011. file := files[file_index]
  1012. filename := slashpath.base(str(file.name))
  1013. switch {
  1014. case
  1015. strings.has_suffix(filename, "_windows.odin"),
  1016. strings.has_suffix(filename, "_darwin.odin"),
  1017. strings.has_suffix(filename, "_essence.odin"),
  1018. strings.has_suffix(filename, "_freebsd.odin"),
  1019. strings.has_suffix(filename, "_wasi.odin"),
  1020. strings.has_suffix(filename, "_js.odin"),
  1021. strings.has_suffix(filename, "_freestanding.odin"),
  1022. strings.has_suffix(filename, "_amd64.odin"),
  1023. strings.has_suffix(filename, "_i386.odin"),
  1024. strings.has_suffix(filename, "_arch64.odin"),
  1025. strings.has_suffix(filename, "_wasm32.odin"),
  1026. strings.has_suffix(filename, "_wasm64.odin"),
  1027. false:
  1028. any_hidden = true
  1029. continue source_file_loop
  1030. }
  1031. fmt.wprintf(w, `<li><a href="%s/%s/%s">%s</a></li>`, GITHUB_CORE_URL, path, filename, filename)
  1032. fmt.wprintln(w)
  1033. }
  1034. if any_hidden {
  1035. fmt.wprintln(w, "<li><em>(hidden platform specific files)</em></li>")
  1036. }
  1037. fmt.wprintln(w, "</ul>")
  1038. fmt.wprintln(w, `</article>`)
  1039. {
  1040. write_link :: proc(w: io.Writer, id, text: string) {
  1041. fmt.wprintf(w, `<li><a href="#%s">%s</a>`, id, text)
  1042. }
  1043. fmt.wprintln(w, `<div class="col-lg-3 odin-toc-border navbar-light"><div class="sticky-top odin-below-navbar py-3">`)
  1044. fmt.wprintln(w, `<nav id="TableOfContents">`)
  1045. fmt.wprintln(w, `<ul>`)
  1046. if overview_docs != "" {
  1047. write_link(w, "pkg-overview", "Overview")
  1048. }
  1049. for eo in entity_ordering do if len(eo.entities) != 0 {
  1050. fmt.wprintf(w, `<li><a href="#pkg-{0:s}">{0:s}</a>`, eo.name)
  1051. fmt.wprintln(w, `<ul>`)
  1052. for e in eo.entities {
  1053. fmt.wprintf(w, "<li><a href=\"#{0:s}\">{0:s}</a></li>\n", str(e.name))
  1054. }
  1055. fmt.wprintln(w, "</ul>")
  1056. fmt.wprintln(w, "</li>")
  1057. }
  1058. write_link(w, "pkg-source-files", "Source Files")
  1059. fmt.wprintln(w, `</ul>`)
  1060. fmt.wprintln(w, `</nav>`)
  1061. fmt.wprintln(w, `</div></div>`)
  1062. }
  1063. }