Question

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 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.

UPD: here is how I call htmlize in view.py:

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

UPD2: It's really strang! textile works, but 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>'
>>>

Solved

It was easy: I should watch not only on '\n' but on '\r' too in my htmlize routine)

Was it helpful?

Solution

Don't reinvent the wheel. There are plenty of wiki to html converts written and tested for you.

For example, try textile:

import textile

print textile.textile("""||a||b||
||c||d||

text

||a||b||
||d||c||""")

I understand that this is not an exact answer to the question, it's just a workaround.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top