Improved video content filter to be more flexible, and allow custom template.

This commit is contained in:
Jocelyn Fiat
2017-03-30 15:41:04 +02:00
parent 0d5630bb58
commit a01d161864
4 changed files with 162 additions and 90 deletions

View File

@@ -24,8 +24,8 @@ feature {NONE} -- Initialization
default_create default_create
do do
Precursor Precursor
width := 420 default_width := 420
height := 315 default_height := 315
end end
feature -- Access feature -- Access
@@ -38,6 +38,36 @@ feature -- Access
help: STRING = "Embed video using the following pattern: [video:url width:X height:Y], width and height are optionals." help: STRING = "Embed video using the following pattern: [video:url width:X height:Y], width and height are optionals."
feature -- Settings
default_width: INTEGER;
-- Specifies the width of an <iframe> in pixels.
default_height: INTEGER
-- Specifies the height of an <iframe> in pixels.
template: detachable READABLE_STRING_8
-- Optional template using $url, $att .
-- For instance:
-- <iframe src="$url" $att></iframe>"
feature -- Settings change
set_default_width (w: like default_width)
do
default_width := w
end
set_default_height (h: like default_height)
do
default_height := h
end
set_template (tpl: like template)
do
template := tpl
end
feature -- Conversion feature -- Conversion
filter (a_text: STRING_8) filter (a_text: STRING_8)
@@ -77,10 +107,10 @@ feature -- Conversion
local local
i,j,n: INTEGER i,j,n: INTEGER
s,k,v: STRING_8 s,k,v: STRING_8
l_url: STRING_8 l_url, l_att: STRING_8
l_width, l_height: detachable STRING l_width, l_height, l_extra: detachable STRING
do do
s := a_text.substring (a_lower + 1, a_upper - 1) s := a_text.substring (a_lower + 7, a_upper - 1)
s.left_adjust s.left_adjust
i := next_space_position (s, 1) i := next_space_position (s, 1)
if i > 0 then if i > 0 then
@@ -96,6 +126,8 @@ feature -- Conversion
j := s.index_of (':', i) j := s.index_of (':', i)
if j > 0 then if j > 0 then
k := s.head (j - 1) k := s.head (j - 1)
k.left_adjust
k.right_adjust
s.remove_head (j) s.remove_head (j)
s.left_adjust s.left_adjust
i := 1 i := 1
@@ -103,10 +135,14 @@ feature -- Conversion
j := next_space_position (s, 1) j := next_space_position (s, 1)
if j > 0 then if j > 0 then
v := s.head (j - 1) v := s.head (j - 1)
v.left_adjust
v.right_adjust
s.remove_head (j) s.remove_head (j)
s.left_adjust s.left_adjust
else else
v := s.substring (i, n) v := s.substring (i, n)
v.left_adjust
v.right_adjust
s.wipe_out s.wipe_out
end end
n := s.count n := s.count
@@ -116,41 +152,69 @@ feature -- Conversion
elseif k.is_case_insensitive_equal ("height") then elseif k.is_case_insensitive_equal ("height") then
l_height := v l_height := v
else else
check supported: False end -- Ignore
end end
else else
s.left_adjust
s.right_adjust
if not s.is_whitespace then
l_extra := s
end
i := n + 1 i := n + 1
end end
end end
else else
s.remove_head (6)
l_url := s l_url := s
end end
if not l_url.is_whitespace then if not l_url.is_whitespace then
create Result.make_from_string ("<iframe src=%"")
Result.append (l_url)
Result.append_character ('%"')
if l_width = Void then if l_width = Void then
if width > 0 then if default_width > 0 then
l_width := width.out l_width := default_width.out
end end
end end
if l_height = Void then if l_height = Void then
if height > 0 then if default_height > 0 then
l_height := height.out l_height := default_height.out
end end
end end
create l_att.make_empty
if l_width /= Void then if l_width /= Void then
Result.append (" width=%"") if not l_att.is_empty then
Result.append (l_width) l_att.append_character (' ')
Result.append_character ('%"') end
l_att.append ("width=%"")
l_att.append (l_width)
l_att.append_character ('%"')
end end
if l_height /= Void then if l_height /= Void then
Result.append (" height=%"") if not l_att.is_empty then
Result.append (l_height) l_att.append_character (' ')
Result.append_character ('%"') end
l_att.append ("height=%"")
l_att.append (l_height)
l_att.append_character ('%"')
end
if l_extra /= Void and then not l_extra.is_empty then
if not l_att.is_empty and not l_extra[1].is_space then
l_att.append_character (' ')
end
l_att.append (l_extra)
end
if attached template as tpl then
create Result.make_from_string (tpl)
Result.replace_substring_all ("$url", l_url)
Result.replace_substring_all ("$att", l_att)
else
create Result.make_from_string ("<iframe src=%"")
Result.append (l_url)
Result.append_character ('%"')
if not l_att.is_empty then
Result.append_character (' ')
end
Result.append (l_att)
Result.append ("></iframe>")
end end
Result.append ("frameborder=%"0%" allowfullscreen></iframe>")
end end
end end
@@ -171,12 +235,23 @@ feature -- Conversion
end end
end end
feature {NONE} -- Implementation next_non_space_position (a_text: STRING; a_start_index: INTEGER): INTEGER
local
n: INTEGER
do
from
Result := a_start_index
n := a_text.count
until
not a_text[Result].is_space or Result > n
loop
Result := Result + 1
end
if Result > n then
Result := 0
end
end
width: INTEGER; invariant
-- Specifies the width of an <iframe> in pixels.
height: INTEGER
-- Specifies the height of an <iframe> in pixels.
end end

View File

@@ -15,7 +15,7 @@ inherit
feature -- Test routines feature -- Test routines
test_video_filter test_video_filter_01
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
@@ -23,88 +23,88 @@ feature -- Test routines
expected_text: STRING expected_text: STRING
do do
text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk]" text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk]"
expected_text := "<iframe width=%"420%" height=%"315%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"420%" height=%"315%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_2 test_video_filter_02
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
text: STRING text: STRING
expected_text: STRING expected_text: STRING
do do
text := "[ video : https://www.youtube.com/embed/jBMOSSnCMCk ]" text := "[video: https://www.youtube.com/embed/jBMOSSnCMCk ]"
expected_text := "<iframe width=%"420%" height=%"315%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"420%" height=%"315%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_3 test_video_filter_03
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
text: STRING text: STRING
expected_text: STRING expected_text: STRING
do do
text := "[ video :https://www.youtube.com/embed/jBMOSSnCMCk ]" text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk ]"
expected_text := "<iframe width=%"420%" height=%"315%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"420%" height=%"315%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_4 test_video_filter_04
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
text: STRING text: STRING
expected_text: STRING expected_text: STRING
do do
text := "[ video :https://www.youtube.com/embed/jBMOSSnCMCk height:425]" text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height:425]"
expected_text := "<iframe width=%"420%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"420%" height=%"425%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_5 test_video_filter_05
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
text: STRING text: STRING
expected_text: STRING expected_text: STRING
do do
text := "[ video :https://www.youtube.com/embed/jBMOSSnCMCk height : 425]" text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height : 425]"
expected_text := "<iframe width=%"420%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"420%" height=%"425%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_6 test_video_filter_06
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
text: STRING text: STRING
expected_text: STRING expected_text: STRING
do do
text := "[ video :https://www.youtube.com/embed/jBMOSSnCMCk height : 425 width: 425]" text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height : 425 width: 425]"
expected_text := "<iframe width=%"425%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"425%" height=%"425%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_7 test_video_filter_07
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
@@ -112,43 +112,13 @@ feature -- Test routines
expected_text: STRING expected_text: STRING
do do
text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height:425 width:425]" text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height:425 width:425]"
expected_text := "<iframe width=%"425%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"425%" height=%"425%"></iframe>"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_08
test_video_filter_8
-- New test routine
local
f: VIDEO_CONTENT_FILTER
text: STRING
expected_text: STRING
do
text := "[height:425 width:425 video:https://www.youtube.com/embed/jBMOSSnCMCk ]"
expected_text := "<iframe width=%"425%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>"
create f
f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text))
end
test_video_filter_9
-- New test routine
local
f: VIDEO_CONTENT_FILTER
text: STRING
expected_text: STRING
do
text := "[ width:425 video:https://www.youtube.com/embed/jBMOSSnCMCk height:425]"
expected_text := "<iframe width=%"425%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>"
create f
f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text))
end
test_video_filter_10
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
@@ -156,14 +126,27 @@ feature -- Test routines
expected_text: STRING expected_text: STRING
do do
text := "[ wrong:425 video:https://www.youtube.com/embed/jBMOSSnCMCk height:425]" text := "[ wrong:425 video:https://www.youtube.com/embed/jBMOSSnCMCk height:425]"
expected_text := "<iframe width=%"420%" height=%"425%"%Nsrc=%"https://www.youtube.com/embed/jBMOSSnCMCk%">%N</iframe>" expected_text := "[ wrong:425 video:https://www.youtube.com/embed/jBMOSSnCMCk height:425]"
create f create f
f.filter (text) f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_09
-- New test routine
local
f: VIDEO_CONTENT_FILTER
text: STRING
expected_text: STRING
do
text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height:425 foo:bar foo=bar foobar frameborder=%"0%" allowfullscreen]"
expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"420%" height=%"425%" foo=bar foobar frameborder=%"0%" allowfullscreen></iframe>"
create f
f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text))
end
test_video_filter_11 test_video_filter_10
-- New test routine -- New test routine
local local
f: VIDEO_CONTENT_FILTER f: VIDEO_CONTENT_FILTER
@@ -177,6 +160,24 @@ feature -- Test routines
assert ("expected iframe with video", text.same_string (expected_text)) assert ("expected iframe with video", text.same_string (expected_text))
end end
test_video_filter_tpl_01
-- New test routine
local
f: VIDEO_CONTENT_FILTER
text: STRING
expected_text: STRING
do
text := "[video:https://www.youtube.com/embed/jBMOSSnCMCk height:425 foo:bar foo=bar foobar]"
create f
f.set_template ("<iframe src=%"$url%" $att allowfullscreen></iframe>")
f.set_default_width (500)
f.set_default_height (400)
expected_text := "<iframe src=%"https://www.youtube.com/embed/jBMOSSnCMCk%" width=%"500%" height=%"425%" foo=bar foobar allowfullscreen></iframe>"
f.filter (text)
assert ("expected iframe with video", text.same_string (expected_text))
end
end end

View File

@@ -1,3 +0,0 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<redirection xmlns="http://www.eiffel.com/developers/xml/configuration-1-16-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-16-0 http://www.eiffel.com/developers/xml/configuration-1-16-0.xsd" uuid="BD491995-C14C-4413-B09A-C1B4EDDA3116" message="Obsolete: use testing.ecf !" location="testing.ecf">
</redirection>

View File

@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-16-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-16-0 http://www.eiffel.com/developers/xml/configuration-1-16-0.xsd" name="embedded_video_testing" uuid="BD491995-C14C-4413-B09A-C1B4EDDA3116"> <system xmlns="http://www.eiffel.com/developers/xml/configuration-1-15-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-15-0 http://www.eiffel.com/developers/xml/configuration-1-15-0.xsd" name="embedded_video_testing" uuid="BD491995-C14C-4413-B09A-C1B4EDDA3116">
<target name="embedded_video_testing"> <target name="embedded_video_testing">
<root class="ANY" feature="default_create"/> <root class="ANY" feature="default_create"/>
<option warning="true" void_safety="all">
</option>
<file_rule> <file_rule>
<exclude>/.git$</exclude> <exclude>/.git$</exclude>
<exclude>/.svn$</exclude> <exclude>/.svn$</exclude>
@@ -9,13 +11,10 @@
</file_rule> </file_rule>
<option warning="true"> <option warning="true">
</option> </option>
<capability> <library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<concurrency use="none"/> <library name="embedded_module" location="..\embedded_video-safe.ecf" readonly="false"/>
</capability> <library name="testing" location="$ISE_LIBRARY\library\testing\testing-safe.ecf"/>
<library name="base" location="$ISE_LIBRARY\library\base\base.ecf"/> <library name="text_filter" location="$ISE_LIBRARY\unstable\library\text\text_filter\text_filter-safe.ecf"/>
<library name="embeded_module" location="..\video.ecf"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing.ecf"/>
<library name="text_filter" location="$ISE_LIBRARY\unstable\library\text\text_filter\text_filter.ecf"/>
<tests name="src" location=".\"/> <tests name="src" location=".\"/>
</target> </target>
</system> </system>