#!/usr/bin/python
import sys
class Article:
"""Converts text files to HTML, for use in web pages.
The output will not be a full HTML page, just the relevant HTML.
The file:
"Hello World.
Paragraph 1.
Paragraph 2.
Another line."
Would be converted to:
"
Hello World
Paragraph 1.
Paragraph 2.
Another line.
"
All CSS should be done by the user, this would probably be made easy
by encapsulating the article in a \n')
paragraph = self.__tagReplace(paragraph) + ''
return paragraph
def __tagReplace(self, paragraph):
"""Converts BBCode to HTML"""
tagStack, retParagraph = [], ""
while 1:
start = paragraph.find('[')
if (start == -1):
break
retParagraph += paragraph[0:start]
paragraph = paragraph.replace(paragraph[0:start], '')
end = paragraph.find(']')
tag = paragraph[1:end]
if tag[0] == '\\':
if tag[1:] != tagStack[-1]:
print "Unmatched " + '[' + tagStack.pop() + ']' + " tag, aborting."
print "Paragraphs contained: " + paragraph
sys.exit(1)
else:
tagStack.pop()
else:
tagStack.append(tag)
retParagraph += '<' + tag + '>'
paragraph = paragraph.replace(paragraph[:end+1], '')
retParagraph += paragraph
while len(tagStack):
tag = tagStack.pop()
retParagraph += '<\\' + tag + '>'
return retParagraph
if __name__ == "__main__":
Test = Article('hello.txt')
print Test.html