#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
clean_styles_full.py
Supprime tous les styles inline, les <rect>, et nettoie le SVG
sans toucher à la géométrie.

Auteur : Sylvain SCATTOLINI
Date : 2025-11-26
Version : 1.1
"""

from lxml import etree

SVG_INPUT = "carte_analyses_clean.svg"
SVG_OUTPUT = "carte_analyses_clean_nostyle.svg"

NS = {"svg": "http://www.w3.org/2000/svg"}

def remove_inline_styles(elem):
    for attr in ["style", "fill", "stroke", "stroke-width", "opacity", "fill-opacity"]:
        if attr in elem.attrib:
            del elem.attrib[attr]

def clean_svg():
    parser = etree.XMLParser(remove_comments=True)
    tree = etree.parse(SVG_INPUT, parser)
    root = tree.getroot()

    # 1. Supprimer tous les <rect>
    rects = root.findall(".//svg:rect", namespaces=NS)
    print(f"Rectangles supprimés : {len(rects)}")
    for r in rects:
        parent = r.getparent()
        parent.remove(r)

    # 2. Supprimer styles inline sur TOUT
    for elem in root.xpath(".//*"):
        remove_inline_styles(elem)

    tree.write(SVG_OUTPUT, encoding="utf-8", pretty_print=True, xml_declaration=True)
    print(f"SVG nettoyé → {SVG_OUTPUT}")

if __name__ == "__main__":
    clean_svg()