Repository navigation
Expand file tree
/
Copy pathrssreader_opml.lua
More file actions
729 lines (655 loc) · 28.1 KB
/
Copy pathrssreader_opml.lua
File metadata and controls
729 lines (655 loc) · 28.1 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local util = require("util")
local OPML = {}
-- ---------------------------------------------------------------------------
-- OPML Import
-- ---------------------------------------------------------------------------
-- Decode XML entities in attribute values
local function decodeXmlEntities(s)
if not s then return nil end
s = s:gsub("&", "&")
s = s:gsub("<", "<")
s = s:gsub(">", ">")
s = s:gsub(""", '"')
s = s:gsub("'", "'")
return s
end
-- Encode XML entities for export
local function encodeXmlEntities(s)
if not s then return "" end
s = s:gsub("&", "&")
s = s:gsub("<", "<")
s = s:gsub(">", ">")
s = s:gsub('"', """)
s = s:gsub("'", "'")
return s
end
-- Extract a named attribute from a tag string
local function attr(tag, name)
-- Try double quotes first, then single quotes
local value = tag:match(name .. '%s*=%s*"([^"]*)"')
if not value then
value = tag:match(name .. "%s*=%s*'([^']*)'")
end
return decodeXmlEntities(value)
end
-- Parse a flat list of <outline> tags and build a tree structure.
-- OPML groups are <outline> tags without xmlUrl that contain child <outline> tags.
-- Leaf feeds are <outline> tags with xmlUrl.
function OPML.parseOPML(content)
if not content then
return nil, "No content"
end
-- Extract the <body> section
local body = content:match("<body>(.-)</body>")
if not body then
body = content:match("<body>(.*)")
end
if not body then
return nil, "No <body> found in OPML"
end
-- Result: array of groups, each group has title + feeds array
-- Top-level feeds without a group go into a special ungrouped list
local groups = {}
local ungrouped_feeds = {}
-- Strategy: find top-level outline tags that are groups (contain nested outlines)
-- and top-level outline tags that are feeds (have xmlUrl)
-- We process the body line-by-line approach using pattern matching
-- First, try to find group outlines (ones that have children)
-- Pattern: <outline ...> ... </outline> (multi-line, contains child outlines)
-- Also handle self-closing leaf outlines: <outline ... />
-- Split into top-level outline blocks
local pos = 1
while pos <= #body do
-- Find the next <outline tag
local tag_start, tag_end = body:find("<outline%s", pos)
if not tag_start then break end
-- Check if this is a self-closing tag
local self_close_end = body:find("/>", tag_end)
local open_close_end = body:find(">", tag_end)
if not open_close_end then break end
if self_close_end and self_close_end < open_close_end then
-- Self-closing <outline ... /> => this is a leaf feed
local tag_str = body:sub(tag_start, self_close_end + 1)
local xml_url = attr(tag_str, "xmlUrl")
if xml_url then
local title = attr(tag_str, "title") or attr(tag_str, "text") or xml_url
table.insert(ungrouped_feeds, { title = title, url = xml_url })
end
pos = self_close_end + 2
else
-- Opening tag <outline ...>
local tag_str = body:sub(tag_start, open_close_end)
local xml_url = attr(tag_str, "xmlUrl")
if xml_url then
-- This is a feed with an opening tag but no children
local title = attr(tag_str, "title") or attr(tag_str, "text") or xml_url
table.insert(ungrouped_feeds, { title = title, url = xml_url })
-- Skip to closing tag or next element
local close_tag = body:find("</outline>", open_close_end)
if close_tag then
pos = close_tag + 10
else
pos = open_close_end + 1
end
else
-- This is a group/folder outline
local group_title = attr(tag_str, "title") or attr(tag_str, "text") or "Unnamed Group"
-- Find the matching </outline> for this group
-- We need to handle nesting: count open/close outline tags
local depth = 1
local search_pos = open_close_end + 1
local group_end = nil
while depth > 0 and search_pos <= #body do
local next_open = body:find("<outline%s", search_pos) or body:find("<outline>", search_pos)
local next_self_close = body:find("<outline[^>]*/>", search_pos)
local next_close = body:find("</outline>", search_pos)
if not next_close then
-- Malformed, break
group_end = #body
break
end
-- Determine which comes first
if next_self_close and next_self_close < next_close and (not next_open or next_self_close <= next_open) then
-- Self-closing tag doesn't change depth
local sc_end = body:find("/>", next_self_close)
search_pos = sc_end and (sc_end + 2) or (next_self_close + 1)
elseif next_open and next_open < next_close then
-- Check if this open tag is actually self-closing
local tag_region_end = body:find(">", next_open)
local tag_sc = body:find("/>", next_open)
if tag_sc and tag_region_end and tag_sc < tag_region_end then
-- Self-closing, skip
search_pos = tag_sc + 2
else
depth = depth + 1
search_pos = (tag_region_end or next_open) + 1
end
else
depth = depth - 1
if depth == 0 then
group_end = next_close + 9 -- length of "</outline>"
end
search_pos = next_close + 10
end
end
if not group_end then
group_end = #body
end
-- Extract the inner content of the group
local inner = body:sub(open_close_end + 1, group_end - 10)
-- Parse child outline tags (feeds inside this group)
local group_feeds = {}
for child_tag in inner:gmatch("<outline([^>]-)/>") do
local child_url = attr(child_tag, "xmlUrl")
if child_url then
local child_title = attr(child_tag, "title") or attr(child_tag, "text") or child_url
table.insert(group_feeds, { title = child_title, url = child_url })
end
end
-- Also match <outline ...>...</outline> leaves inside the group
for child_tag in inner:gmatch("<outline([^>]-)>[^<]*</outline>") do
local child_url = attr(child_tag, "xmlUrl")
if child_url then
local child_title = attr(child_tag, "title") or attr(child_tag, "text") or child_url
-- Avoid duplicates
local found = false
for _, f in ipairs(group_feeds) do
if f.url == child_url then found = true; break end
end
if not found then
table.insert(group_feeds, { title = child_title, url = child_url })
end
end
end
if #group_feeds > 0 then
table.insert(groups, { title = group_title, feeds = group_feeds })
end
pos = group_end + 1
end
end
end
return {
groups = groups,
ungrouped_feeds = ungrouped_feeds,
}
end
-- Read an OPML file and return parsed data
function OPML.importFromFile(filepath)
local f, err = io.open(filepath, "r")
if not f then
return nil, "Cannot open file: " .. tostring(err)
end
local content = f:read("*a")
f:close()
if not content or #content == 0 then
return nil, "File is empty"
end
return OPML.parseOPML(content)
end
-- Serialize a Lua table to a pretty-printed Lua source string
-- (for writing rssreader_local_defaults.lua and rssreader_configuration.lua)
local function serializeValue(val, indent)
indent = indent or ""
local next_indent = indent .. " "
local t = type(val)
if t == "string" then
-- Use %q for safe quoting
return string.format("%q", val)
elseif t == "number" then
return tostring(val)
elseif t == "boolean" then
return tostring(val)
elseif t == "nil" then
return "nil"
elseif t == "table" then
-- Detect if this is an array or a hash table
local is_array = true
local max_index = 0
for k, _ in pairs(val) do
if type(k) == "number" and k == math.floor(k) and k > 0 then
if k > max_index then max_index = k end
else
is_array = false
break
end
end
if is_array and max_index == #val then
-- Array
local parts = {}
table.insert(parts, "{\n")
for i, v in ipairs(val) do
table.insert(parts, next_indent .. serializeValue(v, next_indent) .. ",\n")
end
table.insert(parts, indent .. "}")
return table.concat(parts)
else
-- Hash table
local parts = {}
table.insert(parts, "{\n")
-- Sort keys for deterministic output
local keys = {}
for k, _ in pairs(val) do
table.insert(keys, k)
end
table.sort(keys, function(a, b)
return tostring(a) < tostring(b)
end)
for _, k in ipairs(keys) do
local v = val[k]
local key_str
if type(k) == "string" and k:match("^[%a_][%w_]*$") then
key_str = k
else
key_str = "[" .. serializeValue(k) .. "]"
end
table.insert(parts, next_indent .. key_str .. " = " .. serializeValue(v, next_indent) .. ",\n")
end
table.insert(parts, indent .. "}")
return table.concat(parts)
end
end
return "nil"
end
-- Build the Lua source for rssreader_local_defaults.lua from the data table
function OPML.serializeLocalDefaults(data)
local lines = {}
table.insert(lines, "return {\n")
table.insert(lines, " accounts = {\n")
-- Sort account names for deterministic output
local account_names = {}
for name, _ in pairs(data.accounts) do
table.insert(account_names, name)
end
table.sort(account_names)
for _, name in ipairs(account_names) do
local account = data.accounts[name]
table.insert(lines, ' ["' .. name .. '"] = {\n')
-- Top-level feeds
if account.feeds and #account.feeds > 0 then
table.insert(lines, " feeds = {\n")
for _, feed in ipairs(account.feeds) do
table.insert(lines, string.format(' { title = %q, url = %q },\n', feed.title or "", feed.url or ""))
end
table.insert(lines, " },\n")
end
-- Groups
if account.groups and #account.groups > 0 then
table.insert(lines, " groups = {\n")
for _, group in ipairs(account.groups) do
table.insert(lines, " {\n")
table.insert(lines, string.format(' title = %q,\n', group.title or ""))
if group.description then
table.insert(lines, string.format(' description = %q,\n', group.description))
end
table.insert(lines, " feeds = {\n")
for _, feed in ipairs(group.feeds or {}) do
table.insert(lines, string.format(' { title = %q, url = %q },\n', feed.title or "", feed.url or ""))
end
table.insert(lines, " },\n")
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
table.insert(lines, "}\n")
return table.concat(lines)
end
-- Build the Lua source for rssreader_configuration.lua from the config table
function OPML.serializeConfiguration(config)
local lines = {}
table.insert(lines, "return {\n")
table.insert(lines, " accounts = {\n")
for _, account in ipairs(config.accounts or {}) do
table.insert(lines, " {\n")
table.insert(lines, string.format(' name = %q,\n', account.name or ""))
table.insert(lines, string.format(' type = %q,\n', account.type or "local"))
table.insert(lines, string.format(" active = %s,\n", tostring(account.active ~= false)))
if account.font_size then
table.insert(lines, string.format(" font_size = %d,\n", account.font_size))
end
if account.text_alignment then
table.insert(lines, string.format(' text_alignment = %q,\n', account.text_alignment))
end
if account.auto_mark_as_read ~= nil then
table.insert(lines, string.format(" auto_mark_as_read = %s,\n", tostring(account.auto_mark_as_read)))
end
if account.fetch_full_article_on_open ~= nil then
table.insert(lines, string.format(" fetch_full_article_on_open = %s,\n", tostring(account.fetch_full_article_on_open)))
end
if account.auth then
table.insert(lines, " auth = {\n")
if account.auth.base_url then
table.insert(lines, string.format(' base_url = %q,\n', account.auth.base_url))
end
if account.auth.username then
table.insert(lines, string.format(' username = %q,\n', account.auth.username))
end
if account.auth.password then
table.insert(lines, string.format(' password = %q,\n', account.auth.password))
end
table.insert(lines, " },\n")
end
if account.special_feeds then
table.insert(lines, " special_feeds = {\n")
for _, sf in ipairs(account.special_feeds) do
table.insert(lines, " {\n")
if sf.id then
table.insert(lines, string.format(' id = %q,\n', sf.id))
end
if sf.title then
table.insert(lines, string.format(' title = %q,\n', sf.title))
end
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
end
table.insert(lines, " options = {\n")
if account.options then
for k, v in pairs(account.options) do
if v == nil then
table.insert(lines, string.format(" %s = nil,\n", k))
else
table.insert(lines, string.format(" %s = %s,\n", k, serializeValue(v)))
end
end
else
table.insert(lines, " default_folder = nil,\n")
end
table.insert(lines, " },\n")
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
-- Sanitizers
if config.sanitizers then
table.insert(lines, " sanitizers = {\n")
for _, san in ipairs(config.sanitizers) do
table.insert(lines, " {\n")
if san.order then
table.insert(lines, string.format(" order = %d,\n", san.order))
end
if san.type then
table.insert(lines, string.format(' type = %q,\n', san.type))
end
table.insert(lines, string.format(" active = %s,\n", tostring(san.active == true)))
if san.token then
table.insert(lines, string.format(' token = %q,\n', san.token))
end
if san.base_url then
table.insert(lines, string.format(' base_url = %q,\n', san.base_url))
end
-- Written even when 0 ("no ceiling"): dropping it here would
-- silently restore the default limit on the next rewrite.
if tonumber(san.monthly_request_limit) then
table.insert(lines, string.format(" monthly_request_limit = %d,\n",
tonumber(san.monthly_request_limit)))
end
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
end
-- Encoding converters
if config.encoding_converters then
table.insert(lines, " encoding_converters = {\n")
for _, conv in ipairs(config.encoding_converters) do
table.insert(lines, " {\n")
if conv.order then
table.insert(lines, string.format(" order = %d,\n", conv.order))
end
if conv.type then
table.insert(lines, string.format(' type = %q,\n', conv.type))
end
table.insert(lines, string.format(" active = %s,\n", tostring(conv.active == true)))
if conv.api_key then
table.insert(lines, string.format(' api_key = %q,\n', conv.api_key))
end
if conv.path then
table.insert(lines, string.format(' path = %q,\n', conv.path))
end
if conv.use_cache ~= nil then
table.insert(lines, string.format(" use_cache = %s,\n", tostring(conv.use_cache)))
end
table.insert(lines, " },\n")
end
table.insert(lines, " },\n")
end
-- Features
if config.features then
table.insert(lines, "\n features = {\n")
if config.features.default_folder_on_save then
table.insert(lines, string.format(' default_folder_on_save = %q,\n', config.features.default_folder_on_save))
else
table.insert(lines, " default_folder_on_save = nil,\n")
end
if config.features.download_images_when_sanitize_successful ~= nil then
table.insert(lines, string.format(" download_images_when_sanitize_successful = %s,\n",
tostring(config.features.download_images_when_sanitize_successful)))
end
if config.features.download_images_when_sanitize_unsuccessful ~= nil then
table.insert(lines, string.format(" download_images_when_sanitize_unsuccessful = %s,\n",
tostring(config.features.download_images_when_sanitize_unsuccessful)))
end
if config.features.show_images_in_preview ~= nil then
table.insert(lines, string.format(" show_images_in_preview = %s,\n",
tostring(config.features.show_images_in_preview)))
end
if tonumber(config.features.image_download_workers) then
table.insert(lines, string.format(" image_download_workers = %d,\n",
tonumber(config.features.image_download_workers)))
end
table.insert(lines, " },\n")
end
table.insert(lines, "}\n")
return table.concat(lines)
end
-- ---------------------------------------------------------------------------
-- OPML Export
-- ---------------------------------------------------------------------------
-- Generate OPML XML from the local defaults data
function OPML.generateOPML(local_defaults_data, title)
title = title or "KOReader RSS Feeds"
local lines = {}
table.insert(lines, '<?xml version="1.0" encoding="UTF-8"?>')
table.insert(lines, '<opml version="2.0">')
table.insert(lines, " <head>")
table.insert(lines, " <title>" .. encodeXmlEntities(title) .. "</title>")
table.insert(lines, " <dateCreated>" .. os.date("!%a, %d %b %Y %H:%M:%S GMT") .. "</dateCreated>")
table.insert(lines, " </head>")
table.insert(lines, " <body>")
local accounts = local_defaults_data.accounts or {}
for account_name, account in pairs(accounts) do
table.insert(lines, ' <outline text="' .. encodeXmlEntities(account_name) .. '" title="' .. encodeXmlEntities(account_name) .. '">')
-- Top-level feeds in this account
if account.feeds then
for _, feed in ipairs(account.feeds) do
local feed_title = encodeXmlEntities(feed.title or feed.url or "")
local feed_url = encodeXmlEntities(feed.url or "")
table.insert(lines, ' <outline type="rss" text="' .. feed_title .. '" title="' .. feed_title .. '" xmlUrl="' .. feed_url .. '"/>')
end
end
-- Groups
if account.groups then
for _, group in ipairs(account.groups) do
local group_title = encodeXmlEntities(group.title or "Unnamed")
table.insert(lines, ' <outline text="' .. group_title .. '" title="' .. group_title .. '">')
for _, feed in ipairs(group.feeds or {}) do
local feed_title = encodeXmlEntities(feed.title or feed.url or "")
local feed_url = encodeXmlEntities(feed.url or "")
table.insert(lines, ' <outline type="rss" text="' .. feed_title .. '" title="' .. feed_title .. '" xmlUrl="' .. feed_url .. '"/>')
end
table.insert(lines, " </outline>")
end
end
table.insert(lines, " </outline>")
end
table.insert(lines, " </body>")
table.insert(lines, "</opml>")
return table.concat(lines, "\n") .. "\n"
end
-- ---------------------------------------------------------------------------
-- File path helpers
-- ---------------------------------------------------------------------------
function OPML.getPluginDir()
local path = package.searchpath("rssreader_opml", package.path)
if path then
return path:match("(.*/)")
end
return lfs.currentdir() .. "/plugins/rssreader.koplugin/"
end
function OPML.getDefaultImportPath()
return OPML.getPluginDir() .. "import.opml"
end
function OPML.getDefaultExportPath()
return OPML.getPluginDir() .. "export.opml"
end
-- ---------------------------------------------------------------------------
-- High-level import: merge OPML into existing config files
-- ---------------------------------------------------------------------------
-- Load current configuration
function OPML.loadConfiguration()
package.loaded["rssreader_configuration"] = nil
local ok, config = pcall(require, "rssreader_configuration")
if ok and type(config) == "table" then
return config
end
return { accounts = {} }
end
-- Load current local defaults
function OPML.loadLocalDefaults()
local path = package.searchpath("rssreader_local_defaults", package.path)
if not path then
return { accounts = {} }
end
local chunk, err = loadfile(path)
if not chunk then
return { accounts = {} }
end
local ok2, data = pcall(chunk)
if not ok2 or type(data) ~= "table" then
return { accounts = {} }
end
return data
end
function OPML.getLocalDefaultsPath()
local path = package.searchpath("rssreader_local_defaults", package.path)
if path then return path end
return OPML.getPluginDir() .. "rssreader_local_defaults.lua"
end
function OPML.getConfigurationPath()
local path = package.searchpath("rssreader_configuration", package.path)
if path then return path end
return OPML.getPluginDir() .. "rssreader_configuration.lua"
end
-- Perform the import: parse OPML, create a new local account, write both files
-- account_name: the name for the new local account
-- Returns true on success, or nil + error message
function OPML.performImport(opml_path, account_name)
-- Parse OPML
local parsed, err = OPML.importFromFile(opml_path)
if not parsed then
return nil, err
end
local total_feeds = #(parsed.ungrouped_feeds or {})
for _, g in ipairs(parsed.groups or {}) do
total_feeds = total_feeds + #(g.feeds or {})
end
if total_feeds == 0 then
return nil, "No feeds found in OPML file"
end
-- Load existing data
local config = OPML.loadConfiguration()
local local_defaults = OPML.loadLocalDefaults()
if not config.accounts then config.accounts = {} end
if not local_defaults.accounts then local_defaults.accounts = {} end
-- Check for duplicate account name
for _, acc in ipairs(config.accounts) do
if acc.name == account_name then
return nil, string.format("Account '%s' already exists in configuration", account_name)
end
end
-- Add account to configuration
table.insert(config.accounts, {
name = account_name,
type = "local",
active = true,
options = {
default_folder = nil,
},
})
-- Build local defaults entry
local new_account = {}
if parsed.ungrouped_feeds and #parsed.ungrouped_feeds > 0 then
new_account.feeds = parsed.ungrouped_feeds
end
if parsed.groups and #parsed.groups > 0 then
new_account.groups = parsed.groups
end
local_defaults.accounts[account_name] = new_account
-- Write configuration file
local config_path = OPML.getConfigurationPath()
local config_content = OPML.serializeConfiguration(config)
local f1, e1 = io.open(config_path, "w")
if not f1 then
return nil, "Cannot write configuration: " .. tostring(e1)
end
f1:write(config_content)
f1:close()
-- Write local defaults file
local defaults_path = OPML.getLocalDefaultsPath()
local defaults_content = OPML.serializeLocalDefaults(local_defaults)
local f2, e2 = io.open(defaults_path, "w")
if not f2 then
return nil, "Cannot write local defaults: " .. tostring(e2)
end
f2:write(defaults_content)
f2:close()
logger.info("RSSReader OPML", "Imported", total_feeds, "feeds into account", account_name)
return true, total_feeds
end
-- Perform export: read local defaults, generate OPML, write to file
function OPML.performExport(opml_path)
local local_defaults = OPML.loadLocalDefaults()
if not local_defaults.accounts or not next(local_defaults.accounts) then
return nil, "No local accounts found to export"
end
local content = OPML.generateOPML(local_defaults, "KOReader RSS Feeds")
local f, err = io.open(opml_path, "w")
if not f then
return nil, "Cannot write OPML file: " .. tostring(err)
end
f:write(content)
f:close()
-- Count total feeds exported
local total = 0
for _, account in pairs(local_defaults.accounts) do
if account.feeds then
total = total + #account.feeds
end
if account.groups then
for _, g in ipairs(account.groups) do
total = total + #(g.feeds or {})
end
end
end
logger.info("RSSReader OPML", "Exported", total, "feeds to", opml_path)
return true, total
end
-- Find OPML files in the plugin directory
function OPML.findOPMLFiles()
local dir = OPML.getPluginDir()
local files = {}
for entry in lfs.dir(dir) do
if entry:match("%.opml$") or entry:match("%.xml$") then
table.insert(files, {
name = entry,
path = dir .. entry,
})
end
end
table.sort(files, function(a, b) return a.name < b.name end)
return files
end
return OPML