Show More
@@ -1,116 +1,123 b'' | |||||
1 | """String filters. |
|
1 | """String filters. | |
2 |
|
2 | |||
3 | Contains a collection of useful string manipulation filters for use in Jinja |
|
3 | Contains a collection of useful string manipulation filters for use in Jinja | |
4 | templates. |
|
4 | templates. | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 | # |
|
10 | # | |
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | import re |
|
18 | import re | |
19 | import textwrap |
|
19 | import textwrap | |
20 |
|
20 | |||
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | # Functions |
|
22 | # Functions | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | __all__ = [ |
|
25 | __all__ = [ | |
26 | 'wrap', |
|
26 | 'wrap', | |
27 | 'strip_dollars', |
|
27 | 'strip_dollars', | |
28 | 'rm_fake', |
|
28 | 'rm_fake', | |
29 | 'python_comment', |
|
29 | 'python_comment', | |
30 | 'get_lines' |
|
30 | 'get_lines' | |
31 | ] |
|
31 | ] | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def wrap(text, width=100): |
|
34 | def wrap(text, width=100): | |
35 | """ |
|
35 | """ | |
36 | Intelligently wrap text. |
|
36 | Intelligently wrap text. | |
37 | Wrap text without breaking words if possible. |
|
37 | Wrap text without breaking words if possible. | |
38 |
|
38 | |||
39 | Parameters |
|
39 | Parameters | |
40 | ---------- |
|
40 | ---------- | |
41 | text : str |
|
41 | text : str | |
42 | Text to wrap. |
|
42 | Text to wrap. | |
43 | width : int, optional |
|
43 | width : int, optional | |
44 | Number of characters to wrap to, default 100. |
|
44 | Number of characters to wrap to, default 100. | |
45 | """ |
|
45 | """ | |
46 |
|
46 | |||
47 | split_text = text.split('\n') |
|
47 | split_text = text.split('\n') | |
48 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) |
|
48 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) | |
49 | wrpd = map('\n'.join, wrp) |
|
49 | wrpd = map('\n'.join, wrp) | |
50 | return '\n'.join(wrpd) |
|
50 | return '\n'.join(wrpd) | |
51 |
|
51 | |||
|
52 | def single_line(text): | |||
|
53 | """Wrap multi-line text into a single line | |||
|
54 | ||||
|
55 | Used in markdown heading cells, which are not allowed to be multiline. | |||
|
56 | """ | |||
|
57 | return ''.join(text.splitlines()) | |||
|
58 | ||||
52 |
|
59 | |||
53 | def strip_dollars(text): |
|
60 | def strip_dollars(text): | |
54 | """ |
|
61 | """ | |
55 | Remove all dollar symbols from text |
|
62 | Remove all dollar symbols from text | |
56 |
|
63 | |||
57 | Parameters |
|
64 | Parameters | |
58 | ---------- |
|
65 | ---------- | |
59 | text : str |
|
66 | text : str | |
60 | Text to remove dollars from |
|
67 | Text to remove dollars from | |
61 | """ |
|
68 | """ | |
62 |
|
69 | |||
63 | return text.strip('$') |
|
70 | return text.strip('$') | |
64 |
|
71 | |||
65 |
|
72 | |||
66 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') |
|
73 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') | |
67 |
|
74 | |||
68 | def rm_fake(text): |
|
75 | def rm_fake(text): | |
69 | """ |
|
76 | """ | |
70 | Fix all fake URLs that start with `files/`, |
|
77 | Fix all fake URLs that start with `files/`, | |
71 | stripping out the `files/` prefix. |
|
78 | stripping out the `files/` prefix. | |
72 |
|
79 | |||
73 | Parameters |
|
80 | Parameters | |
74 | ---------- |
|
81 | ---------- | |
75 | text : str |
|
82 | text : str | |
76 | Text in which to replace 'src="files/real...' with 'src="real...' |
|
83 | Text in which to replace 'src="files/real...' with 'src="real...' | |
77 | """ |
|
84 | """ | |
78 | return files_url_pattern.sub(r"\1=\2", text) |
|
85 | return files_url_pattern.sub(r"\1=\2", text) | |
79 |
|
86 | |||
80 |
|
87 | |||
81 | def python_comment(text): |
|
88 | def python_comment(text): | |
82 | """ |
|
89 | """ | |
83 | Build a Python comment line from input text. |
|
90 | Build a Python comment line from input text. | |
84 |
|
91 | |||
85 | Parameters |
|
92 | Parameters | |
86 | ---------- |
|
93 | ---------- | |
87 | text : str |
|
94 | text : str | |
88 | Text to comment out. |
|
95 | Text to comment out. | |
89 | """ |
|
96 | """ | |
90 |
|
97 | |||
91 | #Replace line breaks with line breaks and comment symbols. |
|
98 | #Replace line breaks with line breaks and comment symbols. | |
92 | #Also add a comment symbol at the beginning to comment out |
|
99 | #Also add a comment symbol at the beginning to comment out | |
93 | #the first line. |
|
100 | #the first line. | |
94 | return '# '+'\n# '.join(text.split('\n')) |
|
101 | return '# '+'\n# '.join(text.split('\n')) | |
95 |
|
102 | |||
96 |
|
103 | |||
97 | def get_lines(text, start=None,end=None): |
|
104 | def get_lines(text, start=None,end=None): | |
98 | """ |
|
105 | """ | |
99 | Split the input text into separate lines and then return the |
|
106 | Split the input text into separate lines and then return the | |
100 | lines that the caller is interested in. |
|
107 | lines that the caller is interested in. | |
101 |
|
108 | |||
102 | Parameters |
|
109 | Parameters | |
103 | ---------- |
|
110 | ---------- | |
104 | text : str |
|
111 | text : str | |
105 | Text to parse lines from. |
|
112 | Text to parse lines from. | |
106 | start : int, optional |
|
113 | start : int, optional | |
107 | First line to grab from. |
|
114 | First line to grab from. | |
108 | end : int, optional |
|
115 | end : int, optional | |
109 | Last line to grab from. |
|
116 | Last line to grab from. | |
110 | """ |
|
117 | """ | |
111 |
|
118 | |||
112 | # Split the input into lines. |
|
119 | # Split the input into lines. | |
113 | lines = text.split("\n") |
|
120 | lines = text.split("\n") | |
114 |
|
121 | |||
115 | # Return the right lines. |
|
122 | # Return the right lines. | |
116 | return "\n".join(lines[start:end]) #re-join |
|
123 | return "\n".join(lines[start:end]) #re-join |
@@ -1,53 +1,56 b'' | |||||
1 | """Utility for calling pandoc""" |
|
1 | """Utility for calling pandoc""" | |
2 | #----------------------------------------------------------------------------- |
|
2 | #----------------------------------------------------------------------------- | |
3 | # Copyright (c) 2013 the IPython Development Team. |
|
3 | # Copyright (c) 2013 the IPython Development Team. | |
4 | # |
|
4 | # | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 | # |
|
6 | # | |
7 | # The full license is in the file COPYING.txt, distributed with this software. |
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | from __future__ import print_function |
|
14 | from __future__ import print_function | |
15 |
|
15 | |||
16 | # Stdlib imports |
|
16 | # Stdlib imports | |
17 | import sys |
|
17 | import sys | |
18 | import subprocess |
|
18 | import subprocess | |
19 |
|
19 | |||
20 | # IPython imports |
|
20 | # IPython imports | |
21 | from IPython.utils.py3compat import cast_bytes |
|
21 | from IPython.utils.py3compat import cast_bytes | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Classes and functions |
|
24 | # Classes and functions | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | def pandoc(source, fmt, to, encoding='utf-8'): |
|
27 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): | |
28 | """Convert an input string in format `from` to format `to` via pandoc. |
|
28 | """Convert an input string in format `from` to format `to` via pandoc. | |
29 |
|
29 | |||
30 | This function will raise an error if pandoc is not installed. |
|
30 | This function will raise an error if pandoc is not installed. | |
31 | Any error messages generated by pandoc are printed to stderr. |
|
31 | Any error messages generated by pandoc are printed to stderr. | |
32 |
|
32 | |||
33 | Parameters |
|
33 | Parameters | |
34 | ---------- |
|
34 | ---------- | |
35 | source : string |
|
35 | source : string | |
36 | Input string, assumed to be valid format `from`. |
|
36 | Input string, assumed to be valid format `from`. | |
37 | fmt : string |
|
37 | fmt : string | |
38 | The name of the input format (markdown, etc.) |
|
38 | The name of the input format (markdown, etc.) | |
39 | to : string |
|
39 | to : string | |
40 | The name of the output format (html, etc.) |
|
40 | The name of the output format (html, etc.) | |
41 |
|
41 | |||
42 | Returns |
|
42 | Returns | |
43 | ------- |
|
43 | ------- | |
44 | out : unicode |
|
44 | out : unicode | |
45 | Output as returned by pandoc. |
|
45 | Output as returned by pandoc. | |
46 | """ |
|
46 | """ | |
47 |
|
|
47 | command = ['pandoc', '-f', fmt, '-t', to] | |
|
48 | if extra_args: | |||
|
49 | command = command + extra_args | |||
|
50 | p = subprocess.Popen(command, | |||
48 | stdin=subprocess.PIPE, stdout=subprocess.PIPE |
|
51 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |
49 | ) |
|
52 | ) | |
50 | out, _ = p.communicate(cast_bytes(source, encoding)) |
|
53 | out, _ = p.communicate(cast_bytes(source, encoding)) | |
51 | out = out.decode(encoding, 'replace') |
|
54 | out = out.decode(encoding, 'replace') | |
52 | return out[:-1] |
|
55 | return out[:-1] | |
53 |
|
56 |
General Comments 0
You need to be logged in to leave comments.
Login now