HTML Challenge

HTML Challenge

lawgin
lawgin

January 5th, 2010, 9:34 pm #1

This challenge is for those who know a little bit about html coding or would like to learn about it.

Write a qb program that converts a plain text document (xyz.txt) to an html document (xyz.htm) that can be opened and read with your browser. There are 3 requirements:

1. Both documents must have the same appearance with the same amount of white space. In other words, carriage returns, line feeds, multiple spaces and tabs in the plain text must be preserved in the html.

2. URLs in the plain text will become clickable links in the html. Let's specify that URLs begin with "http://" and contain no spaces.

3. If the first line in the plain text is 80 characters or less in length and is followed by a blank line, then it will become the heading of the html document.
Quote
Share

Joined: June 23rd, 2007, 4:01 pm

January 6th, 2010, 4:19 am #2

Not to be a downer, but that is as simple as using Line Input to read the text and output to write the HTML page. The only thing you need to add are HTML hyperlink tags to any word that starts with http.

I mean you could add <html><body> and end it with </body></html> but you don't even have to do that...

Save as xyz.txt
--------------------------
This is a text file that will be converted into an HTML file with
links to sites like http://www.qb64.net and http://google.com. It
won't do indentation, email hyperlinks, or much of anything else
but at least I finished it during a commercial break for NCIS.
--------------------------

CLS
OPEN "xyz.txt" FOR INPUT AS #1
OPEN "xyz.htm" FOR OUTPUT AS #2
DO UNTIL EOF(1)
LINE INPUT #1, a$
IF RIGHT$(a$, 1) <> " " THEN a$ = a$ + " "
DO UNTIL INSTR(flag + 1, LCASE$(a$), "http") = 0
x = INSTR(flag + 1, LCASE$(a$), "http")
y = LEN(MID$(a$, INSTR(flag + 1, a$, "http"), INSTR(x, LCASE$(a$), " ") - x))
a$ = MID$(a$, 1, x - 1) + "<a href=" + CHR$(34) + MID$(a$, INSTR(flag + 1, a$, "http"), INSTR(x, LCASE$(a$), " ") - x) + CHR$(34) + ">" + MID$(a$, INSTR(flag + 1, a$, "http"), INSTR(x, LCASE$(a$), " ") - x) + "</a>" + MID$(a$, INSTR(x, LCASE$(a$), " "))
flag = x + 12 + y
LOOP
PRINT #2, a$ + "<br />"
LOOP
CLOSE
SHELL "cmd /c start xyz.htm"

----------------------------------

Pete

PS: The code works, but I'm having a little trouble getting it to post correctly.

Quote
Like
Share

lawgin
lawgin

January 6th, 2010, 5:08 pm #3

But it doesn't nearly meet the challenge. Multiple spaces and tabs are not preserved as you realized. Also your program does not remove periods from the end of the URLs. This doesn't matter with google.com. but maps.google.com/maps. will fail. (I think it's the forward slash.)
Quote
Share

Joined: February 2nd, 2006, 10:38 pm

January 7th, 2010, 8:20 pm #4

* Yeah, you don't wanna mess with Pete on text stuff....SCREEN 0 Hero
Quote
Like
Share

Joined: June 23rd, 2007, 4:01 pm

January 8th, 2010, 7:27 pm #5

But it doesn't nearly meet the challenge. Multiple spaces and tabs are not preserved as you realized. Also your program does not remove periods from the end of the URLs. This doesn't matter with google.com. but maps.google.com/maps. will fail. (I think it's the forward slash.)
CLS
OPEN "xyz.txt" FOR INPUT AS #1
OPEN "xyz.htm" FOR OUTPUT AS #2
DO UNTIL EOF(1)
LINE INPUT #1, a$
IF RIGHT$(a$, 1) <> " " THEN a$ = a$ + " "

REM Handle multiple spaces
DO UNTIL INSTR(a$, SPACE$(2)) = 0
a$ = MID$(a$, 1, INSTR(a$, SPACE$(2))) + "&nbsp;" + MID$(a$, INSTR(a$, SPACE$(2)) + 2)
LOOP

REM Convert CHR$(9) to 8 non-breaking spaces
DO UNTIL INSTR(a$, CHR$(9)) = 0
a$ = MID$(a$, 1, INSTR(a$, CHR$(9)) - 1) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + MID$(a$, INSTR(a$, CHR$(9)) + 1)
LOOP

REM Make hyperlinks and remove period from internal end of link
DO UNTIL INSTR(flag + 1, LCASE$(a$), "http") = 0
x = INSTR(flag + 1, LCASE$(a$), "http")
y = LEN(MID$(a$, INSTR(flag + 1, a$, "http"), INSTR(x, LCASE$(a$), " ") - x))
a$ = MID$(a$, 1, x - 1) + "<a href=" + CHR$(34) + MID$(a$, INSTR(flag + 1, a$, "http"), INSTR(x, LCASE$(a$), " ") - x) + CHR$(34) + ">" + MID$(a$, INSTR(flag + 1, a$, "http"), INSTR(x, LCASE$(a$), " ") - x) + "</a>" + MID$(a$, INSTR(x, LCASE$(a$), _
" "))
z = INSTR(LCASE$(a$), "a href=" + CHR$(34) + "http://")
DO
IF INSTR(z + 15, LCASE$(a$), "." + CHR$(34)) THEN
a$ = MID$(a$, 1, INSTR(z + 15, LCASE$(a$), "." + CHR$(34)) - 1) + CHR$(34) + MID$(a$, INSTR(z + 15, LCASE$(a$), "." + CHR$(34)) + 2)

REM Remove period from the external displayed link
DO
IF INSTR(a$, ".</a>") <> 0 THEN
a$ = MID$(a$, 1, INSTR(a$, ".</a>") - 1) + "</a>. " + MID$(a$, INSTR(a$, ".</a>") + 5)
ELSE
EXIT DO
END IF
LOOP
ELSE
EXIT DO
END IF
LOOP
flag = x + 12 + y
LOOP
PRINT #2, a$ + "<br />"
LOOP
CLOSE
SHELL "cmd /c start xyz.htm"

-------------------------------

Pete
Quote
Like
Share

lawgin
lawgin

January 8th, 2010, 10:29 pm #6

I did a lot of experimenting with this. Tabs are difficult to handle because they don't always indent the same number of places. And even if all spaces in the text file are preserved, if the font changes in the html version, things don't line up right.

I found it best to enclose everything that you don't want to change between the <pre> and </pre> tags. The only thing outside these tags is the coding for the heading (if any) and the URLs.


INPUT "Name of file to convert"; f$
OPEN f$ FOR INPUT AS #1
h$ = LEFT$(f$, LEN(f$) - 4) + ".htm"

'Check for heading
OPEN h$ FOR OUTPUT AS #2
LINE INPUT #1, a$
LINE INPUT #1, b$
IF (LEN(a$) < 81) AND (b$ = "") THEN
PRINT #2, "<h1>"; a$; "</h1><br />"
ELSE
CLOSE 1
OPEN f$ FOR INPUT AS #1
END IF

PRINT #2, "<pre>"
DO
LINE INPUT #1, a$

'Look for URLs
u = INSTR(a$, "http://")
IF u THEN
w = u
DO
w = w + 1
b$ = MID$(a$, w, 1)
LOOP UNTIL (b$ = CHR$(32)) OR (w = LEN(a$) + 1)
u$ = MID$(a$, u, w - u)
k = INSTR(LEN(u$) - 2, u$, ".")
IF k THEN u$ = LEFT$(u$, k - 1)
u2$ = "</pre><A HREF=" + CHR$(34) + u$ + CHR$(34) + ">" + u$ + "</a><pre>"
a$ = LEFT$(a$, u - 1) + u2$ + LTRIM$(MID$(a$, w))
END IF
PRINT #2, a$
LOOP UNTIL EOF(1)
CLOSE
SHELL "cmd /c start " + h$


Quote
Share

Joined: June 23rd, 2007, 4:01 pm

January 9th, 2010, 2:19 am #7

But if you are actually trying to make a QB to HTML editor, you have a lot of more work to do. There are tables, margins, dividers, etc., which all take a considerable degree of thought and coding. I took the easy way out with TAB, because I know it is a pain in the ASCII to make it exact. (It was difficult enough to program one for a word-processor app i created several years ago.) Anyway, good luck with it if you are actually trying to build one of quality. Frankly, I would recommend you use an rtf format. That is a lot easier to convert to html, because the rtf coding can be used to format the html page without a hellish amount of WP-style coding. (One of the few M$ programs I have respect for, M$ Word, at least for the WP part and before they tried to make it into an HTML converter. It&#8217;s a great WP, but a crappy way to write HTML in my opinion, although FrontPage absolutely sucks even more.)

Anyway, if that is what yo are attempting, you don't have alot of competition, just a lot of work. If you want to see a great HTML markup tool, chaeck out Dreamweaver.

Pete

Quote
Like
Share

lawgin
lawgin

January 9th, 2010, 6:58 pm #8

I used to send out daily emails to blind members of Bookshare.org that contained a list of books that were added to the Bookshare site that day. After a while, rather than maintain a growing email list, I decided to create a blind friendly web site containing the daily book lists. But all my lists were in text format so I needed a tool to convert them to html, hence the reason for this project.

The site is at http://myplace.frontier.com/~bshare/

All the content of the book lists on this site were gleaned from downloads of Bookshare.org pages using a program written entirely in qbasic.
Quote
Share

Joined: February 2nd, 2006, 10:38 pm

January 9th, 2010, 8:51 pm #9

* Are you saying that blind people can only see HTML? :-)
Quote
Like
Share

lawgin
lawgin

January 9th, 2010, 9:34 pm #10

They use braille translators or text to speech screen readers regardless of the format.
Quote
Share