forked from n350071/enex-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.rb
84 lines (71 loc) · 1.87 KB
/
parse.rb
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
require 'nokogiri'
require 'reverse_markdown'
require 'date'
require 'pry'
def first(note)
value = Nokogiri::XML(
note.children.search('content').first
).xpath('//div')
return if value.empty?
ReverseMarkdown.convert(value.last.to_html)
end
def content(note)
return first(note) unless first(note).nil?
content = Nokogiri::XML(note.search('content').to_html)
.search('en-note').first
ReverseMarkdown.convert(content.children.to_html) unless content.nil?
end
def source(note)
source = note.search('note-attributes').first.search('source-url')
return source.first.content unless source.empty?
end
def parts(note)
[
note.children.search('title').first.content,
content(note),
note.children.search('tag').map(&:content),
DateTime.parse(note.children.search('created').first.content),
source(note)
]
end
def parse(file = './notes.enex')
Nokogiri::XML(File.read(file)).xpath('//note').map do |note|
title, content, tags, time, source = parts(note)
{ title: title, note: note, content: <<-END
#{title}
====================
* Created At: #{time}
* URL: #{source}
* Tags: #{tags.join ','}
#{content}
END
}
end
end
def output_note(note)
title = note[:title].gsub '/', '__'
title = title[0..50] if title.length > 100
file = "./notes/#{title}.txt"
File.open(file, 'w') do |f|
f.write note[:content]
end
end
def delete(note_title)
puts "Deleting '#{note_title}'"
# command = "/usr/local/bin/geeknote remove --note \"#{note_title}\""
# IO.popen(command, 'r+', err: [:child, :out]) do |io|
# io.puts 'Yes'
# puts io.readlines
# end
puts "Finished deleting #{note_title}"
end
def run
notes = parse
puts "There are #{notes.count} notes to migrate."
notes.each do |note|
output_note note
# delete note[:title]
end
puts 'All done migrating notes.'
end
run if __FILE__ == $PROGRAM_NAME