Gossamer Forum
Home : General : Perl Programming :

Formatting of SSI .txt files

Quote Reply
Formatting of SSI .txt files
Is it possible (using Links 2.0 BTW - but I believe this is general CGI knowledge question) to format a server side included .txt file?

ie. line breaks

My troubles are with an actual .txt file that has line breaks, but when called into the page it is all bunched together. Help?

Quote Reply
Re: Formatting of SSI .txt files In reply to
This is my next bug to work out so I'm bumping up this message.

Overview of what I am doing:
I have a links 2.0 directory that accepts .txt files that are then called into a .shtml page via SSI

Output of the .txt file is bunched together regardless of line breaks (hard returns)

It is a similar problem to that stated in the following thread - but out put from the .txt file of course so the defined solution would not work.

http://www.gossamer-threads.com/...mp;part=all&vc=1

Is there any other course of action/code that can make hard returns into < br > (no spacing) or <p> html codes?

Quote Reply
Re: Formatting of SSI .txt files In reply to
Use \n.

Regards,

Eliot Lee
Quote Reply
Re: Formatting of SSI .txt files In reply to
I know that \n is involved somehow - I am just wondering how to implement it (is it links 2.0 specific? Do I use a style sheet? etc)

I cannot use the suggested solutuion in the above thread because the txt files are not generated by an input text field, they are uploaded then called via ssi.

suggestions now?

Quote Reply
Re: Formatting of SSI .txt files In reply to
I don't know how links works but could you have something like this?

#name of file includetext.cgi
#!/usr/bin/perl

$textpath = '/path/to/textdir';
$textfile = "text.txt";

open (FILE,"$textpath/$textfile") or die "Can't open file Reason:$!";
@textssi = <FILE>;
close(FILE);

print "Content-type: text/html\n\n";
foreach $line(@textssi) {
$line =~ s/\n/<BR>/;
print qq| $line |;
}
exit;

and then call includetext.cgi in a similar way to your existing text file.

Code:
!--#exec cgi="/cgi-bin/includetext.cgi" -->
(Beginning arrow missed off, I can't seem to display it)

rog


Quote Reply
Re: Formatting of SSI .txt files In reply to
I can't use that because the txt files are uploaded by my users. I can't arbitraily name a cgi file that would match all the uploads

Quote Reply
Re: Formatting of SSI .txt files In reply to
Sure you can...

Change the codes provided to the following:

Code:

#name of file includetext.cgi
#!/usr/bin/perl

eval {
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script location: UNIX /
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script location: Windows \

require "/path/to/links.cfg"; # Change this to full path to links.cfg if you have problems.
require "$db_lib_path/db_utils.pl";
require "$db_lib_path/links.def";
$build_use_templates ?
require "$db_lib_path/site_html_templates.pl" :
require "$db_lib_path/site_html.pl";
};

if ($@) {
print "Content-type: text/plain\n\n";
print "Error including libraries: $@\n";
print "Make sure they exist, permissions are set properly, and paths are set correctly.";
exit;
}
sub main {
# --------------------------------------------------------
local (%in) = &parse_form();
my (%rec) = &get_record ($in{'ID'});
open (FILE,"$build_attach_path/$rec{'Image'}") or die "Can't open file Reason:$!";
@textssi = <FILE>;
close(FILE);

print "Content-type: text/html\n\n";
foreach $line(@textssi) {
$line =~ s/\n/<BR>/;
print qq| $line |;
}
exit;
}


Replace $rec{'Image'} with whatever the name of your upload field's name is.

Then add the following codes in your detailed.html file:

Code:

!--#exec cgi="/cgi-bin/includetext.cgi?ID=<%ID%>" -->


Regards,


Eliot Lee
Quote Reply
Re: Formatting of SSI .txt files In reply to
It must be me, but nothing ever works right away.

Here's what's happening and my idea why it doesn't go:

the nasty [ an error occurred while processing this directive ] came up using the exec cgi call

using the exec cmd call it returns nothing

I checked sysntax - it's fine - the line I believe that may need to be changed is:

open (FILE,"$build_attach_path/$rec{'Story'}") or die "Can't open file Reason:$!";

I have the build_attach_path defined correctly in the links.cfg file but correct me if I'm wrong - the field 'Story' is written out as http://www.myurl.com/linksdirectory/Attach/ID#-filename.txt

where ID# is obviously the actual number of the ID - 10 etc.

Therefore the logical answer (for me) is to use:

open (FILE,"$rec{'Story'}") or die "Can't open file Reason:$!";

But that doesn't work either

I've also tried every ssi call available - include virtual, exec cmd, exec cgi etc

ideas?


Quote Reply
Re: Formatting of SSI .txt files In reply to
Try changing the following codes:

Code:

open (FILE,"$rec{'Story'}") or die "Can't open file Reason:$!";


to the following:

Code:

open (FILE,"$build_attach_path/$rec{'ID'}-filename.txt") or die "Can't open file Reason:$!";


You need to include absolute paths when opening files not relative paths.

Replace filename with whatever field in your database is the name of the file that is uploaded.

Regards,

Eliot Lee
Quote Reply
Re: Formatting of SSI .txt files In reply to
nope - same errors (even when I name the 'filename' to an exact file that has been uploaded)

If it did work (well, it couldn't because the filenames are specified by the uploaders and are no way controlled by me).

Further ideas?

(BTW - I'm reading a pretty interesting anthro-type book on the Guarani by Reed - Forest Dwellers, Forest Protectors...pretty interesting stuff...u familiar?)