1
+ local function read_inv_text (filename )
2
+ -- read file
3
+ local file = io.open (filename , " r" )
4
+ if file == nil then
5
+ return nil
6
+ end
7
+ local str = file :read (" a" )
8
+ file :close ()
9
+
10
+
11
+ local project = str :match (" # Project: (%S+)" )
12
+ local version = str :match (" # Version: (%S+)" )
13
+
14
+ local data = {project = project , version = version , items = {}}
15
+
16
+ local ptn_data =
17
+ " ^" ..
18
+ " (.-)%s+" .. -- name
19
+ " ([%S:]-):" .. -- domain
20
+ " ([%S]+)%s+" .. -- role
21
+ " (%-?%d+)%s+" .. -- priority
22
+ " (%S*)%s+" .. -- uri
23
+ " (.-)\r ?$" -- dispname
24
+
25
+
26
+ -- Iterate through each line in the file content
27
+ for line in str :gmatch (" [^\r\n ]+" ) do
28
+ if not line :match (" ^#" ) then
29
+ -- Match each line against the pattern
30
+ local name , domain , role , priority , uri , dispName = line :match (ptn_data )
31
+
32
+ -- if name is nil, raise an error
33
+ if name == nil then
34
+ error (" Error parsing line: " .. line )
35
+ end
36
+
37
+ data .items [# data .items + 1 ] = {
38
+ name = name ,
39
+ domain = domain ,
40
+ role = role ,
41
+ priority = priority ,
42
+ uri = uri ,
43
+ dispName = dispName
44
+ }
45
+ end
46
+ end
47
+ return data
48
+ end
49
+
1
50
local function read_json (filename )
51
+
2
52
local file = io.open (filename , " r" )
3
53
if file == nil then
4
54
return nil
5
55
end
6
56
local str = file :read (" a" )
7
57
file :close ()
8
- return quarto .json .decode (str )
58
+
59
+ local decoded = quarto .json .decode (str )
60
+ return decoded
61
+ end
62
+
63
+ local function read_inv_text_or_json (base_name )
64
+ local file = io.open (base_name .. " .txt" , " r" )
65
+ if file then
66
+ -- TODO: refactors so we don't just close the file immediately
67
+ io.close (file )
68
+ json = read_inv_text (base_name .. " .txt" )
69
+
70
+ else
71
+ json = read_json (base_name .. " .json" )
72
+ end
73
+
74
+ return json
9
75
end
10
76
11
77
local inventory = {}
12
78
13
- function lookup (search_object )
79
+ local function lookup (search_object )
14
80
15
81
local results = {}
16
- for ii , inventory in ipairs (inventory ) do
17
- for jj , item in ipairs (inventory .items ) do
82
+ for _ , inv in ipairs (inventory ) do
83
+ for _ , item in ipairs (inv .items ) do
18
84
-- e.g. :external+<inv_name>:<domain>:<role>:`<name>`
19
85
if item .inv_name and item .inv_name ~= search_object .inv_name then
20
86
goto continue
@@ -31,7 +97,9 @@ function lookup(search_object)
31
97
if search_object .domain and item .domain ~= search_object .domain then
32
98
goto continue
33
99
else
34
- table.insert (results , item )
100
+ if search_object .domain or item .domain == " py" then
101
+ table.insert (results , item )
102
+ end
35
103
36
104
goto continue
37
105
end
@@ -44,19 +112,17 @@ function lookup(search_object)
44
112
return results [1 ]
45
113
end
46
114
if # results > 1 then
47
- print (" Found multiple matches for " .. search_object .name )
48
- quarto .utils .dump (results )
49
- return nil
115
+ quarto .log .warning (" Found multiple matches for " .. search_object .name .. " , using the first match." )
116
+ return results [1 ]
50
117
end
51
118
if # results == 0 then
52
- print (" Found no matches for object:" )
53
- quarto .utils .dump (search_object )
119
+ quarto .log .warning (" Found no matches for object:\n " , search_object )
54
120
end
55
121
56
122
return nil
57
123
end
58
124
59
- function mysplit (inputstr , sep )
125
+ local function mysplit (inputstr , sep )
60
126
if sep == nil then
61
127
sep = " %s"
62
128
end
@@ -97,15 +163,15 @@ local function build_search_object(str)
97
163
search .role = normalize_role (t [3 ])
98
164
search .name = t [4 ]:match (" %%60(.*)%%60" )
99
165
else
100
- print (" couldn't parse this link: " .. str )
166
+ quarto . log . warning (" couldn't parse this link: " .. str )
101
167
return {}
102
168
end
103
169
else
104
170
search .name = str :match (" %%60(.*)%%60" )
105
171
end
106
172
107
173
if search .name == nil then
108
- print (" couldn't parse this link: " .. str )
174
+ quarto . log . warning (" couldn't parse this link: " .. str )
109
175
return {}
110
176
end
111
177
@@ -116,7 +182,7 @@ local function build_search_object(str)
116
182
return search
117
183
end
118
184
119
- function report_broken_link (link , search_object , replacement )
185
+ local function report_broken_link (link , search_object , replacement )
120
186
-- TODO: how to unescape html elements like [?
121
187
return pandoc .Code (pandoc .utils .stringify (link .content ))
122
188
end
@@ -154,7 +220,7 @@ function Link(link)
154
220
return link
155
221
end
156
222
157
- function fixup_json (json , prefix )
223
+ local function fixup_json (json , prefix )
158
224
for _ , item in ipairs (json .items ) do
159
225
item .uri = prefix .. item .uri
160
226
end
@@ -167,11 +233,12 @@ return {
167
233
local json
168
234
local prefix
169
235
for k , v in pairs (meta .interlinks .sources ) do
170
- json = read_json (quarto .project .offset .. " /_inv/" .. k .. " _objects.json" )
236
+ local base_name = quarto .project .offset .. " /_inv/" .. k .. " _objects"
237
+ json = read_inv_text_or_json (base_name )
171
238
prefix = pandoc .utils .stringify (v .url )
172
239
fixup_json (json , prefix )
173
240
end
174
- json = read_json (quarto .project .offset .. " /objects.json " )
241
+ json = read_inv_text_or_json (quarto .project .offset .. " /objects" )
175
242
if json ~= nil then
176
243
fixup_json (json , " /" )
177
244
end
0 commit comments