1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

GeSHi syntax highlighting for nearly 200 programming languages

  1. Oct 4, 2014 #1
    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]
    Code (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]
    Code (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]
    Code (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 occured while trying to write to the file");
            }
        }
    }
    [code=javascript]
    Code (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 :)
     
  2. jcsd
  3. Oct 9, 2014 #2

    Mark44

    Staff: Mentor

    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 '='):
    Code (C):
    printf("Hi there");
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: GeSHi syntax highlighting for nearly 200 programming languages
  1. Python syntax (Replies: 5)

  2. Matlab syntax (Replies: 2)

Loading...