Shaun McCance
shaunm@gnome.org 2008-2009
Mark up a block of code or the contents of a file.

# Code Blocks

    mal_block_code = element code {
      attribute style { xsd:NMTOKENS } ?,
      attribute mime { text } ?,
      attribute * - (mal:* | local:*) { text } *,

      mal_inline
    }

Use the `code` element to mark up a block of text from a computer language. This will typically be used for programming languages, markup languages, and configuration files; however, you may use `code` for the contents of any text file.

Use the `var` element inside a `code` element to indicate text that should be replaced by the user.

# Notas

The `code` element can contain a mixture of text and any general inline elements. Whitespace is interpreted literally.

The `code` element can occur in any general block context, including inside pages, sections, and certain block elements.

The `style` attribute takes a space-separated list of style hints. Processing tools should adjust their behavior according to those style hints they understand.

The `mime` attribute takes a valid MIME type. Processing tools may adjust their behavior for particular MIME types.

The `code` element can have attributes from external namespaces. See for more information on external-namespace attributes.

The `code` element may also be used in an inline context. See for more information.

Use the `code` element inside a `listing` element to provide a title and description for the code block. This is frequently used to provide the name of the file whose contents are being shown.

# Ejemplos

Use `code` to mark up a class definition:

<code mime="text/x-c++src"> class BeanStalk { public: void add_bean(Bean bean); int count_beans(); }</code>
class BeanStalk { public: void add_bean(Bean bean); int count_beans(); }

# Processing Expectations

Code blocks should be displayed verbatim, with all whitespace and line breaks reproduced in the rendered output. The only exception is a single leading line break, which should be stripped by display tools if present. Display tools should only strip a leading line break in an initial text node. They are not expected to strip line breaks from child elements.

Code blocks should be displayed in a fixed-width font. Inline markup may cause style variations, but they should not cause a change to a variable-width font.

# Comparison to Other Formats

The `code` element is similar to the `programlisting` element in DocBook. DocBook also contains numerous elements for modeling code in procedural and object-oriented programming languages. Many of these elements can be seen by browsing the content models for the `classsynopsis` and `funcsynopsis` elements. Mallard does not attempt to model any programming languages.

The `code` element is similar to the `codeblock` element in DITA.