Nisus HTML script now handles floating content
My Nisus simple HTML publish script now handles floating images and floating text boxes. This means that floating images are no longer lost, and neither are tables or other text inside of floating text boxes.
This was made possible by several new features in Nisus’s macro language. For images, the script no longer has to extract the image from RTF. Selections have an enclosedInlineImages and an enclosedFloatingContents method, making it simple enough to grab any images in a selection:
[toggle code]
- $currentSelection = $thisDocument.textSelection
- #are there any images?
- $images = $currentSelection.enclosedInlineImages
-
If $images
-
ForEach $image in $images
- SaveImage($image)
- End
-
ForEach $image in $images
- End
- $floats = $currentSelection.enclosedFloatingContents
-
If $floats
-
ForEach $float in $floats
-
If $float.isImage
- SaveImage($float.image)
- End
-
If $float.isImage
- End
-
ForEach $float in $floats
- End
And images themselves are now objects with properties and methods, including the ability to write themselves out to a file:
[toggle code]
-
Define Command SaveImage($image, $imageFolder)
- File.createFolderWithPath $imageFolder
- $imagePath = $image.writeWebImageToFolderAtPath $imageFolder
- $imageFileName = $imagePath.lastFilePathComponent
- $imageName = $imageFileName
-
Begin Perl
- use URI::Escape;
- $imageFileName = uri_escape($imageFileName);
- End
- $imageHTML = "<img src=\"images/$imageFileName\" alt=\"$imageName\" />"
- return $imageHTML
- End
The writeWebImageToFolderAtPath ensures that the image is in a standard web format, regardless of its format in the document. This means that embedded PDFs and EPS images will be written to a more standard format, currently JPEG.
The ability to define commands contributed heavily to the main improvement of this version. It allowed me to separate out the code that handles text conversions, which in turn allowed me to call it both for text in the normal document flow and on text in floating text boxes.
[toggle code]
-
Define Command FloatingExtractor($float)
- $tempDocument = Document.newWithText $float.text
- $tempDocument.show
-
While Select Next Paragraph
- $currentSelection = $tempDocument.textSelection
- paragraphExtractor($currentSelection, $tempDocument)
- End
- $tempDocument.close True
- End
-
While Select Next Paragraph
- paragraphExtractor($currentSelection, $thisDocument)
-
If $floats
-
ForEach $float in $floats
-
If $float.isTextBox
- FloatingExtractor($float)
- $thisDocument.show
- End
-
If $float.isTextBox
- End
-
ForEach $float in $floats
- End
- End
When the script detects floating text content, it extracts the floating content into a new document, then selects each part in order just as it does for the main document.
Nisus’s new macro features, including hash arrays, also removed the need for a Perl include file; if you look at the code in the zip file, you’ll see that has gone away. The script still uses Perl extensively for Perl’s text munging capability, but it no longer needs separate global functions and variables; those can all be handled in Nisus.
A somewhat lesser new feature is that it knows when a paragraph style inherits from a headline style, and uses the headline style’s textual level (that is, heading 1 is level 1, heading 2 is level 2, etc.). It does not use the table of contents level from the style, because paragraphs can have multiple table of contents levels.
In response to Nisus HTML conversion: New features in Nisus’s scripting language make HTML conversion almost a breeze.
More Nisus
- Importing an index into Nisus
- Nisus makes it very easy to import an externally-generated index into a document.
- Text to image filter for Smashwords conversions
- Smashwords has very strange requirements for ebooks. This script is what I use to convert books to .doc format for Smashwords, including converting tables to images.
- Lulu, Nisus, and Gods & Monsters
- Lulu is sometimes really annoying. But they usually get the job done. Nisus, on the other hand, is rarely annoying to use and always gets the job done.
- Nisus Writer Pro 2.0
- The new Nisus is pure awesome: very easy to use, and it does everything I need.
- Automatically grab flavor text snippets in Nisus
- In Nisus, it is very easy to grab all text of a specific style, and its nearest heading. This makes it easy to make “room description cards” for handing to the players after reading them.
- Four more pages with the topic Nisus, and other related pages