From 9fe1c6daefd41598759173d67397376978998251 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Jan 31 2019 08:48:48 +0000 Subject: Force highlight.js to use certain highlighting schemes in file view Basically we split the extension of the file and use a configured mapping to tell highlight.js how to render the file. If the extension isn't in the mapping, highlight.js will keep its current logic. fixes #4169 --- diff --git a/doc/configuration.rst b/doc/configuration.rst index b859991..494b468 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -1616,6 +1616,18 @@ projects (from third party git server). Defaults to: ``False`` +SYNTAX_ALIAS_OVERRIDES +~~~~~~~~~~~~~~~~~~~~~~ + +This configuration key allows to force highlight.js to use a certain logic +on certain files based on their extensions. + +It should be a dictionary containing the file extensions as keys and +the highlighting language/category to use as values. + +Defaults to: ``{".spec": "specfile", ".patch": "diff"}`` + + RepoSpanner Options ------------------- diff --git a/pagure/templates/file.html b/pagure/templates/file.html index 1f94632..6a16801 100644 --- a/pagure/templates/file.html +++ b/pagure/templates/file.html @@ -173,7 +173,7 @@ {% endif %} {% if output_type=='file' %} -
{{ content }}
+
{{ content }}
{% elif output_type == 'markup' %}
{% autoescape false %} diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index 51c6c22..7c3cea7 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -17,6 +17,7 @@ from __future__ import unicode_literals, absolute_import import textwrap import logging +from os.path import splitext import arrow import flask @@ -88,6 +89,27 @@ def linkify_text(text): return "" +@UI_NS.app_template_filter("syntax_alias") +def get_syntax_alias(filename): + """ return an alias based on the filename that is used to + override the automatic syntax highlighting dectection + by highlight.js + """ + + override_rules = pagure_config.get( + "SYNTAX_ALIAS_OVERRIDES", {".spec": "specfile", ".patch": "diff"} + ) + fn, fn_ext = splitext(filename) + + output = "" + if fn_ext == "": + output = "lang-plaintext" + elif fn_ext in override_rules: + output = "lang-" + override_rules[fn_ext] + + return output + + @UI_NS.app_template_filter("format_loc") def format_loc( loc, diff --git a/tests/test_pagure_flask_ui_no_master_branch.py b/tests/test_pagure_flask_ui_no_master_branch.py index 297cc42..1eeff35 100644 --- a/tests/test_pagure_flask_ui_no_master_branch.py +++ b/tests/test_pagure_flask_ui_no_master_branch.py @@ -223,7 +223,8 @@ class PagureFlaskNoMasterBranchtests(tests.SimplePagureTest): ''', output_text) self.assertIn( - '
foo\n bar
', + '
'
+            'foo\n bar
', output_text ) diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index ab86f71..9bdb959 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -2239,7 +2239,8 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( - '
foo\n bar
', + '
'
+            'foo\n bar
', output_text) # Empty files should also be displayed @@ -2336,7 +2337,8 @@ class PagureFlaskRepotests(tests.Modeltests): '  Šource', output_text) self.assertIn( - '
Row 0\n
', + '
'
+            'Row 0\n
', output_text ) @@ -2376,7 +2378,8 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( - '
foo\n barRow 0\n'
+            '
'
+            'foo\n barRow 0\n'
             'Row 1\nRow 2\nRow 3\nRow 4\nRow 5\nRow 6\nRow 7\nRow 8\n'
             'Row 9\n
', output_text) diff --git a/tests/test_pagure_flask_ui_repo_view_file.py b/tests/test_pagure_flask_ui_repo_view_file.py index 7330024..a827369 100644 --- a/tests/test_pagure_flask_ui_repo_view_file.py +++ b/tests/test_pagure_flask_ui_repo_view_file.py @@ -99,7 +99,8 @@ class PagureFlaskRepoViewFiletests(LocalBasetests): self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( - '
foo\n bar
', + '
'
+            'foo\n bar
', output_text ) @@ -213,7 +214,8 @@ class PagureFlaskRepoViewFiletests(LocalBasetests): 'text/html; charset=utf-8') self.assertIn('  Šource', output_text) self.assertIn( - '
Row 0\n
', + '
'
+            'Row 0\n
', output_text ) @@ -313,7 +315,8 @@ class PagureFlaskRepoViewFileForktests(LocalBasetests): self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( - '
foo\n barRow 0\n'
+            '
'
+            'foo\n barRow 0\n'
             'Row 1\nRow 2\nRow 3\nRow 4\nRow 5\nRow 6\nRow 7\nRow 8\n'
             'Row 9\n
', output_text ) diff --git a/tests/test_pagure_flask_ui_slash_branch_name.py b/tests/test_pagure_flask_ui_slash_branch_name.py index d2f5997..7bb19c5 100644 --- a/tests/test_pagure_flask_ui_slash_branch_name.py +++ b/tests/test_pagure_flask_ui_slash_branch_name.py @@ -225,7 +225,8 @@ class PagureFlaskSlashInBranchtests(tests.SimplePagureTest): ''', output_text) self.assertIn( - '
*~
', + '
'
+            '*~
', output_text )