Show More
@@ -57,6 +57,8 b' default_filters = {' | |||||
57 | 'rm_ansi': filters.remove_ansi, |
|
57 | 'rm_ansi': filters.remove_ansi, | |
58 | 'rm_dollars': filters.strip_dollars, |
|
58 | 'rm_dollars': filters.strip_dollars, | |
59 | 'rm_fake': filters.rm_fake, |
|
59 | 'rm_fake': filters.rm_fake, | |
|
60 | 'html_text' : filters.html_text, | |||
|
61 | 'add_anchor': filters.add_anchor, | |||
60 | 'ansi2latex': filters.ansi2latex, |
|
62 | 'ansi2latex': filters.ansi2latex, | |
61 | 'rm_math_space': filters.rm_math_space, |
|
63 | 'rm_math_space': filters.rm_math_space, | |
62 | 'wrap': filters.wrap |
|
64 | 'wrap': filters.wrap |
@@ -1,3 +1,4 b'' | |||||
|
1 | # coding: utf-8 | |||
1 | """String filters. |
|
2 | """String filters. | |
2 |
|
3 | |||
3 | Contains a collection of useful string manipulation filters for use in Jinja |
|
4 | Contains a collection of useful string manipulation filters for use in Jinja | |
@@ -17,6 +18,9 b' templates.' | |||||
17 |
|
18 | |||
18 | import re |
|
19 | import re | |
19 | import textwrap |
|
20 | import textwrap | |
|
21 | from xml.etree import ElementTree | |||
|
22 | ||||
|
23 | from IPython.utils import py3compat | |||
20 |
|
24 | |||
21 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
22 | # Functions |
|
26 | # Functions | |
@@ -24,6 +28,8 b' import textwrap' | |||||
24 |
|
28 | |||
25 | __all__ = [ |
|
29 | __all__ = [ | |
26 | 'wrap', |
|
30 | 'wrap', | |
|
31 | 'html_text', | |||
|
32 | 'add_anchor', | |||
27 | 'strip_dollars', |
|
33 | 'strip_dollars', | |
28 | 'rm_fake', |
|
34 | 'rm_fake', | |
29 | 'python_comment', |
|
35 | 'python_comment', | |
@@ -49,12 +55,34 b' def wrap(text, width=100):' | |||||
49 | wrpd = map('\n'.join, wrp) |
|
55 | wrpd = map('\n'.join, wrp) | |
50 | return '\n'.join(wrpd) |
|
56 | return '\n'.join(wrpd) | |
51 |
|
57 | |||
52 | def single_line(text): |
|
58 | ||
53 | """Wrap multi-line text into a single line |
|
59 | def html_text(element): | |
|
60 | """extract inner text from html | |||
|
61 | ||||
|
62 | Analog of jQuery's $(element).text() | |||
|
63 | """ | |||
|
64 | if not isinstance(element, (ElementTree.ElementTree, ElementTree.Element)): | |||
|
65 | element = ElementTree.fromstring(element) | |||
|
66 | ||||
|
67 | text = element.text or "" | |||
|
68 | for child in element: | |||
|
69 | text += html_text(child) | |||
|
70 | text += (element.tail or "") | |||
|
71 | return text | |||
|
72 | ||||
|
73 | ||||
|
74 | def add_anchor(html): | |||
|
75 | """Add an anchor-link to an html header tag | |||
54 |
|
76 | |||
55 | Used in markdown heading cells, which are not allowed to be multiline. |
|
77 | For use in heading cells | |
56 | """ |
|
78 | """ | |
57 | return ''.join(text.splitlines()) |
|
79 | h = ElementTree.fromstring(py3compat.cast_bytes_py2(html)) | |
|
80 | link = html_text(h).replace(' ', '-') | |||
|
81 | h.set('id', link) | |||
|
82 | a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) | |||
|
83 | a.text = u'¶' | |||
|
84 | h.append(a) | |||
|
85 | return ElementTree.tostring(h) | |||
58 |
|
86 | |||
59 |
|
87 | |||
60 | def strip_dollars(text): |
|
88 | def strip_dollars(text): |
General Comments 0
You need to be logged in to leave comments.
Login now