Compress validated rules.
(rules: List[str], include_wildcards: bool)
| 240 | return res |
| 241 | |
| 242 | def compress_rules(rules: List[str], include_wildcards: bool) -> Tuple[List[str], List[str]]: |
| 243 | """Compress validated rules.""" |
| 244 | seen_hosts: Set[str] = set() |
| 245 | compressible: List[AdblockRule] = [] |
| 246 | filtered_text: List[str] = [] |
| 247 | kept_wildcards: List[str] = [] |
| 248 | |
| 249 | # Phase 1: Classification & Deduplication |
| 250 | for line in rules: |
| 251 | parsed_list = parse_rule(line) |
| 252 | if not parsed_list: |
| 253 | filtered_text.append(line) |
| 254 | continue |
| 255 | |
| 256 | for r in parsed_list: |
| 257 | if r.can_compress and r.hostname: |
| 258 | if r.hostname not in seen_hosts: |
| 259 | compressible.append(r) |
| 260 | seen_hosts.add(r.hostname) |
| 261 | else: |
| 262 | filtered_text.append(r.original_text) |
| 263 | else: |
| 264 | # Handle non-compressible (Raw processing) |
| 265 | is_cmt = r.rule_text.lstrip().startswith(('!', '#')) |
| 266 | if include_wildcards and '*' in r.rule_text and not is_cmt: |
| 267 | kept_wildcards.append(r.rule_text) |
| 268 | else: |
| 269 | filtered_text.append(r.original_text) |
| 270 | |
| 271 | # Phase 2: Redundancy Removal & Output Construction |
| 272 | final_list: List[str] = [] |
| 273 | |
| 274 | for r in compressible: |
| 275 | parts = r.hostname.split('.') |
| 276 | covered = False |
| 277 | # Check all parent domains |
| 278 | for i in range(1, len(parts)): |
| 279 | parent = ".".join(parts[i:]) |
| 280 | if parent in seen_hosts: |
| 281 | covered = True |
| 282 | break |
| 283 | |
| 284 | if covered: |
| 285 | filtered_text.append(r.original_text) |
| 286 | else: |
| 287 | if RE_OUTPUT_FILTER.match(r.rule_text): |
| 288 | final_list.append(r.rule_text) |
| 289 | else: |
| 290 | filtered_text.append(r.original_text) |
| 291 | |
| 292 | # Phase 3: Final Merge and STRICT ENFORCEMENT |
| 293 | merged_output = final_list + kept_wildcards |
| 294 | strictly_validated_output: List[str] = [] |
| 295 | |
| 296 | for rule in merged_output: |
| 297 | if RE_OUTPUT_FILTER.match(rule): |
| 298 | strictly_validated_output.append(rule) |
| 299 | else: |