Re: Blatt wrapper question

new topic     » goto parent     » topic index » view thread      » older message » newer message

George Walters wrote:

> Wonderful, thanks. I'll give it a try.

The code that I had posted previously is not correct. sad
I had assumed by mistake, that between <pre> and </pre> it's not necessary
to mask certain characters. But it _is_ necessary. Here is the corrected
version:

-- simple txt2HTML program, 2005-05-04 by Juergen Luethje
-- (requires at least Eu 2.5 because of $)

-------------------------[ utility functions ]--------------------------

function rextract (sequence source, object x)
   for i = length(source) to 1 by -1 do
      if equal(source[i], x) then
         return source[1..i-1]
      end if
   end for
   return source
end function

function matchs (sequence s, sequence source, integer start)
   integer posn

   if start >= 1 and start <= length(source) then
      posn = match(s, source[start..$])
      if posn then
         return posn + start - 1
      end if
   end if
   return 0
end function

function replace (sequence source, sequence whatToReplace, sequence replaceWith)
   integer posn

   if length(whatToReplace) then
      posn = match(whatToReplace, source)
      while posn do
source = source[1..posn-1] & replaceWith &
         source[posn+length(whatToReplace)..$]
         posn = matchs(whatToReplace, source, posn+length(replaceWith))
      end while
   end if
   return source
end function

----------------------------[ text to HTML ]----------------------------

constant
HTML_HEADER = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01
   Transitional//EN\">\n"
               & "<html>\n"
               & "<head>\n"
& "<meta http-equiv=\"Content-Type\" content=\"text/html;
               charset=iso-8859-15\">\n"
               & "<title>%s</title>\n"
               & "</head>\n\n"
               & "<body>\n",

   HTML_FOOTER = "</body>\n"
               & "</html>"

global procedure text2html (sequence infile)
   object line
   sequence title, outfile
   integer ifn, ofn

   title = rextract(infile, '.')
   outfile = title & ".htm"
   ifn = open(infile, "r")
   ofn = open(outfile, "w")

   printf(ofn, HTML_HEADER, {title})
   puts(ofn, "<pre>\n")
   while 1 do
      line = gets(ifn)
      if atom(line) then
         exit
      end if
      line = replace(line, "&", "&amp;")
      line = replace(line, "<", "&lt;")
      line = replace(line, ">", "&gt;")
      line = replace(line, "\"", "&quot;")
      line = replace(line, {128}, "&euro;")
      puts(ofn, line)
   end while
   puts(ofn, "</pre>\n")
   puts(ofn, HTML_FOOTER)

   close(ifn)
   close(ofn)
end procedure

--------------------------------[ main ]--------------------------------

include file.e                  -- for dir()
include get.e                   -- for wait_key()

constant CMD = command_line()

object list, void

if length(CMD) < 3 then
   puts(1, "Usage: txt2html <filespec>")
   void = wait_key()
   abort(1)
end if

list = dir(CMD[3])
if atom(list) then
   puts(1, "No matching file found: " & CMD[3])
   void = wait_key()
   abort(1)
end if

for i = 1 to length(list) do
   text2html(list[i][D_NAME])
end for


Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu