Opened 12 years ago

Closed 12 years ago

#20320 closed Bug (invalid)

strange </tr> error with my function

Reported by: Ishayahu Owned by: nobody
Component: Template system Version: 1.4
Severity: Normal Keywords: <tr>
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is my code to make table from wiki markup:

def htmlize(str=''):
    # print 'in htmlize',str.encode('koi8-r')
    links = re.findall(r'https?://\S*',str)
    # links += re.findall(r'https://\S*',str)       
    html = ''
    inBold = False
    inItalic = False
    # для таблицы
    inTable = False
    inRow = False
    inCell = False
    tegs = {True:'</', False:'<'}
    count = 0
    while count < len(str):
        #print count,'||',str[count],'||',inTable,'||',inRow,'||',inCell,'||'
        if str[count] == '\n' and not inTable:
            html += '<br />'
        elif str[count] == '*' and count+1<len(str) and str[count+1] != '*':
            html = html + tegs[inBold] + 'b>'
            inBold = not inBold
        elif str[count] == '*' and count+1<len(str) and str[count+1] == '*':
            html = html + tegs[inItalic] + 'i>'
            count +=1
            inItalic = not inItalic
        elif str[count] == '*' and inBold:
            html = html + '</b>'
        elif str[count] == '\\' and count+1==len(str):
            html += '\\'
        elif str[count] == '\\':
            html += str[count+1]
            count += 1
        elif str[count] == '<':
            html += '&lt'
            # count +=1
        elif str[count] == '>':
            html += '&gt'
            count +=1
        elif str[count] == '&':
            html += '&amp'
            # count +=1
        # обработка создания таблиц
        elif count+3<len(str) and str[count]=='|' and str[count+1]=='|':
            # обрабатываем создание начала таблицы
            if (str[count-1]=='\n' or count-1<0) and not inTable:
                html += '<table border="1"><tr><td>'
                inTable = True
                inRow = True
                inCell = True
            elif inTable and not inRow:
                html += '<tr><td>'
                inRow = True
                inCell = True
            elif inCell:
                if str[count+2]!='\n':
                    html+='</td><td>'
                    inCell = True
                if str[count+2] == '\n':
                    html+='</td></tr>'
                    inCell = False
                    inRow=False
                    count+1
                    if str[count+3]!='|':
                        html+='</table>'
                        inTable=False
            count+=1
        elif (count+2>=len(str) and inTable) or (count+3<len(str) and str[count+2]=='\n' and inTable and str[count+3]!='|'):
            if inCell:
                html += '</td>'
                inCell = False
            if inRow:
                html += '</tr>'
                inRow = False
            html+='</table>'
            inTable = False
            count+=1

        else:
            html += str[count]
        count +=1
    for link in links:
        html = html.replace(link.replace('&','&amp'),'<a href='+link+'>'+link+'</a>')
    return html

When I run this code on python 2.7.3 I've got:

>>> b="""||a||b||
... ||c||d||
... text
... ||a||b||
... ||d||c||"""
>>> print(htmlize(b))
<table border="1"><tr><td>a</td><td>b</td></tr>
<tr><td>c</td><td>d</td></tr></table><br />text<br /><table border="1"><tr><td>a</td><td>b</td></tr>
<tr><td>d</td><td>c</td></tr></table>

but under Django 1.4 in source of html page I've got only:

<table border="1"><tr><td>a</td><td>b</td><td> </td><td>c</td><td>d</td><td> text </td><td>a</td><td>b</td><td> </td><td>d</td><td>c</td></tr></table>

without some and tags. What could be the matter? With safe I lost that tags too, so I can't make a table with more than one row.

here is how I call htmlize in view.py:

for note in notes:
    note.note = htmlize(note.note)

And it's really strange! textile works, with my function I've get the same result but in django it doesn't work:

ishayahu@test_pg_master:/home/ishayahu/tasks % ./manage.py shell

Python 2.7.3 (default, Jan 22 2013, 12:19:56) 
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import textile
>>> from todoes.ize import htmlize
>>> a="""||a||b||
... ||c||d||
... text
... ||a||b||
... ||c||d||"""
>>> htmlize(a)
'<table border="1"><tr><td>a</td><td>b</td>\t</tr>\n<tr><td>c</td><td>d</td>\t</tr></table><br />text<br /><table border="1"><tr><td>a</td><td>b</td>\t</tr>\n<tr><td>c</td><td>d</td>\t</tr></table>'
>>> textile.textile(a)
'\t<table>\n\t\t<tr>\n\t\t\t<td></td>\n\t\t\t<td>a</td>\n\t\t\t<td></td>\n\t\t\t<td>b</td>\n\t\t\t<td></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td></td>\n\t\t\t<td>c</td>\n\t\t\t<td></td>\n\t\t\t<td>d</td>\n\t\t\t<td></td>\n\t\t</tr>\n\t\t<tr>\n\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td></td>\n\t\t\t<td>a</td>\n\t\t\t<td></td>\n\t\t\t<td>b</td>\n\t\t\t<td></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td></td>\n\t\t\t<td>c</td>\n\t\t\t<td></td>\n\t\t\t<td>d</td>\n\t\t\t<td></td>\n\t\t</tr>\n\t</table>'
>>>

Change History (2)

comment:1 by Ishayahu, 12 years ago

This is part of page source:

                                        
                                        ||a||b||
||c||d||
text
||a||b||
||d||c||

	<table>
		<tr>
			<td></td>
			<td>a</td>
			<td></td>
			<td>b</td>
			<td></td>
		</tr>
		<tr>
			<td></td>
			<td>c</td>
			<td></td>
			<td>d</td>
			<td></td>
		</tr>
		<tr>

		</tr>
		<tr>
			<td></td>
			<td>a</td>
			<td></td>
			<td>b</td>
			<td></td>
		</tr>
		<tr>
			<td></td>
			<td>d</td>
			<td></td>
			<td>c</td>
			<td></td>
		</tr>
	</table>

<table border="1"><tr><td>a</td><td>b</td><td>
</td><td>c</td><td>d</td><td>
text
</td><td>a</td><td>b</td><td>
</td><td>d</td><td>c</td>	</tr></table><br />

for

if note.id != int(note_to_edit_id):
                            note.note = note.note+'\n\n'+textile(note.note)+'\n\n'+htmlize(note.note)

comment:2 by Aymeric Augustin, 12 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top