odin_html_docs_main.odin 41 KB

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