Color code

  1. Description
  2. Examples (and test)
    1. XML
    2. Ruby
    3. ECMAScript
    4. CSS
    5. PHP
    6. Io
    7. Perl

Description

class に言語を書くことによって言語を明示し、Javascript (クライアント側) でコードに色を付けてみるテスト

このサイト用のスクリプトでは pre (現状 blockcode の代わり的なもの) と code (インライン) についてやってみてる。

Examples (and test)

インライン Digest::MD5.hexdigest("str").hex, <pre><![CDATA[<foo></foo>]]></pre>

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/xml.xsl"?>
<!DOCTYPE diary SYSTEM "taglibro.dtd">
<diary xml:lang="ja" xmlns="http://lowreal.net/d/">
    <day date="2004-12-01">
        <section datetime="2004-12-01T03:26:57+09:00">
            <title>Namazu</title>
            <!--<cat>web</cat>
            <cat>soft</cat>-->
            <body>
                <p>なんだか今度は <q>Done</q> ま...</p>
                <p>正確には追加されてるみ...</p>
                <pre><![CDATA[<?xml-stylesheet type="text/xsl" href="/xml.xsl"?>]]></pre>
            </body>
        </section>
    </day>
</diary>

Ruby

#!/usr/local/bin/ruby

require "cgi"
@cgi = CGI.new

# test
:symbol; :"symbol"; :$symbol; :@@symbol; :+
@@class_variable; @instance_variable; $global_variable; $_; $&

foo = <<EOS
fdlbfdkb
gfjlkvfdkl
""bvlsdlkjvsdEOS
dfb''EOS
EOS

Foo.instance_method(:foo).call
# => -:5:in

?a; ?\C-a

%q!I said, "You said, 'She said it.'"!
%!I said, "You said, 'She sa\!id it.'"!
%Q('This is it.'\n)
%Q{'This is it.'\n}
%Q<'This <is> it.'\n> # 括弧は一重まで
#%Q<'Thi<s <is> i>t.'\n> # 括弧は一重まで
#%Q<'Thi<<s <is> i>>t.'\n> # 括弧は一重まで

begin
    # body
rescue => e
    puts @cgi.header({"type" => "text/plain"})
    puts e.message
    puts e.backtrace
end

ECMAScript

Javascript, JScript, ActionScript も同じ

/*
 * グローバルな初期化関数。
 * 全てがロードされたら実行される。
 */
function initialize() {
    addInsDelDatetime();
    addBlockquoteCite();
    addQCite();
    addListTitle();
    // document.implementation.hasFeature("StyleSheets", "2.0");
    if (document.styleSheets) enumStyleSheets();
    markupCode();
}
window.addEventListener("load", initialize, true);

CSS

@charset "utf-8";

/*
 * another-morning.css
 * (c) 2004-11-23 cho45(砂糖)
 * http://lowreal.net/
 *
 */

html {
    height: 100%;
    background: #036 url("/img/am_bg");
    color: #333;
}

body {
    padding: 13px 6.25% 0 6.25% !important;
    background: transparent url("/img/am_bg");
}

PHP

// Content-Type の設定と flavor のやつをやっちまいます。
$content_type = $default_content_type;
if ($flavor && $flavor != "") {
    if (file_exists("$xsl.$flavor.xsl")) {
        $xsl .= ".$flavor";
        if (array_key_exists($flavor, $flavor_content_type)) {
            $content_type = $flavor_content_type[$flavor];
        }
    }
    $params["flavor"] = ".$flavor";
}

Io

Object p := method(
    self print
    write("\n")
    self
)

// replace findRegexString by the value returned from aBlock
String gsubByBlock := method(findRegexString, aBlock,
    r := Regex clone setPattern(findRegexString) setString(self)
    r allMatches foreach(i, v,
        if (v type != "List", v := list(v))
        rep := aBlock(v)
        self := self replace(v at(0), rep)
    )
    self
)

ret := "あああ" gsubByBlock("[^ a-zA-Z0-9_.-]+",
    block(match,
        ret := ""
        match at(0) foreach(i, v,
            ret := ret .. "%" .. v asString toBase(16)
        )
    )
) replace(" ", "+") p

if (ifobj) = ""

"""
aaaaaa
""" print

Perl

$days               # 単純なスカラ変数 "days" の値
$days[28]           # 配列 @days の 29 番目の要素の値
$days{'Feb'}        # ハッシュ %days の 'Feb' の値
$#days              # 配列 @days の最後のインデクス値

@days               # ($days[0], $days[1],..., $days[n])
@days[3,4,5]        # @days[3..5] と同じ
@days{'a','c'}      # ($days{'a'},$days{'c'}) と同じ

%days               # (key1, val1, key2, val2,...)

LINE: while (<STDIN>) {
    while (s|({.*}.*){.*}|$1 |) {}
    s|{.*}| |;
    if (s|{.*| |) {
        $front = $_;
        while (<STDIN>) {
            if (/}/) {      # コメントの終わり
                s|^|$front{|;
                redo LINE;
            }
        }
    }
    print;
}

sub require {
    local($filename) = @_;
    return 1 if $INC{$filename};
    local($realfilename,$result);
    ITER: {
        foreach $prefix (@INC) {
            $realfilename = "$prefix/$filename";
            if (-f $realfilename) {
                $result = do $realfilename;
                last ITER;
            }
        }
        die "Can't find $filename in \@INC";
    }
    die $@ if $@;
    die "$filename did not return true value" unless $result;
    $INC{$filename} = $realfilename;
    $result;
}

$pwd = (getpwuid($<))[1];
$salt = substr($pwd, 0, 2);

system "stty -echo";
print "Password: ";
chop($word = <STDIN>);
print "\n";
system "stty echo";

if (crypt($word, $salt) ne $pwd) {
    die "Sorry...\n";
} else {
    print "ok\n";
}