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.
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.