Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

A better Code Block

I have a problem with [code] and [php] blocks, and that problem does not entail the way this site looks, and as such - has nothing to do with the layout. I'm saying this because I know at least one of you are going to come in here yelling "No layout suggestions! It's against the rules!". I know that. As aforementioned, I believe the current code blocks are not functioning as they rightly should.

The problems I've identified are as follows:
  • Two different types: [code] and [php] (Why not one consistent option?)

  • Indentation is malformed unless the code contained within is 2-space indented

  • Only one syntax highlight option, and it highlights PHP properly, but not other languages

  • Sometimes, code is truncated into shorter syntax - "bbbb" becomes just "b"



These prove to be a pain in the ass. They really do. One other thing, and a rather personal one, as it doesn't really matter as much, is having to scroll down a very tiny viewport to see large pieces of code.

Here's what I believe should be changed:

One:
Use one consistent tag. Instead of [code] and [php], why not [code=php]? This will make it more maintainable for syntax highlighting other languages - [code=ruby], [code=python], etc. Adding support for syntax highlighting other languages will not be hard, either. There are so many client-side (and server-side) syntax highlighting libraries out there that cover hundreds of languages - and all they look for are CSS class names on HTML <code> or <pre> tags that match the language of the code in the code block. A few even automatically detect the syntax if no syntax is supplied. There are literally so many options - Google Code Prettify, Prism.js, Highlight.js, etc - and some of these libraries weigh in at under 2kb when minified and gzipped. So there really is no excuse.

Two:
A code block should not care what type of indentation the code included in the block uses, and as such, should not butcher the code therein if the syntax is not consistent with the code block's default nature. This is a standard part of any decent syntax highlighter library, so fixing number one should fix this anyway.

Three:
Adding a syntax highlighter library will add syntax highlighting support for many languages without much work involved, so fixing number one will fix this too, just like it'll fix number two. Wonderfully easy, right?

Four:
Remove this truncation. Why the hell is it even here in the first place? Am I being oblivious in thinking it's not needed? Seriously, answer me on this one - because I do not know any possible reason this is needed.

As for scrolling down large snippets of code in a tiny viewport - I don't mind it, but it sure would be SO much better not having to do that. I understand that you don't want posts to be too large on a screen (more thread pages = more bandwidth), otherwise you wouldn't have enabled [spoiler]s or made quotes automatically purge any nested quotes (if any), but, perhaps there can be a small compromise:

A simple little expand button. By default, [code] tags will have the same height viewport that they do now. But a button at the bottom with a tiny arrow pointing down or a plus icon, that, when clicked, expands the code block to the full length of the code. Of course this isn't necessary - I'm sure people would enjoy it though.

Oh, and a "select all" option that selects all of the code within a block for easy copying to clipboard. That's really all I have to say on the matter.

I hear it rumoured somewhere that this'll be too much work. If it is, and I offered to create a plugin that does this, would that make it easier on you, Omni?
Why not just paste your code on PasteBin, and link to it in the thread?
(Mar 4, 2013 08:49 PM)Cryptic™ Wrote:  Why not just paste your code on PasteBin, and link to it in the thread?

Because that is incredibly inconsistent. Here's why: There are a lot of programming tutorials on HackForums. I wouldn't be surprised if at minimum, ten new tutorials were posted each day. Now, imagine you're the reader of any one of these tutorials. What would you prefer: 1. Seeing all the instructions, explanation, and code in one place OR 2. Switching tabs between the tutorial and PasteBin?

It would be much easier to embed something like Pastebin or Github gists, but not everyone uses Pastebin or Github and as far as I'm aware they're not easy to modify in terms of style (i.e. they won't fit the theme of the site and will look out of place). Whereas with code tags, everybody on the forum knows they're there, the majority know how to use them, and they remain consistent in all areas of the layout of the site and usability.
^

This is exactly why I made my Python tutorial into a interactive Website.

My Python Tutorial - http://imtiax.net/python/

Worked out better than using code tags on HF or Pastebin.

Took a bit of work but paid off in the end

If anyone wants to see the thread https://hackforums.net/showthread.php?tid=2583101
I agree with this, we should have different syntax choices for the code.
Want Niche Edit backlinks to rank in Google? Click Here
ZeroSecurity: Latest Security News
https://x.com/zer0security
(Mar 4, 2013 09:06 PM)Cryptic™ Wrote:  ^

This is exactly why I made my Python tutorial into a interactive Website.

My Python Tutorial - http://imtiax.net/python/

Worked out better than using code tags on HF or Pastebin.

Took a bit of work but paid off in the end

If anyone wants to see the thread https://hackforums.net/showthread.php?tid=2583101

Not everyone has the ability to do that. ;)
I really support this suggestion. For the few times that I have been using the code block, I must say it was somewhat hard to make a long piece of code readable.
[Image: Pi0HBk0.gif]
I personally support this 100%, GL OP
[Image: vgx9T6b.gif]
(Mar 4, 2013 09:16 PM)~Dec Wrote:  Not everyone has the ability to do that. ;)

Even with that ability, most people wouldn't want to spend that time.

I agree with this completely, nice suggestion
This is a shortcoming of MyBB. I actually preferred their 1.4x code tag which didn't place it inside a small window.

code=php tags are no good because it assumes we'll have syntax for other languages which we don't. There is a plugin which sort of worked but that's gonna add load for parsing imho. I did consider adding it at one point though.

MyBB uses the built in functions for php syntax.
http://php.net/manual/en/function.highlight-string.php

Geshi is one of the nicer libs but I fear running it would lead to exploits. And again the load imho isn't worth it...at least not yet. I'm hoping MyBB does more for syntax highlighting in future versions.

Quote:Oh, and a "select all" option that selects all of the code within a block for easy copying to clipboard. That's really all I have to say on the matter.


I'd love to add this too but it's not very simple with Prototype (MyBB's current javascript lib). However when 1.8x is released it will use JQuery which should make it easier. If you can do it without core edits for MyBB I'll consider using your code.
I'm glad you haven't rejected the suggestion, surprised in fact. I will play around with creating a plugin to improve the code tags. For now though, I have created a userscript which implements a lot of this functionality. If anyone is interested in it, you may download it from this thread: https://hackforums.net/showthread.php?tid=3306780
I'll try and help also if you don't mind, OP. Plus I'm for this suggestion. :)
Thread: Backend Web-Dev. - by SomeWhiteGuy

WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro
Omniscient won't implement other people's work into the site, as he doesn't trust their coding skills (For the most part). Also, he has said that things like this should be taken up with MyBB to provide better default support, as Omniscient doesn't like making core modifications to HackForums.
(Mar 6, 2013 05:53 AM)RDCA Wrote:  Omniscient won't implement other people's work into the site, as he doesn't trust their coding skills (For the most part). Also, he has said that things like this should be taken up with MyBB to provide better default support, as Omniscient doesn't like making core modifications to HackForums.

He said he'd consider it if it didn't make core modifications - and it shouldn't, the way I'm thinking of doing it. As for coding skills, I got my certification four years ago, so a little bit of practice to shed away the rust should do fine.
I agree this is a problem. For now, I share my code with either Dropbox or Pastebin. On here, I usually just use Pastebin, because it is easier and less of a risk (at least how other users interpret it) than to download the source file.