odin_html_docs_main.odin 37 KB

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