aboutsummaryrefslogtreecommitdiff
path: root/export-koreader-note
blob: 35762abd1bc1c154264f44420245066ed4c7f9fb (plain)
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
#!/usr/bin/env lua
--
-- Convert koreader metadata to markdown.
--

-- Sort by page number
function compare(a, b)
  return a.page < b.page
end

-- Check if a file exists
-- Thanks https://stackoverflow.com/questions/4990990/check-if-a-file-exists-with-lua#4991602
function file_exists(name)
  local f=io.open(name,"r")
  if f~=nil then io.close(f) return true else return false end
end

-- Get filename
file = arg[1]

-- Usage
if file == nil then
  print('usage: ' .. arg[0] .. ' <filename>')
  os.exit(1)
else
  if not file_exists(file) then
    print('file not found: ' .. file)
    os.exit(1)
  end
end

-- Load metadata
content     = assert(loadfile(file))
data        = content()
bookmarks   = data.bookmarks
annotations = data.annotations

-- Sort bookmarks
if bookmarks then
  table.sort(bookmarks,   compare)
end

-- Sort annotations
if annotations then
  table.sort(annotations, compare)
end

-- Iterate over bookmarks
if bookmarks then
  for key, item in ipairs(bookmarks) do
    print('Page ' .. item.page .. ':')
    print('')
    print('> ' .. item.notes)
    print('')
  end
end

-- Iterate over annotations
if annotations then
  for key, item in ipairs(annotations) do
    print('Page ' .. item.page .. ':')
    print('')
    print('> ' .. item.text)
    print('')
  end
end