rouge icon indicating copy to clipboard operation
rouge copied to clipboard

http lexer should handle blocks of HTTP headers

Open garethsb opened this issue 4 years ago • 1 comments

Is your enhancement request related to a problem? Please describe.

The http lexer currently fails if the code is not a whole request or response. It would be very useful to be able to highlight just a block of HTTP headers for example.

The GitHub Markdown highlighting mechanism for the same http id does support this, so when moving documentation from GitHub to a Jekyll/Rouge host, it is surprising when a block of HTTP headers suddenly renders entirely in red as error.

Describe the solution you'd like

Accept headers whether or not preceded by request or response line.

Additional context

I discovered this has been previously raised as https://github.com/rouge-ruby/rouge/issues/373.


Sample code:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, POST, PATCH, HEAD, OPTIONS, DELETE
Access-Control-Allow-Headers: Content-Type, Accept
Access-Control-Max-Age: 3600

Rendering with Rouge on the left, rendering on GitHub on the right:

image

garethsb avatar May 17 '21 12:05 garethsb

I think the following patch would solve this issue simply and a related one of wanting a request without a trailing HTTP/version to render nicely (again like GitHub does):

GET /foo
Content-Type: bar/baz

Tweak /lib/rouge/lexers/http.rb like so:

29,30c29,30
<           ([^ ]+)([ ]+)                          # path
<           (HTTPS?)(/)(\d(?:\.\d)?)(\r?\n|$)      # http version
---
>           ([^ ]+)(?:([ ]+)                       # path
>           (HTTPS?)(/)(\d(?:\.\d)?))?(\r?\n|$)    # http version
53a54
>         mixin :headers

Obviously additional test cases required, if this change would be acceptable?

garethsb avatar May 18 '21 10:05 garethsb