Web::Scraperを使って、PHPの関数一覧をスクレイプする。

php-completion.elを作っていて、PHPの関数一覧が必要になったので、php.netからスクレイプするスクリプトWeb::Scraper を使って書いた。

xpathに疎い自分ですが、cssセレクターで指定できたので無問題でした。素晴らしすぎる。

use strict;
use warnings;

use List::MoreUtils qw(uniq);
use Web::Scraper;
use URI;

my $functions = scraper {
    process "div#content div.index a.function", 'functions[]' => sub {
        my $text = $_->as_text or return;
        return $text;
    };
    result 'functions';
}->scrape(URI->new('http://jp.php.net/manual/ja/indexes.php'));

map {
    s/\(\)$//;
    s/.*->(.*)$/$1/;
    s/.*::(.*)$/$1/;
} @$functions;

print join "\n", uniq @$functions;

このスクリプトをscrape-funcs.plとか適当な名前で保存して、Emacsの中から

(let ((s (shell-command-to-string "perl scrape-funcs.pl"))
      (insert (lambda (s) (insert (format "%S" s) "\n"))))
  (mapcar insert (split-string s "\n")))

を実行して、プログラムを書かせた。