Skip to content

Instantly share code, notes, and snippets.

@bduggan
Last active September 20, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bduggan/93cdaa8a03b34df9b7f71f4f632b141b to your computer and use it in GitHub Desktop.
Save bduggan/93cdaa8a03b34df9b7f71f4f632b141b to your computer and use it in GitHub Desktop.
!/usr/bin/env perl6
# pxdoc
# requires: curl, lynx (or other browser), JSON::Fast
# See: https://www.reddit.com/r/perl6/comments/9h0xy6/pxdoc_quick_hack_for_command_line_docs/
use JSON::Fast;
#| Search for Perl 6 documentation and load it in a browser.
sub MAIN (
#| A Perl 6 term you want to look up
Str:D $term
) {
my $browser = %*ENV<BROWSER> // 'lynx';
my $index = (%*ENV<XDG_CACHE_HOME> // "%*ENV<HOME>/.cache").IO.add('pxdoc').child(".p6index.js");
$index.dirname.IO.d or mkdir $index.dirname;
if (!$index.e or $index.modified.DateTime < DateTime.now.earlier(:1day)) {
shell "curl https://docs.perl6.org/js/search.js?v=3 > $index";
}
my $js = $index.slurp.subst( /^ .* 'var items ='/, '' )
.subst(/';'\s* 'var results =' .* $/, '' )
.subst('category:','"category":',:g)
.subst('value:','"value":',:g)
.subst('url:','"url":',:g)
;
my $data = from-json($js);
my @matches = $data.grep({ .<value> ~~ /:i $term /}) or exit note "no matches";
for @matches {
say ++$ ~ ": {.<value>} ( {.<category>} )"
}
my $which = prompt "Choose: " or exit;
my $match = @matches[$which-1] or exit;
my $url = 'https://docs.perl6.org' ~ trim($match<url>);
shell "$browser $url";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment