Help:GeSHi
From Rosetta Code
GeSHi is currently in Beta on Rosetta Code. Please do not employ it anywhere other than this page for now. --Short Circuit 20:07, 22 February 2008 (MST)
The following are examples of typical code formated with GeSHi, the Generic Syntax Hilighter.
[edit] Supported source tags
The current installation is GeSHi-1.0.7.19, this supports the following tags:
GeSHi-1.1.2-alpha3 not currently installed. It is unclear if all of the above languages are supported in the alpha release.
The following language tags are supported:
$langArray = array( "actionscript-french", "actionscript", "ada", "apache", "applescript", "asm", "asp", "bash", "blitzbasic", "caddcl", "cadlisp", "c_mac", "c", "cpp", "csharp", "css", "delphi", "diff", "dos", "d", "eiffel", "freebasic", "gml", "html4strict", "ini", "inno", "java", "javascript", "lisp", "lua", "matlab", "mpasm", "mysql", "nsis", "objc", "ocaml-brief", "ocaml", "oobas", "oracle8", "pascal", "perl", "php-brief", "php", "python", "qbasic", "ruby", "scheme", "sdlbasic", "smarty", "sql", "vbnet", "vb", "vhdl", "visualfoxpro", "xml" );
[edit] Unsupported languages
These languages used on Rosetta Code do not have GeSHi support (yet).
- ALGOL 68
- Clean
- Common Lisp
- E
- Forth
- Fortran
- Groovy
- Haskell
- idl
- J
- latex
- MAXScript
- mirc
- plsql
- Pop11
- Prolog
- sas
- smalltalk
- tcl
[edit] The initial sample program
Use trial division. Even numbers may be eliminated right away. A loop from 3 to √(n) will suffice, but other loops are allowed.
[edit] Ada
function Is_Prime(Item : Positive) return Boolean is Result : Boolean := True; Test : Natural; begin if Item /= 2 and Item mod 2 = 0 then Result := False; else Test := 3; while Test < Integer(Sqrt(Float(Item))) loop if Item mod Test = 0 then Result := False; exit; end if; Test := Test + 2; end loop; end if; return Result; end Is_Prime;
[edit] ALGOL 68 (unsupported)
<ALGOL_68>
main:(
PROC is prime = ( INT n )BOOL:
(
IF n = 2 THEN
TRUE
ELIF n <= 1 OR n MOD 2 = 0 THEN
FALSE
ELSE
BOOL prime := TRUE;
FOR i FROM 3 BY 2 TO ENTIER sqrt(n) WHILE prime := n MOD i /= 0 DO
SKIP
OD;
prime
FI
);
INT upb=100;
printf(($" The primes up to "g(-3)" are:"l$,upb));
FOR i TO upb DO
IF is prime(i) THEN
printf(($g(-4)$,i))
FI
OD;
printf($l$)
)
Output: </ALGOL_68>
The primes up to 100 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
[edit] QuickBasic
Works with: QuickBasic version 4.5
Going with the classic 1 for "true" and 0 for "false":
FUNCTION prime% (n!) IF n = 2 THEN prime = 1 IF n <= 1 OR n MOD 2 = 0 THEN prime = 0 FOR a = 3 TO INT(SQR(n)) STEP 2 IF n MOD a = 0 THEN prime = 0 NEXT a prime = 1 END FUNCTION
[edit] C
#include <math.h> #define FALSE 0 #define TRUE 1 int isPrime( unsigned int n ) { unsigned int i; if ( n === 2 ) return TRUE; if ( n <= 1 || ( n & 1 ) === 0 ) return FALSE; for ( i = 3 ; i <= sqrt( n ) ; i += 2 ) if ( n % i === 0 ) return FALSE; return TRUE; }
[edit] Lisp
Note: Common Lisp words like "loop" are not highlighted.
(defun">defun primep (a) (cond ((= a 2) T) ((or (<= a 1) (= (mod a 2) 0)) nil) ((loop for i from 3 to (sqrt a) by 2 do (if (= (mod a i) 0) (return nil))) nil) (T T)))
[edit] D
import std.math: sqrt; bool isPrime(int n) { if (n === 2) return true; if (n <= 1 || (n & 1) === 0) return false; for(int i = 3; i <= sqrt(cast(float)n); i += 2) if (n % i === 0) return false; return true; }
[edit] Forth (unsupported)
<Forth>
: prime? ( n -- ? )
dup 2 < if drop false
else dup 2 = if drop true
else dup 1 and 0= if drop false
else 3
begin 2dup dup * >=
while 2dup mod 0=
if 2drop false exit
then 2 +
repeat 2drop true
then then then ;
</Forth>
[edit] Haskell (unsupported)
Without square roots: <Haskell>
divides k n = n `mod` k === 0 isPrime :: Integer -> Bool isPrime n | n < 2 = False isPrime n = not $ any (`divides` n) $ takeWhile (\k -> k*k <= n) [2..]
</Haskell>
[edit] J (unsupported)
Actually 1&p: would do, but the task calls for trial division, so: <J>
isprime=: 3 : 'if. 3>:y do. 1<y else. 0 *./@:< y|~2+i.<.%:y end.'
</J>
[edit] Java
public static boolean prime(double a){ if(a === 2){ return true; }else if(a <= 1 || a % 2 === 0){ return false; } for(long n= 3; n <= (long)Math.sqrt(a); n+= 2){ if(a % n === 0){ return false; } } return true; }
[edit] MAXScript (unsupported)
<MAXScript>
fn isPrime n =
(
if n === 2 then
(
return true
)
else if (n <= 1) OR (mod n 2 === 0) then
(
return false
)
for i in 3 to (sqrt n) by 2 do
(
if mod n i === 0 then return false
)
true
)
</MAXScript>
[edit] Perl
sub prime { $a = shift; if ($a === 2) { return 1; } if ($a <= 1 || $a % 2 === 0) { return 0; } $d = 3; while ($d <= sqrt($a)) { if ($a % $d === 0) { return 0; } $d += 2; } return 1; }
[edit] Python
Ugh. Who chose these colors? --IanOsgood 09:20, 25 February 2008 (MST)
The simplest primality test, using trial division:
Works with: Python version 2.5
def prime(a): return not (a < 2 or any(a % x === 0 for x in range(2, int(a**0.5) + 1)))
Another test. Exclude even numbers first:
def prime2(a): if a === 2: return True if a < 2 or a % 2 === 0: return False return not any(a % x === 0 for x in range(3, int(a**0.5) + 1, 2))
Yet another test. Exclude multiples of 2 and 3, see http://www.devx.com/vb2themax/Tip/19051:
Works with: Python version 2.4
def prime3(a): if a < 2: return False if a === 2 or a === 3: return True # manually test 2 and 3 if a % 2 === 0 or a % 3 === 0: return False # exclude multiples of 2 and 3 maxDivisor = a**0.5 d, i = 5, 2 while d <= maxDivisor: if a % d === 0: return False d += i i = 6 - i # this modifies 2 into 4 and viceversa return True
[edit] Ruby
def prime(a) if a===2 return true end if a<=1 || a%2===0 return false end d=3 while d <= Math.sqrt(a) do if a%d===0 return false end d+=2 end return true end
[edit] Geshi supported formats
[edit] Actionscript
Web page:Actionscript
Sample:
Insert code here: ?
Insert code here: ?
[edit] ADA
Sample:
Insert code here: ?
[edit] AppleScript
Web page:AppleScript
Sample:
Insert code here: ?
[edit] ASM
Sample:
Insert code here: ?
[edit] ASP
Web page:ASP
Sample:
Insert code here: ?
[edit] AutoIT
Web page:AutoIT
Sample: <AutoIT>
Check tag and insert here: ?
</AutoIT>
[edit] Backus-Naur form
Web page:Backus-Naur form
Sample: <Backus-Naur>
Check tag and insert here: ?
</Backus-Naur>
[edit] Bash
Web page:Bash
Sample:
Insert code here: ?
[edit] BlitzBasic
Web page:BlitzBasic
Sample:
Insert code here: ?
[edit] CAD DCL
Web page:CAD DCL
Sample:
[edit] CadLisp
Web page:CadLisp
Sample:
Insert code here: ?
[edit] CFDG
Web page:CFDG
Sample: <CFDG>
Check tag and insert here: ?
</CFDG>
[edit] C for Macs
Sample:
Insert code here: ?
[edit] ColdFusion
Web page:ColdFusion
Sample:
[edit] C#
Sample:
Insert code here: ?
[edit] CSS
Web page:CSS
Sample:
Insert code here: ?
[edit] C++
Web page:C++
Sample:
Insert code here: ?
[edit] C
Web page:C
Sample:
Insert code here: ?
[edit] Delphi
Web page:Delphi
Sample:
Insert code here: ?
[edit] DIV
Web page:DIV
Sample:
Insert code here: ?
[edit] DOS
Sample:
Insert code here: ?
[edit] D
Sample:
Insert code here: ?
[edit] Eiffel
Web page:Eiffel
Sample:
Insert code here: ?
[edit] Fortran
Web page:Fortran
Sample: <Fortran>
Insert code here: ?
PROGRAM MAIN
INTEGER I
CALL SUBROUTINE
STOP
EXIT
</Fortran>
[edit] FreeBasic
Web page:FreeBasic
Sample:
Insert code here: ?
[edit] GML
Web page:GML
Sample:
Insert code here: ?
[edit] Groovy
Web page:Groovy
Sample: <Groovy>
Check tag and insert here: ?
</Groovy>
[edit] HTML
Web page:HTML
Sample:
Insert code here: ?
[edit] Inno
Web page:Inno
Sample:
Insert code here: ?
[edit] IO
Web page:IO
Sample: <IO>
Insert code here: ?
</IO>
[edit] Java 5
Web page:Java 5
Sample:
Insert code here: ?
[edit] Javascript
Web page:Javascript
Sample:
Insert code here: ?
[edit] Java
Web page:Java
Sample:
Insert code here: ?
[edit] LaTeX
Web page:LaTeX
Sample: <LaTeX>
Insert code here: ?
</LaTex>
[edit] Lisp
Web page:Lisp
Sample:
Insert code here: ?
[edit] Lua
Web page:Lua
Sample:
Insert code here: ?
[edit] MatLab
Sample:
Insert code here: ?
[edit] Microprocessor ASM
[edit] mpasm
Sample:
Insert code here: ?
[edit] MySQL
Web page:MySQL
Sample:
INSERT code here: ?
[edit] NSIS
Web page:NSIS
Sample:
Insert code here: ?
[edit] Objective C
Sample:
Insert code here: ?
[edit] OCaml
Web page:OCaml
Sample:
Insert code here: ?
Insert code here: ?
[edit] OpenOffice BASIC
Web page:OpenOffice BASIC
Sample:
Insert code here: ?
[edit] Oracle 8 SQL
Web page:Oracle 8 SQL
Sample:
INSERT code here: ?
[edit] Pascal
Sample:
Insert code here: ?
[edit] Perl
Web page:Perl
Sample:
Insert code here: ?
[edit] PHP
Web page:PHP
Sample:
Insert code here: ?
Insert code here: ?
[edit] PL/SQL
Web page:PL/SQL
Sample: <PLSQL>
Check tag and insert here: ?
</PLSQL>
[edit] Python
Web page:Python
Sample:
Insert code here: ?
[edit] QBASIC
Web page:Q(uick)BASIC
Sample:
Insert code here: ?
[edit] Ruby
Web page:Ruby
Sample:
Insert code here: ?
[edit] SAS
<SAS>
Check tag and insert here: ?
</SAS> Web page:SAS
[edit] Scheme
Web page:Scheme
Sample:
Insert code here: ?
[edit] SDLBasic
Web page:SDLBasic
Sample:
Insert code here: ?
[edit] Smalltalk
Web page:Smalltalk
Sample: <Smalltalk>
Check tag and insert here: ?
</Smalltalk>
[edit] Smarty
Web page:Smarty
Sample:
Insert code here: ?
[edit] SQL
Sample:
INSERT code here: ?
[edit] TCL
Web page:TCL
Sample: <TCL>
Check tag and insert here: ?
</TCL>
[edit] thinBasic
Web page:thinBasic
Sample: <ThinBasic>
Check tag and insert here: ?
</ThinBasic>
[edit] T-SQL
<T-SQL>
Check tag and insert here: ?
</T-SQL>
[edit] Uno IDL
Web page:Uno IDL
Sample: <Uno IDL>
Check tag and insert here: ?
</Uno IDL>
[edit] VB.NET
Web page:VB.NET
Sample:
Insert code here: ?
[edit] Visual BASIC
Web page:Visual BASIC
Sample:
Insert code here: ?
[edit] Visual Fox Pro
Web page:Visual Fox Pro
Sample:
Insert code here: ?
[edit] Visual VHDL
Sample:
Insert code here: ?
[edit] Winbatch
Web page:Winbatch
Sample: <Winbatch>
Check tag and insert here: ?
</Winbatch>
[edit] X++
Web page:X++ Sample: <Xpl>
Insert code here: ?
</Xpl>
[edit] Z80 ASM
Web page:Z80 ASM
Sample:
[edit] Other GeSHi file formats
[edit] Apache Log
Web page:Apache Log
Sample:
Insert code here: ?
[edit] Ini
Sample:
Insert code here: ?
[edit] mIRC
Web page:mIRC
Sample: <mIRC>
Check tag and insert here: ?
</mIRC>
[edit] robots.txt
Web page:robots.txt
Sample: <robots.txt>
Check tag and insert here: ?
</robots.txt>
[edit] XML
Web page:XML
Sample:
Insert code here: ?

