Created
August 3, 2015 23:02
-
-
Save yanick/ecc260879b1bd60b4d0d to your computer and use it in GitHub Desktop.
Get the parsed jsdoc, then generate a text version of the document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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