New Feature Request : Prettify Code

  • 11 Replies
  • 5741 Views
*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
New Feature Request : Prettify Code
« on: July 21, 2017, 11:16:58 pm »
8pla.net asked for this and I thought it was a good idea. I just picked a theme I liked, because I'm used to reading code on a dark background, mainly because I like my retina, but also because the colours show up better.

Other themes are available if you all want to take a look and they could be customised : https://jmblog.github.io/color-themes-for-google-code-prettify/

I'm going to drop a few tests in here now of different code. I had to hack together a language definition that covers the main languages we have people using here so that it plays nice. See how it goes.
« Last Edit: July 22, 2017, 04:41:45 am by Freddy »

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #1 on: July 21, 2017, 11:18:04 pm »
PHP

Code
function Array_Sort($arr, $index, $order = 'asc') {
    $b = array();
    $c = array();
    foreach ($arr as $key => $value) {
        $b[$key] = $value[$index];
    }

    if ($order == 'asc')
        asort($b);
    elseif ($order == 'desc')
        arsort($b);

    foreach ($b as $key => $value) {
        $c[] = $arr[$key];
    }

    return $c;
}

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #2 on: July 21, 2017, 11:20:34 pm »
C:

Code
#include <stdio.h>
#include <string.h>

void main()
{
   const char *scan = "Scan Terminal", *terminal = "Terminal";
   char *node;
   int pos;

   node = strstr(scan, terminal);
   pos = node - scan;

   printf("Scanned, \"%s\". For, \"%s\" (at position %d).\n", scan, node,pos);
}

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #3 on: July 21, 2017, 11:21:16 pm »
JS :

Code
<script type="text/javascript">
// Say hello world until the user starts questioning
// the meaningfulness of their existence.
function helloWorld(world) {
  for (var i = 42; --i >= 0;) {
    alert('Hello ' + String(world));
  }
}
</script>
<style>
p { color: pink }
b { color: blue }
u { color: "umber" }
</style>

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #4 on: July 22, 2017, 04:42:34 am »
XML :

Code
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
   </description>
    <calories>650</calories>
</food>
<food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>
    Light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>
    Belgian waffles covered with assorted fresh berries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>
    Thick slices made from our homemade sourdough bread
    </description>
    <calories>600</calories>
</food>
<food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>
    Two eggs, bacon or sausage, toast, and our ever-popular hash browns
    </description>
    <calories>950</calories>
</food>
</breakfast_menu>

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #5 on: July 22, 2017, 04:43:50 am »
ElfScript

Code
/*
A short example of stringing questions together
to learn something about the user
*/

// Start the goal from scratch for demo purposes.
i: start books;
s: $userbook = "";
s: $userbookauth = "";
s: $userbookmovie = "";
o: okay;

// Escaping further questions for demo purposes.
f: $goal != "";
 i: break;
 o: Okay I will stop !;
 s: $goal = "";
f:;

// This starts the series of questions. Or says what the bot already knows.
i: i like books;
c: ($userbook == "") {
    o: What is your favourite book ?;
    s: $prev = "what is your favourite book";
    s: $goal = "book";
}
e: c: ($userbook != "") {
    o: Yes I know you like $userbook.;
    c: ($userbookauth != "")
     o: I also know that it was written by $userbookauth.;
    c: ($userbookkind != "")
     o: And that $userbookkind too !;
}

// Asking Questions.
f: $goal == "book" && $prev == "what is your favourite book";
 i: %||it is %||my favourite book is %||that is %||i really like %;
 s: $userbook = %1;
 s: $prev = "who wrote that";
 o: Hmm $userbook, who wrote that ?;
f:;

f: $goal == "book" && $prev == "who wrote that";
 i: %;
 s: $userbookauth = %1;
 s: $prev = "what kind of book is it";
 o: Nice. So you like $userbook by $userbookauth, cool.;
 o: What kind of book is it ?;
f:;

f: $goal == "book" && $prev == "what kind of book is it";
 i: %;
 s: $userbookkind = %1;
 s: $prev = "";
 o: $userbookkind, I think I might like it then;
f:;

Unfortunately some of the keywords get picked out when they shouldn't. I'm not very good with complex regular expressions, so it's likely to stay that way unless anyone wants a crack at it.
« Last Edit: July 22, 2017, 06:34:07 am by Freddy »

*

infurl

  • Administrator
  • ***********
  • Eve
  • *
  • 1371
  • Humans will disappoint you.
    • Home Page
Re: New Feature Request : Prettify Code
« Reply #6 on: July 22, 2017, 06:33:44 am »
What sort of syntax files are they (examples and/or documentation would suffice) and what sort of regular expressions?

I write vim syntax files for the languages that I develop and I've had plenty of practice with different flavours of Posix regular expressions such as those documented here https://www.postgresql.org/docs/9.6/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #7 on: July 22, 2017, 06:37:38 am »
Well I couldn't tell you. I'm trying to shoe-horn my PHP knowledge of regex into it, js regex seem different.

I did get somewhere with the ElfScript though, see above I edited it.

Here's what the language definition I made looks like :

Code
var PR_KEYWORD_COMMON = "kwd";
var PR_KEYWORD_C = "kwd";
var PR_KEYWORD_ELF = "kwd";
var PR_OPERATOR_ELF = "opr";
var PR_KEYWORD_PY = "kwd";
var PR_FUNCTION = "fun";
var PR_VAR_PHP = "var";
var PR_COMMENT = "com";
var PR_FLOW_CONTROL = "flow";
var PR_STRING = "str";
var PR_LITERAL = "lit";

PR.registerLangHandler(
    PR.createSimpleLexer(
        [], [

//[PR_STRING, /(i:|o:|s:|f:|e:|c:)(.*?);/],
            // Unescaped content in an unknown language
            ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/],
            ['lang-', /^<%([\s\S]+?)(?:%>|$)/],
            ['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i],
            // Unescaped content in javascript.  (Or possibly vbscript).
            ['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],
            // Contains unescaped stylesheet content
            ['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i],
            ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i],
[PR_STRING, /^(?:"(?:[^\\"\r\n]|\\.)*(?:"|$))/],
            [PR_FUNCTION, /\b(function)\b/],
            [PR_VAR_PHP, /\$[a-z]+/],
            [PR_KEYWORD_C, /\b(auto|case|char|const|default|double|enum|extern|float|goto|inline|int|long|register|restrict|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile)\b/],
[PR_COMMENT, /^\/\/[^\r\n]*/],
            [PR_COMMENT, /^#(?:(?:define|e(?:l|nd)if|else|error|ifn?def|include|line|pragma|undef|warning)\b|[^\r\n]*)/],
            [PR_FLOW_CONTROL, /\b(break|continue|do|else|for|if|elseif|else|foreach|return|while)\b/],
            [PR_STRING, /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h(?:h|pp|\+\+)?|[a-z]\w*)>/],         
            [PR_KEYWORD_PY, /\b(and|as|assert|class|def|del|elif|except|exec|finally|from|global|import|in|is|lambda|nonlocal|not|or|pass|print|raise|try|with|yield|False|True|None)\b/],
           
[PR_LITERAL,       /[%0-9]/],

        ]), ['aidreams']);


And here's their home page : https://github.com/google/code-prettify

*

Freddy

  • Administrator
  • **********************
  • Colossus
  • *
  • 6860
  • Mostly Harmless
Re: New Feature Request : Prettify Code
« Reply #8 on: July 22, 2017, 06:38:50 am »
The variables at the top are matched and generate the CSS elements to surround the chunks of code it finds. It's a catch all affair.

I need some sleep now, so catch you later.

*

infurl

  • Administrator
  • ***********
  • Eve
  • *
  • 1371
  • Humans will disappoint you.
    • Home Page
Re: New Feature Request : Prettify Code
« Reply #9 on: July 22, 2017, 07:05:37 am »
Looking over that, scratching my head and taking a wild guess, I can offer the following.

Code
[PR_STRING, /^(?:"(?:[^\\"\r\n]|\\.)*(?:"|$))/]

The regular expression between the / and / matches:
^ start of line
(?:
" double quote
(?:[^\\"\r\n]|\\.)* anything but double quote or end of line
(?:"|$) double quote or end of line
)

So, if you match i: or o: as the start of the string and end of line as the end of the string, you'll catch most of them.

Code
/^(?:[io]:(?:[^\\"\r\n]|\\.)*(?:$))/

A refinement would be to stop the string at special characters like parentheses and dollar signs, as well as end of line. e.g. (?:[()$]|$)

Since you aren't explicitly marking the starts of strings, it's a bit hard to detect string fragments after the special characters with mere regular expressions, for that you'd need a more sophisticated grammar.

*

8pla.net

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1307
  • TV News. Pub. UAL (PhD). Robitron Mod. LPC Judge.
    • 8pla.net
Re: New Feature Request : Prettify Code
« Reply #10 on: July 23, 2017, 11:33:56 pm »
AIML:

Code
<?xml version = "1.0" encoding = "UTF-8"?>
<aiml>
   <category>
      <pattern>HEY FREDDY</pattern> 
      <template>What's up?</template>
   </category>
   <category>
      <pattern>THANKS</pattern> 
      <template>You're welcome!</template>
   </category>
</aiml>

BASH:

Code
#!/bin/bash

read -p "Who are you? " who

echo "Hello, $who. Let's chat!"

My Very Enormous Monster Just Stopped Using Nine

*

paulh5268

  • Roomba
  • *
  • 1
  • Clap Creative
    • Clap Creative
Re: New Feature Request : Prettify Code
« Reply #11 on: November 25, 2017, 10:51:01 am »
Hii, Thanks for sharing this code. I am a developer so this code is useful to me.

 


Who's the AI?
by frankinstien (Future of AI)
November 04, 2024, 05:45:05 am
Will LLMs ever learn what is ... is?
by frankinstien (Future of AI)
November 03, 2024, 08:11:00 pm
Project Acuitas
by WriterOfMinds (General Project Discussion)
October 27, 2024, 09:17:10 pm
Ai improving AI
by infurl (AI Programming)
October 19, 2024, 03:43:29 am
Atronach's Eye
by WriterOfMinds (Home Made Robots)
October 13, 2024, 09:52:42 pm
Running local AI models
by spydaz (AI Programming)
October 07, 2024, 09:00:53 am
Hi IM BAA---AAACK!!
by MagnusWootton (Home Made Robots)
September 16, 2024, 09:49:10 pm
Attempting Hydraulics
by MagnusWootton (Home Made Robots)
August 19, 2024, 04:03:23 am
LLaMA2 Meta's chatbot released
by spydaz (AI News )
August 24, 2024, 02:58:36 pm
ollama and llama3
by spydaz (AI News )
August 24, 2024, 02:55:13 pm
AI controlled F-16, for real!
by frankinstien (AI News )
June 15, 2024, 05:40:28 am
Open AI GPT-4o - audio, vision, text combined reasoning
by MikeB (AI News )
May 14, 2024, 05:46:48 am
OpenAI Speech-to-Speech Reasoning Demo
by MikeB (AI News )
March 31, 2024, 01:00:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am

Users Online

303 Guests, 0 Users

Most Online Today: 357. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles