#4182 WIP Force highlight.js to use certain highlighting schemes in file view
Merged 6 years ago by pingou. Opened 6 years ago by ryanlerch.
ryanlerch/pagure issue4169  into  master

file modified
+12
@@ -1616,6 +1616,18 @@ 

  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

  -------------------

  

file modified
+1 -1
@@ -173,7 +173,7 @@ 

          {% endif %}

  

      {% if output_type=='file' %}

-         <pre class="syntaxhighlightblock"><code>{{ content }}</code></pre>

+         <pre class="syntaxhighlightblock"><code class="{{filename|syntax_alias}}">{{ content }}</code></pre>

      {% elif output_type == 'markup' %}

          <div class="m-2">

          {% autoescape false %}

file modified
+22
@@ -17,6 +17,7 @@ 

  

  import textwrap

  import logging

+ from os.path import splitext

  

  import arrow

  import flask
@@ -88,6 +89,27 @@ 

          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,

@@ -223,7 +223,8 @@ 

            </li>

          </ol>''',  output_text)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">foo\n bar</code></pre>',

              output_text

          )

  

@@ -2239,7 +2239,8 @@ 

          self.assertEqual(output.status_code, 200)

          output_text = output.get_data(as_text=True)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">foo\n bar</code></pre>',

              output_text)

  

          # Empty files should also be displayed
@@ -2336,7 +2337,8 @@ 

              '</span>&nbsp; Å ource',

              output_text)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>Row 0\n</code></pre>',

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">Row 0\n</code></pre>',

              output_text

          )

  
@@ -2376,7 +2378,8 @@ 

          self.assertEqual(output.status_code, 200)

          output_text = output.get_data(as_text=True)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>foo\n barRow 0\n'

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">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)

  

@@ -99,7 +99,8 @@ 

          self.assertEqual(output.status_code, 200)

          output_text = output.get_data(as_text=True)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">foo\n bar</code></pre>',

              output_text

          )

  
@@ -213,7 +214,8 @@ 

                           'text/html; charset=utf-8')

          self.assertIn('</span>&nbsp; Å ource', output_text)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>Row 0\n</code></pre>',

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">Row 0\n</code></pre>',

              output_text

          )

  
@@ -313,7 +315,8 @@ 

          self.assertEqual(output.status_code, 200)

          output_text = output.get_data(as_text=True)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>foo\n barRow 0\n'

+             '<pre class="syntaxhighlightblock"><code class="lang-plaintext">'

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

          )

@@ -225,7 +225,8 @@ 

            </li>

          </ol>''', output_text)

          self.assertIn(

-             '<pre class="syntaxhighlightblock"><code>*~</code></pre>',

+             '<pre class="syntaxhighlightblock">'

+             '<code class="lang-plaintext">*~</code></pre>',

              output_text

          )

  

Currently, highlight.js tries to autodetect languages, and in some cases fails.

This is a work in progress

This currently works to override .spec and .patch files (with that extension specifically) so that highlighter is used on those files.

Not sure about the approach here, but this works, and @pingou if you have any feedback, that would be awesome

We can simplify this:

override_rules = pagure_config.get("SYNTAX_ALIAS_OVERRIDES", {".spec":"specfile", ".patch":"diff"})

We'll also need to document this configuration key in the doc

rebased onto c792e7c514beee48528e0d02177f5bd3a795bf8f

6 years ago

rebased onto e7a1a4b35385f7389752bdbc6e6605d27250515e

6 years ago

We can simplify this:
override_rules = pagure_config.get("SYNTAX_ALIAS_OVERRIDES", {".spec":"specfile", ".patch":"diff"})

We'll also need to document this configuration key in the doc

Yeah was tossing up if we wanted this as a config key or not, and do we want it olny as a key? or do a default hardcoded like i already have?

I don't know if it's a good idea to expose this as a configurable thing. Usually these override rules would be generally applicable anyway, so we'd want to incorporate those changes to all Pagure instances everywhere.

the main reason why it would be good configurable, is that sometimes filename extensions mean different things. this would allow configuration here rather than having it hardcoded in pagure itself.

I kept it in purpose as I think there will be case where we want to adjust the highlighting without touching the code base, one example is https://pagure.io/pagure/issue/3514 and the .fmf files.

rebased onto e9f97393c68a36d433d0a5f46addc0bf785fb1b1

6 years ago

Outside of fixing the commit message, anything planned for this PR ?

@ryanlerch ?

rebased onto d26e95fcbd0ae2b33c63b2b9da2bc1da7c233028

6 years ago

rebased onto 2f670bd86d5b17e606ffe7b3ee444394060ed42d

6 years ago

rebased onto 9e130a27327b03047be20b81d2e2ada837a83074

6 years ago

rebased onto 146ae6fb6bdb582c56ae7084d8ebd9e2f68ea5df

6 years ago

I believe this is also ready for a review :)

rebased onto 9fe1c6d

6 years ago

Thanks for the review :)

Pull-Request has been merged by pingou

6 years ago