Reputation: 2393
I wrote a class to make html out of string and some markup tags.
The following is my data:
test_string =('1 Then was Jesus led up of the Spirit into the wilderness to be '
'tempted of the devil.')
test_code = [('equal', 0, 7, 0, 7),
('delete', 7, 11, 7, 7),
('equal', 11, 15, 7, 11),
('insert', 15, 15, 11, 15),
('equal', 15, 37, 15, 37),
('insert', 37, 37, 37, 38),
('equal', 37, 57, 38, 58),
('insert', 57, 57, 58, 59),
('equal', 57, 64, 59, 66),
('replace', 64, 75, 66, 68),
('equal', 75, 77, 68, 70),
('delete', 77, 78, 70, 70),
('equal', 78, 79, 70, 71),
('insert', 79, 79, 71, 73),
('equal', 79, 80, 73, 74),
('delete', 80, 84, 74, 74),
('equal', 84, 86, 74, 76)]
I intend to write a class to achieve this.
output = build_html(test_string,test_code)
output.get_html()
"<p class='verse'>1 Then <span class='delete'>was </span>Jesu<span class='insert'></span>s led up of the Spirit<span class='insert'></span> into the wilderness<span class='insert'></span> to be <span class='replace'>tempted of </span>th<span class='delete'>e</span> <span class='insert'></span>d<span class='delete'>evil</span>.</p>"
This is my code:
class build_html():
def __init__(self, ori_text, opcodes):
self.text = ""
self.ori_text = ori_text
self.opcodes = opcodes
def makehtml(self, ori_text, code, start, end):
if code=='equal':
self.text += ori_text[start:end]
elif code =='delete':
self.text += "<span class='delete'>{}</span>".format(ori_text[start:end])
elif code =='insert':
self.text += "<span class='insert'>{}</span>".format(ori_text[start:end])
elif code =='replace':
self.text += "<span class='replace'>{}</span>".format(ori_text[start:end])
def get_html(self):
for modify in self.opcodes:
self.makehtml(self.ori_text, modify[0],modify[1], modify[2])
return "<p class='verse'>" + self.text + "</p>"
But when every time I do output.get_html()
, the output was made and appended again. So it will be twice as long.
For example:
>>> output.get_html() # first time
"<p class='verse'>1 Then <span class='delete'>was </span>Jesu<span class='insert'></span>s led up of the Spirit<span class='insert'></span> into the wilderness<span class='insert'></span> to be <span class='replace'>tempted of </span>th<span class='delete'>e</span> <span class='insert'></span>d<span class='delete'>evil</span>.</p>"
>>> output.get_html() # called second time
"<p class='verse'>1 Then <span class='delete'>was </span>Jesu<span class='insert'></span>s led up of the Spirit<span class='insert'></span> into the wilderness<span class='insert'></span> to be <span class='replace'>tempted of </span>th<span class='delete'>e</span> <span class='insert'></span>d<span class='delete'>evil</span>.</p><p class='verse'>1 Then <span class='delete'>was </span>Jesu<span class='insert'></span>s led up of the Spirit<span class='insert'></span> into the wilderness<span class='insert'></span> to be <span class='replace'>tempted of </span>th<span class='delete'>e</span> <span class='insert'></span>d<span class='delete'>evil</span>.</p>"
How can I correct it?
Upvotes: 1
Views: 33
Reputation: 123501
You need to (re)initialize the self.text
attribute at the beginning of the get_html()
method so it doesn't contain the results of the last time it was called:
def get_html(self):
self.text = "" # ADDED
for modify in self.opcodes:
self.makehtml(self.ori_text, modify[0],modify[1], modify[2])
return "<p class='verse'>" + self.text + "</p>"
Upvotes: 2