Skip to content

Instantly share code, notes, and snippets.

@yanick
Created August 3, 2015 23:02
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 yanick/ecc260879b1bd60b4d0d to your computer and use it in GitHub Desktop.
Save yanick/ecc260879b1bd60b4d0d to your computer and use it in GitHub Desktop.
Get the parsed jsdoc, then generate a text version of the document
#!/usr/bin/perl
use 5.20.0;
package JSDoc::Viewer;
use Moose;
use Template::Caribou;
with 'Template::Caribou';
use MooseX::App::Simple;
use JSON qw/ from_json /;
use Path::Tiny;
use experimental 'signatures', 'postderef';
parameter source => (
is => 'ro',
required => 1,
);
has doc => (
is => 'ro',
lazy => 1,
default => sub ($self){
open my $convert, '-|', 'jsdoc-parse ' . $self->source
or die $!;
from_json( join '', <$convert> );
},
);
sub run($self) {
say $self->render('doc');
}
template doc => sub($self) {
show( function => $_ ) for grep { $_->{kind} eq 'function' } $self->doc->@*;
};
template function => sub($self,$func) {
say '';
show( func_name => $func );
show( func_params => $func );
say "\n", $func->{description};
};
sub print_params($func) {
return unless $func->{params};
join '', map { "( $_ )" } join ', ', map { $_->{name} } $func->{params}->@*;
}
template func_name => sub($self,$func) {
no warnings;
print $func->{name} . print_params($func) ."\n";
};
template func_params => sub( $self, $func ){
for ( eval { $func->{params}->@* } ) {
print "\t", $_->{name},
( $_->{type} ? "[ ". $_->{type}{names}[0] . " ]" : '' ) . " - $_->{description}\n"
}
};
path('foo.js')->spew(join '', <DATA>);
__PACKAGE__->new( source => 'foo.js' )->run;
1;
__DATA__
/**
* Does something regretable
* @example
* var pity = foo();
* */
function foo() {
}
/**
*
* Could be a long, long description of that function.
*
* @param {int} max - goes up to eleven
*/
function bar(i){ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment