Include Images with Exported Text in rtfText or htmlText?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Include Images with Exported Text in rtfText or htmlText?

Post by deeverd » Sun Jun 28, 2015 4:45 pm

Hello Forum,

Only recently became aware of imageSource for attaching images to text in scrolling fields, which works great, but was surprised to see that the images don't export with the formatted text when using rtfText or htmlText. Is there a general way that users can export or print a user manual in a program that comes complete with the images?

I can't imagine this is a very new question but after a day of experimentation and spending a couple hours looking through the forum this morning, I still don't feel any closer to finding a solution.

Any suggestions would be most welcome.

Cheers,
deeverd

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Include Images with Exported Text in rtfText or htmlText

Post by FourthWorld » Sun Jun 28, 2015 5:26 pm

The rtfText property is described in the Dictionary as applying to text styles within the field contents only, and only for the subset of styles common to both LiveCode and the RTF spec. It might be nice to see that extended to include image references, but currently it's for style runs only.

The htmlText property is designed to provide a plain-text representation of a field's contents, or any part of a field's contents, with complete fidelity sufficient to reproduce that content in another LiveCode field. It isn't true HTML, but is so named because it adopts many conventions commonly found in HTML.

While htmlText will, like the HTML that inspired it, include image references, like HTML it doesn't include the image itself.

Translating htmlText to Web-ready HTML is doable but challenging, since the two specs are designed for very different purposes. For example, font sizes in htmlText as pixel-specific, whereas in HTML they're rendered as relative. Also, modern HTML prefers CSS for styling, but in keeping with its own goals htmlText embeds style info inline and relies on no separate languages like CSS. In fact, the two are so different that had it not been for so much legacy code referring to the property by its current name there was an interest at one point in renaming it to "xmlText" to more clearly distinguish it from the HTML browsers expect to see.

If you were writing an HTML page which included images, you'd need to provide those images as separate files. This is true for htmlText as well. Both HTML and htmlText include references to any images they display, but not the image itself, in the "src" attribute of the "<img>" tag. If your images are already referenced from files this will be a little simpler, but if your images are referenced by IDs of imported image objects you'll need to export those image objects and rewrite the "src" attribute in your output HTML accordingly.

All that said, your core question may be much easier to satisfy:
Is there a general way that users can export or print a user manual in a program that comes complete with the images?
The revPrintField command will print a field of any length, complete with images.

If you also need to export, you can choose a more portable document format than HTML by using the built-in support for the Portable Document Format, PDF - see the Dictionary entry for "open printing to pdf".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by deeverd » Sun Jun 28, 2015 5:43 pm

Hello FourthWorld,

That's a lot of awesome information. Thanks! Now, I can now move forward over this latest hurdle. I had been so busy trying to export fields with images, I had neglected to look and see if it was possible to print with the images. Certainly very good news.

I like your idea of exporting as PDF. I'll roll up my sleeves and spend a good part of today figuring out how that works. Now that you mentioned pdf, a light bulb came on about another possibility. It's been years since I've seen it, but I remember that Curry Kenworthy came up with a superb program for exporting documents as MS Word. Actually superb is an understatement. It was a really great program. I'm guessing that his plug-in would have no trouble including the images in an export.

There is one pdf question with LiveCode I've been wondering... If I were to make my user manual on a word processor, and then save as pdf, how would I embed it into a stack so that a user could easily download it?

Searching around the forum, I saw some interesting talk on using pdf files to embed audio, etc, but missed finding anything about just simply putting it into a stack in a user-friendly retrievable manner.

If you have any knowledge along those lines, it would be extremely helpful.

Thanks so much,
deeverd
Last edited by deeverd on Wed Jul 15, 2015 6:03 am, edited 1 time in total.

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by SparkOut » Sun Jun 28, 2015 6:03 pm

Any data, formatted text or raw binary, can be imported into a stack and stored as a custom property. This will be saved as part of the stack or standalone.
To retrieve it you can interrogate the data by reading the custom property if it is a suitable format. For binary and third-party file types you would be able to write the data to a file on the local user's computer in a suitable location, such as the documents or temporary folder. You can then launch the document, eg a PDF file.

answer file "Select the pdf (or other type of file)"
set the cStoredFile of this stack to url ("binfile:" & it)

(Needs error trapping but you get the idea)

After distributing your application,

put specialFolderPath("documents") & "/myFile.PDF" into tPath
put the cStoredFile of this stack into url ("binfile:" & tPath)
launch document tPath


With some extra work, you might display a pdf within the stack, perhaps in a browser, rather than launching a separate reader, but you would still need to write the file out first.
Last edited by SparkOut on Sun Jun 28, 2015 6:14 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Include Images with Exported Text in rtfText or htmlText

Post by FourthWorld » Sun Jun 28, 2015 6:12 pm

deeverd wrote:That's a lot of awesome information. Thanks!
Very glad to hear that was useful. Sunday mornings are hard to come by, but they're quiet enough that it affords me an opportunity to explore ways of explaining things in preparation for some LiveCode training materials I'm working on. Your feedback is very helpful guidance on that - thanks.
Now that you mentioned pdf, a light bulb came on about another possibility. It's been years since I've seen it, but I remember that Ken Curry came up with a superb program for exporting documents as MS Word. Actually superb is an understatement. It was a really great program. I'm guessing that his plug-in would have no trouble including the images in an export.
Curry has an uncommon tenacity for details, and his WordLib library is worth far more than he asks for it.
There is one pdf question with LiveCode I've been wondering... If I were to make my user manual on a word processor, and then save as pdf, how would I embed it into a stack so that a user could easily download it?
I'm a big fan of storing PDFs on a Web site where I can, so I can continually refine them independent of the app itself. If that would work for your needs remember that you can launch any URL, even initiating a download, with the "launch url" command, e,g.:

Code: Select all

launch url "http://fourthworld.net/revnet/devolution/4W_FlightRecorder.rev.gz"
If you want to store a PDF within the app itself you can use a custom property, e.g.:

Code: Select all

put the uManual of this stack into url ("binfile:"& specialFolderPath("desktop")&/MyAppManual.pdf")
To store the PDF file into the app you can make a tool that uses this in development:

Code: Select all

on mouseUp
   answer file "Select your manual file:"
   if it is empty then exit to top
   put url ("binfile:"& it) into tData
   set the uManual of this stack to tData
   save this stack
end mouseUp
Custom properties are super-cool for many uses. They can contain any type of data, plain text or binary, or even arrays. And you can even use array syntax with them for all kinds of interesting storage solutions - here's a description of some of that:
http://lists.runrev.com/pipermail/use-l ... 06158.html
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by deeverd » Sun Jun 28, 2015 6:18 pm

Thank you SparkOut,

I apparently have a lot more to learn about custom properties. Would have never guessed that's how it's done.

Along those lines, earlier this morning I found Gerard McCarthy's interesting plugin for speeding the copying of object properties on LiveShare. I know that's a different ballgame but it looks like it's going to increase my knowledge of properties in general, besides being very useful for formatting groups of objects.

Anyway, thanks again. I'm a completely self-taught software developer with no formal training in any programming languages, so without this brilliant forum, there would be a lot less I could learn and accomplish. Great place and great people!

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by deeverd » Sun Jun 28, 2015 6:24 pm

Thanks FourthWorld,

Just downloaded the article "Updating Data Substacks" from the link you provided. Looks like some excellent reading for a Sunday afternoon.

Also copied down the helpful examples you provided. Great stuff! Thank you!

Actually, just a bit of history, but many years ago, one of the first major challenges I had programming had to do with frequency lists. Way back then, you provided an incredible HyperCard trick that I've used hundreds of times for rapidly processing text in a myriad of ways. Just some proof that one good deed can be remembered and appreciated for a long, long time.

Cheers,
deeverd (Allen)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Include Images with Exported Text in rtfText or htmlText

Post by FourthWorld » Sun Jun 28, 2015 6:45 pm

deeverd wrote:Actually, just a bit of history, but many years ago, one of the first major challenges I had programming had to do with frequency lists. Way back then, you provided an incredible HyperCard trick that I've used hundreds of times for rapidly processing text in a myriad of ways. Just some proof that one good deed can be remembered and appreciated for a long, long time.
That's a wonderful tale, Allen. Thanks. If you happen across that stack I'd love to see it. A hard drive failure many years ago (before I became obsessive with five-fold multi-location backup redundancy) caused me to lose some older things I'd worked on. Who knows, if there's anything there that would be useful today I might even port it to LiveCode and release it as open source.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by deeverd » Sun Jun 28, 2015 6:59 pm

Hello FourthWorld,

Just found your original text for the word frequency script your provided way back on 8-2-07 (back when dinosaurs still roamed the Earth). A correction, I had said it was a HyperCard trick, but looking at your notes on the original script, I now see it's from MetaCard:

Code: Select all

Here's a word count example from the MetaCard IDE: 

on mouseUp 
put empty into field "result" 
answer file "Select a text file for input:" 
if it is empty then exit mouseUp 
# let user know we're working on it 
set the cursor to watch 
put it into inputFile 
open file inputFile for read 
read from file inputFile until eof 
put it into fileContent 
close file inputFile 
# wordCount is an associative array, its indexes are words 
# with the contents of each element being number of times 
# that word appears 
repeat for each word w in fileContent 
add 1 to wordCount[w] 
end repeat 
# copy all the indexes that is in the wordCount associative array 
put keys(wordCount) into keyWords 
# sort the indexes -- keyWords contains a list of elements in array 
sort keyWords 
repeat for each line l in keyWords 
put l & tab & wordCount[l] & return after displayResult 
end repeat 
put displayResult into field "result" 
end mouseUp
Cheers,
Allen

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Include Images with Exported Text in rtfText or htmlText

Post by FourthWorld » Sun Jun 28, 2015 9:11 pm

I wish I could take credit for that, Allen, but that example comes from Dr. Scott Raney, inventor of the MetaCard engine which was later acquired to become LiveCode.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by Thierry » Mon Jun 29, 2015 2:29 pm

deeverd wrote: Any suggestions would be most welcome.
Hello Allen,

I would like to express my thanks to you for this question :)

This makes me write another code sample for my sunnYrex tutorial
to convert HtmlText with imagesource to a true Html file.

So, here is the script which works on the htmlText of the green field ( bottom-right).
sunnY 2015-06-29.png
and you'll get this:

Code: Select all

<p></p><img src="cat.png"></img> sunnYrex <img src="Logo.png"></img> rocks!</p>
This one is the simplest solution.

Now imagine that all your images are only in your stack as image objects.
Basically, you 'll have to scan for each <img> tag in your htmlText,
catch the name of the image, export the image in a folder, transform the <img> tag to a pure Html one,
do that for every <img> tag and save the result as an html file.

Well, this is one way to do it with sunnYrex:

Code: Select all


local Imgfolder = "Imagespathfolder/"

on SaveMyImage  anImage
    put Imgfolder & anImage into fullpathImage
    -- save the image on disk
    put image anImage into url ( "binfile: " & fullpathImage)
   -- build the text which will replace the match of our calling regex
    return format( "<img src=%s></img>", fullpathImage)
end SaveMyImage

-- get the htmlText
put the htmlText of fld "Fin" into theText

-- regex to catch <img> tag plus capture the image name
put "<img src=(.+?" &quote& ")[^>]*>"  into theRegex

-- call SaveMyImage with the name of the captured image for each match
put "${ SaveMyImage \1 }"  into smartReplacement

if sunnYreplace( theText, theRegex, smartReplacement, outText) then
     -- save outText (your Html  text) wherever you like and open it in your navigator.
      set the text of  field "Fout" to outText
end if
Obviously, this can be done without this library.
But be ready for a script with more lines...

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by deeverd » Mon Jun 29, 2015 7:24 pm

Hello Thierry,

You're quite welcome!

History repeatedly shows that the springs of great genius is often inspired by the ignorance of another. So glad my ignorance has transformed into incredible brilliance in your capable hands.

BTW, when will SunnYrex be on the market? That looks like a tutorial program that is an absolute must have.

Cheers,
deeverd

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Include Images with Exported Text in rtfText or htmlText

Post by Thierry » Tue Jun 30, 2015 11:52 am

deeverd wrote:Hello Thierry,

You're quite welcome!

History repeatedly shows that the springs of great genius is often inspired by the ignorance of another.
So glad my ignorance has transformed into incredible brilliance in your capable hands.
Deeverd, thanks for your kind words :)
I'm always hunting for nice exercices which are a bit but not too much complex.

Honestly, I'm not a genius. I did worked professionaly with regexes for 30 years or so,
having managed few complexes projects with quite a number of regexes inside them :)
I like very much regex because it is an incredible language for what it has been done!
BTW, when will SunnYrex be on the market?
That looks like a tutorial program that is an absolute must have.
In fact it's a library which should help any Livecoders to write less code around any regex.
So, first you need a bit of understanding of how you can code with regex in LiveCode.
As you have seen, the LC script to export your images in a pure Html is quite simple and short.

The library is almost fine and didn't changed since last October,
but I have not much of free time to deploy it,
and not much Livecoders will be interested in it.

For those interested,
you could always send me a note, so I could figure out who might like it.

Kind regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply