Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
Add escaped filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 6, 2017
1 parent ae3c230 commit 8e2ec68
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hamlpy/parser/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from future.utils import raise_from

from .core import ParseException
from .utils import html_escape


# ----------------------------------------------------------------------------------
Expand All @@ -53,6 +54,10 @@ def preserve(text, options):
return text.replace('\r', '')


def escaped(text, options):
return html_escape(text)


def cdata(text, options):
text = '\n' + text.rstrip()
text = text.replace("\n", "\n ")
Expand Down Expand Up @@ -156,6 +161,7 @@ def script_filter(text, mime_type, comment, options):
FILTERS = {
'plain': plain,
'preserve': preserve,
'escaped': escaped,
'cdata': cdata,
'css': css,
'stylus': stylus,
Expand Down
13 changes: 13 additions & 0 deletions hamlpy/parser/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import unicode_literals


def html_escape(s):
"""
Escapes HTML entities, matching substitutions used the Ruby Haml library
"""
s = s.replace("&", "&")
s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")
s = s.replace('"', "&quot;")
s = s.replace("'", "&#039;")
return s
2 changes: 2 additions & 0 deletions hamlpy/test/templates/filters.hamlpy
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:escaped
<"&'>
:javascript
$(document).ready(function(){
$("#form{{form.initial.id}}").submit(form_submit);
Expand Down
1 change: 1 addition & 0 deletions hamlpy/test/templates/filters.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
&lt;&quot;&amp;&#039;&gt;
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function(){
Expand Down

0 comments on commit 8e2ec68

Please sign in to comment.