[Prev][Next][Index][Thread]

Critique a first prog?



Hello,
    I'm new to Dylan, and I don't know anyone personally who knows
Dylan, so I'm posting my first Dylan program here for everyone to pick
apart. The purpose of the program is to take all the html files in a
directory and concatenate them, after first stripping out unwanted
lines. Aside from the general lack of error-handling, what could be
improved about this code? Does anything about this approach suggest
'non-Dylanesque' thinking? I'm especially interested in the read-lines
functions - is there already equivalent functionality available
somewhere? Is the use of blocks and handlers the way you would implement
these functions? Also, is there a regular expressions library available?

Thanks,

Bryn



Module:      docbuilder
Synopsis:    Convert chunked up docs to one printable page.


define method read-lines(stream :: <stream>)
  let v = make(<stretchy-vector>);
  block(exit)
    let handler <end-of-stream-error>
      = method(condition, next)
     exit();
 end;
    while (#t)
      add!(v, read-line(stream));
    end;
  end;
  v;
end;


define function good-line?(line)
  let bad = vector("SRC=next.gif", "<HTML>", "<BODY",
     "<!DOCTYPE", "HEAD>", "<TITLE>", "<ADDRESS>", "</HTML>",
     "</BODY", "<LINK", "<META", "<DIV", "</DIV");
  block(break)
    for (thing in bad)
      if (subsequence-position(line, thing))
        break(#f);
      end;
    end;
    #t;
  end;
end;

define function process-file(filename, writer)
  let fis = make(<file-stream>, locator: filename);
  let lines = read-lines(fis);
  close(fis);
  lines := choose(good-line?, lines);
  do(writer, lines);
end;

define function find-files(pathname :: <string>, criterion? ::
<function>)
  let files = make(<stretchy-vector>);
  do-directory(compose(curry(add!, files), second, list), pathname);
  choose(criterion?, files);
end;

define function html-file?(file)
  (subsequence-position(file, ".htm") = (file.size - 4));
end;

define function docbuilder-top-level ()
  let arguments = application-arguments();
  if (arguments.size == 0)
    format-out("Usage: %s pathname\n", application-name());
  else
    format-out("Checking path: %s\n", arguments[0]);
    let files = find-files(arguments[0], html-file?);
    let fos = make(<file-stream>, locator: concatenate(arguments[0],
"Build.htm"),
     direction: #"output", if-does-not-exist: #"create" );
    let write-fos = curry(write-line, fos);
    for (file in files)
      process-file(concatenate(arguments[0], file), write-fos);
    end;
    close(fos);
  end
end function docbuilder-top-level;

docbuilder-top-level();










Follow-Ups: