Created
July 13, 2011 07:48
-
-
Save JJ/1079899 to your computer and use it in GitHub Desktop.
Pretty-print XML files with user-specified colors and indents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
?xml version="1.0"?> | |
<?xml-stylesheet href="tienda0.xsl" type="text/xsl"?> | |
<equipo> | |
<jugador posicion='portero'>Araña</jugador> | |
<jugador posicion='delantero'>Cazagoles</jugador> | |
</equipo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use YAML qw(LoadFile); | |
use File::Slurp qw(read_file); | |
my $conf = shift || die "Falta fichero de configuracion\n\tUso: $0 configuracion.yaml fichero.xml\n"; | |
my $fichero_xml = shift || die "Falta fichero\n\tUso: $0 configuracion.yaml fichero.xml\n"; | |
my $defs = LoadFile($conf) || die "No puedo cargar $conf: $!\n"; | |
my @colors = @{$defs->{'colors'}}; | |
my %indents = %{$defs->{'indents'}}; | |
my $page = read_file( $fichero_xml) || die "No puedo leer $fichero_xml: $!\n"; | |
#Instrucciones | |
$page =~ s{<\?}{<font color='$colors[0]'>\<\?}g; | |
$page =~ s{\?>}{\?\></font><br />}g; | |
#tags | |
for ( keys %indents ) { | |
my $indent = " "x$indents{$_}->[0]; | |
my $color = $colors[$indents{$_}->[1]]; | |
my $post = $indents{$_}->[2]; | |
$page =~ s{<($_[^>]*)>}{$indent<font color="$color">\<$1\></font>$post}g; | |
$page =~ s{</$_>}{$indent<font color="$color">\</$_\></font><br />}g; | |
} | |
print $page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
colors: | |
- red | |
- #993300 | |
- blue | |
indents: | |
equipo: | |
- 0 | |
- 1 | |
- '<br />' | |
jugador: | |
- 2 | |
- 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need to pretty-print XML files for tutorials or presentations, this file can do the trick. It specifies first the colors you are going to use for every level from the root (red in this case) down, and then some specifics for each tag you might find: level of indentation, tree level (0 and 1 in the case of equipo) and suffix after start tag. To apply it, ./format-any.pl format.yaml file.xml