Created
December 14, 2012 03:06
-
-
Save pylemon/4282379 to your computer and use it in GitHub Desktop.
django: html filter for clean up html tags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| from django.template.defaultfilters import stringfilter, striptags, slice_filter | |
| def cut_string(value, limit): | |
| s = striptags(value) | |
| s = s.replace('\n', '').replace(' ', '') | |
| return slice_filter(s, str(limit)) | |
| def test(): | |
| test_string = """ | |
| <html> | |
| <title>测试一下</title> | |
| <body> | |
| <h1>Hello, world</h1> | |
| <div class="test"> | |
| <li><span><a href="http://www.google.com" title="google">最后一句</a></span></li> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| print cut_string(test_string, 20) | |
| if __name__ == '__main__': | |
| test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment