#3729 Fix syntax highlighting of files.
Merged 5 years ago by pingou. Opened 5 years ago by ryanlerch.
ryanlerch/pagure syntax-highlighting-fixes  into  master

@@ -0,0 +1,8 @@ 

+ 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

@@ -0,0 +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}"><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);

file modified
+38 -42
@@ -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 @@ 

  {{ 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 %}

@@ -226,7 +226,8 @@ 

            </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')

@@ -2227,12 +2227,9 @@ 

          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 @@ 

          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 @@ 

          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',

@@ -99,14 +99,10 @@ 

          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 @@ 

          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 @@ 

          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. """

@@ -228,7 +228,9 @@ 

            </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):

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.

rebased onto 9b014d06fd091df9444b8c1dcee65678034db13d

5 years ago

rebased onto 75cbc19302ee5a49d8479b0615d6a3108033f09b

5 years ago

Tested locally, no |noJS required apparently

rebased onto 25bee97

5 years ago

Pull-Request has been merged by pingou

5 years ago