92

In MediaWiki (wikipedia's) wiki syntax, is there a way to have a numbered list with a code block in the middle?

For example:

# Number 1
# Number 2
  Indented section that will become a code block
# Number 3
# Number 4

What happens in MediaWiki is you end up with something like this:

1. Number 1
2. Number 2
   Indented section that will become a code block
1. Number 3
2. Number 4

(Note how "Number 3" and "Number 4" are reset as 1 and 2... It looks like StackOverflow is much smarter than MediaWiki, i had to put my example in PRE tags to make it screw up!)

I know you can indent text using "#:" syntax...

# Number 1
# Number 2
#: Indented section that will merely be indented
# Number 3
# Number 4

...but I really would like to get the same visual CSS class for my code even if it's in a numbered list.

It gets even more entertaining with nested lists. This syntax...

# MainEntry 1
## Number 1
## Number 2
# MainEntry 2
## Number 1
## Number 2
  Indented section that will become a code block
## Number 3
## Number 4

...becomes...

1. MainEntry 1
   1. Number 1
   2. Number 2
2. MainEntry 2
   1. Number 1
   2. Number 2
      Indented section that will become a code block
1. 1. Number 3
   2. Number 4

(Note how "Number 3" is now "1. 1.")

inanutshellus
  • 9,683
  • 9
  • 53
  • 71

7 Answers7

80

You could try the following wiki syntax, it works for me on 1.17

# one
#:<pre>
#::some stuff
#::some more stuff</pre>
# two

It is not perfect, because you end up with a more indent but it does allow one to use the wiki syntax for correctly formatted pre blocks over multiple lines.

As previously mentioned, the other proper way would be to use HTML mark up.

<ol>
<li>one</li>
<li>two</li>
<pre>some stuff
some more stuff</pre>
<li>three</li>
</ol>
Kyle
  • 966
  • 7
  • 8
  • 1
    That wiki syntax worked for me on MediaWiki 1.15.1. It puts a little extra space around the code lines, but it works. – Nick Chammas Jun 27 '12 at 18:28
  • 1
    I used this same syntax you suggested for images within numbered lists: `#:[[File:image.jpg]]` between numbered items. This helps a ton. – Michael Plautz Mar 23 '15 at 20:20
  • Worked for me on MediaWiki 1.17; Thanks! – Nay Feb 16 '16 at 21:24
  • I'm not sure which version of MediaWiki we use, but this works. There is just one caveat: If you precede the 'pre' lines with another #: indented line, say for a title or something, the 'pre' doesn't render correctly. – anuragw Mar 21 '16 at 17:49
  • Does not work in mediawiki used by GitHub (See [example here](https://github.com/jonathancross/jc-docs/blob/master/Github%20markdown%20list%20and%20code.mediawiki)). Instead, use HTML version. – Jonathan Cross Sep 25 '16 at 00:58
36

Use html:

<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol>

it will work in mediawiki.

Note from the example that I posted below, it is the </li> that makes it work properly.

Adrian Archer
  • 2,323
  • 1
  • 17
  • 20
  • I edited your example to show how it would work: http://en.wikipedia.org/w/index.php?title=Wikipedia:Sandbox&oldid=414124677 – Adrian Archer Feb 15 '11 at 20:50
  • This is a known problem with wiki syntax. – Adrian Archer Feb 16 '11 at 14:20
  • Kyle's answer should be accepted answer. If doing nested lists, it will be difficult to use html. Additionally, the above answer is burried in ancient wikipedia sandbox page. The actual text in the Stackoverflow answer above, does not solve the OP's problem. – Nay Feb 13 '16 at 23:34
19

This works fine in MediaWiki 1.17.0:

===Alternative way of using pre in numbered lists.===    
# Numbered line 1.
# Numbered line 2.<pre>code line 1&#10;code line 2</pre>
# Numbered line 3.

The secret is to replace the newlines with the entity and write everything in one line.

John
  • 349
  • 4
  • 3
14

Your issue is the subject of two bugs filled in the MediaWiki bug tracker in late 2004 and 2005 :

Bug 1115 - Newline as list item terminator is troublesome

Bug 1584 - Need method for multiparagraph list items, continuing numbered lists, and assigning specific numbers to list items

By reading them, you will find the solution is to not use the MediaWiki syntax but to rely on "pure" HTML.

  • 1
    We also have an entire [Help:Newlines and spaces](https://meta.wikimedia.org/wiki/Help:Newlines_and_spaces) page and a specific discussion of this issue at [List-agnostic markup insertions](https://meta.wikimedia.org/wiki/Help_talk:List#List-agnostic_markup_insertions). – Nemo May 03 '15 at 07:39
5

I'm suggesting a different answer: don't do it.

I've attempted to use all the workarounds for this basic Mediawiki issue and found that they are all very imperfect. I've learned to live without numbers, and instead:

  • Use the splat (*) instead of (#) for all my lists
  • Continue to use the leading space for all my code blocks

This is far far simpler and maintainable than any workaround. Besides, use of any reference to a number is subject to change as the steps are edited - and this then becomes another maintenance issue.

moodboom
  • 6,225
  • 2
  • 41
  • 45
2

In the above example the second indentation (::) is not necessary.

Just one indentation works fine (:) as follows:

# one
#:<pre>
#:some stuff
#:some more stuff</pre>
# two

Produces:

  • 1. one
       some stuff (just one indent level, not two)
       some more stuff
  • 2. two
  • Kory Lovre
    • 146
    • 5
    • 8
      I found @Kyle's answer with ::
       on lines between 
      IS NECESSARY for me. Just 1 : as you say, did nto work for me...
      – cellepo Mar 25 '14 at 20:00
    • 1
      This answer did not work for me in Mediawiki 1.17. I too find @Kyle answer necessary. – Nay Feb 13 '16 at 23:30
    -3

    You can also try adding a "blockquote" tag surrounding the "pre" tag, makes it look a little more polished.

    == HAProxy Configuration ==
    #'''File:''' /etc/haproxy/haproxy.cfg
    <blockquote>
    <pre>
    global
      log 127.0.0.1 local1 notice
      maxconn 4096
      #daemon
      debug
      crt-base /usr/local/haproxy/ssl
    </pre>
    </blockquote>
    

    Which will indent the gray box in line with your bullets/numbers without using colons.

    Mogsdad
    • 44,709
    • 21
    • 151
    • 275
    mars
    • 55
    • 3
    • 1
      This does not work for me, even if I correct the order of the closing tags. It breaks the numbering of the list. – P.Péter Apr 10 '14 at 15:49
    • Same with unordered lists (*). If they are nested, the nesting after the blockquote does not continue correctly. – Nay Feb 13 '16 at 23:09