odin_html_docs_main.odin 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. io.write_string(w, "; ")
  798. write_type(writer, types[type_types[1]], flags)
  799. }
  800. io.write_string(w, "]")
  801. case .Simd_Vector:
  802. io.write_string(w, "#simd[")
  803. io.write_uint(w, uint(type.elem_counts[0]))
  804. io.write_byte(w, ']')
  805. case .SOA_Struct_Fixed:
  806. io.write_string(w, "#soa[")
  807. io.write_uint(w, uint(type.elem_counts[0]))
  808. io.write_byte(w, ']')
  809. case .SOA_Struct_Slice:
  810. io.write_string(w, "#soa[]")
  811. case .SOA_Struct_Dynamic:
  812. io.write_string(w, "#soa[dynamic]")
  813. case .Relative_Pointer:
  814. io.write_string(w, "#relative(")
  815. write_type(writer, types[type_types[1]], flags)
  816. io.write_string(w, ") ")
  817. write_type(writer, types[type_types[0]], flags)
  818. case .Relative_Slice:
  819. io.write_string(w, "#relative(")
  820. write_type(writer, types[type_types[1]], flags)
  821. io.write_string(w, ") ")
  822. write_type(writer, types[type_types[0]], flags)
  823. case .Multi_Pointer:
  824. io.write_string(w, "[^]")
  825. write_type(writer, types[type_types[0]], flags)
  826. case .Matrix:
  827. io.write_string(w, "matrix[")
  828. io.write_uint(w, uint(type.elem_counts[0]))
  829. io.write_string(w, ", ")
  830. io.write_uint(w, uint(type.elem_counts[1]))
  831. io.write_string(w, "]")
  832. write_type(writer, types[type_types[0]], flags)
  833. }
  834. }
  835. write_doc_line :: proc(w: io.Writer, text: string) {
  836. text := text
  837. for len(text) != 0 {
  838. if strings.count(text, "`") >= 2 {
  839. n := strings.index_byte(text, '`')
  840. io.write_string(w, text[:n])
  841. io.write_string(w, "<code class=\"code-inline\">")
  842. remaining := text[n+1:]
  843. m := strings.index_byte(remaining, '`')
  844. io.write_string(w, remaining[:m])
  845. io.write_string(w, "</code>")
  846. text = remaining[m+1:]
  847. } else {
  848. io.write_string(w, text)
  849. return
  850. }
  851. }
  852. }
  853. write_doc_sidebar :: proc(w: io.Writer) {
  854. }
  855. write_docs :: proc(w: io.Writer, pkg: ^doc.Pkg, docs: string) {
  856. if docs == "" {
  857. return
  858. }
  859. Block_Kind :: enum {
  860. Paragraph,
  861. Code,
  862. }
  863. Block :: struct {
  864. kind: Block_Kind,
  865. lines: []string,
  866. }
  867. lines: [dynamic]string
  868. it := docs
  869. for line_ in strings.split_iterator(&it, "\n") {
  870. line := strings.trim_right_space(line_)
  871. append(&lines, line)
  872. }
  873. curr_block_kind := Block_Kind.Paragraph
  874. start := 0
  875. blocks: [dynamic]Block
  876. for line, i in lines {
  877. text := strings.trim_space(line)
  878. switch curr_block_kind {
  879. case .Paragraph:
  880. if strings.has_prefix(line, "\t") {
  881. if i-start > 0 {
  882. append(&blocks, Block{curr_block_kind, lines[start:i]})
  883. }
  884. curr_block_kind, start = .Code, i
  885. } else if text == "" {
  886. if i-start > 0 {
  887. append(&blocks, Block{curr_block_kind, lines[start:i]})
  888. }
  889. start = i
  890. }
  891. case .Code:
  892. if text == "" || strings.has_prefix(line, "\t") {
  893. continue
  894. }
  895. if i-start > 0 {
  896. append(&blocks, Block{curr_block_kind, lines[start:i]})
  897. }
  898. curr_block_kind, start = .Paragraph, i
  899. }
  900. }
  901. if start < len(lines) {
  902. if len(lines)-start > 0 {
  903. append(&blocks, Block{curr_block_kind, lines[start:]})
  904. }
  905. }
  906. for block in &blocks {
  907. trim_amount := 0
  908. for trim_amount = 0; trim_amount < len(block.lines); trim_amount += 1 {
  909. line := block.lines[trim_amount]
  910. if strings.trim_space(line) != "" {
  911. break
  912. }
  913. }
  914. block.lines = block.lines[trim_amount:]
  915. }
  916. for block, i in blocks {
  917. if len(block.lines) == 0 {
  918. continue
  919. }
  920. prev_line := ""
  921. if i > 0 {
  922. prev_lines := blocks[i-1].lines
  923. if len(prev_lines) > 0 {
  924. prev_line = prev_lines[len(prev_lines)-1]
  925. }
  926. }
  927. prev_line = strings.trim_space(prev_line)
  928. lines := block.lines[:]
  929. end_line := block.lines[len(lines)-1]
  930. if block.kind == .Paragraph && i+1 < len(blocks) {
  931. if strings.has_prefix(end_line, "Example:") && blocks[i+1].kind == .Code {
  932. lines = lines[:len(lines)-1]
  933. }
  934. }
  935. switch block.kind {
  936. case .Paragraph:
  937. io.write_string(w, "<p>")
  938. for line, line_idx in lines {
  939. if line_idx > 0 {
  940. io.write_string(w, "\n")
  941. }
  942. io.write_string(w, line)
  943. }
  944. io.write_string(w, "</p>\n")
  945. case .Code:
  946. all_blank := len(lines) > 0
  947. for line in lines {
  948. if strings.trim_space(line) != "" {
  949. all_blank = false
  950. }
  951. }
  952. if all_blank {
  953. continue
  954. }
  955. if strings.has_prefix(prev_line, "Example:") {
  956. io.write_string(w, "<details open class=\"code-example\">\n")
  957. defer io.write_string(w, "</details>\n")
  958. io.write_string(w, "<summary>Example:</summary>\n")
  959. io.write_string(w, `<pre><code class="hljs" data-lang="odin">`)
  960. for line in lines {
  961. io.write_string(w, strings.trim_prefix(line, "\t"))
  962. io.write_string(w, "\n")
  963. }
  964. io.write_string(w, "</code></pre>\n")
  965. } else {
  966. io.write_string(w, "<pre>")
  967. for line in lines {
  968. io.write_string(w, strings.trim_prefix(line, "\t"))
  969. io.write_string(w, "\n")
  970. }
  971. io.write_string(w, "</pre>\n")
  972. }
  973. }
  974. }
  975. }
  976. write_pkg_sidebar :: proc(w: io.Writer, curr_pkg: ^doc.Pkg, collection: ^Collection) {
  977. fmt.wprintln(w, `<nav id="pkg-sidebar" class="col-lg-2 odin-sidebar-border navbar-light">`)
  978. defer fmt.wprintln(w, `</nav>`)
  979. fmt.wprintln(w, `<div class="sticky-top odin-below-navbar py-3">`)
  980. defer fmt.wprintln(w, `</div>`)
  981. fmt.wprintf(w, "<h4>%s Library</h4>\n", collection.name)
  982. fmt.wprintln(w, `<ul>`)
  983. defer fmt.wprintln(w, `</ul>`)
  984. for dir in collection.root.children {
  985. fmt.wprint(w, `<li class="nav-item">`)
  986. defer fmt.wprintln(w, `</li>`)
  987. if dir.pkg == curr_pkg {
  988. fmt.wprintf(w, `<a class="active" href="%s/%s">%s</a>`, collection.base_url, dir.path, dir.name)
  989. } else if dir.pkg != nil {
  990. fmt.wprintf(w, `<a href="%s/%s">%s</a>`, collection.base_url, dir.path, dir.name)
  991. } else {
  992. fmt.wprintf(w, "%s", dir.name)
  993. }
  994. if len(dir.children) != 0 {
  995. fmt.wprintln(w, "<ul>")
  996. defer fmt.wprintln(w, "</ul>\n")
  997. for child in dir.children {
  998. fmt.wprint(w, `<li>`)
  999. defer fmt.wprintln(w, `</li>`)
  1000. if child.pkg == curr_pkg {
  1001. fmt.wprintf(w, `<a class="active" href="%s/%s">%s</a>`, collection.base_url, child.path, child.name)
  1002. } else if child.pkg != nil {
  1003. fmt.wprintf(w, `<a href="%s/%s">%s</a>`, collection.base_url, child.path, child.name)
  1004. } else {
  1005. fmt.wprintf(w, "%s", child.name)
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. write_pkg :: proc(w: io.Writer, path: string, pkg: ^doc.Pkg, collection: ^Collection) {
  1012. fmt.wprintln(w, `<div class="row odin-main">`)
  1013. defer fmt.wprintln(w, `</div>`)
  1014. write_pkg_sidebar(w, pkg, collection)
  1015. fmt.wprintln(w, `<article class="col-lg-8 p-4 documentation odin-article">`)
  1016. if false { // breadcrumbs
  1017. fmt.wprintln(w, `<div class="row">`)
  1018. defer fmt.wprintln(w, `</div>`)
  1019. fmt.wprintln(w, `<nav aria-label="breadcrumb">`)
  1020. defer fmt.wprintln(w, `</nav>`)
  1021. io.write_string(w, "<ol class=\"breadcrumb\">\n")
  1022. defer io.write_string(w, "</ol>\n")
  1023. fmt.wprintf(w, `<li class="breadcrumb-item"><a class="breadcrumb-link" href="%s">core</a></li>`, collection.base_url)
  1024. dirs := strings.split(path, "/")
  1025. for dir, i in dirs {
  1026. url := strings.join(dirs[:i+1], "/")
  1027. short_path := strings.join(dirs[1:i+1], "/")
  1028. a_class := "breadcrumb-link"
  1029. is_curr := i+1 == len(dirs)
  1030. if is_curr {
  1031. io.write_string(w, `<li class="breadcrumb-item active" aria-current="page">`)
  1032. } else {
  1033. io.write_string(w, `<li class="breadcrumb-item">`)
  1034. }
  1035. if !is_curr && (short_path in collection.pkgs_to_use) {
  1036. fmt.wprintf(w, `<a href="%s/%s">%s</a>`, collection.base_url, url, dir)
  1037. } else {
  1038. io.write_string(w, dir)
  1039. }
  1040. io.write_string(w, "</li>\n")
  1041. }
  1042. }
  1043. fmt.wprintf(w, "<h1>package core:%s</h1>\n", path)
  1044. overview_docs := strings.trim_space(str(pkg.docs))
  1045. if overview_docs != "" {
  1046. fmt.wprintln(w, "<h2>Overview</h2>")
  1047. fmt.wprintln(w, "<div id=\"pkg-overview\">")
  1048. defer fmt.wprintln(w, "</div>")
  1049. write_docs(w, pkg, overview_docs)
  1050. }
  1051. fmt.wprintln(w, `<h2>Index</h2>`)
  1052. fmt.wprintln(w, `<div id="pkg-index">`)
  1053. pkg_procs: [dynamic]^doc.Entity
  1054. pkg_proc_groups: [dynamic]^doc.Entity
  1055. pkg_types: [dynamic]^doc.Entity
  1056. pkg_vars: [dynamic]^doc.Entity
  1057. pkg_consts: [dynamic]^doc.Entity
  1058. for entity_index in array(pkg.entities) {
  1059. e := &entities[entity_index]
  1060. name := str(e.name)
  1061. if name == "" || name[0] == '_' {
  1062. continue
  1063. }
  1064. switch e.kind {
  1065. case .Invalid, .Import_Name, .Library_Name:
  1066. // ignore
  1067. case .Constant: append(&pkg_consts, e)
  1068. case .Variable: append(&pkg_vars, e)
  1069. case .Type_Name: append(&pkg_types, e)
  1070. case .Procedure: append(&pkg_procs, e)
  1071. case .Proc_Group: append(&pkg_proc_groups, e)
  1072. }
  1073. }
  1074. entity_key :: proc(e: ^doc.Entity) -> string {
  1075. return str(e.name)
  1076. }
  1077. slice.sort_by_key(pkg_procs[:], entity_key)
  1078. slice.sort_by_key(pkg_proc_groups[:], entity_key)
  1079. slice.sort_by_key(pkg_types[:], entity_key)
  1080. slice.sort_by_key(pkg_vars[:], entity_key)
  1081. slice.sort_by_key(pkg_consts[:], entity_key)
  1082. write_index :: proc(w: io.Writer, name: string, entities: []^doc.Entity) {
  1083. fmt.wprintln(w, `<div>`)
  1084. defer fmt.wprintln(w, `</div>`)
  1085. fmt.wprintf(w, `<details open class="doc-index" id="doc-index-{0:s}" aria-labelledby="#doc-index-{0:s}-header">`+"\n", name)
  1086. fmt.wprintf(w, `<summary id="#doc-index-{0:s}-header">`+"\n", name)
  1087. io.write_string(w, name)
  1088. fmt.wprintln(w, `</summary>`)
  1089. defer fmt.wprintln(w, `</details>`)
  1090. if len(entities) == 0 {
  1091. io.write_string(w, "<p>This section is empty.</p>\n")
  1092. } else {
  1093. fmt.wprintln(w, "<ul>")
  1094. for e in entities {
  1095. name := str(e.name)
  1096. fmt.wprintf(w, "<li><a href=\"#{0:s}\">{0:s}</a></li>\n", name)
  1097. }
  1098. fmt.wprintln(w, "</ul>")
  1099. }
  1100. }
  1101. entity_ordering := [?]struct{name: string, entities: []^doc.Entity} {
  1102. {"Types", pkg_types[:]},
  1103. {"Constants", pkg_consts[:]},
  1104. {"Variables", pkg_vars[:]},
  1105. {"Procedures", pkg_procs[:]},
  1106. {"Procedure Groups", pkg_proc_groups[:]},
  1107. }
  1108. for eo in entity_ordering {
  1109. write_index(w, eo.name, eo.entities)
  1110. }
  1111. fmt.wprintln(w, "</div>")
  1112. write_entity :: proc(w: io.Writer, e: ^doc.Entity) {
  1113. write_attributes :: proc(w: io.Writer, e: ^doc.Entity) {
  1114. for attr in array(e.attributes) {
  1115. io.write_string(w, "@(")
  1116. name := str(attr.name)
  1117. value := str(attr.value)
  1118. io.write_string(w, name)
  1119. if value != "" {
  1120. io.write_string(w, "=")
  1121. io.write_string(w, value)
  1122. }
  1123. io.write_string(w, ")\n")
  1124. }
  1125. }
  1126. pkg_index := files[e.pos.file].pkg
  1127. pkg := &pkgs[pkg_index]
  1128. writer := &Type_Writer{
  1129. w = w,
  1130. pkg = pkg_index,
  1131. }
  1132. defer delete(writer.generic_scope)
  1133. collection := pkg_to_collection[pkg]
  1134. github_url := collection.github_url if collection != nil else GITHUB_CORE_URL
  1135. name := str(e.name)
  1136. path := pkg_to_path[pkg]
  1137. filename := slashpath.base(str(files[e.pos.file].name))
  1138. fmt.wprintf(w, "<h3 id=\"{0:s}\"><span><a class=\"doc-id-link\" href=\"#{0:s}\">{0:s}", name)
  1139. fmt.wprintf(w, "<span class=\"a-hidden\">&nbsp;¶</span></a></span>")
  1140. if e.pos.file != 0 && e.pos.line > 0 {
  1141. src_url := fmt.tprintf("%s/%s/%s#L%d", github_url, path, filename, e.pos.line)
  1142. fmt.wprintf(w, "<div class=\"doc-source\"><a href=\"{0:s}\"><em>Source</em></a></div>", src_url)
  1143. }
  1144. fmt.wprintf(w, "</h3>\n")
  1145. fmt.wprintln(w, `<div>`)
  1146. switch e.kind {
  1147. case .Invalid, .Import_Name, .Library_Name:
  1148. // ignore
  1149. case .Constant:
  1150. fmt.wprint(w, `<pre class="doc-code">`)
  1151. the_type := types[e.type]
  1152. init_string := str(e.init_string)
  1153. assert(init_string != "")
  1154. ignore_type := true
  1155. if the_type.kind == .Basic && is_type_untyped(the_type) {
  1156. } else {
  1157. ignore_type = false
  1158. type_name := str(the_type.name)
  1159. if type_name != "" && strings.has_prefix(init_string, type_name) {
  1160. ignore_type = true
  1161. }
  1162. }
  1163. if ignore_type {
  1164. fmt.wprintf(w, "%s :: ", name)
  1165. } else {
  1166. fmt.wprintf(w, "%s: ", name)
  1167. write_type(writer, the_type, {.Allow_Indent})
  1168. fmt.wprintf(w, " : ")
  1169. }
  1170. io.write_string(w, init_string)
  1171. fmt.wprintln(w, "</pre>")
  1172. case .Variable:
  1173. fmt.wprint(w, `<pre class="doc-code">`)
  1174. write_attributes(w, e)
  1175. fmt.wprintf(w, "%s: ", name)
  1176. write_type(writer, types[e.type], {.Allow_Indent})
  1177. init_string := str(e.init_string)
  1178. if init_string != "" {
  1179. io.write_string(w, " = ")
  1180. io.write_string(w, "…")
  1181. }
  1182. fmt.wprintln(w, "</pre>")
  1183. case .Type_Name:
  1184. fmt.wprint(w, `<pre class="doc-code">`)
  1185. fmt.wprintf(w, "%s :: ", name)
  1186. the_type := types[e.type]
  1187. type_to_print := the_type
  1188. if the_type.kind == .Named && .Type_Alias not_in e.flags {
  1189. if e.pos == entities[array(the_type.entities)[0]].pos {
  1190. bt := base_type(the_type)
  1191. #partial switch bt.kind {
  1192. case .Struct, .Union, .Proc, .Enum:
  1193. // Okay
  1194. case:
  1195. io.write_string(w, "distinct ")
  1196. }
  1197. type_to_print = bt
  1198. }
  1199. }
  1200. write_type(writer, type_to_print, {.Allow_Indent})
  1201. fmt.wprintln(w, "</pre>")
  1202. case .Procedure:
  1203. fmt.wprint(w, `<pre class="doc-code">`)
  1204. fmt.wprintf(w, "%s :: ", name)
  1205. write_type(writer, types[e.type], nil)
  1206. write_where_clauses(w, array(e.where_clauses))
  1207. fmt.wprint(w, " {…}")
  1208. fmt.wprintln(w, "</pre>")
  1209. case .Proc_Group:
  1210. fmt.wprint(w, `<pre class="doc-code">`)
  1211. fmt.wprintf(w, "%s :: proc{{\n", name)
  1212. for entity_index in array(e.grouped_entities) {
  1213. this_proc := &entities[entity_index]
  1214. this_pkg := files[this_proc.pos.file].pkg
  1215. io.write_byte(w, '\t')
  1216. if this_pkg != pkg_index {
  1217. fmt.wprintf(w, "%s.", str(pkgs[this_pkg].name))
  1218. }
  1219. pkg := &pkgs[this_pkg]
  1220. collection := pkg_to_collection[pkg]
  1221. name := str(this_proc.name)
  1222. fmt.wprintf(w, `<a class="code-procedure" href="{2:s}/{0:s}/#{1:s}">`, pkg_to_path[pkg], name, collection.base_url)
  1223. io.write_string(w, name)
  1224. io.write_string(w, `</a>`)
  1225. io.write_byte(w, ',')
  1226. io.write_byte(w, '\n')
  1227. }
  1228. fmt.wprintln(w, "}")
  1229. fmt.wprintln(w, "</pre>")
  1230. }
  1231. fmt.wprintln(w, `</div>`)
  1232. the_docs := strings.trim_space(str(e.docs))
  1233. if the_docs != "" {
  1234. fmt.wprintln(w, `<details class="odin-doc-toggle" open>`)
  1235. fmt.wprintln(w, `<summary class="hideme"><span>&nbsp;</span></summary>`)
  1236. write_docs(w, pkg, the_docs)
  1237. fmt.wprintln(w, `</details>`)
  1238. }
  1239. }
  1240. write_entities :: proc(w: io.Writer, title: string, entities: []^doc.Entity) {
  1241. fmt.wprintf(w, "<h2 id=\"pkg-{0:s}\">{0:s}</h2>\n", title)
  1242. fmt.wprintln(w, `<section class="documentation">`)
  1243. if len(entities) == 0 {
  1244. io.write_string(w, "<p>This section is empty.</p>\n")
  1245. } else {
  1246. for e in entities {
  1247. fmt.wprintln(w, `<div class="pkg-entity">`)
  1248. write_entity(w, e)
  1249. fmt.wprintln(w, `</div>`)
  1250. }
  1251. }
  1252. fmt.wprintln(w, "</section>")
  1253. }
  1254. for eo in entity_ordering {
  1255. write_entities(w, eo.name, eo.entities)
  1256. }
  1257. fmt.wprintln(w, `<h2 id="pkg-source-files">Source Files</h2>`)
  1258. fmt.wprintln(w, "<ul>")
  1259. any_hidden := false
  1260. source_file_loop: for file_index in array(pkg.files) {
  1261. file := files[file_index]
  1262. filename := slashpath.base(str(file.name))
  1263. switch {
  1264. case
  1265. strings.has_suffix(filename, "_windows.odin"),
  1266. strings.has_suffix(filename, "_darwin.odin"),
  1267. strings.has_suffix(filename, "_essence.odin"),
  1268. strings.has_suffix(filename, "_freebsd.odin"),
  1269. strings.has_suffix(filename, "_wasi.odin"),
  1270. strings.has_suffix(filename, "_js.odin"),
  1271. strings.has_suffix(filename, "_freestanding.odin"),
  1272. strings.has_suffix(filename, "_amd64.odin"),
  1273. strings.has_suffix(filename, "_i386.odin"),
  1274. strings.has_suffix(filename, "_arch64.odin"),
  1275. strings.has_suffix(filename, "_wasm32.odin"),
  1276. strings.has_suffix(filename, "_wasm64.odin"),
  1277. false:
  1278. any_hidden = true
  1279. continue source_file_loop
  1280. }
  1281. fmt.wprintf(w, `<li><a href="%s/%s/%s">%s</a></li>`, collection.github_url, path, filename, filename)
  1282. fmt.wprintln(w)
  1283. }
  1284. if any_hidden {
  1285. fmt.wprintln(w, "<li><em>(hidden platform specific files)</em></li>")
  1286. }
  1287. fmt.wprintln(w, "</ul>")
  1288. {
  1289. fmt.wprintln(w, `<h2 id="pkg-generation-information">Generation Information</h2>`)
  1290. now := time.now()
  1291. 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)
  1292. }
  1293. fmt.wprintln(w, `</article>`)
  1294. {
  1295. write_link :: proc(w: io.Writer, id, text: string) {
  1296. fmt.wprintf(w, `<li><a href="#%s">%s</a>`, id, text)
  1297. }
  1298. fmt.wprintln(w, `<div class="col-lg-2 odin-toc-border navbar-light"><div class="sticky-top odin-below-navbar py-3">`)
  1299. fmt.wprintln(w, `<nav id="TableOfContents">`)
  1300. fmt.wprintln(w, `<ul>`)
  1301. if overview_docs != "" {
  1302. write_link(w, "pkg-overview", "Overview")
  1303. }
  1304. for eo in entity_ordering do if len(eo.entities) != 0 {
  1305. fmt.wprintf(w, `<li><a href="#pkg-{0:s}">{0:s}</a>`, eo.name)
  1306. fmt.wprintln(w, `<ul>`)
  1307. for e in eo.entities {
  1308. fmt.wprintf(w, "<li><a href=\"#{0:s}\">{0:s}</a></li>\n", str(e.name))
  1309. }
  1310. fmt.wprintln(w, "</ul>")
  1311. fmt.wprintln(w, "</li>")
  1312. }
  1313. write_link(w, "pkg-source-files", "Source Files")
  1314. fmt.wprintln(w, `</ul>`)
  1315. fmt.wprintln(w, `</nav>`)
  1316. fmt.wprintln(w, `</div></div>`)
  1317. }
  1318. }