Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Created February 26, 2014 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/6a75572af1ce8f66d8c0 to your computer and use it in GitHub Desktop.
Save FROGGS/6a75572af1ce8f66d8c0 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::PrettyPrint;
use HTML::Entities;
use Syntax::Highlight::Engine::Kate::XML;
my $hl = Syntax::Highlight::Engine::Kate::XML->new(
format_table => {
Alert => ["<font color=\"#0000ff\">", "</font>"],
BaseN => ["<font color=\"#007f00\">", "</font>"],
BString => ["<font color=\"#c9a7ff\">", "</font>"],
Char => ["<font color=\"#ff00ff\">", "</font>"],
Comment => ["<font color=\"#7f7f7f\"><i>", "</i></font>"],
DataType => ["<font color=\"#0000ff\">", "</font>"],
DecVal => ["<font color=\"#00007f\">", "</font>"],
Error => ["<font color=\"#ff0000\"><b><i>", "</i></b></font>"],
Float => ["<font color=\"#00007f\">", "</font>"],
Function => ["<font color=\"#007f00\">", "</font>"],
IString => ["<font color=\"#ff0000\">", ""],
Keyword => ["<font color=\"#00007f\">", "</font>"],
Normal => ["<b>", "</b>"],
Operator => ["<font color=\"#ffa500\">", "</font>"],
Others => ["<font color=\"#b03060\">", "</font>"],
RegionMarker => ["<font color=\"#96b9ff\"><i>", "</i></font>"],
Reserved => ["<font color=\"#9b30ff\"><b>", "</b></font>"],
String => ["<font color=\"#ff0000\">", "</font>"],
Variable => ["<font color=\"#0000ff\"><b>", "</b></font>"],
Warning => ["<font color=\"#0000ff\"><b><i>", "</b></i></font>"],
},
);
my $str = join('', <STDIN>);
my $doc = XML::LibXML->load_xml( string => $str );
my $callback = sub {
my $node = shift;
my @nodes = $node->childNodes();
scalar(@nodes) == 1 && ref($node->firstChild) eq 'XML::LibXML::Text'
};
my $pp = XML::LibXML::PrettyPrint->new(
indent_string => " ",
element => {
compact => [$callback],
}
);
$pp->pretty_print($doc);
$str = $doc->toString;
$str =~ s/"\sxmlns:/"\"\n".(" " x 10)."xmlns:"/e while $str =~ /" xmlns:/;
my @lines = split "\n", $str;
my $pat = "";
for (@lines) {
$pat = /^(.+?\s)xmlns/ && " " x length $1 if /\S\sxmlns/;
/^\s+xmlns/ && s/^\s+/$pat/e;
}
$str = join "\n", @lines;
$str = $hl->highlightText($str);
$str =~ s{<(?!/?(font|b)\b)}{&lt;}g;
print $str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment