Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

HTML To ASP Converter

by patgas (Friar)
on Apr 30, 2001 at 21:48 UTC ( #76679=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info patgas
Description: This script grabs an HTML file, and converts it into a VBScript Response.Write command for use in ASP pages. Allows custom levels of indenting, and does proper double-quote escaping. Simple, really, but I find myself using it all the time.
# By patgas
# April 30, 2001
# Takes an HTML file, and converts it into a
# Response.Write command for an ASP page.
# Usage:
#    perl [input_file] [output_file] (command_indent) (HTM
#    input_file and output_file are required.
#    command_indent inserts whitespace before the actual Response.Writ
+e command.
#    HTML_indent inserts whitespace in the string argument to Response

my ($infile, $outfile, $indentCommand, $indentHTML) = @ARGV;

die "Missing required argument: $!" if (!$infile || !$outfile);

$indentCommand ||= "2";
$indentHTML ||= "0";

open(IN, $infile) or die "Can't open input file: $!";
open(OUT, ">$outfile") or die "Can't open output file: $!";

print "Starting... ";


print "<%\n\n";

$indentCommand = ' ' x $indentCommand;
$indentHTML = ' ' x $indentHTML;

my @lines = <IN>;
my $writeCommand = 1;

while (@lines) {
    chomp(my $line = shift @lines);

    print $indentCommand;

    if ($writeCommand == 1) {
        print "Response.Write(";
        $writeCommand = 0;
    else {
        print "               ";

    print $indentHTML;

    if ( $line ) {
        $line =~ s/\"/\"\"/g;
        print "\"$line\" & ";

    print "vbNewLine";

    if (@lines) {
        print " & _";
    else {
        print ")\n";

    print "\n";

print "%>";
print STDOUT "Done.";

close IN or die "Can't close input file: $!";
close OUT or die "Can't close output file: $!";
Replies are listed 'Best First'.
Re: HTML To ASP Converter
by $code or die (Deacon) on Apr 30, 2001 at 23:54 UTC
    How about this code alternative:
    use strict; use File::Basename; my ($name,$path,$suffix) = fileparse($ARGV[0], '\.html?'); unless (($suffix) && (-e $ARGV[0])) { complain() } rename ($ARGV[0], $path . $name . ".asp") && print "File converted to ASP!" or complain(); sub complain { print "Error: Check and try again"; exit; }
    Admittedly the error checking could be better and we should use a GetOpt::* module to get the input filename but I think it does the job.

    Basically, ignore my previous post because even though you are converting a static htm file to ASP, it will still be slower than regular html on IIS 5 because you are adding unnecessary Response.Write statments. You don't need them at all. You can display static HTML as static html with an ASP extension, you don't need to enclose it all in <% tags. You're making the webserver do more work by having to process the file with asp.dll even on IIS5

    Does this make sense?
    Update: I don't intend this post to sound harsh, but you are making the HTML very ugly and hard to maintain because each line will be wrapped between Response.Write(?). I also think it will break if you have any text on multiple lines that are between <pre> tags and multi-line text boxes - you'll have an extra carriage return in there, plus vbNewLine - written out.

    $code or die
    $ perldoc perldoc

      Well, I intended it for converting little bits of HTML that get stuck within an actual application. All the benchmarks I've seen show that having an all-ASP page using those long, ugly, concatenated Response.Writes are much faster than mixed HTML/ASP or one Response.Write for each line.

      You're right about straight HTML being faster, though. Like I said, it's meant for bits and pieces (like a <head>) to put into an application. I've been using it mostly to convert tables for a calendar-type app that I hand-coded... I guess this script's purpose needs a little better explanation.

      -- More than perfect! Let us engage the Concord!
        That's strange that it should benchmark that way. I've noticed that VBScript doesn't have anything like Perl's here-doc capability (unless I am missing something), it would be much nicer to be able to do this (I'll give you a PerlScript ASP example):
        $Response->Write(<<SNIPPET); <HTML> <Head> <Title>$title</Title> </Head> <Body> SNIPPET
        This way, the HTML is left alone still. But that's PerlScript for you! - No hard feelings?
        $code or die
        $ perldoc perldoc
Re: HTML To ASP Converter
by $code or die (Deacon) on Apr 30, 2001 at 23:25 UTC
    Just want to mention that unless you are running the site on IIS 5, it is not worth converting static HTM files to ASP because there is a performance overhead in runing the ASP through the ASP processor.

    According to Microsoft they have removed this overhead - I think that IIS 5 detects that the ASP page is static the first time and doesn't load the ASP processor on subsequent hits to the page.

    $code or die $ perldoc perldoc
      Welcome to Market Portal Centre Net.
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://76679]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-04-25 20:35 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (463 votes). Check out past polls.