Instructional Technology Portfolio | Design and Development Tools | Lessons | Zope Lessons
Login   |  Résumé   |  IT Portfolio   |  Home

Quoted HTML Engine

This is an example of how to combine python scripts and form inputs. This engine takes an input string and replaces all < and > signs with "&lt;" and "&gt;", thus emitting code that can readily be copied to a source HTML file.

Code Snippet:

The input form

The following HTML is for the form above, which takes the code pasted in the input area and passes it to the dtml2html python script below.

<form action="." method="POST"> <table> <tr> <td align="right" valign=top>Code Snippet:</td> <td align="left"> <textarea name="snippet" cols="50" rows="10"></textarea> </td> </tr> <tr><td></td><td><input type="submit" value="Convert" name="dtml2html:method"></td></tr> </table> </form>

The Python script: dtml2html

The following script takes the input from the form above and surrounds the given code snippet with <blockquote><pre><hr> tags to offset the code example and then reads each letter of the input text, replacing any occurrances of < or > with HTML markup equivalents, which prevents the code from actually being interpreted when rendered for displaying.

# dtml2html script # Replace all < and > with "<" and ">" # This scripts accepts one input parameter, "snippet" newsnippet = "<blockquote><pre><hr noshade size=1 color=#cccccc>" for i in snippet: if i == "<": newsnippet = newsnippet + "&lt;" elif i == ">": newsnippet = newsnippet + "&gt;" else: newsnippet = newsnippet + i newsnippet = newsnippet + "<hr noshade size=1 color=#cccccc></pre></blockquote>" return newsnippet

mwlang@cybrains.net
Guest Login   |  Home