這是我的代碼,使表從維基標記:Django的安全過濾器<tr>刪除(解決)
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 += '<'
# count +=1
elif str[count] == '>':
html += '>'
count +=1
elif str[count] == '&':
html += '&'
# 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('&','&'),'<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:這裏是我如何調用htmlize在view.py:
for note in notes:
note.note = htmlize(note.note)
UPD2:這真是斯特朗!紡織工程,但與我的功能我已經得到了相同的結果,但在Django它不工作:
[email protected]_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>'
>>>
UPD3:這很容易:我應該看不僅是「\ n」但「\ r'也在我的htmlize例程中)
請說明你如何從django視圖中調用你的'htmlize'函數。 – alecxe 2013-04-21 12:08:45