Search code examples
vbscriptasp-classic

Read an ASP-Classic file , but display the results line by line in text format instead of rendering the results first


My question involves reading ASP files, but instead of rendering them in ASP or HTML. I need them to be in Text format when it displays the results to the screen. What I mean is that all of the HTML tags such as <html> and <script>, are rendered instead of displayed. So instead of printing the words <style> to the screen , it executes the command, which is the opposite of what I want to happen, if possible.

Further complicating things, after I solve that, I run into a problem I faced before where its difficult to do the same thing with the <% and %> tags for the ASP blocks, which is obviously just runs the blocks when I need it to display the results instead.

Summary - I am trying to get the pure text from the asp file as if it was just a text file instead of executing the ASP classic segments. Then I need to run each line through either a replace or regex system to then put the rendering back into place with my own CSS markups, think of a homebrew IDE type system that marks the lines when I specify certain display elements to trigger when found. I want to put custom labels for CSS styling so when I display each line i marks them up with colors and styling so it visually makes more sense on screen for example making be dark blue on the screen when it reads that line. I hope that makes sense, I will provide any code you want to see.

Edit - the Summary was to just give you scope, the main question is just how to pull the textstream in as text without executing the html or asp classic code when displaying that text to the screen. Is it maybe even something that mixing some javascript with might be a good method? I just had figured there might be a pure ASP Classic way to do this with how it can deal with reading files and such.

IF VALIDATED_PAGE <> "" THEN                                                                    

set fs=Server.CreateObject("Scripting.FileSystemObject")                                            'Create a File Object to access files
set fso = Server.CreateObject("Scripting.FilesystemObject")                                         'Create a File Object to access files
fs.CopyFile ASP_FILE_LOCATION,EDT_FILE_FORMAT_LOCATION
'Copy the ASP file we want to edit into a .EDT extension
set txt = fso.OpenTextFile(EDT_FILE_FORMAT_LOCATION)                                            'Set the Text from  to use this EDIT File 
LINECOUNT = 0                                                                                       'Set this to ZERO to do manual line number tracking must start at zero

do while txt.AtEndOfStream = false                                                                  'Do these thing while the text file is NOT End Of Stream (END)
    LINECOUNT = LINECOUNT + 1                                                                       'Increase the Line Count number as we go through the file
    Response.Write("<LABEL class='LINENUMBER'>LINE#" & LINECOUNT & "</label>")                      'Mark and display the line numbers  

    IF NOT txt.AtEndOfStream THEN 
        IF InStr(txt.Readline,"div_a1") >= 1 then 
            response.write "A1FOUND"
            NEWLINE = Replace(txt.Readline,"div_a1",("<label style='color:blue;'>" & txt.Readline & "</label>"))
            RESPONSE.WRITE NEWLINE
            DIV_A1 = 1
        END IF
    END IF
    

    IF NOT txt.AtEndOfStream = TRUE THEN Response.Write(txt.ReadLine & "<br>")                                                      'Write the current line to the screen
         
LOOP 
END IF
  1. LINE#1
  2. LINE#2
  3. LINE#3
  4. LINE#4
  5. LINE#5
  6. LINE#6 div.css_grid{
  7. LINE#7 text-shadow:2px 2px black;
  8. LINE#8 margin:0px;
  9. LINE#9}

See how the first few lines don't have any information to display as they were html header code and the tag for line 5, but line 6 after running the <style> command then displays the text after it until it hits line 10 for the </style>. Maybe this helps you see more what I am talking about.


Solution

  • Whenever you need to display HTML as text in a web browser you need to first HTML encode it or the web browser will interpret and render the HTML tags instead of simply displaying them as plain text.

    You do this in Classic ASP by using the method

    <%
    Call Response.Write(Server.HTMLEncode(content))
    %>
    

    You will need to use Server.HTMLEncode() on any Response.Write method call that is returning the file content.


    Useful Links