Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Awesome Asciidoctor: Don't Wrap Lines in All Listing or Literal Blocks of Document

DZone 's Guide to

Awesome Asciidoctor: Don't Wrap Lines in All Listing or Literal Blocks of Document

Learn more about writing Asciidoctor code without wrapping lines in all listing or literal blocks.

· Java Zone ·
Free Resource

In a previous post, we learned about setting the value of options attribute to nowrap on listing and literal blocks, so the lines in the block are not wrapped. In the comments, a user suggested another option to disable line wrapping for all listing and literal blocks in the document by using the document attribute prewrap. We must negate the document attribute, :prewrap!:, to disable all wrapping. If we place this document attribute at the top of our Asciidoctor document, it is applied for the whole document. We can also place it at other places in the document to change the setting for all listing and literal blocks following the prewrap document attribute. To enable wrapping again, we set :prewrap: (leaving out the exclamation mark).

In the following example, we have markup with a listing, literal, and example block, and we use the document attribute :prewrap!: to disable the wrapping for the listing and literal blocks:

= Wrapping lines sample

// Disable wrapping in listing and literal blocks.
:prewrap!:

== Listing

[.groovy]
----
class GroovyHelloWorld {
    String sayHello(final String message = 'world') { // Set default argument value for message argument
        "Hello $message !"
     }
}
----

== Literal

....
class GroovyHelloWorld {
    String sayHello(final String message = 'world') { // Set default argument value for message argument
        "Hello $message !"
     }
}
....

== Example

====
// We use spacing to mimic a code block.
class GroovyHelloWorld { +
{nbsp}{nbsp}{nbsp}{nbsp}String sayHello(final String message = 'world') { // Set default argument value for message argument +
{nbsp}{nbsp}{nbsp}{nbsp}{nbsp}{nbsp}{nbsp}{nbsp}"Hello $message !" +
{nbsp}{nbsp}{nbsp}{nbsp}} +
}
====


When we create HTML from this markup, we get the following output:

The code in the listing and literal blocks is now not wrapped but continues on the same line.

Written with Asciidoctor 2.0.2.

Topics:
java ,asciidoctor ,prewrap ,tutorial ,groovy ,blocks

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}