Commits

Anonymous committed 4bd34ee

The `OneLinerFormatter` now collapse blocks into "[...]".

In order to make this effective, the line shortening is done within the formatter.
That change will be also useful in the future for implementing a truncating
that respects the Wiki syntax (see #2064).

Closes #2232.

Comments (0)

Files changed (9)

 
 from trac.core import *
 from trac.perm import IPermissionRequestor
-from trac.util import TracError, escape, format_datetime, shorten_line
+from trac.util import TracError, escape, format_datetime
 from trac.web import IRequestHandler
 from trac.web.chrome import add_link, add_stylesheet, INavigationContributor
 from trac.wiki import IWikiSyntaxProvider
 
 from trac.core import *
 from trac.perm import IPermissionRequestor
-from trac.util import enum, escape, format_date, format_time, http_date, \
-                      shorten_line
+from trac.util import enum, escape, format_date, format_time, http_date
 from trac.web import IRequestHandler
 from trac.web.chrome import add_link, add_stylesheet, INavigationContributor
 

trac/ticket/roadmap.py

                                            absurls=True)
                 else:
                     href = self.env.href.milestone(name)
-                    message = wiki_to_oneliner(shorten_line(description),
-                                               self.env, db)
+                    message = wiki_to_oneliner(description, self.env, db,
+                                               shorten=True)
                 yield 'milestone', href, title, completed, None, message
 
     # IRequestHandler methods

trac/ticket/web_ui.py

                         message = util.escape(message)
                 else:
                     href = self.env.href.ticket(id)
-                    message = util.shorten_line(message)
                     if status != 'new':
                         message = ': '.join(filter(None, [
                             resolution,
-                            wiki_to_oneliner(message, self.env, db)
+                            wiki_to_oneliner(message, self.env, db,
+                                             shorten=True)
                         ]))
                     else:
-                        message = util.escape(message)
+                        message = util.escape(util.shorten_line(message))
                 yield kinds[status], href, title, t, author, message
 
     # Internal methods
                 message = ''
                 if len(field_changes) > 0:
                     message = ', '.join(field_changes) + ' changed.<br />'
-                message += wiki_to_oneliner(util.shorten_line(comment),
-                                            self.env, db, absurls=absurls)
+                message += wiki_to_oneliner(comment, self.env, db,
+                                            shorten=True, absurls=absurls)
                 yield 'editedticket', href, title, t, author, message

trac/versioncontrol/web_ui/changeset.py

                 if chgset.date < start:
                     return
                 if chgset.date < stop:
-                    excerpt = util.shorten_line(chgset.message or '--')
+                    message = chgset.message or '--'
                     if format == 'rss':
-                        title = 'Changeset <em>[%s]</em>: %s' % (
-                            util.escape(chgset.rev), util.escape(excerpt))
+                        title = 'Changeset <em>[%s]</em>: %s' \
+                                % (util.escape(chgset.rev),
+                                   util.escape(util.shorten_line(message)))
                         href = self.env.abs_href.changeset(chgset.rev)
-                        message = wiki_to_html(chgset.message or '--', self.env,
-                                               db, absurls=True)
+                        message = wiki_to_html(message, self.env, db,
+                                               absurls=True)
                     else:
-                        title = 'Changeset <em>[%s]</em> by %s' % (
-                            util.escape(chgset.rev), util.escape(chgset.author))
+                        title = 'Changeset <em>[%s]</em> by %s' \
+                                % (util.escape(chgset.rev),
+                                   util.escape(chgset.author))
                         href = self.env.href.changeset(chgset.rev)
-                        message = wiki_to_oneliner(excerpt, self.env, db)
+                        message = wiki_to_oneliner(message, self.env, db,
+                                                   shorten=True)
                     if show_files:
                         files = []
                         for chg in chgset.get_changes():

trac/versioncontrol/web_ui/util.py

 import re
 import urllib
 
-from trac.util import escape, format_datetime, pretty_timedelta, shorten_line,\
+from trac.util import escape, format_datetime, pretty_timedelta, shorten_line, \
                       TracError
 from trac.wiki import wiki_to_html, wiki_to_oneliner
 
     changes = {}
     for rev in revs:
         changeset = repos.get_changeset(rev)
-        message = changeset.message
-        shortlog = shorten_line(message)        
+        message = changeset.message or '--'
         files = None
         if format == 'changelog':
             files = [change[0] for change in changeset.get_changes()]
         elif message:
             if not full:
-                message = wiki_to_oneliner(shortlog, env, db)
+                message = wiki_to_oneliner(message, env, db,
+                                           shorten=True)
             else:
                 message = wiki_to_html(message, env, req, db,
                                        absurls=(format == 'rss'),
             'date': format_datetime(changeset.date),
             'age': pretty_timedelta(changeset.date),
             'author': changeset.author or 'anonymous',
-            'shortlog': shortlog,
             'message': message,
+            'shortlog': shorten_line(message),
             'files': files
         }
     return changes

trac/wiki/formatter.py

     """
     flavor = 'oneliner'
 
+    _non_nested_block_re = re.compile(r"(?:^|\n)\{\{\{(?:\n(#![\w+-/]+))?"
+                                      r"(?:\n([^{}]|\{(?!\{\{)|\}(?!\}\}))+)+"
+                                      r"\}\}\}")
+    
     def __init__(self, env, absurls=0, db=None):
         Formatter.__init__(self, env, None, absurls, db)
 
             args = fullmatch.group('macroargs')
             return '[[%s%s]]' % (name,  args and '(...)' or '')
 
-    def format(self, text, out):
+    def format(self, text, out, shorten=False):
         if not text:
             return
         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 '\n' in fullmatch.group():
+                return ' ![...]'
+            else:
+                return '`%s`' % match[3:-3]
+
+        old = ''
+        while old != result:
+            old = result
+            result = re.sub(self._non_nested_block_re, simplify, old)
+
+        if shorten:
+            result = util.shorten_line(result)
+
+        result = re.sub(self.rules, self.replace, util.escape(result, False))
+        result = result.replace('[...]', '[&hellip;]')
+        if result.endswith('...'):
+            result = result[:-3] + '&hellip;'
+
         # Close all open 'one line'-tags
         result += self.close_tag(None)
         out.write(result)
     Formatter(env, req, absurls, db).format(wikitext, out, escape_newlines)
     return out.getvalue()
 
-def wiki_to_oneliner(wikitext, env, db=None, absurls=0):
+def wiki_to_oneliner(wikitext, env, db=None, shorten=False, absurls=0):
     out = StringIO()
-    OneLinerFormatter(env, absurls, db).format(wikitext, out)
+    OneLinerFormatter(env, absurls, db).format(wikitext, out, shorten)
     return out.getvalue()
 
 def wiki_to_outline(wikitext, env, db=None, absurls=0, max_depth=None,

trac/wiki/tests/wiki-tests.txt

 <pre class="wiki">Preformatted text.
 </pre>
 ------------------------------
+ [&hellip;]
+==============================
 {{{
-Preformatted text.
+Outer block.
+{{{
+Inner block.
 }}}
+}}}
+------------------------------
+<pre class="wiki">Outer block.
+{{{
+Inner block.
+}}}
+</pre>
+------------------------------
+ [&hellip;]
+==============================
+Block 
+{{{
+number one
+}}}
+and block
+{{{
+number two
+}}}
+------------------------------
+<p>
+Block 
+</p>
+<pre class="wiki">number one
+</pre>
+<p>
+and block
+</p>
+<pre class="wiki">number two
+</pre>
+------------------------------
+Block  [&hellip;]
+and block [&hellip;]
 ==============================
 {{{
 #!default
 <pre class="wiki">Preformatted text.
 </pre>
 ------------------------------
-{{{
-#!default
-Preformatted text.
-}}}
+ [&hellip;]
 ==============================
 {{{
 #!/bin/sh
 echo &#34;foo&#34;
 </pre>
 ------------------------------
-{{{
-#!/bin/sh
-echo "foo"
-}}}
+ [&hellip;]
 ==============================
 {{{
 #!html
 ------------------------------
 <p>Hello World</p>
 ------------------------------
-{{{
-#!html
-&lt;p&gt;Hello World&lt;/p&gt;
-}}}
+ [&hellip;]
 ==============================
 {{{
 #!html
 </pre>
 </div>
 ------------------------------
-{{{
-#!html
-&lt;script&gt;alert("");&lt;/script&gt;
-}}}
+ [&hellip;]
 ==============================
 {{{
 #!html
 </pre>
 </div>
 ------------------------------
-{{{
-#!html
-&lt;div onclick="alert(<i>)"&gt;Click me&lt;/div&gt;
-}}}</i>
+ [&hellip;]
 ==============================
 ^superscript^, ,,subscript,,, normal.
 ------------------------------
 </p>
 ------------------------------
 Test comment blocks
-{{{
-#!comment
-This is simply removed from the output
-}}}
 ==============================
 Inline [[comment(This should not be seen)]] comment
 ------------------------------
 </i></strong></p>
 ------------------------------
 ==============================
+{{{verbatim}}}
+{{{
+{{{in `block`
+}}}
+`{{{this is verbatim}}}` and {{{`that` should also `be` verbatim}}}
+------------------------------
+<p>
+<tt>verbatim</tt>
+</p>
+<pre class="wiki">{{{in `block`
+</pre>
+<p>
+<tt>{{{this is verbatim}}}</tt> and <tt>`that` should also `be` verbatim</tt>
+</p>
+------------------------------
+<tt>verbatim</tt>
+{{{
+{{{in <tt>block</tt>
+}}}
+<tt>{{{this is verbatim}}}</tt> and <tt>`that` should also `be` verbatim</tt>
+==============================
  term:: definition
 ------------------------------
 <dl><dt>term</dt><dd>definition

trac/wiki/web_ui.py

                                            absurls=True)
                 else:
                     href = self.env.href.wiki(name)
-                    comment = wiki_to_oneliner(shorten_line(comment), self.env,
-                                               db)
+                    comment = wiki_to_oneliner(comment, self.env, db,
+                                               shorten=True)
                 yield 'wiki', href, title, t, author, comment
 
     # Internal methods