You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2019/09/03 22:16:26 UTC

[allura] 01/01: [#8332] skip syntax highlighting for really large files; add timermiddleware timer for pygments.highlight

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8332
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 989e9f66f5159c3cafa841a02a28e07948d7f995
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Tue Sep 3 18:09:21 2019 -0400

    [#8332] skip syntax highlighting for really large files; add timermiddleware timer for pygments.highlight
---
 Allura/allura/lib/app_globals.py       | 16 ++++++++--------
 Allura/allura/lib/custom_middleware.py |  2 ++
 Allura/development.ini                 |  3 +++
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index a30607e..b948a65 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -402,23 +402,23 @@ class Globals(object):
             return h.html.literal('<em>Empty file</em>')
         # Don't use line numbers for diff highlight's, as per [#1484]
         if lexer == 'diff':
-            formatter = pygments.formatters.HtmlFormatter(
-                cssclass='codehilite', linenos=False)
+            formatter = pygments.formatters.HtmlFormatter(cssclass='codehilite', linenos=False)
         else:
             formatter = self.pygments_formatter
         text = h.really_unicode(text)
         if lexer is None:
-            try:
-                lexer = pygments.lexers.get_lexer_for_filename(
-                    filename, encoding='chardet')
-            except pygments.util.ClassNotFound:
+            if len(text) < asint(config.get('scm.view.max_syntax_highlight_bytes', 500000)):
+                try:
+                    lexer = pygments.lexers.get_lexer_for_filename(filename, encoding='chardet')
+                except pygments.util.ClassNotFound:
+                    pass
+            if lexer is None:
                 # no highlighting, but we should escape, encode, and wrap it in
                 # a <pre>
                 text = cgi.escape(text)
                 return h.html.literal(u'<pre>' + text + u'</pre>')
         else:
-            lexer = pygments.lexers.get_lexer_by_name(
-                lexer, encoding='chardet')
+            lexer = pygments.lexers.get_lexer_by_name(lexer, encoding='chardet')
         return h.html.literal(pygments.highlight(text, lexer, formatter))
 
     def forge_markdown(self, **kwargs):
diff --git a/Allura/allura/lib/custom_middleware.py b/Allura/allura/lib/custom_middleware.py
index 7b52480..27aa2bd 100644
--- a/Allura/allura/lib/custom_middleware.py
+++ b/Allura/allura/lib/custom_middleware.py
@@ -295,6 +295,7 @@ class AlluraTimerMiddleware(TimerMiddleware):
         import socket
         import urllib2
         import activitystream
+        import pygments
 
         timers = self.entry_point_timers() + [
             Timer(
@@ -340,6 +341,7 @@ class AlluraTimerMiddleware(TimerMiddleware):
             Timer('urlopen', urllib2, 'urlopen'),
             Timer('base_repo_tool.{method_name}',
                   allura.model.repository.RepositoryImplementation, 'last_commit_ids'),
+            Timer('pygments', pygments, 'highlight'),  # often called from within a template so will overlap w/ jinja
         ] + [Timer('sidebar', ep.load(), 'sidebar_menu') for ep in tool_entry_points]
 
         try:
diff --git a/Allura/development.ini b/Allura/development.ini
index 4478d73..03d3897 100644
--- a/Allura/development.ini
+++ b/Allura/development.ini
@@ -425,6 +425,9 @@ scm.view.log.limit = 25
 ;   Details at https://forge-allura.apache.org/p/allura/tickets/5496/#1b4a
 scm.view.commit_browser.limit = 500
 
+; Default limit for when to stop doing syntax highlighting (can take a lot of CPU for large files)
+scm.view.max_syntax_highlight_bytes = 500000
+
 ; bulk_export_enabled = true
 ; If you keep bulk_export_enabled, you should set up your server to securely share bulk_export_path with users somehow
 bulk_export_path = /tmp/bulk_export/{nbhd}/{project}