Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

RonW's scratchpad

by RonW (Deacon)
on Mar 31, 2014 at 20:07 UTC ( #1080445=scratchpad: print w/ replies, xml ) Need Help??

This script will look for files with the extension .story (this can be changed) then generate a list of links. The resulting links will run with a path to the story. As a bonus, it also creates links to Perl scripts.

use strict; use warnings FATAL => qw( all ); use File::Find; my $baseURL = '//'; my @directories_to_search = qw{ /path/to/writings /path/to/musings }; my @stories; my @paths; my @scripts; sub wanted { if (/[.]story$/) { s/[.]story$//; push @stories, $_; push @scripts, $File::Find::name; } if (/[.]pl$/) { push @paths, $File::Find::name; } } find(\&wanted, @directories_to_search); for (@stories) { my $p = shift @paths; # format into links (or whatever your menu generator needs) my $l = "<a href='$baseURL/$p'>$_</a>"; # For now, just print print "$l\n"; } for (@scripts) { my $l = "<a href='$baseURL/$_'>$_</a>"; print "$l\n"; }

The following script can probably be invoked as:

where story.txt is the name of a file containing a story.

Warning: Because the script is very basic, the story file name must be simple. No spaces, slashes or other "interesting" punctuation.

In the script, you will need to edit the name of the default story file and the path to the directory where the stories are.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib 'files/lib'; use Base::HTML qw(html story); use Base::HTML::LineMagic qw($line_magic); use Base::Roots qw(get_root); # Get requested story name (or use default story) my $story = $ENV{QUERY_STRING} ? $ENV{QUERY_STRING} : 'default.txt'; # Make path to story file my $sfile = '/path/to/stories/' . $story; my $root_link = get_root('link'); my $root_name = get_root('name'); $line_magic->{'name'} = qq(A<$root_name|href="$root_link">); $line_magic->{'bare'} = qq(A<$root_link|href="$root_link">); if (-r $sfile) { open my $sf, '<', $sfile or die "open failed: $story: $!\n"; html( 'heading' => $root_name, 'code' => sub { story($sf, { 'line +magic' => $line_magic }) } ); } else { html( 'heading' => $root_name, 'code' => sub { story(*DATA, { 'lin +e magic' => $line_magic }) } ); } __DATA__ We are sorry, but the story you requested cannot be found. Please go back and try another selection.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-10-04 12:24 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (102 votes), past polls