jgm/skylighting

Support for HTTP styling

Opened this issue · 4 comments

Is there a support for http syntax highlight. This will be useful for api documentations.
For example documentation like git markup support http syntax high light for statements like POST /test/

jgm commented

No, there doesn't seem to be a syntax definition upstream for http.

jgm commented

You could always write one and use it with pandoc via --syntax-definition.

Iam checking this option now. Written a basic one. It seems git is using this rule for htttp. Any way to convert these format to KDE style.

Here is a first version that did everything at least I needed.

This assumes that the HTTP body is separated by a blank line from the headers (as specified in the HTTP request syntax).

It also assumes the request body is JSON, which may not always be what you want. Modify the lineEmptyContext field in the line <context attribute="Normal Text" lineEndContext="#pop" name="Normal Text" lineEmptyContext="Normal##JSON"> if you want something else (like Start##XML for XML). I could not be bothered to figure out a way to do this dynamically, though I guess it would be possible by branching on the Content-Type header, if present.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="http" version="1.0" kateversion="5.0" section="Markup" casesensitive="false">
    <highlighting>
        <list name="methods">
            <item>GET</item>
            <item>HEAD</item>
            <item>POST</item>
            <item>PUT</item>
            <item>DELETE</item>
            <item>CONNECT</item>
            <item>OPTIONS</item>
            <item>TRACE</item>
            <item>PATCH</item>
        </list>
        <list name="headers">
            <item>Accept</item>
            <item>Accept-Charset</item>
            <item>Accept-Datetime</item>
            <item>Accept-Encoding</item>
            <item>Accept-Language</item>
            <item>Access-Control-Request-Method</item>
            <item>Access-Control-Request-Headers</item>
            <item>Authorization</item>
            <item>Cache-Control</item>
            <item>Connection</item>
            <item>Content-Encoding</item>
            <item>Content-Length</item>
            <item>Content-MD5</item>
            <item>Content-Type</item>
            <item>Cookie</item>
            <item>Date</item>
            <item>Expect</item>
            <item>Forwarded</item>
            <item>From</item>
            <item>Host</item>
            <item>If-Match</item>
            <item>If-Modified-Since</item>
            <item>If-None-Match</item>
            <item>If-Range</item>
            <item>If-Unmodified-Since</item>
            <item>Max-Forwards</item>
            <item>Origin</item>
            <item>Pragma</item>
            <item>Proxy-Authorization</item>
            <item>Range</item>
            <item>Referer</item>
            <item>TE</item>
            <item>User-Agent</item>
            <item>Upgrade</item>
            <item>Via</item>
            <item>Warning</item>
        </list>
        <contexts>
            <context attribute="Normal Text" lineEndContext="#pop" name="Normal Text" lineEmptyContext="Normal##JSON">
                <!-- HTTP methods -->
                <keyword attribute="Keyword" context="#stay" String="methods"/>
                <!-- HTTP version number -->
                <RegExpr attribute="Version" context="#stay" String="(HTTP|http)\/\d\.\d"/>
                <!-- Request headers -->
                <RegExpr attribute="Header" context="Header" String="^([^\s:]+): ?([^\n\r]*)" lookAhead="true"/>
                <!-- Request URI -->
                <RegExpr attribute="URI" context="#stay" String="https?:\/\/[^\s]+"/>
            </context>
            <context attribute="Header" lineEndContext="#pop" name="Header">
                <!-- Headers and values -->
                <StringDetect context="#stay" attribute="Header" String="%1" dynamic="true" />
                <StringDetect context="#pop" attribute="Value" String="%2" dynamic="true" />
            </context>
        </contexts>
        <itemDatas>
            <itemData name="Normal Text" defStyleNum="dsNormal"/>
            <itemData name="Keyword" defStyleNum="dsKeyword"/>
            <itemData name="Header" defStyleNum="dsAttribute"/>
            <itemData name="Value" defStyleNum="dsDataType"/>
            <itemData name="URI" defStyleNum="dsNormal"/>
            <itemData name="Version" defStyleNum="dsSpecialString"/>
        </itemDatas>
    </highlighting>
</language>

This style generates syntax highlighting like this:

GET /foo/bar HTTP/1.1
Host: www.example.com
Content-Type: application/json

{
  "datapoints": [
    {
      "datapoint_id": "foo",
      "feature_vector": [1, 2, 3],
      "restricts": {
        "namespace": "type",
        "allow_list": ["bar"]
      }
    }
  ]
}

image