| 1 | package Util::Medley::YAML; | ||
| 2 | $Util::Medley::YAML::VERSION = '0.047'; | ||
| 3 | use Modern::Perl; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use Kavorka '-all'; | ||
| 7 | use Data::Printer alias => 'pdump'; | ||
| 8 | use Carp; | ||
| 9 | |||
| 10 | with 'Util::Medley::Roles::Attributes::List'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Util::Medley::XML - utility XML methods | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | version 0.047 | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | my $util = Util::Medley::XML->new; | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | ######################################################## | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | Provides utility methods for working with XML. All methods confess on | ||
| 33 | error. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | ######################################################## | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =head2 xmlBeautifyFile | ||
| 42 | |||
| 43 | Beautifies an XML file. Requires the xmllint command. | ||
| 44 | |||
| 45 | =over | ||
| 46 | |||
| 47 | =item usage: | ||
| 48 | |||
| 49 | $util->xmlBeautifyFile($path); | ||
| 50 | |||
| 51 | $util->xmlBeautifyFile(path => $path); | ||
| 52 | |||
| 53 | =item args: | ||
| 54 | |||
| 55 | =over | ||
| 56 | |||
| 57 | =item path [Str] | ||
| 58 | |||
| 59 | Location of the xml file. | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | =cut | ||
| 66 | use YAML; | ||
| 67 | multi method yamlBeautifyFile (Str :$path!, | ||
| 68 | Int :$sortDepth = 0) { | ||
| 69 | |||
| 70 | $YAML::SortKeys = 0; | ||
| 71 | $YAML::UseHeader = 0; | ||
| 72 | |||
| 73 | my $yaml = YAML::LoadFile($path); | ||
| 74 | say '---'; | ||
| 75 | foreach my $key ($self->List->nsort(keys %$yaml)) { | ||
| 76 | my $node = YAML::Dump({ $key => $yaml->{$key}}); | ||
| 77 | say $node; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | multi method yamlBeautifyFile (Str $path, | ||
| 82 | Int $sortDepth = 0) { | ||
| 83 | |||
| 84 | $self->yamlBeautifyFile(path => $path, | ||
| 85 | sortDepth => $sortDepth); | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 xmlBeautifyString | ||
| 89 | |||
| 90 | Formats an XML string. Requires the xmllint command. | ||
| 91 | |||
| 92 | =over | ||
| 93 | |||
| 94 | =item usage: | ||
| 95 | |||
| 96 | $util->xmlBeautifyString($xml); | ||
| 97 | |||
| 98 | $util->XmlBeautifyString(xml => $xml); | ||
| 99 | |||
| 100 | =item args: | ||
| 101 | |||
| 102 | =over | ||
| 103 | |||
| 104 | =item xml [Str] | ||
| 105 | |||
| 106 | An XML string. | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | multi method xmlBeautifyString (Str :$xml!) { | ||
| 115 | |||
| 116 | my @cmd = ( 'xmllint', '--format', '-' ); | ||
| 117 | my ( $stdout, $stderr, $exit ) = | ||
| 118 | $self->Spawn->capture( cmd => \@cmd, stdin => $xml ); | ||
| 119 | |||
| 120 | return $stdout; | ||
| 121 | } | ||
| 122 | |||
| 123 | multi method xmlBeautifyString (Str $xml) { | ||
| 124 | |||
| 125 | return $self->xmlBeautifyString(xml => $xml); | ||
| 126 | } | ||
| 127 | |||
| 128 | ###################################################################### | ||
| 129 | |||
| 130 | 1; |