<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Injecting images to text with tags to build display box in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Injecting-images-to-text-with-tags-to-build-display-box/m-p/542862#M76214</link>
    <description>&lt;P&gt;My first instinct is to do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// example associative array
aa = Associative Array();
aa["text"] = "First line with no image
¤tile|img¤
text and ¤windmap|img¤ image
multiple ¤tile|img¤ ¤windmap|img¤ images
last line";
aa["images"] = Associative Array();
aa["images"]["tile"] = New Image("$SAMPLE_IMAGES/tile.jpg");
aa["images"]["windmap"] = New Image("$SAMPLE_IMAGES/windmap.png");

for each({imgn}, aa["images"]&amp;lt;&amp;lt;get keys, aa["images"][imgn] &amp;lt;&amp;lt; Set Size({128,128}));

nw = New Window("test", vlb = v list box() );
hlb = {};

lines = words( aa["text"], "\!N" );

for each({line, l}, lines,
	parts = words( line, "¤" );
	hlb[l] = h list box();
	for each ({p}, parts, 
		cur_img = Regex(p, "^([^ ]+)\|img$", "\1");
		if(	Is Missing(cur_img),
			insert into(hlb[l], text box(p) ),
			hlb[l] &amp;lt;&amp;lt; Append( aa["images"][cur_img] );
		);
	);
	insert into(vlb, hlb[l])
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Sep 2022 13:23:54 GMT</pubDate>
    <dc:creator>ih</dc:creator>
    <dc:date>2022-09-09T13:23:54Z</dc:date>
    <item>
      <title>Injecting images to text with tags to build display box</title>
      <link>https://community.jmp.com/t5/Discussions/Injecting-images-to-text-with-tags-to-build-display-box/m-p/542791#M76208</link>
      <description>&lt;P&gt;I have a JSON which I get with GET request. The JSON contains two keys, &lt;STRONG&gt;text&lt;/STRONG&gt; and &lt;STRONG&gt;images&lt;/STRONG&gt;.&amp;nbsp;&lt;STRONG&gt;images&lt;/STRONG&gt; has list of objects with name of image and then the image.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In &lt;STRONG&gt;text&lt;/STRONG&gt; "¤imagename|img¤" parts are to be replaced with "imagename" image found from &lt;STRONG&gt;images&lt;/STRONG&gt;.&amp;nbsp;Below is an example in associative array format&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;aa = Associative Array();
aa["text"] = "First line with no image
¤tile|img¤
text and ¤windmap|img¤ image
multiple ¤tile|img¤ ¤windmap|img¤ images
last line";
aa["images"] = Associative Array();
aa["images"]["tile"] = New Image("$SAMPLE_IMAGES/tile.jpg");
aa["images"]["windmap"] = New Image("$SAMPLE_IMAGES/windmap.png");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the original application, the text has images embedded within the text, so it looks something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1662705837440.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/45333iCB45114E7ABCC0E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1662705837440.png" alt="jthi_0-1662705837440.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the question is: how to build this with JSL? I have written recursive function to do this (below), but I'm looking for other ideas / to provide a challenge to other scripters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My current solution (which works good enough for now):&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// example associative array
aa = Associative Array();
aa["text"] = "First line with no image
¤tile|img¤
text and ¤windmap|img¤ image
multiple ¤tile|img¤ ¤windmap|img¤ images
last line";
aa["images"] = Associative Array();
aa["images"]["tile"] = New Image("$SAMPLE_IMAGES/tile.jpg");
aa["images"]["windmap"] = New Image("$SAMPLE_IMAGES/windmap.png");


find_imgs = function({str}, {Default Local},
	/* find img names from string */
	imgs = {};
	cur_img = Regex(str, "(¤.+?\|img¤)");
	While(!IsMissing(cur_img),
		Insert Into(imgs, cur_img);
		Substitute Into(str, cur_img, "");
		cur_img = Regex(str, "(¤.+?\|img¤)");
	);
	return(imgs);
);

create_line_hlb = function({cur_str, img_list, aa, hlb = H List Box()}, {Default Local},
	/* create one line of the text */
	If(Length(cur_str) &amp;lt; 1, // no more cur str
		return(hlb);
	);

	If(N Items(img_list) == 0, // no images left -&amp;gt; add rest of text to text box 
		Insert Into(hlb, Text Box(cur_str));
		return(hlb);
	);

	img_idx = Contains(cur_str, img_list[1]);
	If(img_idx == 1,
		cur_img = Remove From(img_list, 1)[1];
		img = aa[Substr(Word(1, cur_img, "|"), 2)];
		hlb &amp;lt;&amp;lt; Append(img);
		cur_str = Substr(cur_str, Length(cur_img) + 1);
	,
		txt = Substr(cur_str, 1, img_idx - 1);
		Insert Into(hlb, Text Box(txt));
		cur_str = Substr(cur_str, img_idx );
	);
	create_line_hlb(cur_str, img_list, aa, hlb);
);


text_to_displaybox = function({aa}, {Default Local},
	/* create the displaybox based on associative array */
	vlb = V List Box();
	For Each({line}, Words(aa["text"], "\!N"),
		img_list = find_imgs(line);
		hlb = create_line_hlb(line, img_list, aa["images"] /*,H List Box()*/);
		vlb &amp;lt;&amp;lt; Append(hlb);
	);
	return(vlb);
);

// Demo of script
// change size to make new window a bit smaller
aa["images"]["tile"] &amp;lt;&amp;lt; Set Size({128, 128});
aa["images"]["windmap"] &amp;lt;&amp;lt; Set Size({128, 128});

nw = New window("test", text_to_displaybox(aa));
Write();&amp;nbsp;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 15:58:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Injecting-images-to-text-with-tags-to-build-display-box/m-p/542791#M76208</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-06-09T15:58:09Z</dc:date>
    </item>
    <item>
      <title>Re: Injecting images to text with tags to build display box</title>
      <link>https://community.jmp.com/t5/Discussions/Injecting-images-to-text-with-tags-to-build-display-box/m-p/542862#M76214</link>
      <description>&lt;P&gt;My first instinct is to do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// example associative array
aa = Associative Array();
aa["text"] = "First line with no image
¤tile|img¤
text and ¤windmap|img¤ image
multiple ¤tile|img¤ ¤windmap|img¤ images
last line";
aa["images"] = Associative Array();
aa["images"]["tile"] = New Image("$SAMPLE_IMAGES/tile.jpg");
aa["images"]["windmap"] = New Image("$SAMPLE_IMAGES/windmap.png");

for each({imgn}, aa["images"]&amp;lt;&amp;lt;get keys, aa["images"][imgn] &amp;lt;&amp;lt; Set Size({128,128}));

nw = New Window("test", vlb = v list box() );
hlb = {};

lines = words( aa["text"], "\!N" );

for each({line, l}, lines,
	parts = words( line, "¤" );
	hlb[l] = h list box();
	for each ({p}, parts, 
		cur_img = Regex(p, "^([^ ]+)\|img$", "\1");
		if(	Is Missing(cur_img),
			insert into(hlb[l], text box(p) ),
			hlb[l] &amp;lt;&amp;lt; Append( aa["images"][cur_img] );
		);
	);
	insert into(vlb, hlb[l])
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 13:23:54 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Injecting-images-to-text-with-tags-to-build-display-box/m-p/542862#M76214</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2022-09-09T13:23:54Z</dc:date>
    </item>
  </channel>
</rss>

