Add remove_code_chunks template filter
It will be needed when generating feeds
This commit is contained in:
parent
a5b6e90183
commit
4f8db2dc97
@ -28,6 +28,39 @@ def hilite(language, code):
|
|||||||
formatter = CodeFormatter(style = SolarizedDarkStyle, linenos = False, cssclass = language + " code", noclasses = True)
|
formatter = CodeFormatter(style = SolarizedDarkStyle, linenos = False, cssclass = language + " code", noclasses = True)
|
||||||
return highlight(code, lexer, formatter)
|
return highlight(code, lexer, formatter)
|
||||||
|
|
||||||
|
@register.filter(needs_autoescape=True)
|
||||||
|
@stringfilter
|
||||||
|
def remove_code_chunks(value, autoescape=None):
|
||||||
|
p = re.compile('\[\$ code:(?P<lang>[^:]+):(?P<slug>[^ ]+) \$\]')
|
||||||
|
i = p.finditer(value)
|
||||||
|
diff = 0
|
||||||
|
|
||||||
|
for match in i:
|
||||||
|
end, start = match.span()
|
||||||
|
oldlen = start - end
|
||||||
|
start += diff
|
||||||
|
end += diff
|
||||||
|
newstr = ""
|
||||||
|
newlen = len(newstr)
|
||||||
|
value = value[:end] + newstr + value[start:]
|
||||||
|
diff += newlen - oldlen
|
||||||
|
|
||||||
|
p = re.compile('\[\$ code:(?P<lang>[^:]+):(?P<code>.+?) \$\]', re.DOTALL)
|
||||||
|
i = p.finditer(value)
|
||||||
|
diff = 0
|
||||||
|
|
||||||
|
for match in i:
|
||||||
|
end, start = match.span()
|
||||||
|
oldlen = start - end
|
||||||
|
start += diff
|
||||||
|
end += diff
|
||||||
|
newstr = ""
|
||||||
|
newlen = len(newstr)
|
||||||
|
value = value[:end] + newstr + value[start:]
|
||||||
|
diff += newlen - oldlen
|
||||||
|
|
||||||
|
return mark_safe(value)
|
||||||
|
|
||||||
@register.filter(needs_autoescape=True)
|
@register.filter(needs_autoescape=True)
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def insert_code_chunks(value, autoescape=None):
|
def insert_code_chunks(value, autoescape=None):
|
||||||
|
Loading…
Reference in New Issue
Block a user