From 423e813490158f9442c2f3d80583e894f9d54a04 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Tue, 2 May 2023 16:49:00 +0200 Subject: [PATCH] Pretty printing sitemap.xml --- Build.PL | 1 + lib/BurguillosInfo/Controller/Sitemap.pm | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Build.PL b/Build.PL index 2f0539c..20df077 100755 --- a/Build.PL +++ b/Build.PL @@ -18,6 +18,7 @@ my $build = Module::Build->new( 'DateTime::Format::ISO8601.pm' => 0, 'DateTime::Format::Mail.pm' => 0, 'SVG' => 0, + 'XML::LibXML::PrettyPrint' => 0, }, ); $build->create_build_script; diff --git a/lib/BurguillosInfo/Controller/Sitemap.pm b/lib/BurguillosInfo/Controller/Sitemap.pm index ad4ccf7..7f8d6fd 100644 --- a/lib/BurguillosInfo/Controller/Sitemap.pm +++ b/lib/BurguillosInfo/Controller/Sitemap.pm @@ -10,6 +10,9 @@ use BurguillosInfo::Posts; use DateTime::Format::ISO8601; +use XML::LibXML; +use XML::LibXML::PrettyPrint; + use Mojo::Base 'Mojolicious::Controller', '-signatures'; sub sitemap ($self) { @@ -23,7 +26,12 @@ sub sitemap ($self) { for my $category_key ( keys %$categories ) { $self->_append_category_dom( $dom, $category_key, $categories ); } - $self->render(text => $dom, format => 'xml'); + my $xml_string = "$dom"; + my $document = XML::LibXML->load_xml(string => $xml_string); + my $pretty_print = XML::LibXML::PrettyPrint->new(indent_string => (" " x 4)); + $pretty_print->pretty_print($document); + $xml_string = $document->toString(); + $self->render(text => $xml_string, format => 'xml'); } sub _append_category_dom ( $self, $dom, $category_key, $categories ) {