Toggling paragraph breaks

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Toggling paragraph breaks

Post by RoryOF »

Suppose I have a text, a Writer file (.odt format). In my text I have a sequence of paragraphs (Trivial example):

Code: Select all

Text 1 
Text 2 
Text 3 
Text 4  
For valid reasons, I may want these to show as

Code: Select all

Text 1 Text 2 Text 3 Text 4 
but be capable of being switched back to the separate line format, whether by setting an internal variable, eg "Collapse" = 0 or 1, or by running a small external program that massages the Writer XML.

Any thoughts on a method?

Reason: I am editing a long text written in 1810-20, which contains many letters. At that time it was usual that letters be not paragraphed, merely one long stream of punctuated text. But it may happen it is decided that, for easier comprehension by modern readers, the letters should be paragraphed. The insertion of paragraph breaks would in initial instance be done by hand, as the sense of the letter demands. Pending a final decision, it might be necessary to switch backwards and forwards between the paragraphed and unparagraphed formats. No such letter will exceed 64k characters in total (if that becomes a consideration).

Note that I write "insert paragraph breaks"; these could be some specific marker that appears as a paragraph break or disappears, depending on the state of the controlling internal variable.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Mountaineer
Posts: 311
Joined: Sun Sep 06, 2020 8:27 am

Re: Toggling paragraph breaks

Post by Mountaineer »

Some years ago I had to reformat texts, and used a macro for several steps of replacements.
I needed to mark some ends of paragraphs,
delete all paragraph-marks (pilcrow) and insert paragraphs at al marks.

I used a visible marking, but in your case I'd assume an invisible marker is necessary. A suggestion would be a special unicode char like "half-width-space" or maybe 4 of them.

J.
OpenOffice 3.1 on Windows Vista
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

Thanks, Mountaineer; that offers a solution - I had not thought of using invisible markers - I had forgotten that such exist.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Toggling paragraph breaks

Post by JeJe »

You could use the pilcrow character ¶ and make them hidden or unhidden with character formatting.

Edit: and search/replace to/from real paragraph breaks.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Toggling paragraph breaks

Post by John_Ha »

White text is invisible too ...
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

The major problem with all suggestions is that F&R is unwilling to work across paragraph breaks.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Toggling paragraph breaks

Post by John_Ha »

Rory

I'm not sure that is correct.

Regular expressions are limited by, I think, end of paragraph. But F&R > Find all..., for example, searches the entire document.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Toggling paragraph breaks

Post by John_Ha »

Rory

I have nearly done it with the Alt F and R extension.

Set a % sign as the space between the text strings.

Use AF&R to change it to an end of para.

Now highlight the four lines and use AF&R to change the end of para back to a %.

It needs a few glitches sorted out, namely setting the replaced % to white and the last % doesn't seem to get inserted.
 Edit: AF&R can set the colour of replaced text 
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Toggling paragraph breaks

Post by John_Ha »

Also, could you put the text in two sections, one in each format.

Now hide the relevant section.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
MrProgrammer
Moderator
Posts: 4903
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Toggling paragraph breaks

Post by MrProgrammer »

RoryOF wrote: Thu May 12, 2022 1:25 pm The major problem with all suggestions is that F&R is unwilling to work across paragraph breaks.
The special regular expression $, if used alone, represents a paragraph break in the Search For field of the OpenOffice Find&Replace dialog and \n represents a paragraph break in its Replace With field.

Within five minutes I created this system and tested it on the toy example:
• Tools → Macros → Record Macro
• Edit → Find&Replace → SearchFor → $ → Replace with → → More Options → ✓ Regular Expressions → Replace All → Close
• Macro Name → Choose name → Save Macro In → Choose location → Save

• Tools → Macros → Record Macro
• Edit → Find&Replace → SearchFor → → Replace with → \n → More Options → ✓ Regular Expressions → Replace All → Close
• Macro Name → Choose name → Save Macro In → Choose location → Save

• Tools → Customize → Toolbars → New → Toolbar Name → Choose Name → Save In → Writer → OK
• Toolbar → Chosen name → Add → Category → OpenOffice Macros → Choosen macro location
→ Commands → First chosen macro name → Add
→ Commands → Second chosen macro name → Add → Close → OK

Now clicking on the new toolbar buttons performs the conversions. Depending on your needs you may want to enable More → Current Selection Only when recording these macros. Of course a custom written macro could provide far more capabilities, but perhaps this quick, simple system is sufficient.
Mountaineer wrote: Wed May 11, 2022 6:35 pm A suggestion would be a special unicode char like "half-width-space" or maybe 4 of them.
While it may be inconvenient to type those in the File&Replace dialog, they can be in the clipboard and pasted in. above represents any character or sequence of characters.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

John_Ha wrote: Thu May 12, 2022 2:06 pm Rory

I'm not sure that is correct.

Regular expressions are limited by, I think, end of paragraph. But F&R > Find all..., for example, searches the entire document.
F&R seems very sensitive to paragraph marks. I tried to set/unset some marks; they found first time, but not after they were unset (i.e., changed and unchanged a number of times).
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

Part of the problem is that in the file there is also commentary, which is delimited into paragraphs. The commentary must not be changed, only the paragraphing of the letters, and that may need to be switchable to/from paragraphs until a final decision is made.

I'm pondering XML code; a personal XML tag, of form <MyTag> some text </MyTag> in the content.xml does not show the "some text" string. In content.xml a paragraph mark is shown by

Code: Select all

End of paragraph text.</text:p><text:p text:style-name="Text_20_body">Start of new paragraph text
where Text_20_body is the name of the style to be applied to the new para. Remove the sequence </text:p><text:p ....> and the text flows from one para continuously into the next.

This code sequence could be placed between custom XML tags; if these tags can be switched on, then the para breaks are inserted. If the tags are switched off, then the breaks are not inserted, and the tags remain as a marker to be switched again if needed. I think this would need an XML schema to handle the tag switching; that schema, as I understand it, could be inserted into the content.xml code or referenced from it. But XML makes my head hurt!

I'll leave the matter stand for the present - other things to do and possibly months before this might be needed, by which time it might all have been done by hand anyway!
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Toggling paragraph breaks

Post by JeJe »

You could have your working document as the paragraphs one and every time you want to see it the other way - create a new document from it with the all-in-one-line change. Then there's no need to have any form of marking.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Toggling paragraph breaks

Post by JeJe »

Here's a rudimentary implementation of my suggestion attached in case anyone ever wants something like it in future.

A toolbar toggles pilcrow characters in the selection to line feeds and vice versa or makes the pilcrows in the selection hidden text to hide them.

The lines with pilcrows would require their own paragraph style to enable a search for styles to easily find and select them separately from the rest of the document.
Attachments
pilcrow.odt
(12.54 KiB) Downloaded 92 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
MrProgrammer
Moderator
Posts: 4903
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Toggling paragraph breaks

Post by MrProgrammer »

RoryOF wrote: Thu May 12, 2022 5:16 pmPart of the problem is that in the file there is also commentary, which is delimited into paragraphs
A recorded macro could still work if the portion to be expanded/collapsed has a distinguishing style. The recorded macro can use Search for StylesFind All then restrict the replacement to Current Selection Only.
RoryOF wrote: Thu May 12, 2022 5:16 pmI'm pondering XML code; a personal XML tag, of form <MyTag> some text </MyTag>
Inserting an XML format sequence has some suggestions for editing XML in UNIX/Linux.

I wonder what OpenOffice will do when it finds the unexpected <MyTag>. If it chokes on that, surely it will accept an XML comment <!--comment--> though you'd have to test if OpenOffice would write the comment out again with File → Save. For hiding XML in a custom <MyTag>, it may be helpful to use the <![CDATA[…]]> syntax since CDATA can enclose arbitrary XML without the need to escape <, &, etc.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

In haste: I found that it accepted the <MyTag> ... <\MyTag> sequence and ignored that sequence and its contents.

My memory of some years ago was that it didn't like the <![CDATA[...]] syntax, but if II have time later I'll explore hat.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

My quick test showed that OpenOffice accepted a file hand edited with XML editor to insert the CDATA section; it opened the file, displaying the text in the CDATA section; Saving the file removed the CDATA bracketing, leaving the enclosed text in the file; I did not examine for inclusion of control characters.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Toggling paragraph breaks

Post by John_Ha »

MrProgrammer wrote: Fri May 13, 2022 4:01 pmI wonder what OpenOffice will do when it finds the unexpected <MyTag>.
Tests I did some time ago showed that AOO ignored tags it didn't know and their enclosed content when the file was opened. When the file was saved the tags and their content were not written back.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Toggling paragraph breaks

Post by RoryOF »

That is what I found; it may be a shortcoming in the SAX parser OO uses.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Mountaineer
Posts: 311
Joined: Sun Sep 06, 2020 8:27 am

Re: Toggling paragraph breaks

Post by Mountaineer »

Thinking a bit outside Writer, I would suggest html-coding and two different css-styles. You could even use media-queries to show paragraphs on "mobile" and inline-markup for "desktop".

Using xml an xslt-transform should be possible for input to writer - but actually I avoided messing around with this until now.

J.
OpenOffice 3.1 on Windows Vista
Post Reply