replace static method

IconParkSvgBuilder replace(
  1. String svg
)

Implementation

static IconParkSvgBuilder replace(String svg) {
  final rawSvg = svg;
  return (IconParkProps props) {
    var svg = rawSvg;
    svg = svg.replaceAllMapped(
      RegExp(r'stroke-linejoin="(\w+)"'),
      (match) =>
          'stroke-linejoin="${props.strokeLineJoin?.name ?? match.group(1)}"',
    );
    svg = svg.replaceAllMapped(
      RegExp(r'stroke-linecap="(\w+)"'),
      (match) =>
          'stroke-linecap="${props.strokeLineCap?.name ?? match.group(1)}"',
    );
    svg = svg.replaceAllMapped(
      RegExp(r'stroke-width="(\d+)"'),
      (match) => 'stroke-width="${props.strokeWidth ?? match.group(1)}"',
    );
    svg = svg.replaceAllMapped(RegExp(r'(fill|stroke)="([^"]+)"'), (match) {
      final type = match.group(1);
      final color = match.group(2)?.toLowerCase();
      String? targetColor;
      if (color == 'black' || color == '#000' || color == '#000000') {
        targetColor = props.c1;
      } else if (color == '#2f88ff') {
        targetColor = props.c2;
      } else if (color == 'white' || color == '#fff' || color == '#ffffff') {
        targetColor = props.c3;
      } else if (color == '#43ccf8') {
        targetColor = props.c4;
      }
      if (targetColor != null) {
        return '$type="$targetColor"';
      }
      return match.group(0) ?? '';
    });
    return svg;
  };
}