11// Copyright: Ankitects Pty Ltd and contributors
22// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
33
4- use lazy_static:: lazy_static;
5- use regex:: Regex ;
6-
7- lazy_static ! {
8- pub ( crate ) static ref IMAGE_OCCLUSION_REGEX : Regex = Regex :: new(
9- r#"(?xsi)
10- ::image-occlusion:
11- "#
12- )
13- . unwrap( ) ;
14- }
15-
16- pub ( crate ) fn contains_image_occlusion ( text : & str ) -> bool {
17- IMAGE_OCCLUSION_REGEX . is_match ( text)
18- }
19-
204// split following
215// text = "rect:399.01,99.52,167.09,33.78:fill=#0a2cee:stroke=1"
226// with
@@ -33,12 +17,12 @@ pub fn get_image_cloze_data(text: &str) -> String {
3317 let mut rx = "" ;
3418 let mut ry = "" ;
3519 let mut points = "" ;
20+ let mut quesmaskcolor = "" ;
3621
3722 let parts: Vec < & str > = text. split ( ':' ) . collect ( ) ;
3823
3924 if parts. len ( ) >= 2 {
40- // parts[0] is image-occlusion, 1 is shape
41- shape = parts[ 1 ] ;
25+ shape = parts[ 0 ] ;
4226 for part in parts[ 1 ..] . iter ( ) {
4327 let values: Vec < & str > = part. split ( '=' ) . collect ( ) ;
4428 if values. len ( ) >= 2 {
@@ -49,10 +33,10 @@ pub fn get_image_cloze_data(text: &str) -> String {
4933 "height" => height = values[ 1 ] ,
5034 "fill" => fill = values[ 1 ] ,
5135 "stroke" => stroke = values[ 1 ] ,
52-
5336 "rx" => rx = values[ 1 ] ,
5437 "ry" => ry = values[ 1 ] ,
5538 "points" => points = values[ 1 ] ,
39+ "quesmaskcolor" => quesmaskcolor = values[ 1 ] ,
5640 _ => { }
5741 }
5842 }
@@ -117,32 +101,36 @@ pub fn get_image_cloze_data(text: &str) -> String {
117101 result. push_str ( & format ! ( "data-stroke=\" {}\" " , stroke) ) ;
118102 }
119103
104+ if !quesmaskcolor. is_empty ( ) {
105+ result. push_str ( & format ! ( "data-quesmaskcolor=\" {}\" " , quesmaskcolor) ) ;
106+ }
107+
120108 result
121109}
122110
123111//----------------------------------------
124112// Tests
125113//----------------------------------------
126114
127- #[ test]
128- fn test_contains_image_occlusion ( ) {
129- assert ! ( contains_image_occlusion(
130- "{{c5::image-occlusion:rect:left=10.0:top=20:width=30:height=10:fill=#ffe34d:stroke=5}}"
131- ) ) ;
132- }
133-
134115#[ test]
135116fn test_get_image_cloze_data ( ) {
136117 assert_eq ! (
137- get_image_cloze_data( "image-occlusion:rect:left=10:top=20:width=30:height=10:fill=#ffe34d:stroke=5" ) ,
138- format!( r#"data-shape="rect" data-left="10" data-top="20" data-width="30" data-height="10" data-fill="{}" data-stroke="5" "# , "#ffe34d" )
139- ) ;
118+ get_image_cloze_data(
119+ "rect:left=10:top=20:width=30:height=10:fill=#ffe34d:stroke=5:quesmaskcolor=#ff0000"
120+ ) ,
121+ format!(
122+ r#"data-shape="rect" data-left="10" data-top="20" data-width="30" data-height="10" data-fill="{}" data-stroke="5" data-quesmaskcolor="{}" "# ,
123+ "#ffe34d" , "#ff0000"
124+ )
125+ ) ;
140126 assert_eq ! (
141- get_image_cloze_data( "image-occlusion:ellipse:left=15:top=20:width=10:height=20:rx=10:ry=5:fill=red:stroke=5" ) ,
127+ get_image_cloze_data(
128+ "ellipse:left=15:top=20:width=10:height=20:rx=10:ry=5:fill=red:stroke=5"
129+ ) ,
142130 r#"data-shape="ellipse" data-rx="10" data-ry="5" data-left="15" data-top="20" data-width="10" data-height="20" data-fill="red" data-stroke="5" "#
143- ) ;
131+ ) ;
144132 assert_eq ! (
145- get_image_cloze_data( "image-occlusion: polygon:points=0,0 10,10 20,0:fill=blue:stroke=5" ) ,
133+ get_image_cloze_data( "polygon:points=0,0 10,10 20,0:fill=blue:stroke=5" ) ,
146134 r#"data-shape="polygon" data-points="[[0,0],[10,10],[20,0]]" data-fill="blue" data-stroke="5" "#
147- ) ;
135+ ) ;
148136}
0 commit comments