Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kelunik/highlight for syntax highlighting #8

Open
wants to merge 1 commit into
base: mdl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ backend/mirror.jpg
backend/mirror.jpg
backend/GeoIP.dat
.idea

/vendor/
5 changes: 5 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
Order allow,deny
Deny from all
</FilesMatch>

<Directory ~ "vendor">
Order allow,deny
Deny from all
</Directory>
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "php/web-php",
"description": "The www.php.net site.",
"type": "project",
"require": {
"kelunik/highlight": "^0.1.0"
}
}
132 changes: 132 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions include/header.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/* $Id$ */

$css_files = array(
'/styles/material.indigo-red.min.css',
'/styles/material.indigo-red.min.css',
'/styles/theme.css',
'/styles/highlight.css',
);

if (isset($config['css'])) {
Expand Down Expand Up @@ -53,7 +53,7 @@ if (!isset($config["languages"])) {
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>PHP: <?= $title ?></title>

Expand Down
44 changes: 14 additions & 30 deletions include/layout.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php
/* $Id$ */

use Kelunik\Highlight\Highlighter;
use Kelunik\Highlight\HtmlFormatter;
use Kelunik\Highlight\PhpLexer;

require_once __DIR__ . "/../vendor/autoload.php";

$_SERVER['STATIC_ROOT'] = $MYSITE;
$_SERVER['MYSITE'] = $MYSITE;

Expand All @@ -14,35 +20,13 @@ ini_set('highlight.html', 'html');
// Highlight PHP code
function highlight_php($code, $return = FALSE)
{
$highlighted = highlight_string($code, TRUE);

// Use this ugly hack for now to avoid code snippets with bad syntax screwing up the highlighter
if(strstr($highlighted, "include/layout.inc</b>")) {
$highlighted = '<span class="html">'. nl2br(htmlentities($code, ENT_HTML5), FALSE) ."</span>";
}

// Fix output to use CSS classes and wrap well
$highlighted = '<div class="phpcode">' . str_replace(
array(
'&nbsp;',
'<br>',
'<span style="color: ',
'</font>',
"\n ",
' ',
' '
),
array(
' ',
"<br>\n",
'<span class="',
'</span>',
"\n&nbsp;",
'&nbsp; ',
'&nbsp; '
),
$highlighted
) . '</div>';
static $highlighter = null;

if ($highlighter === null) {
$highlighter = new Highlighter(new HtmlFormatter("php"), new PhpLexer);
}

$highlighted = $highlighter->highlight($code);

if ($return) { return $highlighted; }
else { echo $highlighted; }
Expand Down Expand Up @@ -229,7 +213,7 @@ function download_link($file, $title, $showsize = TRUE, $mirror = '')
}
echo ']';
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions releases/5_3_3.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1>PHP 5.3.3 Release Announcement</h1>
<p>
The PHP development team would like to announce the immediate
availability of PHP 5.3.3. This release focuses on improving the
availability of PHP 5.3.3. This release focuses on improving the
stability and security of the PHP 5.3.x branch with over 100 bug
fixes, some of which are security related. All users are encouraged
to upgrade to this release.
Expand All @@ -23,7 +23,7 @@
non-namespaced classes.

<p><?php
highlight_string('<?php
highlight_php('<?php
namespace Foo;
class Bar {
public function Bar() {
Expand Down
28 changes: 28 additions & 0 deletions styles/highlight.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.php-variable {
color: blue;
}

.php-string,
.php-doc-comment {
color: green;
font-style: italic;
}

.php-comment {
color: grey;
font-style: italic;
}

.php-open-close {
color: grey;
font-weight: bold;
}

.php-keyword,
.php-control {
color: purple;
}

.php-number {
color: red;
}