0

这是我从 wiki 标记制作表格的代码:

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

当我在 python 2.7.3 上运行这段代码时,我得到了:

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

但在 Django 1.4 下,我只有:

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

没有一些和标签。可能是什么问题?有了安全,我也丢失了那些标签,所以我不能制作一个多行的表格。

UPD:这是我在 view.py 中调用 htmlize 的方式:

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

UPD2:真的很奇怪!Textile 可以工作,但是使用我的功能,我得到了相同的结果,但是在 django 中它不起作用:

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

解决了

这很简单:在我的 htmlize 例程中,我不仅应该看 '\n',还应该看 '\r')

4

1 回答 1

2

不要重新发明轮子。有大量为您编写和测试的wiki 到 html转换。

例如,尝试纺织

import textile

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

text

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

我知道这不是问题的确切答案,它只是一种解决方法。

于 2013-04-21T12:16:18.840 回答