Quantcast

List environment with formatting

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

List environment with formatting

mariocup
Hi,

I would like to create a labeled list environment with pre-formatting e.g. if I describe a command line option of a tool.

Instead of writing

`--option-1`:: description
`--option-2`:: description

I would like to add a list-style-type to format as option.

[role=optentry]
--option-1:: description
--option-2:: description

In addition I would like to define a code highlighting for list environments. This shall be used when functions are described.

int main(void):: description

In technical manuals this can be very useful. The same syntax highlighting like in code snippets make it easy to read. I haven't found a way to use this kind of label list environment in HTML and pdf with Asciidoctor.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: List environment with formatting

mojavelinux
Administrator
You're already part of the way there in making the description list term monospaced. You just need to add a CSS style rule to a custom stylesheet or something you insert with docinfo:

.dlist.optentry dt.hdlist1 {
  font-family: monospace;
}

> I would like to define a code highlighting for list environments.

For this, you'd need to use a Treeprocessor. Something like the following:

require 'asciidoctor/extensions'
require 'pygments'

module RawTerm
  def text
    @text
  end

  def text= text
    @text = text
  end
end

Asciidoctor::Extensions.register do
  treeprocessor do
    process do |doc|
      c_lexer = ::Pygments::Lexer.find_by_name 'C'
      (doc.find_by context: :dlist, role: 'optentry').each do |dlist|
        dlist.items.each do |dlist_entry|
          terms, description = dlist_entry
          term_0 = terms[0]
          unless (term_0.instance_variable_get :@text).start_with? '--'
            term_0.extend RawTerm
            term_0.text = c_lexer.highlight term_0.text, options: { nowrap: true, noclasses: true }
          end
        end
      end
      nil
    end
  end
end

Of course, you could just use the Treeprocessor to wrap the term in a `<code>` element. Lots of options.

Cheers,

-Dan


On Wed, Feb 8, 2017 at 3:22 PM, mariocup [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi,

I would like to create a labeled list environment with pre-formatting e.g. if I describe a command line option of a tool.

Instead of writing

`--option-1`:: description
`--option-2`:: description

I would like to add a list-style-type to format as option.

[role=optentry]
--option-1:: description
--option-2:: description

In addition I would like to define a code highlighting for list environments. This shall be used when functions are described.

int main(void):: description

In technical manuals this can be very useful. The same syntax highlighting like in code snippets make it easy to read. I haven't found a way to use this kind of label list environment in HTML and pdf with Asciidoctor.


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/List-environment-with-formatting-tp5257.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: List environment with formatting

kschwarz
Hi,

I applied your suggested implementation with the rouge syntax highlighter. My code looks as follows:

require 'asciidoctor/extensions'
require 'asciidoctor-pdf'
require 'asciidoctor-pdf/rouge_ext'

include Asciidoctor

module RawTerm
  def text
    @text
  end

  def text= text
    @text = text
  end
end

Asciidoctor::Extensions.register do
  treeprocessor do
    process do |doc|
      (doc.find_by context: :dlist, role: 'fcnentry').each do |dlist|
        dlist.items.each do |dlist_entry|
          terms, description = dlist_entry
          term_0 = terms[0]
          term_0.extend RawTerm
          lexer = Rouge::Lexer.find 'c'
          formatter = if doc.backend == "html5"
            Rouge::Formatters::HTML.new
          else
            Rouge::Formatters::Prawn.new theme: (doc.attr 'rouge-style')
          end
          term_0.text = formatter.format(lexer.lex(term_0.text))
        end
      end
      nil
    end
  end
end

With asciidoctor, this works fine. However with asciidoctor-pdf, this gives me an error. Normally in asciidoctor-pdf the output of the formatter is fed into an internal typesetting function, however I don't see how I can use this in a "clean" way. Do you have an idea?

Best regards
Loading...