-
Notifications
You must be signed in to change notification settings - Fork 1
/
annotation_to_markdown.rb
executable file
·184 lines (160 loc) · 4.18 KB
/
annotation_to_markdown.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
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
#!/usr/bin/env ruby
# encoding: utf-8
gem "nokogiri", "~> 1.5.6"
gem "activesupport", "~> 3.2.12"
require "rubygems"
require "find"
require "nokogiri"
require "yaml"
require "active_support/core_ext/string/inflections.rb"
require "active_support/inflector/transliterate"
include ActiveSupport::Inflector
require "date"
# Configuration
#XML_DIRECTORY = "/media/Cybook/Digital Editions/Annotations/Digital Editions/"
XML_DIRECTORY = "Digital Editions"
MD_DIRECTORY = "markdown-annotations"
MD_TEMPLATE = <<EOF
---
layout: quote
fragment_start: %{fragment_start}
fragment_end: %{fragment_end}
category: Quote
---
%{text}
EOF
# Support template variables in Ruby < 1.9.2
if RUBY_VERSION < '1.9.2'
class String
old_format = instance_method(:%)
define_method(:%) do |arg|
if arg.is_a?(Hash)
self.gsub(/%\{(.*?)\}/) { arg[$1.to_sym] }
else
old_format.bind(self).call(arg)
end
end
end
end
module Find
def self.find_by_pattern dir, pattern
Find.find dir do |path|
if FileTest.directory?(path)
if File.basename(path)[0] == "."
Find.prune
else
next
end
else
if File.basename(path) !~ pattern
Find.prune
end
if block_given?
yield path
end
end
end
end
end
class AnnotationToMarkdown
def self.process directory
Find.find_by_pattern directory, /\.epub\.annot$/ do |path|
annotations_file = EpubAnnotationsFile.new path
annotations_file.save_new_annotations
end
end
end
class EpubAnnotationsFile
def initialize path
@path = path
@title = File.basename @path, ".epub.annot"
@title_param = @title.parameterize
@annotations = annotations
@md_annotations = md_annotations
end
def save_new_annotations
counter = @md_annotations.count
(@annotations - @md_annotations).select { |a| a.is_a? Annotation }.each do |a|
counter += 1
annotation = a.to_markdown :title => @title_param, :counter => counter
annotation.write
end
end
private
def annotations
xml = nil
File.open @path do |file|
xml = Nokogiri::XML file
end
annotations = []
xml.css("annotation").each do |node|
annotations << XmlAnnotation.new(node)
end
return annotations
end
def md_annotations
annotations = []
Find.find_by_pattern MD_DIRECTORY, /^\d{4}-\d{2}-\d{2}-#{@title_param}-\d*\.md$/ do |path|
annotations << MarkdownAnnotation.read(path)
end
return annotations
end
end
class Annotation
attr_reader :date, :title, :counter, :start, :end, :text
def ==(other)
@start == other.start && @end == other.end
end
alias eql? ==
def hash
@start.hash ^ @end.hash
end
end
class XmlAnnotation < Annotation
def initialize node
@start = node.css("fragment")[0]["start"].gsub /.*point\((.*)\)/, '\1'
@end = node.css("fragment")[0]["end"].gsub /.*point\((.*)\)/, '\1'
@text = node.css("text").children.to_xml(:encoding => "UTF-8").to_s.strip
end
def to_markdown args
@title = args[:title]
@counter = args[:counter]
MarkdownAnnotation.new self
end
end
class MarkdownAnnotation < Annotation
def initialize annotation
if annotation.is_a? Annotation
@title = annotation.title
@start = annotation.start
@end = annotation.end
@text = annotation.text
@date = Time.now.strftime "%Y-%m-%d"
@filename = [ @date, @title, annotation.counter ].join("-") + ".md"
else
@path = annotation
@basename = File.basename @path, ".md"
@date, @title = @basename.match(/^(\d{4}-\d{2}-\d{2})-(.*)-\d*$/).captures
read_annotation
end
end
def self.read path
self.new path
end
def write
if @path.nil?
File.open "#{MD_DIRECTORY}/#{@filename}", "w" do |file|
file << MD_TEMPLATE % { :text => @text, :fragment_start => @start, :fragment_end => @end }
end
end
end
private
def read_annotation
file = File.open @path
yaml_front_matter, @text = file.read.match(/^-{3}\n([^-]*)-{3}\n(.*)/m).captures
yaml = YAML.load yaml_front_matter
@start = yaml["fragment_start"]
@end = yaml["fragment_end"]
end
end
AnnotationToMarkdown.process XML_DIRECTORY