-
Notifications
You must be signed in to change notification settings - Fork 23
/
bpgen.py
executable file
·151 lines (128 loc) · 5.34 KB
/
bpgen.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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
################################################################################
##
## This file is part of BetterPonymotes.
## Copyright (c) 2012-2015 Typhos.
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU Affero General Public License as published by
## the Free Software Foundation, either version 3 of the License, or (at your
## option) any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
## for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
################################################################################
import argparse
import json
import time
import bplib
import bplib.condense
import bplib.objects
import bplib.resolve
def build_js_map(context, emotes, all_emotes, tag_name2id):
emote_map = {}
for (name, emote) in emotes.items():
assert name not in emote_map
if emote.source.variant_matches is None:
emote.source.match_variants()
data = encode(context, emote.source, emote, all_emotes, tag_name2id)
emote_map[name] = data
return emote_map
FLAG_NSFW = 1
FLAG_REDIRECT = 1 << 1
def encode(context, source, emote, all_emotes, tag_name2id):
# FFFFFFF,TtTtTtTt[,/base-name]
base = emote.base_variant()
root = source.variant_matches[emote]
all_tags = root.all_tags(context) | emote.all_tags(context)
# F-FF-FFFF = flags, primary source id, and size
is_nsfw = "+nsfw" in all_tags
assert ("+v" in emote.tags) == (emote.name != root.name)
is_redirect = emote.name != root.name
flags = 0
if is_nsfw:
flags |= FLAG_NSFW
if is_redirect:
flags |= FLAG_REDIRECT
size = max(base.size) if hasattr(base, "size") else 0
flag_data = "%1x%02x%04x" % (flags, source.source_id, size)
assert len(flag_data) == 7
# Tt = tag id list
emitted_tags = [tag for tag in all_tags if tag not in context.tag_config["HiddenTags"]]
tag_ids = sorted(tag_name2id[tag] for tag in emitted_tags)
assert all(id < 0xff for id in tag_ids) # One byte per
tag_data = "".join("%02x" % id for id in tag_ids)
data = "%s,%s" % (flag_data, tag_data)
if is_redirect:
data += "," + root.name
return data
def build_css(emotes):
css_rules = {}
for emote in emotes:
for variant in emote.variants.values():
selector, properties = variant.selector(), variant.to_css()
if selector in css_rules and css_rules[selector] != properties:
print("ERROR: Selector %r used twice!" % (selector))
css_rules[selector] = properties
return css_rules
AutogenHeader = """
/*
* This file is AUTOMATICALLY GENERATED. DO NOT EDIT.
* Generated at %s.
*/
""" % (time.strftime("%c"))
def dump_css(file, rules):
file.write(AutogenHeader)
for (selector, properties) in rules.items():
property_strings = ["%s:%s" % i for i in properties.items()]
s = "%s{%s}\n" % (selector, ";".join(property_strings))
file.write(s)
def dump_js_data(file, js_map, sr_id2name, sr_name2id, tag_id2name, tag_name2id):
file.write(AutogenHeader)
_dump_js_obj(file, "sr_id2name", sr_id2name)
_dump_js_obj(file, "sr_name2id", sr_name2id)
# exports is used in Firefox main.js, but doesn't exist elsewhere
file.write("if(typeof(exports) !== 'undefined') {\n")
file.write(" exports.sr_id2name = sr_id2name;\n")
file.write(" exports.sr_name2id = sr_name2id;\n")
file.write("}\n")
_dump_js_obj(file, "tag_id2name", tag_id2name)
_dump_js_obj(file, "tag_name2id", tag_name2id)
_dump_js_obj(file, "emote_map", js_map)
def _dump_js_obj(file, var_name, obj):
file.write("var %s = " % (var_name))
json.dump(obj, file, indent=0, separators=(",", ":"), sort_keys=True)
file.write(";\n")
def main():
parser = argparse.ArgumentParser(description="Generate addon data files from emotes")
parser.add_argument("-j", "--js", help="Output JS data file", default="build/bpm-resources.js")
parser.add_argument("-c", "--css", help="Output CSS file", default="build/emote-classes.css")
parser.add_argument("--no-compress", help="Disable CSS compression", action="store_true")
args = parser.parse_args()
print("Loading emotes")
context = bplib.objects.Context()
context.load_config()
context.load_sources()
print("Processing")
emotes, all_emotes = bplib.resolve.resolve_emotes(context)
tag_id2name, tag_name2id = bplib.resolve.build_tag_map(all_emotes, context)
sr_id2name, sr_name2id = bplib.resolve.build_sr_data(context)
js_map = build_js_map(context, emotes, all_emotes, tag_name2id)
css_rules = build_css(emotes.values())
if not args.no_compress:
bplib.condense.condense_css(css_rules)
bplib.condense.chunkify(css_rules) # See comment in chunkify()
print("Dumping")
with open(args.css, "w") as file:
dump_css(file, css_rules)
with open(args.js, "w") as file:
dump_js_data(file, js_map, sr_id2name, sr_name2id, tag_id2name, tag_name2id)
if __name__ == "__main__":
main()