Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
use strict; #### #### comment_reader.pl #### #### Reads a comment file from command-line arg or standard input #### and outputs the result as a fragment of HTML suitable for #### Perlmonks. #### use IO::File; use Getopt::Long; use Template; ###################### Constants ###################### use constant DEFAULT_TEMPLATE => 'comment_tmpl.tt2'; ###################### Main Program ################### MAIN: { ## Get the options my $template_file = DEFAULT_TEMPLATE; my $outfile = undef; GetOptions('outfile=s' => \$outfile, 'template=s' => \$template_file +); my $entries = read_entries(); my $template = Template->new(); $template->process($template_file, { files => $entries }, $outfile) or die $Template::ERROR; } ###################### Subroutines ##################### ## ## read_entries() ## ## Reads whatever entries are present on the command line, ## then returns them parsed and sorted as a reference ## to an array of hashes, like so: ## [ # List of files ## { 'name' => ## sub read_entries { my %comments = (); while (<>) { chomp; # Bare-bones attempt at avoiding missing closing tags while (m{<(?!\/)([^>]+)>}g) { my $tag = $1; m{</$tag>}i or warn "Missing closing tag for $tag at $.\n"; } my ($file_list, $comment) = split(/\s+/, $_, 2); foreach my $lineref (split(/;/, $file_list) ) { my ($file, $lines) = split(/:/, $lineref, 2); foreach my $line (split(/,/, $lines)) { push( @{ $comments{$file}{$line} }, $comment ); } } } return sort_files(\%comments); } ## ## Given a hash reference, turns it into a list of hashrefs. ## sub sort_files { my ($comments) = @_; my @files = (); foreach my $file (sort keys %$comments) { my @lines = (); foreach my $line (sort {$a <=> $b} keys %{$comments->{$file}}) { push(@lines, { 'number' => $line, 'comments' => $comments->{$fil +e}{$line} }); } push (@files, { name => $file, lines => \@lines }); } return \@files; }

In reply to Annotating Files by stephen

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (5)
    As of 2014-09-16 09:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (158 votes), past polls