PDA

View Full Version : ASP copy markup code


chrishirst
17-07-2003, 18:54/06:54PM
This is an ASP function to read a plain text file line by line and parse the line for a markup code which is then replaced with the appropriate HTML code(s) and served out as HTML.
The idea is similar to forum markup code and I use this code for placing text onto a page when someone else has been writing the copy or product descriptions for dynamic pages. All they have to know is the markup code rather than the equivalent HTML tags, and I don't have to go through their work and add tags or copy & paste loads of text.
The HTML standard of nesting the start and end markups must be maintained of course.

The reason for a line by line replace is that the CR at the end of each line of text will be replaced by the <br> tag, this allows the writer to decide where to put line breaks just as they would naturally when typing.

The code below should be copy & pasted into an asp file and included as a virtual in the file you intend to use the function

eg:
filename readtext.asp
and <!--#include virtual="readtext.asp"-->

when you need to call the function the syntax is
<% ReadText("filename.txt") %>

Function Code
<%
Dim TextFileName
dim line
dim list

Sub ReadText(TextFileName)
' Declare variables for the File System Object and the File to be accessed.
Dim objFSO, objTextFile

' Create an instance of the the File System Object and assign it to objFSO.
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open the file
Set objTextFile = objFSO.OpenTextFile(Server.MapPath(TextFileName))

Do While Not objTextFile.AtEndOfStream
line = objTextFile.ReadLine '& "<BR>" & Chr(13)
' text between and are bolded

line = Replace(line,"","<b>" ,1,-1,1)
line = Replace(line,"","</b>" ,1,-1,1)
line = Replace(line,"","<font color='#FF0000'>" ,1,-1,1)
line = Replace(line,"","</font>" ,1,-1,1)
line = Replace(line,"","<font color='#0000FF'>" ,1,-1,1)
line = Replace(line,"","</font>" ,1,-1,1)
line = Replace(line,"","<font size='7'>" ,1,-1,1)
line = Replace(line,"","</font>" ,1,-1,1)
line = Replace(line,"","<h1>" ,1,-1,1)
line = Replace(line,"","</h1>" ,1,-1,1)

' and so on use the same syntax for these pairs of lines for
' as many markup codes as you need. set the markup code
' you want to use and the HTML codes to be used instead
line = Replace(line,"[icon]","<img src='imagepath' alt='' border='0'>" ,1,-1,1)
' the above line can be used to place a graphic into the text
' this is for writing out a list
' every line between [ list] and [/ list] will have <li> written to it
if list = true then
line = "<li>" & line
end if
if instr(line,"[ list]") then
response.write "<ul>" & chr(13)
line = ""
list = true
end if
if instr(line,"[/ list]") then
line = "</ul>" & chr(13)
list = false
end if


Response.Write Line & "<BR>" & Chr(13)
Loop
' Close the file.
response.write "<br><br>" & vbcrlf
objTextFile.Close

' Release reference to the text file.
Set objTextFile = Nothing

' Release reference to the File System Object.
Set objFSO = Nothing
End Sub
%>

NOTE ' I had to add extra spaces into the list markup codes to avoid them breaking up (using code tags didn't help) remove before testing.

I've never timed this and it may add a few milliseconds onto the page load but it the grand scheme of things it is fairly negligible. It doesn't fall over gracefully if the text filename is wrong or nonexistent either.

I haven't put all the markups I use into this file it would be a very long post, but it is easy enough to follow the idea and add your own, even making combinations of tags from one markup

BTW the chr(13) are in the code to make the source readable otherwise it becomes one very long line of code and can cause some browers to break up the line.

Hope this is useful.

Chris.

chopsticks
18-07-2003, 15:51/03:51PM
Thanks for sharing. Always nice to see the ideas of others.

Anyways, just thought I'd mention that you might want to have a NON-executable file suffix on your included file.

i.e. <!--#include virtual="readtext.asp"-->

obviously works fine.

But <!--#include virtual="readtext.inc"-->

could also work.


The main difference being that you could have the web server confiured to NOT hand out (serve) files with the .inc suffix (or ask your web admin).

You also may want to consider placing the included file (readtext.???) in a directory ABOVE the web root. i.e. treat it the same as a database and place it away from where the public could potentially access it.

Else you risk the possibility of either someone access the file directly and/or modifying the file directly.

Remember that there ARE known exploits of ASP and Microsoft servers. Any number of factors COULD give someone access to your web root directory. Better safe than sorry, eh?

**

Thanks again for sharing the code. I wish more people would share ideas such as you've done.

:cheers:

chrishirst
18-07-2003, 16:32/04:32PM
Thanks chopsticks, and you are quite correct, also the text files do not have to be .txt or have a file suffix at all, it could be filename.123 or just filename, because they are being opened as a textstream and do not need any associated program for Windows.

just in case anyone is wondering how ASP can go outside the webroot.

Your webserver has to have parent paths enabled, Found in
Site Properties | Home Directory
Application Setting | Configuration...
App Options | Enable parent paths checkbox

this may be turned off by some hosts as a rogue script could be used to devastate the server if they haven't changed the default IIS installation location and security.

and then add ../ to the front of the text filename.

Chris.

JuniorHarris
18-07-2003, 19:44/07:44PM
Yes thanks for sharing!~ Some good points/code.