-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtab.py
executable file
·100 lines (78 loc) · 2.95 KB
/
tab.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
#!/usr/bin/python
import os
import yaml
import shutil
from functools import reduce
from optparse import OptionParser
# Command-line options
parser = OptionParser(usage="Usage: %prog [options]")
parser.add_option("-c", dest="columns_num", type="int",
metavar='NUM', help='maximum columns number', default=6)
parser.add_option("-s", "--css", dest="styles", action="store_true",
help='integrate "template/styles.css" into index.html',
default=False)
parser.add_option("-r", dest="remove_icons", action="store_true",
help="remove icons", default=False)
options, args = parser.parse_args()
def iconify(icon_name):
return '<i class="' + icon_name + ' icon"></i>'
if not options.remove_icons:
icons = {'group': 'folder open', 'entry': 'linkify'}
if options.styles:
f = open('template/styles.css', 'r')
styles = f.read()
f.close()
else:
shutil.copyfile('template/styles.css', 'output/styles.css')
# Importing bookmarks
bm = open('bookmarks.yml')
bm_yaml = yaml.safe_load(bm)
bm.close()
# Importing template files
with open('template/index.html') as f:
body_tmp = f.read()
with open('template/group.html') as f:
group_tmp = f.read()
with open('template/link.html') as f:
link_tmp = f.read()
# Iterating YAML
# bm_yaml is a list of dicts
groups_prehtml = []
for group_title, entries in bm_yaml.items():
# Group contains title and entries
# Iterating a group
entries_prehtml = []
# Iterating over entries and attributes
for entry_title, entry_info in entries.items():
# Icons
if not options.remove_icons:
icon = icons['entry'] if 'icon' not in entry_info\
else entry_info['icon']
entry_title = iconify(icon) + entry_title
# Links HTML
rep = ('{title}', entry_title), ('{href}', entry_info['href'])
entry_str = reduce(lambda a, k: a.replace(*k), rep, link_tmp.strip())
entries_prehtml.append(entry_str)
# Combining entries HTMLs
entries_html = " ".join(entries_prehtml)
if not options.remove_icons:
group_title = iconify(icons['group']) + group_title
# Group HTML
rep = ('{groupname}', group_title), ('{links}', entries_html)
group_str = reduce(lambda a, v: a.replace(*v), rep, group_tmp)
groups_prehtml.append(group_str)
# Combining groups
group_html = "\n".join(groups_prehtml)
num_words = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
num = min(len(bm_yaml), options.columns_num)
rep = ('{groups}', group_html), ('{num}', num_words[num]),\
('{styles}', styles if options.styles else ''),\
('{styles_ext}',
'' if options.styles else
'<link rel="stylesheet" type="text/css" href="styles.css">')
body_html = reduce(lambda a, v: a.replace(*v), rep, body_tmp)
# Creating an output dir
os.makedirs('output', exist_ok=True)
# Writing a file
with open('output/index.html', 'w') as f:
f.write(body_html)