forked from saffire/website
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added code-highlighting for the code examples
Richard created a "simple" script that will tokenize the code and returns a highlighted string. I've included this script outside the webroot, for future usage.
- Loading branch information
Berry Langerak
committed
Sep 18, 2012
1 parent
175597a
commit 4f16a97
Showing
6 changed files
with
187 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
use io; | ||
io.print("Hello world"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use io; | ||
|
||
$a = 1; | ||
$a += 1; | ||
|
||
io.print("The value of $a: ", $a , "\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use io; | ||
|
||
class Foo { | ||
const FOOBAR = "foobar"; | ||
|
||
protected property $a = 1; | ||
|
||
public method ctor() { | ||
// Constructor | ||
} | ||
|
||
public method foo() { | ||
return $a; | ||
} | ||
} | ||
|
||
$f = Foo(); | ||
io.print ("The value is: ", $f.foo()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* This is a script that can highlight Saffire-syntax, built by Richard van | ||
* Velzen (https://github.com/rvanvelzen). Many thanks. | ||
*/ | ||
$script = array_shift($argv); | ||
|
||
if(count($argv) === 0) { | ||
printf("Usage: php %s <input>\n", $script); | ||
exit; | ||
} | ||
|
||
$input = array_shift( $argv ); | ||
if(!is_file($input) || !is_readable($input)) { | ||
printf( "File \"%s\" is not readable. Usage: php %s <input>\n", $input, $script ); | ||
exit; | ||
} | ||
|
||
echo highlightSaffire(file_get_contents($input)); | ||
|
||
/** | ||
* A function that can highlight a string that contains Saffire syntax. | ||
* | ||
* @param string $text | ||
* @author Richard van Velzen https://github.com/rvanvelzen | ||
*/ | ||
function highlightSaffire($text) { | ||
$pointer = 0; | ||
$length = strlen($text); | ||
$output = array(); | ||
|
||
$ST_REGEX = 0; | ||
$ST_DIV = 1; | ||
|
||
$state = $ST_REGEX; // 0 is st_regex, 1 is st_div | ||
|
||
while ($pointer < $length) { | ||
if (!preg_match('~(?x) | ||
(?P<comment> //.* | /\*([^*]|\*[^/])*\*/ ) | ||
| (?P<keyword> \b (?: | ||
if | else | use | as | do | for | foreach | switch | class | extends | ||
| implements | abstract | final | interface | const | static | public | ||
| private | protected | method | readonly | property | catch | finally | ||
| throw | return | break | breakelse | continue | try | default | goto | ||
| case | self | parent | yield | in | ||
) \b ) | ||
| (?P<operator> (?: [<>]{2} | [-+*/%&|^\~<>=!] ) = | [|&<>+-]{2} | [][+%<>(){}:;=,.?!*^|-] | ||
' . ($state === $ST_DIV ? ' | / ' : '') . ') | ||
| (?P<regex> ' . ($state === $ST_DIV ? '(?!)' : ' | ||
/[^/\\\\]*(?:\\\\.[^/\\\\]*)*/[a-z]* | ||
' ) . ') | ||
| (?P<identifier> ::\+ | [$a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* | [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* [?!]? ) | ||
| (?P<string> "[^"]*" | \'[^\']*\' ) | ||
| (?P<whitespace> \s+ ) | ||
| (?P<number> \d+ ) | ||
| (?P<error> [\s\S] ) | ||
~u', $text, $match, null, $pointer)) { | ||
break; | ||
} | ||
|
||
$lexeme = htmlspecialchars($match[0]); | ||
|
||
$type = null; | ||
foreach (array( | ||
'comment' => array('comment', null), | ||
'keyword' => array('keyword', $ST_REGEX), | ||
'identifier' => array('identifier', $ST_DIV), | ||
'string' => array('string', $ST_DIV), | ||
'number' => array('number', $ST_DIV), | ||
'operator' => array('operator', $ST_REGEX), | ||
'regex' => array('regex', $ST_DIV), | ||
'whitespace' => array(null, null) | ||
) as $index => $class) { | ||
if (isset($match[$index]) && strlen($match[$index])) { | ||
list($class, $newState) = $class; | ||
$type = $class; | ||
|
||
if ($newState !== null) { | ||
$state = $newState; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
$output[] = $type ? '<span class="' . $type . '">' . $lexeme . '</span>' : $lexeme; | ||
$pointer += strlen($match[0]); | ||
} | ||
|
||
return '<pre class="source saffire">' . implode($output) . '</pre>'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters