Skip to content

Commit 5f00a14

Browse files
committed
implement remaining filters and test filters
1 parent 3e04a31 commit 5f00a14

File tree

4 files changed

+163
-46
lines changed

4 files changed

+163
-46
lines changed

filters/data_type_filter.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

filters/simple_filters.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
DISPLAY_DATA_PRIORITY = [
2+
'text/html',
3+
'application/pdf',
4+
'text/latex',
5+
'image/svg+xml',
6+
'image/png',
7+
'image/jpeg',
8+
'text/markdown',
9+
'text/plain'
10+
];
11+
12+
function filter_data_type(output) {
13+
for (fmt of DISPLAY_DATA_PRIORITY) {
14+
if (output.hasOwnProperty(fmt)) return [fmt];
15+
}
16+
// TODO: handle warning
17+
return [undefined];
18+
}
19+
20+
function get_metadata(output, key, mimetype=undefined) {
21+
// from nbconvert/filters/metadata.py
22+
// Resolve an output metadata key
23+
// If mimetype given, resolve at mimetype level first,
24+
// then fallback to top-level.
25+
// Otherwise, just resolve at top-level.
26+
// Returns None if no data found.
27+
const md = output.metadata;
28+
if (mimetype &&
29+
md.hasOwnProperty(mimetype) &&
30+
md[mimetype] != undefined &&
31+
md[mimetype].hasOwnProperty(key) &&
32+
md[mimetype][key] != undefined) {
33+
return md[mimetype][key];
34+
}
35+
return md[key];
36+
}
37+
38+
function highlight_code(source, language, metadata=undefined) {
39+
return hljs.highlight(language, source, false, false);
40+
}
41+
42+
function json_dumps(obj) {
43+
return JSON.stringify(obj);
44+
}
45+
46+
function markdown2html(source) {
47+
const converter = new showdown.Converter();
48+
return converter.makeHtml(source);
49+
}
50+
51+
function posix_path(path) {
52+
return path.replace(/\\/g,'/');
53+
}
54+
55+
function strip_files_prefix(dirty_text) {
56+
// Fix all fake URLs that start with `files/`, stripping out the `files/` prefix.
57+
// Applies to both urls (for html) and relative paths (for markdown paths).
58+
return dirty_text.replace(/(src|href)\=([\'"]?)\/?files\//g, (m,p1,p2) => p1 + '=' + p2).replace(
59+
/(!?)\[(.*?)\]\(\/?files\/(.*?)\)/g, (m,p1,p2,p3) => p1 + '[' + p2 + '](' + p3 + ')');
60+
}
61+
62+
if (
63+
typeof module !== 'undefined' &&
64+
module.exports &&
65+
typeof exports !== 'undefined'
66+
) {
67+
module.exports = {
68+
filter_data_type: filter_data_type,
69+
get_metadata: get_metadata,
70+
highlight_code: highlight_code,
71+
json_dumps: json_dumps,
72+
markdown2html: markdown2html,
73+
posix_path: posix_path,
74+
strip_files_prefix: strip_files_prefix
75+
};
76+
}
77+
else {
78+
simple_filters = {
79+
filter_data_type: filter_data_type,
80+
get_metadata: get_metadata,
81+
highlight_code: highlight_code,
82+
json_dumps: json_dumps,
83+
markdown2html: markdown2html,
84+
posix_path: posix_path,
85+
strip_files_prefix: strip_files_prefix
86+
};
87+
}

spec/SpecRunner.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<!-- include source files here... -->
1515
<script src="../filters/ansi.js"></script>
16-
<script src="../filters/data_type_filter.js"></script>
16+
<script src="../filters/simple_filters.js"></script>
1717

1818
<!-- include spec files here... -->
1919
<script src="filters_spec.js"></script>

spec/filters_spec.js

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,92 @@ describe("Filters", function() {
9292
});
9393
});
9494

95-
describe("data_type_filter", function() {
95+
describe("simple_filters", function() {
9696
beforeAll(function () {
9797
if (
9898
typeof module !== 'undefined' &&
9999
module.exports &&
100100
typeof exports !== 'undefined'
101101
) {
102-
data_type_filter = require('../filters/data_type_filter.js');
102+
simple_filters = require('../filters/simple_filters.js');
103103
}
104104
});
105105

106106
describe("filter_data_type", function() {
107-
it("24bit rgb", function() {
108-
expect(data_type_filter.filter_data_type({"hair":"1", "water":2, "image/png":3, "rock":4.0})).toEqual(['image/png']);
109-
expect(data_type_filter.filter_data_type({"application/pdf":"file_path", "hair":2, "water":"yay", "png":'not a png', "rock":'is a rock'})).toEqual(['application/pdf']);
110-
expect(data_type_filter.filter_data_type({"hair":"this is not", "water":"going to return anything", "rock":"or is it"})).toEqual([undefined]);
107+
it("all", function() {
108+
expect(simple_filters.filter_data_type({"hair":"1", "water":2, "image/png":3, "rock":4.0})).toEqual(['image/png']);
109+
expect(simple_filters.filter_data_type({"application/pdf":"file_path", "hair":2, "water":"yay", "png":'not a png', "rock":'is a rock'})).toEqual(['application/pdf']);
110+
expect(simple_filters.filter_data_type({"hair":"this is not", "water":"going to return anything", "rock":"or is it"})).toEqual([undefined]);
111+
});
112+
});
113+
114+
describe("get_metadata", function() {
115+
it("all", function() {
116+
var obj = {
117+
'metadata': {
118+
'width': 1,
119+
'height': 2,
120+
'image/png': {
121+
'unconfined': true,
122+
'height': 3,
123+
}
124+
}
125+
}
126+
127+
expect(simple_filters.get_metadata(obj, 'nowhere')).toEqual(undefined);
128+
expect(simple_filters.get_metadata(obj, 'height')).toEqual(2);
129+
expect(simple_filters.get_metadata(obj, 'unconfined')).toEqual(undefined);
130+
expect(simple_filters.get_metadata(obj, 'unconfined', 'image/png')).toEqual(true);
131+
expect(simple_filters.get_metadata(obj, 'width', 'image/png')).toEqual(1);
132+
expect(simple_filters.get_metadata(obj, 'height', 'image/png')).toEqual(3);
133+
});
134+
});
135+
136+
// describe("highlight_code", function() {
137+
// it("all", function() {
138+
// expect(simple_filters.highlight_code()).toEqual();
139+
// });
140+
// });
141+
142+
143+
describe("json_dumps", function() {
144+
it("all", function() {
145+
expect(simple_filters.json_dumps()).toEqual();
146+
});
147+
});
148+
149+
// describe("markdown2html", function() {
150+
// it("all", function() {
151+
// expect(simple_filters.markdown2html()).toEqual();
152+
// });
153+
// });
154+
155+
describe("posix_path", function() {
156+
it("all", function() {
157+
expect(simple_filters.posix_path('path/with/forward\\and\\back')).toEqual('path/with/forward/and/back');
158+
});
159+
});
160+
161+
describe("strip_files_prefix", function() {
162+
it("all", function() {
163+
expect(simple_filters.strip_files_prefix('')).toEqual('');
164+
expect(simple_filters.strip_files_prefix('/files')).toEqual('/files');
165+
expect(simple_filters.strip_files_prefix('test="/files"')).toEqual('test="/files"')
166+
expect(simple_filters.strip_files_prefix('My files are in `files/`')).toEqual('My files are in `files/`');
167+
expect(simple_filters.strip_files_prefix('<a href="files/test.html">files/test.html</a>')).toEqual('<a href="test.html">files/test.html</a>');
168+
expect(simple_filters.strip_files_prefix('<a href="/files/test.html">files/test.html</a>')).toEqual('<a href="test.html">files/test.html</a>');
169+
expect(simple_filters.strip_files_prefix("<a href='files/test.html'>files/test.html</a>")).toEqual("<a href='test.html'>files/test.html</a>");
170+
expect(simple_filters.strip_files_prefix('<img src="files/url/location.gif">')).toEqual('<img src="url/location.gif">');
171+
expect(simple_filters.strip_files_prefix('<img src="/files/url/location.gif">')).toEqual('<img src="url/location.gif">');
172+
expect(simple_filters.strip_files_prefix('hello![caption]')).toEqual('hello![caption]');
173+
expect(simple_filters.strip_files_prefix('hello![caption](/url/location.gif)')).toEqual('hello![caption](/url/location.gif)');
174+
expect(simple_filters.strip_files_prefix('hello![caption](url/location.gif)')).toEqual('hello![caption](url/location.gif)');
175+
expect(simple_filters.strip_files_prefix('hello![caption](url/location.gif)')).toEqual('hello![caption](url/location.gif)');
176+
expect(simple_filters.strip_files_prefix('hello![caption](files/url/location.gif)')).toEqual('hello![caption](url/location.gif)');
177+
expect(simple_filters.strip_files_prefix('hello![caption](/files/url/location.gif)')).toEqual('hello![caption](url/location.gif)');
178+
expect(simple_filters.strip_files_prefix('hello [text](/files/url/location.gif)')).toEqual('hello [text](url/location.gif)');
179+
expect(simple_filters.strip_files_prefix('hello [text space](files/url/location.gif)')).toEqual('hello [text space](url/location.gif)');
111180
});
112181
});
113-
114-
115-
// assert "image/png" in filter({"hair":"1", "water":2, "image/png":3, "rock":4.0})
116-
// assert "application/pdf" in filter({"application/pdf":"file_path", "hair":2, "water":"yay", "png":'not a png', "rock":'is a rock'})
117-
// self.assertEqual(filter({"hair":"this is not", "water":"going to return anything", "rock":"or is it"}), [])
118182
});
119183
});

0 commit comments

Comments
 (0)