Once upon a time I wrote a quick program to convert text files to xhtml. It is just a simple program written in Free Pascal. It assumes that the text file is unwrapped. It add p tags at the start and end of the line, adds the correct header and footer and preserves blank lines. Compiles under Windows and Linux with the current version of Free Pascal. The xhtml has the same name as the text file but with a xhtml extension.
Here is the source
Program text2xhtml;
Uses sysutils;
{$H+}
{$I-}
Const
xmlIntro = '<?xml version="1.0" encoding="UTF-8" ?>';
docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
Var InFile,
OutFile : Text;
InBuff : String;
Begin
If ParamCount = 0 Then
Begin
WriteLn('Usage : ', ParamStr(0), ' [textfile name]');
Halt(1);
End;
If Not FileExists(ParamStr(1)) Then
Begin
WriteLn(ParamStr(1), ' not found...');
Halt(2);
End;
Assign(InFile, ParamStr(1));
Reset(InFile);
If (IOResult <> 0) Then
Begin
WriteLn('Could not open ', ParamStr(1), ' ...');
Halt(3);
End;
Assign(OutFile, ChangeFileExt(ParamStr(1), '.xhtml'));
ReWrite(OutFile);
If (IOResult <> 0) Then
Begin
WriteLn('Could create output file ...');
Halt(4);
End;
WriteLn(OutFile, xmlIntro);
WriteLn(OutFile, docType);
WriteLn(OutFile, '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">');
WriteLn(OutFile, '<head>');
WriteLn(OutFile, ' <title> - </title>');
WriteLn(OutFile, ' <style type="text/css" rel="stylesheet" >');
WriteLn(OutFile, ' </style>');
WriteLn(OutFile, '</head>');
WriteLn(OutFile, '<body>');
While Not EoF(InFile) Do
Begin
ReadLn(InFile, InBuff);
If InBuff <> '' Then InBuff := '<p>' + InBuff + '</p>'
Else InBuff := '<p> </p>';
WriteLn(OutFile, InBuff);
End;
Close(InFile);
WriteLn(OutFile, '</body>');
WriteLn(OutFile, '</html>');
Close(OutFile);
End.
|