-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_descriptions.py
103 lines (81 loc) · 3.91 KB
/
parse_descriptions.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
"""
Creates a table with the metadata of each of the userscripts,
then uses that table to merge it into the README file
"""
import os
import re
def escape(text):
""" Escapes markdown characters """
chars = "\\`*_{}[]()#+-.!>~"
for c in chars:
text = text.replace(c, "\\" + c)
return text
def parse_file(path, file_name, output):
""" Prints the name, description, and match patterns of the userscript """
lines = [line.rstrip('\n') for line in open(path + file_name)]
match = ''
for line in lines:
if re.match("// ==/UserScript==", line):
break
# this would print them line by line
# if re.match("// ( |@(name|match|description)) ", line):
# output.write(line[4:] + '\n')
# write a row for a markdown table
if "// @name " in line:
name = escape(line).split(' ', 2)[2].lstrip() # remove first 2 'words'
current = 'name'
if "// @description" in line:
description = escape(line).split(' ', 2)[2].lstrip() # remove first 2 'words'
current = 'description'
if "// @match" in line:
# add break starting with second line
match += ('' if match == '' else '<br>') + \
"``" + line.split(' ', 2)[2].lstrip() + "``" # remove first 2 'words'
current = 'match'
if "// " in line:
line = escape(line).split(' ', 1)[1].lstrip() # remove first 'word'
if current == 'name':
name += '<br>' + line
if current == 'description':
description += '<br>' + line
if current == 'match':
match += '<br>' + line
branch = 'master' if path == './' else path
# output.write("| {} | {} | {} | {} |\n".format(name, description, match, file_name))
# source code: github.com/aljgom/UserScripts/blob/master/airbnb.user.js
# install: aljgom.github.io/UserScripts/airbnb.user.js
make_link = lambda text, url, alt='': f"[{text}]({url} \"{alt}\")"
source_code = f"https://github.com/aljgom/UserScripts/blob/master/{path}{file_name}"
install = f"https://aljgom.github.io/UserScripts/{path}{file_name}"
gif_path = f"{'..' if path == './old' else '.'}/demos/{file_name.split('.')[0]}.gif"
source_link = make_link("Source Code", source_code, "Source Code")
install_link = make_link("Download/Install", install, "Download/Install")
gif_link = make_link(f"![]({gif_path})", gif_path, " ")
output.write(f"| **{name}** <br> {source_link} {install_link}")
output.write(f"| {description} <br> _Match:_ <br> {match} <br> {gif_link}|\n")
def create_table(path):
""" Read files, and parse the ones that match user.js """
# Save the result in _preview_table.md
with open(path + '_preview_table.md', 'w') as output:
output.write('| Name / File | Description / Match |\n')
output.write('|---|---|\n')
if path == "./":
output.write('| [Old UserScripts](old/) | Older Userscripts | \n')
# else:
# output.write('| fd[Main Userscripts](/../) | Return to main table | \n')
for file_name in os.listdir(path):
if "user.js" in file_name:
parse_file(path, file_name, output)
merge_readme(path)
def merge_readme(path):
""" Merge _readme.md and _preview_table.md into README.md """
file_names = ['_readme.md', '_preview_table.md']
with open(path + 'README.md', 'w') as outfile:
outfile.write('<!-- \n\n\n\n\n\n **DO NOT EDIT THIS FILE.** Make changes to `_readme.md`, ' +
'and that will be used to create this file -->\n\n\n\n\n\n')
for f_name in file_names:
with open(path + f_name) as infile:
outfile.write(infile.read())
outfile.write("\n")
create_table('./')
create_table('old/')