From 25bee9793b6011c0de8158b076d7260c9b5d836b Mon Sep 17 00:00:00 2001
From: Ryan Lerch <rlerch@redhat.com>
Date: Sep 26 2018 12:20:15 +0000
Subject: [PATCH 1/3] add hilightjs-line-numbers plugin


---

diff --git a/pagure/static/vendor/highlightjs-line-numbers/INFO b/pagure/static/vendor/highlightjs-line-numbers/INFO
new file mode 100644
index 0000000..1eaef34
--- /dev/null
+++ b/pagure/static/vendor/highlightjs-line-numbers/INFO
@@ -0,0 +1,2 @@
+This Plugin is from:
+https://github.com/wcoder/highlightjs-line-numbers.js/blob/59f905d3634e09bfd0cb2f4a6efbc3dde1e2f7c5/dist/highlightjs-line-numbers.min.js
\ No newline at end of file
diff --git a/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js b/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js
new file mode 100644
index 0000000..6112cb6
--- /dev/null
+++ b/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js
@@ -0,0 +1 @@
+!function(n,e){"use strict";function t(){var n=e.createElement("style");n.type="text/css",n.innerHTML=h(".{0}{border-collapse:collapse}.{0} td{padding:0}.{1}:before{content:attr({2})}",[f,m,j]),e.getElementsByTagName("head")[0].appendChild(n)}function r(t){"complete"===e.readyState?l(t):n.addEventListener("DOMContentLoaded",function(){l(t)})}function l(t){try{var r=e.querySelectorAll("code.hljs");for(var l in r)r.hasOwnProperty(l)&&i(r[l],t)}catch(o){n.console.error("LineNumbers error: ",o)}}function i(n,e){if("object"==typeof n){e=e||{singleLine:!1};var t=e.singleLine?0:1;u(function(){s(n),n.innerHTML=o(n.innerHTML,t)})}}function o(n,e){var t=c(n);if(""===t[t.length-1].trim()&&t.pop(),t.length>e){for(var r="",l=0,i=t.length;l<i;l++)r+=h('<tr><td class="{0}"><div class="{1} {2}" {3}="{5}"></div></td><td class="{4}"><div class="{1}">{6}</div></td></tr>',[v,g,m,j,p,l+1,t[l].length>0?t[l]:" "]);return h('<table class="{0}">{1}</table>',[f,r])}return n}function s(n){var e=n.childNodes;for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];d(r.textContent)>0&&(r.childNodes.length>0?s(r):a(r.parentNode))}}function a(n){var e=n.className;if(/hljs-/.test(e)){for(var t=c(n.innerHTML),r=0,l="";r<t.length;r++)l+=h('<span class="{0}">{1}</span>\n',[e,t[r]]);n.innerHTML=l.trim()}}function c(n){return 0===n.length?[]:n.split(L)}function d(n){return(n.trim().match(L)||[]).length}function u(e){n.setTimeout(e,0)}function h(n,e){return n.replace(/\{(\d+)\}/g,function(n,t){return e[t]?e[t]:n})}var f="hljs-ln",g="hljs-ln-line",p="hljs-ln-code",v="hljs-ln-numbers",m="hljs-ln-n",j="data-line-number",L=/\r\n|\r|\n/g;n.hljs?(n.hljs.initLineNumbersOnLoad=r,n.hljs.lineNumbersBlock=i,t()):n.console.error("highlight.js not detected!")}(window,document);
\ No newline at end of file

From 4d2cd9d272213d610da6f78a6627f632a4e7abac Mon Sep 17 00:00:00 2001
From: Ryan Lerch <rlerch@redhat.com>
Date: Sep 26 2018 12:20:15 +0000
Subject: [PATCH 2/3] Fix syntax highlighting of files.


This updates the file display to use just highlight.js
Previously, highlight.js was run over the old style
codetable that was generated for when we used pygments,
and this was causing each line to be rendered seperately.
This caused hilight.js to determine some strange filetypes
for these fragments, and typically hilight them as bash output.

---

diff --git a/pagure/static/vendor/highlightjs-line-numbers/INFO b/pagure/static/vendor/highlightjs-line-numbers/INFO
index 1eaef34..56b6b91 100644
--- a/pagure/static/vendor/highlightjs-line-numbers/INFO
+++ b/pagure/static/vendor/highlightjs-line-numbers/INFO
@@ -1,2 +1,8 @@
-This Plugin is from:
+This is a slightly patched version of the Highlightjs-line-numbers plugin from
+https://github.com/wcoder/highlightjs-line-numbers.js
+
+It adds some additional classes and links on the line numbers to get some stuff
+in Pagure working.
+
+This was patched over this version of the upstream file:
 https://github.com/wcoder/highlightjs-line-numbers.js/blob/59f905d3634e09bfd0cb2f4a6efbc3dde1e2f7c5/dist/highlightjs-line-numbers.min.js
\ No newline at end of file
diff --git a/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js b/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js
index 6112cb6..f98c85b 100644
--- a/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js
+++ b/pagure/static/vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js
@@ -1 +1 @@
-!function(n,e){"use strict";function t(){var n=e.createElement("style");n.type="text/css",n.innerHTML=h(".{0}{border-collapse:collapse}.{0} td{padding:0}.{1}:before{content:attr({2})}",[f,m,j]),e.getElementsByTagName("head")[0].appendChild(n)}function r(t){"complete"===e.readyState?l(t):n.addEventListener("DOMContentLoaded",function(){l(t)})}function l(t){try{var r=e.querySelectorAll("code.hljs");for(var l in r)r.hasOwnProperty(l)&&i(r[l],t)}catch(o){n.console.error("LineNumbers error: ",o)}}function i(n,e){if("object"==typeof n){e=e||{singleLine:!1};var t=e.singleLine?0:1;u(function(){s(n),n.innerHTML=o(n.innerHTML,t)})}}function o(n,e){var t=c(n);if(""===t[t.length-1].trim()&&t.pop(),t.length>e){for(var r="",l=0,i=t.length;l<i;l++)r+=h('<tr><td class="{0}"><div class="{1} {2}" {3}="{5}"></div></td><td class="{4}"><div class="{1}">{6}</div></td></tr>',[v,g,m,j,p,l+1,t[l].length>0?t[l]:" "]);return h('<table class="{0}">{1}</table>',[f,r])}return n}function s(n){var e=n.childNodes;for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];d(r.textContent)>0&&(r.childNodes.length>0?s(r):a(r.parentNode))}}function a(n){var e=n.className;if(/hljs-/.test(e)){for(var t=c(n.innerHTML),r=0,l="";r<t.length;r++)l+=h('<span class="{0}">{1}</span>\n',[e,t[r]]);n.innerHTML=l.trim()}}function c(n){return 0===n.length?[]:n.split(L)}function d(n){return(n.trim().match(L)||[]).length}function u(e){n.setTimeout(e,0)}function h(n,e){return n.replace(/\{(\d+)\}/g,function(n,t){return e[t]?e[t]:n})}var f="hljs-ln",g="hljs-ln-line",p="hljs-ln-code",v="hljs-ln-numbers",m="hljs-ln-n",j="data-line-number",L=/\r\n|\r|\n/g;n.hljs?(n.hljs.initLineNumbersOnLoad=r,n.hljs.lineNumbersBlock=i,t()):n.console.error("highlight.js not detected!")}(window,document);
\ No newline at end of file
+!function(n,e){"use strict";function t(){var n=e.createElement("style");n.type="text/css",n.innerHTML=h(".{0}{border-collapse:collapse}.{0} td{padding:0}.{1}:before{content:attr({2})}",[f,m,j]),e.getElementsByTagName("head")[0].appendChild(n)}function r(t){"complete"===e.readyState?l(t):n.addEventListener("DOMContentLoaded",function(){l(t)})}function l(t){try{var r=e.querySelectorAll("code.hljs");for(var l in r)r.hasOwnProperty(l)&&i(r[l],t)}catch(o){n.console.error("LineNumbers error: ",o)}}function i(n,e){if("object"==typeof n){e=e||{singleLine:!1};var t=e.singleLine?0:1;u(function(){s(n),n.innerHTML=o(n.innerHTML,t)})}}function o(n,e){var t=c(n);if(""===t[t.length-1].trim()&&t.pop(),t.length>e){for(var r="",l=0,i=t.length;l<i;l++)r+=h('<tr><td class="{0}"><a class="{1} {2}" {3}="{5}" href="#_{5}" id="_{5}"></a></td><td class="{4}"><div class="{1}">{6}</div></td></tr>',[v,g,m,j,p,l+1,t[l].length>0?t[l]:" "]);return h('<table class="{0}">{1}</table>',[f,r])}return n}function s(n){var e=n.childNodes;for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];d(r.textContent)>0&&(r.childNodes.length>0?s(r):a(r.parentNode))}}function a(n){var e=n.className;if(/hljs-/.test(e)){for(var t=c(n.innerHTML),r=0,l="";r<t.length;r++)l+=h('<span class="{0}">{1}</span>\n',[e,t[r]]);n.innerHTML=l.trim()}}function c(n){return 0===n.length?[]:n.split(L)}function d(n){return(n.trim().match(L)||[]).length}function u(e){n.setTimeout(e,0)}function h(n,e){return n.replace(/\{(\d+)\}/g,function(n,t){return e[t]?e[t]:n})}var f="hljs-ln",g="hljs-ln-line notblue text-muted",p="hljs-ln-code pl-2",v="hljs-ln-numbers bg-light text-right pr-2 pl-2 border-right mr-3",m="hljs-ln-n",j="data-line-number",L=/\r\n|\r|\n/g;n.hljs?(n.hljs.initLineNumbersOnLoad=r,n.hljs.lineNumbersBlock=i,t()):n.console.error("highlight.js not detected!")}(window,document);
diff --git a/pagure/templates/file.html b/pagure/templates/file.html
index 300bc43..7a88740 100644
--- a/pagure/templates/file.html
+++ b/pagure/templates/file.html
@@ -173,13 +173,7 @@
         {% endif %}
 
     {% if output_type=='file' %}
-        {% autoescape false %}
-        {% if huge %}
-                {{ content | e | format_loc }}
-        {% else %}
-                {{ content | format_loc }}
-        {% endif %}
-        {% endautoescape %}
+        <pre class="syntaxhighlightblock"><code>{{ content }}</code></pre>
     {% elif output_type == 'markup' %}
         <div class="m-2">
         {% autoescape false %}
@@ -265,47 +259,49 @@ No content found in this repository
 {{ super() }}
 <script type="text/javascript"
   src="{{ url_for('static', filename='vendor/highlight.js/highlight.pack.js') }}"></script>
+<script src="{{ url_for('static', filename='vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js') }}"></script>
 <script type="text/javascript"
   src="{{ url_for('static', filename='vendor/highlight.js/spec.js') }}"></script>
 
-<script>hljs.initHighlightingOnLoad();</script>
+<script>
+  $(document).ready(function() {
+  $('pre.syntaxhighlightblock code').each(function(i, block) {
+    hljs.highlightBlock(block);
+    hljs.lineNumbersBlock(block);
+  })
+});
+</script>
 
 <script type="text/javascript">
-function updateHighlight() {
-  var cls = "highlighted-line";
-  $('.' + cls).removeClass(cls)
-  if (location.hash !== '') {
-    var lines = location.hash.substr(2).split('-').map(function (x) { return parseInt(x, 10) });
-    for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
-      $('[data-line-number=' + i + ']').closest('tr').addClass(cls);
+  function updateHighlight() {
+    var cls = "highlighted-line";
+    $('.' + cls).removeClass(cls)
+    if (location.hash !== '') {
+      var lines = location.hash.substr(2).split('-').map(function (x) { return parseInt(x, 10) });
+      for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
+        $('[data-line-number=' + i + ']').closest('tr').addClass(cls);
+      }
+      return lines;
     }
-    return lines;
-  }
-  return [];
-}
-$(document).ready(function () {
-  var lines = updateHighlight();
-  if (lines) {
-    var offset = $('[data-line-number=' + lines[0] + ']').offset().top;
-    $('html, body').animate({scrollTop: offset}, 200);
-  }
-});
-$(window).on('hashchange', updateHighlight);
-var selected = [];
-$("[data-line-number]").click(function (ev) {
-  var line = $(this).attr('data-line-number');
-  if (ev.shiftKey) {
-    selected = selected.slice(-1).concat(line);
-  } else {
-    selected = [line];
+    return [];
   }
+  $(window).on('hashchange', updateHighlight);
+  var selected = [];
+  $("[data-line-number]").click(function (ev) {
+    var line = $(this).attr('data-line-number');
+    if (ev.shiftKey) {
+      selected = selected.slice(-1).concat(line);
+    } else {
+      selected = [line];
+    }
+
+    var hash = '_' + selected[0];
+    if (selected.length === 2) {
+      hash = '_' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);
+    }
+    window.location.hash = hash;
+    return false;
+  });
+  </script>
 
-  var hash = '_' + selected[0];
-  if (selected.length === 2) {
-    hash = '_' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);
-  }
-  window.location.hash = hash;
-  return false;
-});
-</script>
 {% endblock %}

From 2d982934f2c7fea6935853601344dc49480391d2 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Sep 26 2018 12:20:15 +0000
Subject: [PATCH 3/3] Fix the unit-tests for the change in the way the files are highlighted


Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/tests/test_pagure_flask_ui_no_master_branch.py b/tests/test_pagure_flask_ui_no_master_branch.py
index 8700210..af288c7 100644
--- a/tests/test_pagure_flask_ui_no_master_branch.py
+++ b/tests/test_pagure_flask_ui_no_master_branch.py
@@ -226,7 +226,8 @@ class PagureFlaskNoMasterBranchtests(tests.SimplePagureTest):
           </li>
         </ol>''',  output_text)
         self.assertIn(
-            '<td class="cell2"><pre><code>foo</code></pre></td>', output_text
+            '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',
+            output_text
         )
 
     @patch('pagure.lib.notify.send_email')
diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py
index 311df42..f60746a 100644
--- a/tests/test_pagure_flask_ui_repo.py
+++ b/tests/test_pagure_flask_ui_repo.py
@@ -2227,12 +2227,9 @@ class PagureFlaskRepotests(tests.Modeltests):
         output = self.app.get('/test/blob/master/f/sources')
         self.assertEqual(output.status_code, 200)
         output_text = output.get_data(as_text=True)
-        self.assertIn('<table class="code_table">', output_text)
         self.assertIn(
-            '<tr><td class="cell1"><a id="_1" href="#_1" data-line-number="1"></a></td>',
+            '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',
             output_text)
-        self.assertIn(
-            '<td class="cell2"><pre><code> bar</code></pre></td>', output_text)
 
         # Empty files should also be displayed
         tests.add_content_to_git(
@@ -2327,12 +2324,8 @@ class PagureFlaskRepotests(tests.Modeltests):
         self.assertIn(
             '</span>&nbsp; Šource',
             output_text)
-        self.assertIn('<table class="code_table">', output_text)
-        self.assertIn(
-            '<tr><td class="cell1"><a id="_1" href="#_1" '
-            'data-line-number="1"></a></td>', output_text)
         self.assertIn(
-            '<td class="cell2"><pre><code>Row 0</code></pre></td>',
+            '<pre class="syntaxhighlightblock"><code>Row 0\n</code></pre>',
             output_text
         )
 
@@ -2371,13 +2364,10 @@ class PagureFlaskRepotests(tests.Modeltests):
         output = self.app.get('/fork/pingou/test3/blob/master/f/sources')
         self.assertEqual(output.status_code, 200)
         output_text = output.get_data(as_text=True)
-        self.assertIn('<table class="code_table">', output_text)
-        self.assertIn(
-            '<tr><td class="cell1"><a id="_1" href="#_1" data-line-number="1"></a></td>',
-            output_text)
         self.assertIn(
-            '<td class="cell2"><pre><code> barRow 0</code></pre></td>',
-            output_text)
+            '<pre class="syntaxhighlightblock"><code>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</code></pre>', output_text)
 
     @patch(
         'pagure.lib.encoding_utils.decode',
diff --git a/tests/test_pagure_flask_ui_repo_view_file.py b/tests/test_pagure_flask_ui_repo_view_file.py
index cd551b3..06baca2 100644
--- a/tests/test_pagure_flask_ui_repo_view_file.py
+++ b/tests/test_pagure_flask_ui_repo_view_file.py
@@ -99,14 +99,10 @@ class PagureFlaskRepoViewFiletests(LocalBasetests):
         output = self.app.get('/test/blob/master/f/sources')
         self.assertEqual(output.status_code, 200)
         output_text = output.get_data(as_text=True)
-        self.assertTrue('<table class="code_table">' in output_text)
-        self.assertTrue(
-            '<tr><td class="cell1"><a id="_1" href="#_1" '
-            'data-line-number="1"></a></td>'
-            in output_text)
         self.assertIn(
-            '<td class="cell2"><pre><code> bar</code></pre></td>',
-             output_text)
+            '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',
+            output_text
+        )
 
     def test_view_file_empty_file(self):
         """ Test the view_file with an empty file. """
@@ -217,12 +213,8 @@ class PagureFlaskRepoViewFiletests(LocalBasetests):
         self.assertEqual(output.headers['Content-Type'].lower(),
                          'text/html; charset=utf-8')
         self.assertIn('</span>&nbsp; Šource', output_text)
-        self.assertIn('<table class="code_table">', output_text)
-        self.assertIn(
-            '<tr><td class="cell1"><a id="_1" href="#_1" '
-            'data-line-number="1"></a></td>', output_text)
         self.assertIn(
-            '<td class="cell2"><pre><code>Row 0</code></pre></td>',
+            '<pre class="syntaxhighlightblock"><code>Row 0\n</code></pre>',
             output_text
         )
 
@@ -321,14 +313,11 @@ class PagureFlaskRepoViewFileForktests(LocalBasetests):
         output = self.app.get('/fork/pingou/test/blob/master/f/sources')
         self.assertEqual(output.status_code, 200)
         output_text = output.get_data(as_text=True)
-        self.assertIn('<table class="code_table">', output_text)
         self.assertIn(
-            '<tr><td class="cell1"><a id="_1" href="#_1" '
-            'data-line-number="1"></a></td>',
-            output_text)
-        self.assertIn(
-            '<td class="cell2"><pre><code> barRow 0</code></pre></td>',
-            output_text)
+            '<pre class="syntaxhighlightblock"><code>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</code></pre>', output_text
+        )
 
     def test_view_file_fork_and_edit_on_fork_logged_out(self):
         """ Test the view_file on a text file on a fork when logged out. """
diff --git a/tests/test_pagure_flask_ui_slash_branch_name.py b/tests/test_pagure_flask_ui_slash_branch_name.py
index 43f657f..56421d3 100644
--- a/tests/test_pagure_flask_ui_slash_branch_name.py
+++ b/tests/test_pagure_flask_ui_slash_branch_name.py
@@ -228,7 +228,9 @@ class PagureFlaskSlashInBranchtests(tests.SimplePagureTest):
           </li>
         </ol>''', output_text)
         self.assertIn(
-            '<td class="cell2"><pre><code>*~</code></pre></td>', output_text)
+            '<pre class="syntaxhighlightblock"><code>*~</code></pre>',
+            output_text
+        )
 
     @patch('pagure.lib.notify.send_email')
     def test_view_raw_file(self, send_email):

