Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Pretty-Print Multiline Strings #64

Closed
yfeldblum opened this Issue · 2 comments

3 participants

@yfeldblum

It would be convenient for users who want to view and edit YAML documents if Psych would print multi-line strings a little prettier, e.g. using the | pipe.

The following example shows how Psych dumps a multi-line string. In the example, x is simply a 16-line string where each line is 64 characters long. The content of x is generated randomly in this example, but its purpose is that of lorem ipsum.

> YAML
 => Psych 
> Psych::VERSION
 => "1.3.2" 
> x = 16.times.map{SecureRandom.hex(32).force_encoding(Encoding::UTF_8) << "\n"}.join and nil
 => nil 
> puts x
75eada72efdd980926de4a551006ef35eb8c55f14abb735fa96b1a417518d797
c1e52f617c551a415e08f0cb3658a7739bb1f4233f62535a56e582164df51bd1
16c0ec96a591eb7ae5bb0b1671b054c3f41f2a32ec80171a7b564ccbfb9f9d2a
d39e74915e88c258cb6656172e5336cb95b01c997744add19347fc9c69e5ed20
b3679ec3ed2d3995fcfa134b17b8686cbbb0f139fa0634e28a712fd3aae1eecc
85947299d4795e033b4bd1e1d49a9c5043b10ea8f70de33ec17f9fdc58ed6c55
568e8558bb27e123c825a3976697e5f4dc90f1ecebe75b2fbb5aefb732c93e61
56c0838776e10e388874c9a6ca76ac2f43110d9a9d4a15c0b4f606870df39fc5
ffeb4e116a71abe88607a4dc946d90e0812967f5a769326bac2b3398b02a0aa8
5c95f26512cb8209152ab6f8a28dfcebe82e954a85427a1023a0b87cac7aa9c5
0cd9b884b63aa9c0562c931203af19978a4c82c4ae4f20c6c6ad2ae0b8aaf8f4
8c4ae650bc5b46389e9753cd17be099478c3d34786eac058e574f57a93387a39
a0648e5549f775be2396c272340b2f883bc0a7e44d93509225d8279604724870
d622061beb1d2c587ab6fb2130d58af8ea8235c05c92de207f7665a79f93bfbc
33b363cc08109e74342adfe4f22d5b8cf6f5c8938784679d9799129055e3e8bd
3aa32002e4c86f50aea8f84b63e504d851e26fd7d99df685fe8fc791588f111e
 => nil 
> puts YAML.dump(x)
--- ! '75eada72efdd980926de4a551006ef35eb8c55f14abb735fa96b1a417518d797

  c1e52f617c551a415e08f0cb3658a7739bb1f4233f62535a56e582164df51bd1

  16c0ec96a591eb7ae5bb0b1671b054c3f41f2a32ec80171a7b564ccbfb9f9d2a

  d39e74915e88c258cb6656172e5336cb95b01c997744add19347fc9c69e5ed20

  b3679ec3ed2d3995fcfa134b17b8686cbbb0f139fa0634e28a712fd3aae1eecc

  85947299d4795e033b4bd1e1d49a9c5043b10ea8f70de33ec17f9fdc58ed6c55

  568e8558bb27e123c825a3976697e5f4dc90f1ecebe75b2fbb5aefb732c93e61

  56c0838776e10e388874c9a6ca76ac2f43110d9a9d4a15c0b4f606870df39fc5

  ffeb4e116a71abe88607a4dc946d90e0812967f5a769326bac2b3398b02a0aa8

  5c95f26512cb8209152ab6f8a28dfcebe82e954a85427a1023a0b87cac7aa9c5

  0cd9b884b63aa9c0562c931203af19978a4c82c4ae4f20c6c6ad2ae0b8aaf8f4

  8c4ae650bc5b46389e9753cd17be099478c3d34786eac058e574f57a93387a39

  a0648e5549f775be2396c272340b2f883bc0a7e44d93509225d8279604724870

  d622061beb1d2c587ab6fb2130d58af8ea8235c05c92de207f7665a79f93bfbc

  33b363cc08109e74342adfe4f22d5b8cf6f5c8938784679d9799129055e3e8bd

  3aa32002e4c86f50aea8f84b63e504d851e26fd7d99df685fe8fc791588f111e

'
 => nil 

Prettier printing would look like:

--- |
  75eada72efdd980926de4a551006ef35eb8c55f14abb735fa96b1a417518d797
  c1e52f617c551a415e08f0cb3658a7739bb1f4233f62535a56e582164df51bd1
  16c0ec96a591eb7ae5bb0b1671b054c3f41f2a32ec80171a7b564ccbfb9f9d2a
  d39e74915e88c258cb6656172e5336cb95b01c997744add19347fc9c69e5ed20
  b3679ec3ed2d3995fcfa134b17b8686cbbb0f139fa0634e28a712fd3aae1eecc
  85947299d4795e033b4bd1e1d49a9c5043b10ea8f70de33ec17f9fdc58ed6c55
  568e8558bb27e123c825a3976697e5f4dc90f1ecebe75b2fbb5aefb732c93e61
  56c0838776e10e388874c9a6ca76ac2f43110d9a9d4a15c0b4f606870df39fc5
  ffeb4e116a71abe88607a4dc946d90e0812967f5a769326bac2b3398b02a0aa8
  5c95f26512cb8209152ab6f8a28dfcebe82e954a85427a1023a0b87cac7aa9c5
  0cd9b884b63aa9c0562c931203af19978a4c82c4ae4f20c6c6ad2ae0b8aaf8f4
  8c4ae650bc5b46389e9753cd17be099478c3d34786eac058e574f57a93387a39
  a0648e5549f775be2396c272340b2f883bc0a7e44d93509225d8279604724870
  d622061beb1d2c587ab6fb2130d58af8ea8235c05c92de207f7665a79f93bfbc
  33b363cc08109e74342adfe4f22d5b8cf6f5c8938784679d9799129055e3e8bd
  3aa32002e4c86f50aea8f84b63e504d851e26fd7d99df685fe8fc791588f111e
@atambo

+1, in 1.8.7 multiline strings used the pipe syntax while in 1.9.3 it uses the exclamation mark syntax, which is ugly. @tenderlove, was this change intentional? If not, would you accept a patch for changing it back to using the pipe for multiline strings?

@tenderlove
Owner

I don't think it was intentional. I'll take a patch, but it depends on the change.

@atambo atambo referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@tenderlove tenderlove referenced this issue from a commit
@tenderlove Merge branch 'master' into embed
* master:
  * ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when   parsing Floats and Integers. Thanks riffraff [ruby-core:44426] * test/psych/test_numeric.rb: associated test
  * ext/psych/lib/psych/core_ext.rb: move Kernel#y so that it can   manually be required as 'psych/y'.
  Use literal style when emitting multiline strings, fixes #64
249f908
@tenderlove tenderlove referenced this issue from a commit
@tenderlove Merge branch 'master' into nirvdrum-speed_up_node_mapping
* master: (28 commits)
  merge changes from ruby trunk
  Strings that start with non-word characters should double quote without exclamation mark
  fixing rsync
  make some header file adjustments for ruby compile
  updating the version to 2.0
  add the license for libyaml
  Update README.rdoc
  fix c file name
  only do have_header checks if we are embedding libyaml
  mkmf has a global for this test
  fix build on windows
  only embed libyaml if the system does not have it
  * ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when   parsing Floats and Integers. Thanks riffraff [ruby-core:44426] * test/psych/test_numeric.rb: associated test
  * ext/psych/lib/psych/core_ext.rb: move Kernel#y so that it can   manually be required as 'psych/y'.
  for compiling on mswin
  embedding libyaml
  * ruby.c (load_file_internal): set default source encoding as   UTF-8 instead of US-ASCII. [ruby-core:46021] [Feature #6679]
  * ext/psych/lib/psych/handlers/recorder.rb: added a class for   recording YAML parse and emit events.
  Use literal style when emitting multiline strings, fixes #64
  * ext/psych/lib/psych/visitors/yaml_tree.rb: `tree` should return the   same thing on every call.
  ...
f22f976
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.