Quantcast

List environment with formatting

classic Classic list List threaded Threaded
4 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: List environment with formatting

mojavelinux
Administrator
A TreeProcessor does not run during the rendering phase and thus you don't have an active PDF document. Therefore, when converting to PDF, what you need to do is produce inline HTML. The PDF converter uses a pseudo-HTML parser that can handle inline span elements with style attributes.

Here's the line you need to use instead of Rouge::Formatters::Prawn.new.

Rouge::Formatters::HTMLInline.new(Rouge::Theme.find(doc.attr('rouge-style', 'github')).new)

That will set the text to:

<span style="color: #445588;font-weight: bold">int</span> <span style="background-color: #f8f8f8">main</span><span style="background-color: #f8f8f8">(</span><span style="color: #445588;font-weight: bold">void</span><span style="background-color: #f8f8f8">)</span>

The background color style doesn't work yet. It's waiting on https://github.com/asciidoctor/asciidoctor-pdf/pull/664 to be reviewed and merged.

Cheers,

-Dan

p.s. In order to use the Rouge formatter for Prawn, you'd need to override the convert_dlist method in the Asciidoctor PDF converter and pass that to typeset_formatted_text. Needless to say, it's rather complex. Using inline HTML is far simpler.


On Tue, Mar 21, 2017 at 9:11 AM, kschwarz [via Asciidoctor :: Discussion] <[hidden email]> wrote:
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


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/List-environment-with-formatting-tp5257p5401.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
Loading...