-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge.red
125 lines (102 loc) · 2.84 KB
/
merge.red
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
Red [
Title: "Merger for AdditionalFilters"
Author: "Crystal RainSlide"
]
make-metadata: function [
metadata-series [series!]
return: [object!]
][
ms: copy metadata-series
make object! [
title: take ms
description: take ms
homepage: take ms
expire: take ms
]
]
rejoin-metadata: function [
metadata [object!]
suffix [string!]
return: [string!]
][
rejoin [
"! Title: " metadata/title suffix LF
"! Description: " metadata/description LF
"! Homepage: " metadata/homepage LF
"! Expire: " metadata/expire LF
"! Last Modified: " ( to string! now ) LF ; AdBlock Plus
]
]
; For each file, the separator and all the text before it will be removed before merge.
comment {
"! ------------------------------------------------------------------------------\^"
}
separator: append ( append/dup "! " #"-" 78 ) LF
clean-ruleset: function [
ruleset-str [string!]
][
result: find/tail ruleset-str separator
either result <> none [
return result
][
return ruleset-str
]
]
merge-ruleset: function [
ruleset-dir [string!]
ruleset-name [string!]
metadata [object!]
][
ruleset-file: clean-path to file! :ruleset-dir
working-dir: dirize ruleset-file
output-file: append ruleset-file ".txt"
either exists? working-dir [
prin "Start at " print to-local-file working-dir
][
cause-error 'access 'cannot-open [ working-dir ]
]
output-result: rejoin-metadata metadata ruleset-name
old-dir: what-dir
change-dir working-dir
; traverse the working directory
foreach file read working-dir [
either all [
%.txt = suffix? file ; file's extention name matches *.txt
output-file <> file ; file is not output-file itself
][
prin " Add " print file
append output-result clean-ruleset read file
][
prin " Ignore " print file
]
]
change-dir old-dir
; write the output
; Till now, 2020-1-24, as far as I know, there is no offical way
; to write a text file with specified line break, but this /binary
; trick will write text file with LF perfectly.
write/binary output-file to-binary output-result
; This will use CRLF on Windows:
; write output-file output-result
print "End"
]
metadata-additional-filters: make-metadata [
"AdditionalFilters"
{
! Additional Filters for browser extension based adblockers like
! https://github.com/gorhill/uBlock
! mainly for Chinese sites, including some obvious trackers
! which should be found & listed & blocked YEARS ago.}
https://github.com/Crystal-RainSlide/AdditionalFiltersCN
"7 days"
]
metadata-rainslide: make-metadata [
"RainSlide's Custom Rules"
"Some custom rules excluded from AdditionalFiltersCN."
https://github.com/Crystal-RainSlide/AdditionalFiltersCN
"7 days"
]
foreach ruleset [ "CN" "Intl" "Game" ][
merge-ruleset ruleset ruleset metadata-additional-filters
]
merge-ruleset "RainSlide" "" metadata-rainslide