Skip to content

Instantly share code, notes, and snippets.

@TerrenceSun
Last active January 9, 2023 18:19
Show Gist options
  • Save TerrenceSun/972ef4eea97f331af1e6abfcafb7c6e5 to your computer and use it in GitHub Desktop.
Save TerrenceSun/972ef4eea97f331af1e6abfcafb7c6e5 to your computer and use it in GitHub Desktop.
This file read file of 'Plain SVG' type and change id with the same value of inkscape:label. The reason of making this script is that some application only read the id attribute, and it needs the value of id following some specified format. Manually way of doing this is to open the 'XML Editor', and type in the id with value you want.
#!/usr/bin/env perl
use strict;
my ($id, $label);
my @all_lines;
my %mapping;
while(<>) {
push @all_lines, $_;
if (/^\s*</) {
$id = undef;
$label = undef;
}
if (/^\s*id="(.*)"/) {
$id = $1;
} elsif (/^\s*inkscape:label="(.*)"/) {
$label = $1;
}
if (/>$/) {
if ($id and $label) {
if (exists $mapping{$id}) {
die("$id existing, mapping it with $mapping{$id}");
} else {
if ($id ne $label) {
$mapping{$id} = $label;
}
}
}
}
}
foreach (@all_lines) {
if (/^\s*id="(.*)"/) {
$id = $1;
if (exists ${mapping}{$id}) {
print stderr "replacing $id with $mapping{$id}\n";
s/"$id"/"$mapping{$id}"/;
}
}
print $_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment