allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kentontay...@apache.org
Subject [allura] branch master updated: [#8332] skip syntax highlighting for really large files; add timermiddleware timer for pygments.highlight
Date Thu, 05 Sep 2019 13:29:39 GMT
This is an automated email from the ASF dual-hosted git repository.

kentontaylor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/master by this push:
     new 8f7321f  [#8332] skip syntax highlighting for really large files; add timermiddleware
timer for pygments.highlight
8f7321f is described below

commit 8f7321f45ba4d8fa580a59415049182e2314568d
Author: Dave Brondsema <dave@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 c6e96df..f558b74 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}


Mime
View raw message