forked from zotero/zotero-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2xize
executable file
·66 lines (55 loc) · 1.95 KB
/
2xize
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 python
from __future__ import print_function
import sys
import os
import re
def append2x(content, dir):
header = """/* BEGIN 2X BLOCK -- DO NOT EDIT MANUALLY -- USE 2XIZE */
@media (min-resolution: 1.5dppx) {\n"""
footer = "\n}\n"
output = ""
for line in content.split("\n"):
matches = re.match('(.+chrome://zotero/skin/)(.+\.png)(.+)', line)
if not matches:
output += line
continue
png = matches.group(2)
png2x = png.replace('.png', '@2x.png')
if os.path.exists(os.path.join(root_dir, png2x)):
output += matches.group(1) + png2x + matches.group(3)
else:
output += line
output += "}"
# Collapse all spaces
output = re.sub("\s+", " ", output)
# Add space before and after "{"
output = re.sub("(?!: ){\s*", " { ", output)
# Add newline after }
output = re.sub("}", "}\n", output)
# Add space before }
output = re.sub("(?!: )}", " }", output)
# Strip comments
output = re.sub("/\*[^*]+\*/", "", output)
# Remove all blocks without 2x rules
output = "\n".join(["\t" + line for line in output.split("\n") if "2x.png" in line])
return header + output + footer
if __name__ == "__main__":
if len(sys.argv) != 2:
print("""Appends 2x image rules to a CSS file for all images with @2x.png versions in the same directory
Usage: {0} /path/to/css/file""".format(sys.argv[0]))
sys.exit(1)
css_file = sys.argv[1]
if not os.path.exists(css_file):
print("File not found: " + css_file)
sys.exit(1)
root_dir = os.path.dirname(os.path.realpath(css_file))
css = ""
with open(css_file) as f:
for line in f:
if "BEGIN 2X BLOCK" in line:
break
css += line
css = css.strip()
css += "\n\n\n" + append2x(css, root_dir)
with open(css_file, 'w') as f:
f.write(css)