Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript or PHP syntax highlighting of query strings?

There are a lot of syntax highlighters out there but something I have not seen is one that supports highlighting query strings!

I'm looking for something to use when documenting my API and well being an API there are a lot of query strings involved. so. What good javascript or PHP syntax highlighters are there that support query strings?

I have looked into Google code prettify, highlight.js, prismjs and a few others but none of them seem to be able to highlight this:

/oauth/authorize?client_id=wG2X7q1qz74zdSbgiFkyL5JFOeloQwg2opfrPfaJ&response_type=code&redirect_uri=https%3A%2F%2Fmyapplication.com%2Foauth&scope=account%2Ccompetition%2Cvideos&state=d41d8cd98f00b204e9800998ecf8427e

Which should highlight all the keys on one colour, and all the values in another with the &?= being a different colour again... something like: enter image description here

like image 275
Hailwood Avatar asked Nov 12 '22 22:11

Hailwood


1 Answers

As far as i am concerned all characters like '?', '&' and '=' should be replaced in names / values in a proper URL. This makes it super easy to parse.

So why not invent something on your own:

function parseQuery(query){
    parsed = query.split('?');
    if (parsed.length > 1) {
        parsed[1] = parsed[1].split('&');
        for(i in parsed[1]) {
            parsed[1][i] = parsed[1][i].split('=');
        }
    }
    retStr = '<span class="path">' + parsed[0] + '</span>';
    if (parsed.length > 1) {
        retStr += '<span class="qm">?</span>';
        first = true;
        for(i in parsed[1]) {
            if (first) {
                first = false;
            } else {
                retStr += '<span class="amp">&</span>';
            }
            retStr += '<span class="name">' + parsed[1][i][0] + '</span>';
            if (parsed[1][i].length > 1) {
                retStr += '<span class="eq">=</span>' +
                '<span class="value">' + parsed[1][i][1] + '</span>';
            }
            if (parsed[1][i].length > 2) {
                for (var j = 2; j < parsed[1][i].length; j++) {
                      retStr += '<span class="eq">=</span>';
                      retStr += '<span class="error">' + parsed[1][i][j] + '</span>';
                }
            }
        }
    }
    if (parsed.length > 2) {
        for (var i = 2; i < parsed.length; i++) {
              retStr += '<span class="qm">?</span>';
              retStr += '<span class="error">' + parsed[i] + '</span>';
        }
    }
    return retStr;
}

http://jsfiddle.net/YqrpV/1/

like image 75
fsw Avatar answered Nov 14 '22 23:11

fsw