#!/usr/local/bin/perl use strict; use warnings; use File::Temp qw(tempdir tempfile); use List::Util qw(reduce sum); my $tabwidth = 4; # Open/fetch our JS file. my $js_file = pop @ARGV; my $doc_name; ($doc_name = $js_file ) =~ s{.+/}{}; $doc_name =~ s/\..+//; # Fetch my JavaScript my $js; if ( -f $js_file ) { # Open my real JavaScript file. open my $js_fh, '<', $js_file or die "Can't open $js_file: $!"; local $/; $js = <$js_fh>; } else { # Use the given filename as a URL instead. require LWP::Simple; $js = LWP::Simple::get( $js_file ); } # Some *awesome* JavaScript parsing. my @pod = map { unindent( $_ ) . "\n\n=cut\n\n" } grep { defined and /\S/ } $js =~ m{/(?:\*(.*?)\*/|/([^\n]))}gxs; unshift @pod, "=cut\n\n"; # Go make a temporary pod file. my $pod_dir = tempdir( CLEANUP => 1 ); my $pod_file = "$pod_dir/$doc_name.pod"; open my $pod_fh, '>', $pod_file or die "Can't create $pod_file: $!"; END { unlink $pod_file } print {$pod_fh} @pod or die "Can't write to $pod_file: $!"; close $pod_fh or die "Can't close $pod_file: $!"; # Run our pod file under perldoc. system 'perldoc', @ARGV, $pod_file; exit; sub unindent { local $_ = shift @_; # Untabify s/\t/' ' x $tabwidth/ge; # Remove trailing whitespace s/\s+\z//; # Unindent by finding the largest quantity of removeable # whitespace. my %leading_space; ++ $leading_space{length $1}{count} while m/^( +)/mg; return $_ unless keys %leading_space; my @indents = sort { $a <=> $b } keys %leading_space; # Compute the space removed for each indentation level. for my $indent_ix ( 0 .. $#indents ) { my $indent = $indents[$indent_ix]; my $lines = 0; $lines += $leading_space{$indents[$_]}{count} for $indent_ix .. $#indents; $leading_space{$indent}{space} = $lines * $indent; } # Select the indentation with the most area removed. my $suggested_padding = reduce { my $current_space = $leading_space{$a}{space}; my $new_space = $leading_space{$b}{space}; $current_space > $new_space ? $a : $b; } ( @indents ); # Remove that much from the start of every line. s/^ {0,$suggested_padding}//mg; return $_; }

In reply to jsdoc - pod for JavaScript by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.