Author Topic: Syntax Highligting for Code  (Read 11789 times)

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #20 on: February 18, 2013, 10:07:31 PM »
Welp, it looks good on the dark theme now. Enjoy folks. Blizzard probably did just as much work as I have on this.


for your information it attempts to auto detect the language but it can get confused if the section is small you can help it pick the language with the code tag property

[code=ruby][/code] Ruby
[code=php][/code] PHP
[code=cpp][/code] C++
[code=cs][/code]   C#

ect.

EDIT:
At least it would if the code tag still put that value after the :  is the code tag header...
FIX IT BLIZZ :(

(click to show/hide)
« Last Edit: February 18, 2013, 10:43:53 PM by Ryex »
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #21 on: February 18, 2013, 11:03:57 PM »
I'll have to look into that another time. Judging by the code, I'm not even sure that feature is actually supported.

EDIT: I'll add that code right away. Let's see if it works.

EDIT: And you broke my selection code, Ryex. :P

EDIT: Alright, I added your code (with some modifications), but I don't think it's working.
« Last Edit: February 18, 2013, 11:11:30 PM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #22 on: February 18, 2013, 11:17:58 PM »
it doesn't always pick the right syntax sadly but most of the time it does and when it doens't half the time the addition of the right code type will help it pick. actually you might want to put an extra space after the :
« Last Edit: February 18, 2013, 11:19:57 PM by Ryex »
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #23 on: February 18, 2013, 11:59:35 PM »
Sure, if it helps.

EDIT:

Code: (ruby) [Select]
int main()
{
    printf("Hellooooooooooooooooooooo.\n");
    return 0;
}

Code: (cpp) [Select]
int main()
{
    printf("Hellooooooooooooooooooooo.\n");
    return 0;
}

EDIT: I think all it really does is detect keywords, comments and stuff enclosed in quotes, etc.

EDIT: I updated Subs-Highlight.php in my DB in case you wanna use it. But be careful. I removed some stuff that I don't need.
« Last Edit: February 19, 2013, 12:16:40 AM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #24 on: February 19, 2013, 12:51:48 AM »
I figured it out. if you explicitly set the class of the code tag it uses that highlighting
also only the

Code: [Select]
'type' => 'unparsed_equals_content', should set the code header I realized

Code: (php) [Select]
// Code tag
function ch_code_tag(&$codes)
{
    global $modSettings, $txt, $settings;

    $darkThemeIDS = array(3, 5, 7, 9, 11, 13);

    if (in_array($settings['theme_id'], $darkThemeIDS))
    {
        $codeWrapHTML1 = '<div class=\"block_code\" id=\"code_a" . $codecounter . "\"><div class=\"dark_theme_code\"><pre><code>$1</code></pre></div></div>"';
        $codeWrapHTML2 = '<div class=\"block_code\" id=\"code_b" . $codecounter . "\"><div class=\"dark_theme_code\"><pre><code class=\"" . $data[1] . "\">$1</code></pre></div></div>"';
    }
    else
    {
        $codeWrapHTML1 = '<div class=\"block_code\" id=\"code_a" . $codecounter . "\"><pre><code>$1</code></pre></div>"';
        $codeWrapHTML2 = '<div class=\"block_code\" id=\"code_b" . $codecounter . "\"><pre><code class=\"" . $data[1] . "\">$1</code></pre></div>"';
    }

    if (!empty($modSettings['ch_enable']))
    {
        foreach ($codes as $tag => $dump)
            if ($dump['tag'] == 'code') unset($codes[$tag]);
            
        $codes[] =  array(
            'tag' => 'code',
            'type' => 'unparsed_content',
            'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
                global $txt;
                // select all and copy code to clipboard mod
                static $codecounter = 0;
                $codecounter++;
                $tag[\'content\'] = "<div class=\"codeheader\">" . $txt[\'smf238\'] . ": <a href=\"javascript:void(0)\" title=\"" . $txt[\'codeselectall\'] . "\" onclick=\"javascript:selectall(\'code_a" . $codecounter . "\');return false;\">" . $txt[\'codeselectall\'] . "</a></div>' . $codeWrapHTML1 . ';
            '),
            'block_level' => true,
        );
        $codes[] = array(
            'tag' => 'code',
            'type' => 'unparsed_equals_content',
            'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
                global $txt;
                // select all and copy code to clipboard mod
                static $codecounter = 0;
                $codecounter++;
                $tag[\'content\'] = "<div class=\"codeheader\">" . $txt[\'smf238\'] . ": " . $data[1] . " <a href=\"javascript:void(0)\" title=\"" . $txt[\'codeselectall\'] . "\" onclick=\"javascript:selectall(\'code_b" . $codecounter . "\');return false;\">" . $txt[\'codeselectall\'] . "</a></div>' . $codeWrapHTML2 . ';
            '),
            'block_level' => true,
        );          
    }
    
    // Copyright Info
    if (isset($context['current_action']) && $context['current_action'] == 'credits')
        $context['copyrights']['mods'][] = '<a href="http://dragomano.ru/page/code-highlighting" target="_blank">Code Highlighting</a> &copy; 2010&ndash;2012, Bugo';
}


tags you can use for code highlighting:

  • python
  • profile (like output form python's profiler)
  • ruby
  • perl
  • php
  • scala
  • go
  • xml (also supports HTML with inline css and javascript)
  • markdown
  • django (Django templates)
  • css
  • json
  • javascript
  • coffeescript
  • actionscript
  • vbscript
  • http (like output from curl the standard way HTTP headers as displayed in text)
  • lua
  • applescript
  • delphi
  • java
  • cpp (c++)
  • objectivec
  • vala
  • cs (c#)
  • d
  • rsl (RenderMan RSL)
  • rib (RenderMan RIB)
  • mel (Maya Embedded Language)
  • glsl
  • sql
  • smalltalk
  • lisp
  • clojure
  • ini
  • apache (like .htaccess)
  • nginx
  • diff (SVN or GIT diffs ect.)
  • dos (DOS batch files)
  • bash (.sh scripts)
  • cmake
  • axapta
  • 1c
  • avrasm (AVR Assembler)
  • vhdl
  • parser3
  • tex (TeX)
  • brainfuck (BrainFuck)
  • haskell
  • erlang
  • erlang-repl
  • rust
  • matlab
  • r
  • no-highlight (disables highlighting)



You might want to update the actual highlight script too as I've noticed there has been a few updates since this this mode went out.
https://dl.dropbox.com/u/20709011/highlight.pack.js
« Last Edit: February 19, 2013, 01:26:02 AM by Ryex »
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #25 on: February 19, 2013, 07:57:59 PM »
Code: [Select]
class A; end;
Code: (test) [Select]
class A; end;
Code: (Ruby) [Select]
class A; end;
Code: (ruby) [Select]
class A; end;
EDIT: It seems to be working except for one bug. When I used code tags without the =something part, then a weird L appears there (first example). You think you can fix that, Ryex?
« Last Edit: February 19, 2013, 08:00:38 PM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #26 on: February 19, 2013, 09:05:58 PM »
I already did in the code I posted. as I said the array with the type "unparsed_content" SHOULD NOT append the $data[1] part after the "Code:" text. only the "unparsed_equals_content" should

also, did you update the Highlight script like I suggested at the end of my last post?

I appears that as long as you use one of the tags I posted it will use that but if it doesn't recognize the tag it will auto detect anyway.
« Last Edit: February 19, 2013, 09:08:24 PM by Ryex »
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #27 on: February 19, 2013, 09:53:50 PM »
Ah, I see now *fixes* and yes, I did.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #28 on: February 19, 2013, 10:24:39 PM »
well folks. there you go, one fancy Syntax Highlighting system fresh off the presses.

you may bow and bequeath us with favors as time permits.
shouts of adoration are appreciated but we, for the most part, can not return your feelings.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #29 on: February 19, 2013, 11:24:42 PM »
can not return your feelings.

Because we are robots.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #30 on: February 20, 2013, 07:49:19 AM »
I like how when I go into the script help section and I see a code tag it's all highlighted and stuff, makes my want to do a fist pump because my brain is like "OMG YES THIS IS SO EASY TO READ!"
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #31 on: March 13, 2013, 04:09:15 PM »
I just noticed that it seems to often fail to recognize the language, especially in short code segments. I think I'll set it to Ruby by default and then using code=something would allow different highlighting for other languages.

EDIT: Wow, this was frustratingly difficult. Not because it was a difficult thing to do, but because the javascript code looks like it's been horribly unreadable on purpose. :/ The variable names are usually just one letter and the entire thing is in one single line with no spaces. Anyway, this is how it's done.

There should be a code segment like this somewhere in the beginning:

Code: (javascript) [Select]
if(e[p[o]]||p[o]=="no-highlight"){return p[o]}
When this is added at the end, it causes the certain language to become default if none was specified:

Code: (javascript) [Select]
if(p[o]==""){return "ruby"}
EDIT: Hey, Ryex, there seems to be an error in the Ruby parser. Check out this script:

http://forum.chaos-project.com/index.php/topic,2312.msg174554.html#msg174554

Notice how the parameters labeled x0, y0, etc. have their numbers highlighted as if they were literals. This is wrong. :/ You think you can fix that?
« Last Edit: March 19, 2013, 09:32:39 PM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #32 on: April 11, 2013, 08:34:03 PM »
ya no. the highlight.js parser isn't easy to follow the code is a bit wonky. you can try seeing if there is a new update for it though. (checks himself.) nope last update was 13.10.2012
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #33 on: April 11, 2013, 11:23:37 PM »
See if this can at least be reported.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #34 on: June 23, 2014, 07:04:55 AM »
There has been an update to Highlight.js http://highlightjs.org/
Release 8.0 https://github.com/isagalaev/highlight.js/releases/tag/8.0

  • auto detection has been improved
  • a bunch of styles have been improved (including ruby's)
  • new languages added
  • language aliases have been added (ie `js` for javascript and `html` which previously had to xml)


and a bunch more.

update the .js file if you would please Blizz.
« Last Edit: June 23, 2014, 07:12:44 AM by Ryex »
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #35 on: June 23, 2014, 07:47:08 AM »
I remember that I modded some things in the script. I don't think it will be just copy-paste, but I'll check it out.

EDIT: Nope, I simply can't get it to work. Replacing doesn't help and there are no instructions as to where to put the extra folders or if they need to be loaded separately. I tried using the prebuilt version, it didn't work either.
« Last Edit: June 23, 2014, 05:56:44 PM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #36 on: June 24, 2014, 05:55:21 AM »
use this https://dl.dropboxusercontent.com/u/20709011/Random%20Stuff/highlight.zip

you link the highlight.pack.js, link one of the styles from the style directory and you should be good to go.
« Last Edit: June 25, 2014, 05:07:35 AM by Ryex »
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #37 on: June 24, 2014, 08:19:57 AM »
That might work. The old file I have is also called highlight.pack.js. I'll try it out when I get home from work.

EDIT: Nope, I'm still having trouble to get things running. It removes the select-code button and just turns everything into black text. I can give you CP's source if you feel like messing around with it.
« Last Edit: June 26, 2014, 10:26:55 PM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5131
  • LV: 197
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Syntax Highligting for Code
« Reply #38 on: September 23, 2014, 06:43:34 AM »
alright. time to mess around with this. toss it here Blizz. point me to the files in question (i've kinda forgotten :P)
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19929
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Syntax Highligting for Code
« Reply #39 on: September 23, 2014, 02:02:42 PM »
Alright, I'll send it to you when I get home.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.