1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2010-08-18 20:28:25 +0000 (Wed, 18 Aug 2010) $ |
---|
5 | # $Author: jemian $ |
---|
6 | # $Revision: 206 $ |
---|
7 | # $HeadURL$ |
---|
8 | # $Id: makeTagHTML.py 206 2010-08-18 20:28:25Z jemian $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | |
---|
11 | import sys |
---|
12 | import types |
---|
13 | |
---|
14 | #----------------------------------------------------------------------------------------------------- |
---|
15 | |
---|
16 | def makeTagHTML(fullTag, content='', indent=1): |
---|
17 | 'This is the new version of how to make HTML tags' |
---|
18 | #print "[%s] [%s] %d\n" % (fullTag, content, indent) |
---|
19 | #print type(content) |
---|
20 | if (type(content) == types.IntType): |
---|
21 | content = "%d" % (content) |
---|
22 | elif (type(content) == type(123.4)): |
---|
23 | content = "%g" % (content) |
---|
24 | parts = fullTag.split() |
---|
25 | shortTag = parts[0] |
---|
26 | tag = "<" + fullTag + ">" |
---|
27 | if (len(content)): |
---|
28 | if (indent != 0): |
---|
29 | tag = tag + "\n" |
---|
30 | indentation = " " |
---|
31 | for line in content.split("\n"): |
---|
32 | if (len(line) > 0): |
---|
33 | # indent each line of the content |
---|
34 | tag = tag + indentation + line + "\n" |
---|
35 | else: |
---|
36 | tag = tag + content |
---|
37 | tag = tag + "</" + shortTag + ">" |
---|
38 | else: |
---|
39 | # element with no content (xhtml-compatible) |
---|
40 | tag = "<" + fullTag + " />" |
---|
41 | return(tag + "\n") |
---|
42 | |
---|
43 | #----------------------------------------------------------------------------------------------------- |
---|
44 | |
---|
45 | if __name__ == "__main__": |
---|
46 | '''example of how to use this code''' |
---|
47 | # read this source code into a buffer |
---|
48 | sourceFile = sys.argv[0] |
---|
49 | f = open(sourceFile, 'r') |
---|
50 | buf = f.read() |
---|
51 | f.close() |
---|
52 | # make a simple page to show the content |
---|
53 | head = makeTagHTML('title', sourceFile, 0) |
---|
54 | body = makeTagHTML('h1', sourceFile, 0) |
---|
55 | body += '\n' |
---|
56 | # example short-form element with no content but some attribute |
---|
57 | body += makeTagHTML('br color="blue"') |
---|
58 | body += '\n' |
---|
59 | body += makeTagHTML('pre', buf, 1) |
---|
60 | html = makeTagHTML('html', makeTagHTML('head', head, 1)+makeTagHTML('body', body, 1), 1) |
---|
61 | print html |
---|