Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

file-defs.lua 3.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  1. function rust_parse_ref(name)
  2. local tpath, tname, label
  3. tpath, tname = string.match(name, "^~(.*::([^:]+))")
  4. if tpath then
  5. label = pandoc.Code(tname)
  6. else
  7. tpath = name
  8. label = pandoc.Code(name)
  9. end
  10. return tpath, label
  11. end
  12. function rust_link(arg, type)
  13. local name, path, label, tgt
  14. name = pandoc.utils.stringify(arg)
  15. path, label = rust_parse_ref(name)
  16. tgt = "/apidocs/" .. string.gsub(path, "::", "/")
  17. if type == "mod" then
  18. tgt = tgt .. "/"
  19. else
  20. tgt = string.gsub(tgt, "/([^/]*)$", "/" .. type .. ".%1.html")
  21. end
  22. return pandoc.Link(label, tgt)
  23. end
  24. function _load_schema(file)
  25. local path, base, ext, f, text
  26. path = pandoc.path.join({ quarto.project.directory, "..", file })
  27. base, ext = pandoc.path.split_extension(path)
  28. path = base .. ".json"
  29. quarto.log.debug("reading schema file", path)
  30. f = io.open(path)
  31. if f == nil then
  32. quarto.log.warning("could not open schema file", path)
  33. return nil
  34. end
  35. text = f:read("a")
  36. return quarto.json.decode(text)
  37. end
  38. function _schema_table(path, schema)
  39. local header = pandoc.Row({
  40. pandoc.Cell({ pandoc.Div("Field") }),
  41. pandoc.Cell({ pandoc.Div("Type") }),
  42. })
  43. local rows = pandoc.List()
  44. for _, field in ipairs(schema.fields) do
  45. rows[#rows + 1] = pandoc.Row({
  46. pandoc.Cell({ pandoc.Div(field.name) }),
  47. pandoc.Cell({ pandoc.Div(field.data_type) }),
  48. })
  49. end
  50. local caption = { "Schema for ", pandoc.Code(path), "." }
  51. return pandoc.Table(
  52. { long = { pandoc.Div({ caption }) }, short = caption },
  53. { { pandoc.AlignLeft, .6 }, { pandoc.AlignRight, .4 } },
  54. pandoc.TableHead({ header }),
  55. { { attr = pandoc.Attr(), body = rows, head = {}, row_head_columns = 0 } }, pandoc.TableFoot(),
  56. pandoc.Attr("", { "file-schema" })
  57. )
  58. end
  59. Div = function(el)
  60. local file = el.attributes['file']
  61. if file == nil then
  62. return el
  63. end
  64. local id = el.attr.identifier
  65. if id == "" then
  66. id = "file:" .. file
  67. el.attr.identifier = id
  68. end
  69. quarto.log.debug("found file", file, "in", quarto.doc.input_file, "with anchor", id)
  70. el.classes:extend({"file-block"})
  71. local header = pandoc.List({
  72. pandoc.Code(file)
  73. })
  74. local struct = el.attributes['struct']
  75. if struct ~= nil then
  76. header:extend({
  77. " (struct ",
  78. rust_link(struct, "struct"),
  79. ")"
  80. })
  81. end
  82. el.content:insert(1, pandoc.Div({header}, pandoc.Attr("", {"file-header"})))
  83. local meta = {}
  84. if el.classes:includes('parquet') then
  85. local schema = _load_schema(file)
  86. if schema == nil then
  87. meta = {
  88. pandoc.Div({
  89. pandoc.Str("Could not find schema for "),
  90. pandoc.Code(file),
  91. pandoc.Str(".")
  92. }, pandoc.Attr("", { "callout-error" }, {}))
  93. }
  94. else
  95. meta = {
  96. pandoc.RawBlock("html", "<details class='file-details'><summary>File details</summary>"),
  97. _schema_table(file, schema),
  98. pandoc.RawBlock("html", "</details>"),
  99. }
  100. end
  101. end
  102. el.content:extend(meta)
  103. return el
  104. end
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...