shBrushRust.jsの配布
ブログにRust言語の記事を書こうとしたところ、SyntaxHighlighterがRust言語に対応していないことに気付きました。このままではRust言語のコードの色分けが出来ません。
しょうがないのでSyntaxHighlighterをRust言語に対応させるためのshBrushRust.jsを自分で書きました。
バージョン3.0.83と3.0.9で動作確認しています。
せっかくなのでSyntaxHighlighterの作者にプルリクエストを送ろうかと思ったのですが、大規模アップデートの途中で開発が止まっているという悲惨な状況だったのでやめました。
代わりにここで配布します(コピペしてください。)。Rust 1.0のリファレンスを元に作ったものなので自由に使って結構です。
;(function() {
// CommonJS
SyntaxHighlighter = SyntaxHighlighter || (
typeof require !== 'undefined' ? require('shCore').SyntaxHighlighter
: null
);
function Brush() {
var datatypes = 'u8 i8 u16 i16 u32 i32 u64 i64 f32 f64 ' +
'isize usize bool char str Box ' +
'Option Result String Vec Some None Ok Err';
var keywords = 'abstract alignof as become box ' +
'break const continue crate do ' +
'else enum extern false final ' +
'fn for if impl in ' +
'let loop macro match mod ' +
'move mut offsetof override priv ' +
'proc pub pure ref return ' +
'Self self sizeof static struct ' +
'super trait true type typeof ' +
'unsafe unsized use virtual where ' +
'while yield';
var functions = 'drop default from';
var macros = 'assert assert_eq cfg column concat concat_idents ' +
'debug_assert debug_assert_eq env file format ' +
'format_args include include_bytes include_str ' +
'line module_path option_env panic print println ' +
'scoped_thread_local select stringify thread_local ' +
'try unimplemented unreachable vec write writeln';
this.regexList = [
{ regex: SyntaxHighlighter.regexLib.singleLineCComments,
css: 'comments' },
{ regex: SyntaxHighlighter.regexLib.multiLineCComments,
css: 'comments' },
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,
css: 'string' },
{ regex: SyntaxHighlighter.regexLib.singleQuotedString,
css: 'string' },
{ regex: /^ *#\!\[[^\]]+\]/gm,
css: 'preprocessor' },
{ regex: new RegExp(this.getKeywords(datatypes), 'gm'),
css: 'color1 bold' },
{ regex: new RegExp('(' + this.getKeywords(macros) + ')\!', 'gm'),
css: 'color2 bold' },
{ regex: new RegExp(this.getKeywords(functions), 'gm'),
css: 'functions bold' },
{ regex: new RegExp(this.getKeywords(keywords), 'gm'),
css: 'keyword bold' }
];
};
Brush.prototype = new SyntaxHighlighter.Highlighter();
Brush.aliases = ['rust', 'rs'];
SyntaxHighlighter.brushes.Rust = Brush;
// CommonJS
typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();
下のように使います(Rust言語公式サイトトップページのサンプルを引用しました。)。
<script src="shCore.js"></script>
<script src="shBrushRust.js"></script>
<pre class="brush: rust;">
// This code is editable and runnable!
fn main() {
// A simple integer calculator:
// `+` or `-` means add or subtract by 1
// `*` or `/` means multiply or divide by 2
let program = "+ + * - /";
let mut accumulator = 0;
for token in program.chars() {
match token {
'+' => accumulator += 1,
'-' => accumulator -= 1,
'*' => accumulator *= 2,
'/' => accumulator /= 2,
_ => { /* ignore everything else */ }
}
}
println!("The program \"{}\" calculates the value {}",
program, accumulator);
}
</pre>
<script>
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all();
</script>
// This code is editable and runnable!
fn main() {
// A simple integer calculator:
// `+` or `-` means add or subtract by 1
// `*` or `/` means multiply or divide by 2
let program = "+ + * - /";
let mut accumulator = 0;
for token in program.chars() {
match token {
'+' => accumulator += 1,
'-' => accumulator -= 1,
'*' => accumulator *= 2,
'/' => accumulator /= 2,
_ => { /* ignore everything else */ }
}
}
println!("The program \"{}\" calculates the value {}",
program, accumulator);
}
これでひとまずは大丈夫でしょうが、このまま開発が止まっているようでしたらSyntaxHighlighter以外に乗り換えた方が良いかもしれませんね。
SyntaxHighlighterの代わりとしてはhighlight.jsが候補に挙がりそうです。highlight.jsは行番号を表示できないなど、機能が少ない代わりに高速で動作するようです。こちらはRust言語にある程度対応していますが、Rust 1.0には未対応のようです。
また、Rust言語公式サイトではAce Editorが使われています。Ace Editorは色分け機能もありますが、コードの表示よりも編集に適しているので少し用途が違います。こちらもRust言語にある程度対応していますが、Rust 1.0には未対応のようです。