GeSHi syntax highlighting for nearly 200 programming languages

  • Thread starter Thread starter Greg Bernhardt
  • Start date Start date
  • Tags Tags
    Programming
Click For Summary
SUMMARY

Physics Forums has implemented GeSHi syntax highlighting for nearly 200 programming languages, enhancing the code-sharing experience. Users can activate this feature by clicking the button in the editor, selecting the code option, and specifying the programming language in the first bbcode tag. Proper syntax is crucial; for instance, spaces around the '=' in the opening code tag will disrupt highlighting, while correct usage ensures clarity and readability of code snippets.

PREREQUISITES
  • Familiarity with bbcode formatting
  • Understanding of GeSHi syntax highlighting
  • Basic knowledge of programming languages supported by GeSHi
  • Experience with code editors and their functionalities
NEXT STEPS
  • Explore GeSHi documentation for advanced syntax highlighting options
  • Learn about bbcode variations and their applications in forums
  • Research best practices for sharing code snippets in online discussions
  • Investigate common pitfalls in syntax highlighting and how to avoid them
USEFUL FOR

Developers, educators, and forum moderators who share code snippets and seek to improve readability and engagement in programming discussions.

Messages
19,907
Reaction score
10,910
Physics Forums now supports GeSHi syntax highlighting for nearly 200 programming languages. Simply hit the </> button on the editor. Select the code option, use general code, type in your code. Then in the first bbcode tag put in the language you want to use. For example:

[code=perl]
Perl:
use strict;

# Read some lines, concat them together (separated by a space), and print them.
# How exciting. Stops when it reaches the indicated count, the
# end of file, or the line STOP!

# Read the number.
print "How many lines? ";
my $num = <STDIN>;
$num > 0 or die "Num must be positive. You entered $num";

# Read in the strings.
my $accum = "";
my $sep = "";
while(my $line = <STDIN>) {
chomp $line;
if($line eq "STOP!") { last; }
$accum = "$accum$sep$line";
if(--$num == 0) { last; }
$sep = " ";
}

# Print what we got.
print "$accum\n";

[code=ruby]
Ruby:
# Double-quoted strings can substitute variables.
a = 17
print "a = #{a}\n";
print 'a = #{a}\n';

print "\n";

# If you're verbose, you can create a multi-line string like this.
b = <<ENDER
This is a longer string,
perhaps some instructions or agreement
goes here. By the way,
a = #{a}.
ENDER

print "\n[[[" + b + "]]]\n";

print "\nActually, any string
can span lines. The line\nbreaks just become
part of the string.
"

print %Q=\nThe highly intuitive "%Q" prefix allows alternative delimiters.\n=
print %Q[Bracket symbols match their mates, not themselves.\n]

[code=java]
Java:
//sample code to write 100 random ints to a file, 1 per line

import java.io.PrintStream;
import java.io.IOException;
import java.io.File;

import java.util.Random;

public class WriteToFile
{    public static void main(String[] args)
    {    try
        {    PrintStream writer = new PrintStream( new File("randInts.txt"));
            Random r = new Random();
            final int LIMIT = 100;

            for(int i = 0; i < LIMIT; i++)
            {    writer.println( r.nextInt() );
            }
            writer.close();
        }
        catch(IOException e)
        {    System.out.println("An error occurred while trying to write to the file");
        }
    }
}

[code=javascript]
JavaScript:
function calculate(fld, price) {
  var dir = fld.name.[B]charAt[/B](1);
  var num = fld.name.[B]charAt[/B](2);
  var quant = fld.[B]options[/B][fld.[B]selectedIndex[/B]].[B]value[/B];
  var subtotal = [B]eval[/B](quant * price);
  var total = 0;
 [B]eval[/B]('document.order.t' + dir + num).value = [B]fix[/B](subtotal);

  for (i = 1; i < 8; i++) {
  var itemTotal = [B]eval[/B]('document.order.t' + dir + i).value;

  if ([B]parseFloat[/B](itemTotal) > 0) total += [B]parseFloat[/B](itemTotal);
  }

 [B]eval[/B]('document.order.total' + dir).value = total;
}

function fix(total) {
 // var dollars = [B]parseInt[/B](total);
  var dollars = Math[B].floor[/B](total);
 // var cents = total - dollars;
  var cents = (total * 100) - (dollars * 100); [B]//[/B] browsers sometimes have rounding errors

  cents = Math[B].round[/B]( cents);

  if (cents < 10) cents = "0" + cents;
  if (cents == 100) dollars++;
  if (dollars == total || dollars == Math.[B]floor[/B](total) + 1) cents = "00";

  total = dollars + "." + cents;
 [B]return[/B] total;
}

Enjoy :)
 
Last edited by a moderator:
  • Like
Likes   Reactions: Wrichik Basu
Physics news on Phys.org
In the opening code tag, spaces around the '=' break the syntax highlighting.

Broken example:
[code = c]printf("Hi there");[/code]

Working example (no spaces around '='):
C:
printf("Hi there");
 
  • Like
Likes   Reactions: Wrichik Basu

Similar threads

Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
8K