Skip to content

Support pretty printing of XML types#15833

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/xml-pretty-print
May 29, 2025
Merged

Support pretty printing of XML types#15833
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/xml-pretty-print

Conversation

@HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented May 26, 2025

Overrides #pretty_print for XML::Node, XML::NodeSet, XML::Attributes, and XML::Namespace, consistent with their #inspect overrides.

Example:

require "xml"

xml = XML.parse(<<-XML)
  <?xml version="1.0" encoding="UTF-8"?>
  <crystal:foo x="1" y="2" xmlns:crystal="https://crystal-lang.org/">
    <bar/>lorem ipsum<baz/>
  </foo>
  XML

pp xml
# #<XML::Document:0x1b9193c2d80
#  children=
#   [#<XML::Element:0x1b9193cdf00
#     name="foo"
#     attributes=
#      [#<XML::Attribute:0x1b9193b3e00 name="x" content="1">,
#       #<XML::Attribute:0x1b9193b3d90 name="y" content="2">]
#     children=
#      [#<XML::Text:0x1b9193cdd80 "\n" + "  ">,
#       #<XML::Element:0x1b9193cdd00 name="bar">,
#       #<XML::Text:0x1b9193cdc80 "lorem ipsum">,
#       #<XML::Element:0x1b9193cdc00 name="baz">,
#       #<XML::Text:0x1b9193cdb80 "\n">]>]>

pp xml.root.not_nil!.namespace
# #<XML::Namespace:0x1b9193b1b00
#  prefix="crystal"
#  href="https://crystal-lang.org/">

@straight-shoota straight-shoota added this to the 1.17.0 milestone May 26, 2025
@straight-shoota straight-shoota merged commit 600a7d9 into crystal-lang:master May 29, 2025
44 of 45 checks passed
@HertzDevil HertzDevil deleted the feature/xml-pretty-print branch May 29, 2025 13:46
@Blacksmoke16 Blacksmoke16 linked an issue Jun 23, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XML.pretty_print seems to always produce only a single line

2 participants