SyntaxHighlighter を使って Apache で公開している C# ソースコードに色を付けるメモ
2. ディレクトリ構成
3. 手順
3.1 SyntaxHighlighter の配置
- http://alexgorbatchev.com/SyntaxHighlighter/
の download から syntaxhighlighter_3.0.83.zip をダウンロード - zip を解凍し、フォルダ名を syntaxhighlighter として、 C:\xampp\htdocs\src 下に配置
- C:\xampp\htdocs\src
- \syntaxhighlighter
- \scripts フォルダなど
- \syntaxhighlighter
- C:\xampp\htdocs\src
3.2 src.php の配置
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <?php if(isset($_GET['src'])) $src = $_GET['src']; ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><?php if(isset($src)) print($src) ?></title> <script type="text/javascript" src="syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="syntaxhighlighter/scripts/shBrushCSharp.js"></script> <link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript">SyntaxHighlighter.all();</script> </head> <body style="background: white; font-family: 'MS ゴシック',sans-serif;"> <h1><?php if(isset($src)) print($src) ?></h1> <script type="syntaxhighlighter" class="brush: csharp"><![CDATA[ <?php if(isset($src)) include($src); ?> ]]></script> </html>
3.3 .htaccess の配置
- Apache の modRewrite により、
http://〜/src/HelloWorld.cs へのアクセスを、
http://〜/src/src.php?src=HelloWorld.cs としてリダイレクトさせる。 - これを行うための設定ファイル .htaccess を、 C:\xampp\htdocs\src 下に配置
- C:\xampp\htdocs\src
- .htaccess のコード
Options FollowSymLinks RewriteEngine on RewriteCond %{REQUEST_URI} \.cs$ RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.+\.cs)$ /src/src.php?src=$1 [R,L]
4. 確認
http://localhost/src/test/HelloWorld.cs
にアクセスすると、上記 1. の下の画像のように表示されました。
5. 参考
追記 2012/8/10
- 別の環境*1で上のことを試すと、 modRewrite が動かなかった。なぜ??
- 認証がかかっています が参考になりそう。
- ToDo
上記の「別の環境」で modRewrite が動かない原因を探り、動くようにする。- .htaccess による modRewrite の設定は処理が遅くなるそうな。 → mod_rewrite - @bayashi Wiki
なので、 httpd.conf で設定して動くようにしてみる。
追記 2012/8/20
- 上記の「別の環境」で modRewrite が動かなかった原因は以下の2つで、対応したら動くようになった。
やはり、 認証がかかっています がとても参考になった。- .htaccess が無効だった
- mod_rewrite モジュールが無効だった
- 同じく httpd.conf を開いてみると、 mod_rewrite モジュールの読み込み行が下のようにコメントアウトされていた。
#LoadModule rewrite_module modules/mod_rewrite.so - 行頭の # を取って mod_rewrite モジュールを読み込むようにし、 Apache を再起動した。
- 同じく httpd.conf を開いてみると、 mod_rewrite モジュールの読み込み行が下のようにコメントアウトされていた。
*1:XAMPP を使わず、 Apache と PHP を個別にインストールした Windows XP 環境。