-
Notifications
You must be signed in to change notification settings - Fork 589
/
Copy pathhrefutils.py
373 lines (323 loc) · 12.6 KB
/
hrefutils.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
import sys
from urllib.parse import unquote
from urllib.parse import urlsplit
# default to using the preferred media-types ffrom the epub 3.2 spec
# https://www.w3.org/publishing/epub3/epub-spec.html#sec-cmt-supported
ext_mime_map = {
'.bm' : 'image/bmp',
'.bmp' : 'image/bmp',
'.css' : 'text/css',
'.epub' : 'application/epub+zip',
'.gif' : 'image/gif',
'.htm' : 'application/xhtml+xml',
'.html' : 'application/xhtml+xml',
'.jpeg' : 'image/jpeg',
'.jpg' : 'image/jpeg',
'.js' : 'application/javascript',
'.m4a' : 'audio/mp4',
'.m4v' : 'video/mp4',
'.mp3' : 'audio/mpeg',
'.mp4' : 'video/mp4',
'.ncx' : 'application/x-dtbncx+xml',
'.oga' : 'audio/ogg',
'.ogg' : 'audio/ogg',
'.ogv' : 'video/ogg',
'.opf' : 'application/oebps-package+xml',
'.otf' : 'font/otf',
'.pdf' : 'application/pdf',
'.pls' : 'application/pls+xml',
'.png' : 'image/png',
'.smil' : 'application/smil+xml',
'.svg' : 'image/svg+xml',
'.tif' : 'image/tiff',
'.tiff' : 'image/tiff',
'.ttc' : 'font/collection',
'.ttf' : 'font/ttf',
'.ttml' : 'application/ttml+xml',
'.txt' : 'text/plain',
'.vtt' : 'text/vtt',
'.webm' : 'video/webm',
'.webp' : 'image/webp',
'.woff' : 'font/woff',
'.woff2' : 'font/woff2',
'.xhtml' : 'application/xhtml+xml',
'.xml' : 'application/oebps-page-map+xml',
'.xpgt' : 'application/vnd.adobe-page-template+xml',
# '.js' : = "text/javascript',
# '.otf' : 'application/x-font-opentype',
# '.otf' : 'application/font-sfnt',
}
# deprecated font mediatypes
# See https://www.iana.org/assignments/media-types/media-types.xhtml#font
mime_group_map = {
'image/jpeg' : 'Images',
'image/png' : 'Images',
'image/gif' : 'Images',
'image/svg+xml' : 'Images',
'image/bmp' : 'Images', # not a core media type
'image/tiff' : 'Images', # not a core media type
'image/webp' : 'Images', # not a core media type
'text/html' : 'Text',
'application/xhtml+xml' : 'Text',
'application/x-dtbook+xml' : 'Text',
'font/woff2' : 'Fonts',
'font/woff' : 'Fonts',
'font/ttf' : 'Fonts',
'font/otf' : 'Fonts',
'font/sfnt' : 'Fonts',
'font/collection' : 'Fonts',
'application/vnd.ms-opentype' : 'Fonts',
'application/font-sfnt' : 'Fonts', # deprecated
'application/font-ttf' : 'Fonts', # deprecated
'application/font-otf' : 'Fonts', # deprecated
'application/font-woff' : 'Fonts', # deprecated
'application/font-woff2' : 'Fonts', # deprecated
'application/x-font-ttf' : 'Fonts', # deprecated
'application/x-truetype-font' : 'Fonts', # deprecated
'application/x-opentype-font' : 'Fonts', # deprecated
'application/x-font-ttf' : 'Fonts', # deprecated
'application/x-font-otf' : 'Fonts', # deprecated
'application/x-font-opentype' : 'Fonts', # deprecated
'application/x-font-truetype' : 'Fonts', # deprecated
'application/x-font-truetype-collection' : 'Fonts', # deprecated
'audio/mpeg' : 'Audio',
'audio/mp3' : 'Audio',
'audio/mp4' : 'Audio',
'audio/ogg' : 'Audio', # not a core media type
'video/mp4' : 'Video',
'video/ogg' : 'Video',
'video/webm' : 'Video',
'text/vtt' : 'Video',
'application/ttml+xml' : 'Video',
'text/css' : 'Styles',
'application/x-dtbncx+xml' : 'ncx',
'application/oebps-package+xml' : 'opf',
'application/oebps-page-map+xml' : 'Misc',
'application/vnd.adobe-page-map+xml' : 'Misc',
'application/vnd.adobe.page-map+xml' : 'Misc',
'application/smil+xml' : 'Misc',
'application/adobe-page-template+xml' : 'Misc',
'application/pdf' : 'Misc',
'application/vnd.adobe-page-template+xml' : 'Misc',
'text/javascript' : 'Misc',
'application/javascript' : 'Misc',
'application/pls+xml' : 'Misc',
'text/plain' : 'Misc',
}
# Note any # char in the href path component must be url encoded
# if fragments can exist
_ASCII_CHARS = set(chr(x) for x in range(128))
_URL_SAFE = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789' '_.-/~')
# From the IRI spec rfc3987
# iunreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" / ucschar
#
# ucschar = %xA0-D7FF / %xF900-FDCF / %xFDF0-FFEF
# / %x10000-1FFFD / %x20000-2FFFD / %x30000-3FFFD
# / %x40000-4FFFD / %x50000-5FFFD / %x60000-6FFFD
# / %x70000-7FFFD / %x80000-8FFFD / %x90000-9FFFD
# / %xA0000-AFFFD / %xB0000-BFFFD / %xC0000-CFFFD
# / %xD0000-DFFFD / %xE1000-EFFFD
# But currently nothing *after* the 0x30000 plane is even defined
def need_to_percent_encode(char):
cp = ord(char)
if cp < 128:
if char in _URL_SAFE: return False;
return True;
if cp < 0xA0: return True;
if cp <= 0xD7FF: return False;
if cp < 0xF900: return True;
if cp <= 0xFDCF: return False;
if cp < 0xFDF0: return True;
if cp <= 0xFFEF: return False;
if cp < 0x10000: return True;
if cp <= 0x1FFFD: return False;
if cp < 0x20000: return True;
if cp <= 0x2FFFD: return False;
if cp < 0x30000: return True;
if cp <= 0x3FFFD: return False;
return True;
def urlencodepart(part):
if isinstance(part,bytes):
part = part.decode('utf-8')
result = []
for char in part:
if need_to_percent_encode(char):
bs = char.encode('utf-8')
for b in bs:
result.append("%%%02X" % b)
else:
result.append(char)
return ''.join(result)
def urldecodepart(part):
if isinstance(part,bytes):
part = part.decode('utf-8')
part = unquote(part)
return part
# return a properly url encoded relative href
# from path and fragment components
def getRelativeHREF(apath, afragment):
if isinstance(apath, bytes):
apath = apath.decode('utf-8')
if afragment and isinstance(afragment, bytes):
afragment = afragment.decode('utf-8')
href = urlencodepart(apath)
if afragment:
href = href + "#" + urlencodepart(fragment)
return href
# return a properly url decoded path
# and fragment (None) from a url encoded relative href
def parseRelativeHREF(href):
if isinstance(href, bytes):
href = href.decode('utf-8')
parts = href.split('#')
apath = urldecodepart(parts[0])
afragment = None
if len(parts) > 1:
afragment = urldecodepart(parts[1])
return apath, afragment
def relativePath(to_bkpath, start_dir):
# remove any trailing path separators from both paths
dsegs = to_bkpath.rstrip('/').split('/')
ssegs = start_dir.rstrip('/').split('/')
if dsegs == ['']: dsegs = []
if ssegs == ['']: ssegs = []
res = []
i = 0
for s1, s2 in zip(dsegs, ssegs):
if s1 != s2: break
i += 1
for p in range(i, len(ssegs), 1): res.append('..')
for p in range(i, len(dsegs), 1): res.append(dsegs[p])
return '/'.join(res)
def resolveRelativeSegmentsInFilePath(file_path):
res = []
segs = file_path.split('/')
for i in range(len(segs)):
if segs[i] == '.': continue
if segs[i] == '..':
if res:
res.pop()
else:
print("Error resolving relative path segments")
else:
res.append(segs[i])
return '/'.join(res)
def buildRelativePath(from_bkpath, to_bkpath):
if from_bkpath == to_bkpath: return ""
return relativePath(to_bkpath, startingDir(from_bkpath))
def buildBookPath(dest_relpath, start_folder):
if start_folder == "" or start_folder.strip() == "":
return dest_relpath
bookpath = start_folder.rstrip('/') + '/' + dest_relpath
return resolveRelativeSegmentsInFilePath(bookpath)
def startingDir(file_path):
ssegs = file_path.split('/')
ssegs.pop()
return '/'.join(ssegs)
def longestCommonPath(bookpaths):
# handle special cases
if len(bookpaths) == 0: return ""
if len(bookpaths) == 1: return startingDir(bookpaths[0]) + '/'
fpaths = bookpaths
fpaths.sort()
segs1 = fpaths[0].split('/')
segs2 = fpaths[-1].split('/')
res = []
for s1, s2 in zip(segs1, segs2):
if s1 != s2: break
res.append(s1)
if not res or len(res) == 0:
return ""
return '/'.join(res) + '/'
# DEPRECATED: kept only for backwards compatibility
# as it assumes no # chars will ever exist in an href path
_XURL_SAFE = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789' '#' '_.-/~')
_XIRI_UNSAFE = _ASCII_CHARS - _XURL_SAFE
# DEPRECATED: kept only for backwards compatibility
# as it assumes no # chars will ever exist in an href path
# returns a quoted IRI (not a URI)
def quoteurl(href):
if isinstance(href, bytes):
href = href.decode('utf-8')
(scheme, netloc, path, query, fragment) = urlsplit(href, scheme="", allow_fragments=True)
if scheme != "":
scheme += "://"
href = href[len(scheme):]
result = []
for char in href:
if char in _XIRI_UNSAFE:
char = "%%%02x" % ord(char)
result.append(char)
return scheme + ''.join(result)
# DEPRECATED: kept only for backwards compatibility
# as it assumes no # chars will ever exist in an href path
# unquotes url/iri
def unquoteurl(href):
if isinstance(href, bytes):
href = href.decode('utf-8')
href = unquote(href)
return href
def main():
p1 = 'This/is/the/../../end.txt'
print('Testing resolveRelativeSegmentsInFilePath(file_path)')
print(' file_path: ', p1)
print(resolveRelativeSegmentsInFilePath(p1))
print(' ')
p1 = 'hello.txt'
p2 = 'goodbye.txt'
print('Testing buildRelativePath(from_bkpath,to_bkpath')
print(' from_bkpath: ', p1)
print(' to_bkpath: ', p2)
print(buildRelativePath(p1, p2))
print(' ')
p1 = 'OEBPS/Text/book1/chapter1.xhtml'
p2 = 'OEBPS/Text/book2/chapter1.xhtml'
print('Testing buildRelativePath(from_bkpath,to_bkpath)')
print(' from_bkpath: ', p1)
print(' to_bkpath: ', p2)
print(buildRelativePath(p1, p2))
print(' ')
p1 = 'OEBPS/package.opf'
p2 = 'OEBPS/Text/book1/chapter1.xhtml'
print('Testing buildRelativePath(from_bkpath, to_bkpath)')
print(' from_bkpath: ', p1)
print(' to_bkpath: ', p2)
print(buildRelativePath(p1, p2))
print(' ')
p1 = '../../Images/image.png'
p2 = 'OEBPS/Text/book1/'
print('Testing buildBookPath(destination_href, start_dir)')
print(' destination_href: ', p1)
print(' starting_dir: ', p2)
print(buildBookPath(p1, p2))
print(' ')
p1 = 'image.png'
p2 = ''
print('Testing buildBookPath(destination_href, start_dir)')
print(' destination_href: ', p1)
print(' starting_dir: ', p2)
print(buildBookPath(p1, p2))
print(' ')
p1 = 'content.opf'
print('Testing startingDir(bookpath')
print(' bookpath: ', p1)
print('"' + startingDir(p1) + '"')
print(' ')
bookpaths = []
bookpaths.append('OEBPS/book1/text/chapter1.xhtml')
bookpaths.append('OEBPS/book1/html/chapter2.xhtml')
bookpaths.append('OEBPS/book2/text/chapter3.xhtml')
print('Testing longestCommonPath(bookpaths)')
print(' bookpaths: ', bookpaths)
print('"' + longestCommonPath(bookpaths) + '"')
print(' ')
return 0
if __name__ == '__main__':
sys.exit(main())