Wednesday, August 22, 2007

Text to HTML Table Converter 1.2

Now has unbuffered reads and better flag support


Unbuffered Input Reading


I added an pseudo-unbuff_read() function to the program (pseudo because it is really a sentinel-terminated loop concatenating a buffered read to a string)

def unbuff_read(END):
""" Reads one char at a time from stdin until reaches a "stop" character """
str = ""

while 1: # no do-whiles or assignments-in-conditionals in python :`(
# Compare the end of the string (same length as END) to END
if str[ len(END) * -1 :] == END:
break
str += sys.stdin.read(1)

return str


The parameter that you pass into this function called END breaks out of the read loop and returns the str read in up to that point. The default END is '\n.\n' which is a newline followed by a period and then another newline. This syntax is the same as the one used to stop reading the message body(DATA) when talking to smtp mail servers.

To quit the program just type in the END variable by itself.



Better flag support


Function argsContain():

def argsContain(flag):
""" Flag is the letter/word/etc that follows the '-' or '--' in the options """
# search for flag in options part of args i.e. before filename
for arg in sys.argv[1:len(sys.argv)-1]:
if arg.find(flag) != -1:
return True
else:
return False


This function goes through each argument before the filename and searches for a specific flag like "f" or "v". The two instances that used to search for flags have been replaced with a call to this method enabling syntax like
$ python txt2html-table-1.2.py -vf input.txt

and

$ python txt2html-table-1.2.py -fv input.txt



Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under. Download Text to HTML Table (txt2html-table) Version 1.2



Debugging


I kept my debugging code which consists of a function called debug() and a commented call to it after main() to make it easier to play around with the code by just uncommenting the call(and maybe commenting main()) and putting code in the debug() function.

Friday, August 17, 2007

Text to HTML Table Converter 1.0

Background


I was recently creating an description for a videogame I was selling on eBay and I needed to convert some text into an HTML table. Since I also was learning Python at the same time, I figured I would make a Python program that would do the job.

Input text format


The input text needs to be at least one line of text or more(seperated by a newline '\n') with a colon seperating the <th> element on the left and the <td> element on the right. You could also have another delimiter separating the two elements by changing the tList = eachLine.split(':') line to split based on whatever character you want.


The general format of the input text file should be:


thElement1: tdElement1
thElement2: tdElement2
...


where the <th> and <td> elements can have spaces, tabs, and other characters in them as long as they aren't '\n' or ':'.

Output HTML Format


The output which is displayed in the console (stdout) and which can optionally be saved to a file has this general format:


<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>


Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under.
Download Text to HTML Table (txt2html-table) Version 1.0

Usage Instructions


First of all, you need to download Python on your computer. If you're using a *NIX system, it should already by installed. The basic syntax is

python txt2html-table-1.0.py [-f] [-v] input.txt

If you don't include any of the optional flags (-f or -v), it will output the text in the file and confirm if you want to convert it. Type "y" or "yes" without the quotes to confirm or any other character(s) to exit. Upon confirmation, it will convert it to an HTML table and output the results.


Example: With no extra options


$ python txt2html-table-1.0.py input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================


Other command line options/flags


Use:

$ python txt2html-table-1.0.py -f input.txt

to prompt for a filename at the end of a conversion to save the html output to. If you decide to not save the output, press ENTER when it prompts for a filename in order to quit.

Example: Use of -f flag


$ python txt2html-table-1.0.py -f input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================
Enter a filename (Hit ENTER to quit): output.html
Output saved to: output.html


Use:

$ python txt2html-table-1.0.py -v input.txt

for verbose output. It shows you what elements each line was split into.

Example: Use of -v flag


$ python txt2html-table-1.0.py -v input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
' thElement1: tdElement1 ' was split into ['thElement1', 'tdElement1']
' thElement2: tdElement2 ' was split into ['thElement2', 'tdElement2']
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================

You can combine these two flags in either order as long as they're between the python file (.py) and the input filename. However, you cannot type in "-fv" or "-vf".

Errors you might receive



  • "Please pass a filename to me" if you didn't supply any arguments to the program.

  • "File does not exist." if the last argument you gave was not a valid file. Could occur if you added the flags after the input filename.


To avoid other errors, make sure that the python interpreter is in your OS's path, the program is in the current directory, and each line in your input file has one colon separating the <th> and <td> elements.

Todo: Possible features in future versions



  • Allow for specifying the output filename as a parameter i.e. python txt2html-table-1.0.py -f output.html input.txt

  • Allow more than two elements per line i.e. all elements after the first would be extra <td> elements in the table
  • Allow unbuffered reading in of text from the command line(stdin)

  • Add better support for flags (allow -fv or -vf)


Alternatives


Use the Python DOM library to build the table instead of raw HTML-holding variables like tHead, tBody, and tTail


Tools


Used Quick Escape to escape the html output for inserting into this post.