Separate file into sections and then recombine

Everything related to our flagship word processor.
Post Reply
Frank Einstein
Posts: 26
Joined: 2004-12-23 21:53:45

Separate file into sections and then recombine

Post by Frank Einstein »

This is an elementary question, but I couldn't find the answer.

I am writing a large file with images, and the file is made up of several sections. I am making changes, and I would like to extract individual sections from the larger filed then put them back together again. Two questions:

(1) How do I extract an individual section from the larger file to work on it?
(2) I will end up with several sections, each a separate NWP file. How do I recombine them?

Thanks in advance.

Frank
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

Re: Separate file into sections and then recombine

Post by Þorvarður »

Hi Frank,
For splitting documents there are at least 3 macros available that I'm aware of: one by Kino, called 'Split Documents', and two others by Philip Spaelti and Adryan with additions.

To join files, use a macro by Kino called "Join Files.

I guess the Kino macros should be already in your macro folder, waiting for you to use them, and the other two can be found here: <https://nisus.com/forum/viewtopic.php?f=17&t=6336>
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Separate file into sections and then recombine

Post by adryan »

G'day, Þorvarður et al

Yes, I had a look at that Split Documents macro again, in relation to Frank's query. However, it treats page breaks and section breaks in the same way, so it will probably not give the desired result if a section spans several page breaks.

I could not see how to make the required distinction, so I was hoping one of The Macro Gurus out there might come to the rescue!

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Separate file into sections and then recombine

Post by phspaelti »

The best approach to find sections is to use the macro command .rangeOfSectionAtIndex. You can retrieve all sections of a document with a "Kino-loop" like this:

Code: Select all

$doc = Document.active
$loc = 0
while $loc < $doc.text.length
    $sectionRange = $doc.text.rangeOfSectionAtIndex $loc
    $loc = $sectionRange.bound
end
Using this idea to rewrite the macro I came up with this:
Split Document by Section Breaks.nwm
(7.29 KiB) Downloaded 476 times
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Separate file into sections and then recombine

Post by phspaelti »

But having done it this way, it might be more appropriate to split the document by "sections" as indicated by the headings rather than the section breaks. Using section breaks might run into trouble in case the document uses sections for things like changing the number of columns for example.

Let me know if you want a macro to split the document on some other criterion.

Added: Here's an example that splits the document using "Heading 1" headings.
Split Document by Section Headings.nwm
(7.45 KiB) Downloaded 480 times
Addendum 2: Here's an improved (?) version that asks you which style you want to use for making sections. Code has also been annotated.
Split Document by Section Headings.nwm
(9.72 KiB) Downloaded 473 times
philip
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Separate file into sections and then recombine

Post by adryan »

G'day, Philip et al

Your "Split Document by Section Breaks" macro works well, thank you.

The observation that one may wish to exclude certain Sections from such a document split is an astute one.

Your "Split Document by Section Headings" macro is interesting. I was pleased to learn how one might present a user with a list of Styles employed in a document, from which they might then make a choice. I would imagine most documents would yield to this approach.

However, it is conceivable that some Sections of a document may be indistinguishable from others, by way of headings or otherwise. You may, for example, have made a Section just to give yourself the option of keeping a whole chunk of text together while you experiment with where it fits best into the flow of the document. But, for the moment at least, you don't want your document split at that point.

In such a situation, my suggestion would be to insert a dummy string immediately before a Section break where one did not wish a split to occur. The dummy string should not occur elsewhere in the document, so it may just consist of nonsense. Replace the dummy+break combination with a dummy+return combination, then use Philip's "Split Document by Section Breaks" macro to split the document at the remaining Section breaks. One could then search for each occurrence of the dummy+return combination and replace it with the desired Section break. If it were important to retain the class of Section break (same page, odd page, etc), one could employ different dummy strings accordingly.

Because neither the Find system nor the Macro Language appears able to distinguish classes of Section breaks, some of this procedure would have to be stepped through manually. But judicious use of keystroke shortcuts could expedite things. And because breaks cannot be inserted at non-contiguous locations simultaneously, a small looping macro fragment may be needed to do the job of restoring them. This macro fragment would probably have to use named commands from the Insert menu because I don’t think the Macro Language can be specific enough when it comes to classes of Section breaks.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Separate file into sections and then recombine

Post by phspaelti »

Hello Adrian,
I think there is a bit of difference of ideas in what is meant by "Section". Nisus has a technical definition of Section which is what the original macro seemed to be aimed at. So that macro split the document at Section Breaks.

But I think most people will think of sections of their document more like "Chapters", "Parts", or "Volumes", i.e., some logical idea of break in a larger work. This will usually line up with some type of section heading. That was the idea behind the "Split Document by Section Headings".

But I think you could just use the macro this way:
  1. Create a special paragraph style "Break"
  2. Apply the style to a few key (empty) paragraphs or section breaks in your document
  3. Run the macro and select the "Break" style
So this should avoid you needing to insert any kind of gibberish markers or manually steering the split process (apart from applying the style).

As to the more technical question of distinguishing breaks; obviously I think Nisus should provide a more user friendly approach, but in the meantime you could use the following code:

Code: Select all

$doc = Document.active
$breaks = $doc.text.find '\f', 'Ea'
prompt $breaks.count

foreach $i, $break in $breaks
$rtf = Encode RTF $break.subtext
$check = Cast to String $rtf
if $check.find 'sbk(?<type>\w{3,4})', 'E¢'
prompt "Break $i is of type $type"
end
end
This macro looks at the underlying RTF code and checks the "sbk" marker which has four types (sbkpage, sbkodd, sbkeven, sbknone). Page breaks will not have such a marker.
philip
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Separate file into sections and then recombine

Post by adryan »

G'day, Philip et al

I was referring to Nisus's technical Sections, hence my capitalization of the word. Besides the example I gave, multi-column Sections are also ones that would not necessarily have a heading attached (as indicated in a previous post by Philip).

The fact that the class of Section break is encoded in the RTF markup is most illuminating. I don't think you can see the RTF markup in Nisus Writer itself (unless perhaps you go via the Macro Language, as Philip has done), but BBEdit reveals all. Look, you see? There's an odd little "sbk" right there!

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
Post Reply