qw0258 / google-code-prettify

Automatically exported from code.google.com/p/google-code-prettify
Apache License 2.0
0 stars 0 forks source link

Language type json #298

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Is there a way to get a formatter for json? lang-js does not quite work, since 
the keys and value are all the same color (strings).

I would love to have a way to set the keys color, and values color.

Thanks.

Original issue reported on code.google.com by jus...@balancedpayments.com on 19 Jul 2013 at 9:16

GoogleCodeExporter commented 8 years ago
I was also hoping that the keys and values would be differentiated, but they 
are not.

Original comment by hugh.lo...@lodestarbpm.com on 27 Dec 2013 at 6:00

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Something like

PR['registerLangHandler'](
    PR['createSimpleLexer'](
        [
         [PR['PR_PLAIN'],       /^\s+/, null, '\t\n\r '],
         [PR['PR_KEYWORD'],     /^(?:true|false|null)\b/, null, 'tfn'],
         [PR['PR_LITERAL'],     /^[+\-]?(?:0xX[0-9a-f]+|(?:\d+|\d*\.\d+)(?:e[+\-]?\d+)?)/, null, '+-0123456789.'],
         [PR['PR_PUNCTUATION'], /^[,:{}\[\]]+/, null, ',:{}[]']
        ],
        [
         // A string followed by a colon is additionally a key.
         ['lang-jsonkey',       /^(\"(?:[^\n\r\\\"]|\\\S)*\")\s*:/],
         [PR['PR_STRING'],      /^\"(?:[^\n\r\\\"]|\\\S)*\"/]
        ]),
    ['json']);

PR['registerLangHandler'](
    PR['createSimpleLexer'](
        [
         // Apply the classes str and key to the key.
         [PR['PR_STRING'] + ' key', /^[\s\S]*/, null, '"']
        ],
        [
        ]),
    ['jsonkey']);

should do it, but it runs into the problem that many people use the word "JSON" 
for things that are not quire JSON like

    myJsonPCallback({ ... })

    [new Date(2014, 1, 1)]

    { /* Comments are fun */ }

    { 'single quoted keys are fun': true, but: "whatsMoreFun?", unquotedKeys: true }

    /* Math is hard! */ [NaN, Infinity]

Defining JSON mode strictly is hard.  Reusing JS mode neatly side-steps all 
those issues, but colon is overloaded in JS.

Original comment by mikesamuel@gmail.com on 30 Dec 2013 at 4:26

GoogleCodeExporter commented 8 years ago
Nasty hack but this jQuery worked for me:

$('.str + .pun:contains(:)').prev().addClass('tag');

Use whatever class you like...

Original comment by p...@pab.me.uk on 9 Apr 2015 at 4:23