Index: trac/wiki/tests/wiki-tests.txt
===================================================================
--- trac/wiki/tests/wiki-tests.txt	(revision 2403)
+++ trac/wiki/tests/wiki-tests.txt	(working copy)
@@ -394,22 +394,55 @@
 <pre class="wiki">Preformatted text.
 </pre>
 ------------------------------
+{{{...}}}
+==============================
 {{{
-Preformatted text.
+Outer block.
+{{{
+Inner block.
 }}}
+}}}
+------------------------------
+<pre class="wiki">Outer block.
+{{{
+Inner block.
+}}}
+</pre>
+------------------------------
+{{{...}}}
 ==============================
+Block 
 {{{
-#!default
-Preformatted text.
+number one
 }}}
+and block
+{{{
+number two
+}}}
 ------------------------------
-<pre class="wiki">Preformatted text.
+<p>
+Block 
+</p>
+<pre class="wiki">number one
 </pre>
+<p>
+and block
+</p>
+<pre class="wiki">number two
+</pre>
 ------------------------------
+Block {{{...}}}
+and block{{{...}}}
+==============================
 {{{
 #!default
 Preformatted text.
 }}}
+------------------------------
+<pre class="wiki">Preformatted text.
+</pre>
+------------------------------
+{{{#!default ...}}}
 ==============================
 {{{
 #!/bin/sh
@@ -420,10 +453,7 @@
 echo &#34;foo&#34;
 </pre>
 ------------------------------
-{{{
-#!/bin/sh
-echo "foo"
-}}}
+{{{#!/bin/sh ...}}}
 ==============================
 {{{
 #!html
@@ -432,10 +462,7 @@
 ------------------------------
 <p>Hello World</p>
 ------------------------------
-{{{
-#!html
-&lt;p&gt;Hello World&lt;/p&gt;
-}}}
+{{{#!html ...}}}
 ==============================
 {{{
 #!html
@@ -448,10 +475,7 @@
 </pre>
 </div>
 ------------------------------
-{{{
-#!html
-&lt;script&gt;alert("");&lt;/script&gt;
-}}}
+{{{#!html ...}}}
 ==============================
 {{{
 #!html
@@ -464,10 +488,7 @@
 </pre>
 </div>
 ------------------------------
-{{{
-#!html
-&lt;div onclick="alert(<i>)"&gt;Click me&lt;/div&gt;
-}}}</i>
+{{{#!html ...}}}
 ==============================
 ^superscript^, ,,subscript,,, normal.
 ------------------------------
@@ -536,10 +557,6 @@
 </p>
 ------------------------------
 Test comment blocks
-{{{
-#!comment
-This is simply removed from the output
-}}}
 ==============================
 Inline [[comment(This should not be seen)]] comment
 ------------------------------
Index: trac/wiki/formatter.py
===================================================================
--- trac/wiki/formatter.py	(revision 2403)
+++ trac/wiki/formatter.py	(working copy)
@@ -637,10 +637,16 @@
     """
     flavor = 'oneliner'
 
+    _non_nested_block_re = re.compile(r"(?:^|\n)\{\{\{(?:\n(#![\w+-/]+))?"
+                                      r"(?:\n([^{}]|\{(?!\{\{)|\}(?!\}\}))+)+"
+                                      r"\}\}\}")
+    _final_block_re = re.compile(r"(?:^|\n)\{\|\{(.*?)\}\|\}")
+    
     def __init__(self, env, absurls=0, db=None):
         Formatter.__init__(self, env, None, absurls, db)
 
     # Override a few formatters to disable some wiki syntax in "oneliner"-mode
+    def _inlinecode_formatter(self, match, fullmatch): return match
     def _list_formatter(self, match, fullmatch): return match
     def _indent_formatter(self, match, fullmatch): return match
     def _heading_formatter(self, match, fullmatch): return match
@@ -664,9 +670,31 @@
         self.out = out
         self._open_tags = []
 
-        result = re.sub(self.rules, self.replace, util.escape(text.strip(), False))
+        result = text.strip()
+
+        # Simplify code blocks
+        def simplify(fullmatch):
+            processor = fullmatch.group(1)
+            if processor == '#!comment':
+                return ''
+            elif processor:
+                return '\n{|{%s ...}|}' % processor
+            elif '\n' in fullmatch.group():
+                return '\n{|{...}|}'
+            else:
+                return '<tt>%s</tt>' % match[3:-3]
+
+        old = ''
+        while old != result:
+            old = result
+            result = re.sub(self._non_nested_block_re, simplify, old)
+        result = re.sub(self._final_block_re, lambda m: '{{{%s}}}' % m.group(1),
+                        old)
+        result = re.sub(self.rules, self.replace, util.escape(result, False))
+
         # Close all open 'one line'-tags
         result += self.close_tag(None)
+
         out.write(result)
 
 

