summaryrefslogtreecommitdiff
path: root/docs/src/image-wildcard
blob: 5002e5d82406291779fe79787b67fc7ef81a8585 (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
#!/usr/bin/env python

import os, sys, glob

try:
	from os.path import relpath
except:
	def relpath(path, start=os.path.curdir):
	    """Return a relative version of a path"""

	    if not path:
		raise ValueError("no path specified")

	    start_list = os.path.abspath(start).split(os.path.sep)
	    path_list = os.path.abspath(path).split(os.path.sep)

	    # Work out how much of the filepath is shared by start and path.
	    i = len(os.path.commonprefix([start_list, path_list]))

	    rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
	    if not rel_list:
		return os.path.curdir
	    return os.path.join(*rel_list)

if len(sys.argv) > 1:
	image = sys.argv[1]

if len(sys.argv) > 2:
	docpath = sys.argv[2] #os.path.dirname(sys.argv[2])
else:
	docpath = '.'

if len(sys.argv) > 3 and str.strip(sys.argv[3]):
	exts = map(str.strip, sys.argv[3].split(','))
else:
	# Standard images for web
	exts = ['png', 'svg', 'jpg', 'jpeg']

exts = map(lambda e: '.' + e, exts)

def lookup(image):
	images = glob.glob(os.path.join(docpath, image))
	if not images:
		return
	elif len(images) == 1:
		return images[0]

	found = map(lambda s: os.path.splitext(s)[1], images)
	for e in exts:
		if e not in found:
			continue
		return images[found.index(e)]
	else:
		return images[0]
i = lookup(image)
if not i:
	i = image
print relpath(i, docpath)