Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1378)

Unified Diff: appengine/monorail/features/prettify.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « appengine/monorail/features/notify_helpers.py ('k') | appengine/monorail/features/savedqueries.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/monorail/features/prettify.py
diff --git a/appengine/monorail/features/prettify.py b/appengine/monorail/features/prettify.py
new file mode 100644
index 0000000000000000000000000000000000000000..5b12b630d966ba6cdc4505249ca18aea0d286fd0
--- /dev/null
+++ b/appengine/monorail/features/prettify.py
@@ -0,0 +1,73 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is govered by a BSD-style
+# license that can be found in the LICENSE file or at
+# https://developers.google.com/open-source/licenses/bsd
+
+"""Helper functions for source code syntax highlighting."""
+
+from third_party import ezt
+
+from framework import framework_constants
+
+
+# We only attempt to do client-side syntax highlighting on files that we
+# expect to be source code in languages that we support, and that are
+# reasonably sized.
+MAX_PRETTIFY_LINES = 3000
+
+
+def PrepareSourceLinesForHighlighting(file_contents):
+ """Parse a file into lines for highlighting.
+
+ Args:
+ file_contents: string contents of the source code file.
+
+ Returns:
+ A list of _SourceLine objects, one for each line in the source file.
+ """
+ return [_SourceLine(num + 1, line) for num, line
+ in enumerate(file_contents.splitlines())]
+
+
+class _SourceLine(object):
+ """Convenience class to represent one line of the source code display.
+
+ Attributes:
+ num: The line's location in the source file.
+ line: String source code line to display.
+ """
+
+ def __init__(self, num, line):
+ self.num = num
+ self.line = line
+
+ def __str__(self):
+ return '%d: %s' % (self.num, self.line)
+
+
+def BuildPrettifyData(num_lines, path):
+ """Return page data to help configure google-code-prettify.
+
+ Args:
+ num_lines: int number of lines of source code in the file.
+ path: string path to the file, or just the filename.
+
+ Returns:
+ Dictionary that can be passed to EZT to render a page.
+ """
+ reasonable_size = num_lines < MAX_PRETTIFY_LINES
+
+ filename_lower = path[path.rfind('/') + 1:].lower()
+ ext = filename_lower[filename_lower.rfind('.') + 1:]
+
+ # Note that '' might be a valid entry in these maps.
+ prettify_class = framework_constants.PRETTIFY_CLASS_MAP.get(ext)
+ if prettify_class is None:
+ prettify_class = framework_constants.PRETTIFY_FILENAME_CLASS_MAP.get(
+ filename_lower)
+ supported_lang = prettify_class is not None
+
+ return {
+ 'should_prettify': ezt.boolean(supported_lang and reasonable_size),
+ 'prettify_class': prettify_class,
+ }
« no previous file with comments | « appengine/monorail/features/notify_helpers.py ('k') | appengine/monorail/features/savedqueries.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698