Page 1 of 1

Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 4:28 am
by vector
Hi All,
is it possible(and how easy) is to find a line like this

Code: Select all

{title: In Times of Trial}
and remove the brackets and title tag to just leave the text
In Times of Trial as a titlestyle

Id also like to treat

Code: Select all

{subtitle:Traditional}
{c:Verse1}
in a similar manner leaving me with just the text
Traditional as a subtitle style
and Verse1 as a callout style

I can do this manually with a number of F&R and clicks etc but would be nice to have it as a macro? perhaps
I am not wise in the ways of such things and have many files to convert/format. So would appreciate any help.

I would like to simply open the text file with open office, run the macro and apply some formatting and bold/italics etc to the now specified styles.

Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 7:59 am
by Zizi64
but would be nice to have it as a macro?
I think: You must write it. We can help you.

Otherwise you can try the AltSearch extension:
https://extensions.libreoffice.org/exte ... for-writer
https://extensions.openoffice.org/en/pr ... -altsearch

Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 8:16 am
by vector
thx ill look into those links and get back to you

Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 8:41 am
by RoryOF
The AltSearch extension should do it.

Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 2:06 pm
by JeJe
This is easy to do. Adapt this paragraph enumerator from "Useful Macro Information For OpenOffice" By Andrew Pitonyak (which is a free download and you should read)

You just need the Instr function to look at the start of the paragraph string and set the paragraph stylename accordingly and remove the bits at the beginning and end.

Code: Select all

Sub EnumerateParagraphs
REM Author: Andrew Pitonyak
Dim oParEnum 'Enumerator used to enumerate the paragraphs
Dim oPar 'The enumerated paragraph
REM Enumerate the paragraphs.
REM Tables are enumerated along with paragraphs
oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
REM This avoids the tables. Add an else statement if you want to
REM process the tables.
If oPar.supportsService("com.sun.star.text.Paragraph") Then
MsgBox oPar.getString(), 0, "I found a paragraph"
ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
Print "I found a TextTable"
Else
Print "What did I find?"
End If
Loop
End Sub


Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 3:28 pm
by Sébastien C
If AltSearch extension should do it, it not change the fact to open a dialog box and give the instructions each times in more of ONE command.

But search and replace with a regular expression with macro is easy... We have seen that recently.

Title, subtitle and callout styles are all paragraph styles. The treatment is therefore the same for this type of styles.

Code: Select all

Sub seachReplaceStyles()
	Dim   mySearch As Object,    myResult As Object, myElement As Object
	Dim myString() As String, myStyleName As String
	Dim          i As Integer

	mySearch = thisComponent.createSearchDescriptor()

	mySearch.searchString            = "\{.{1,}:.{1,}\}"                     ' The string that you search.
	mySearch.searchRegularExpression = True

	myResult = thisComponent.findAll(mySearch)
       
	If myResult.hasElements Then
	 For i = 0 to myResult.count-1
	                   myElement = myResult.getByIndex(i)
	                    myString = split(myElement.string, ":")
	                 myStyleName = right(myString(0), len(myString(0)) - 1)
	                 myString(0) = ""
	  myString(uBound(myString)) = left(myString(uBound(myString)), len(myString(uBound(myString))) - 1)
	            myElement.string = join(myString, "")
	     myElement.ParaStyleName = myStyleName                               ' The style that you apply to your string.
     Next i
    End If
End Sub
 Edit:  

If you want to delete the possible space(s) in left and in right of the name's style, you can replace the line

Code: Select all

myElement.ParaStyleName = myStyleName                               ' The style that you apply to your string.
by

Code: Select all

myElement.ParaStyleName = Trim(myStyleName)                         ' The style that you apply to your string.
 
:D

Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 10:17 pm
by Lupp
@"vector":
Your question is containing the phrases "a titlestyle", "a subtitle style" and "a callout style".
I feel unsure:
-1- Do you want to assign a specific paragraph style in each case to the complete paragraph?
-2- For what reason do you include the textcontent in the curly brackets? What if there are additional characters? Shall they be removed?
-3a- How is the specific stylename in each case assigned? Is it supposed to be the string between "{" and ":" as Sébastien assumed?
-3b- Or do you need a way to assign stylenames to the "{somelabel:" parts in an additional step?
-3- Is the different usage of a space in the cases an accident?

@Sébastien C:
-1- You searched for text parts by RegEx without excluding positions anywhere inside a paragraph. A string like "...some text {mySubtitle: and} another text..." anywhere in a paragraph would also trigger the style assignment. The different placement of text parts in-/outside of the curly brackets would be accepted as meaning nothing.
-2- Comments on parts of a quotation as sometimes done in German like in "there are not brackets {orth: Lupp}" would cause a script error.

Re: Convert text in {} to paragraph style

Posted: Wed Jun 13, 2018 11:24 pm
by JeJe
Okay... here's one way to do my solution... it assumes the paragraphs are all plain text and the styles are already in the document.

Code: Select all


Sub EnumerateParagraphs
	MODIFIED FROM REM Author: Andrew Pitonyak
	Dim oParEnum 'Enumerator used to enumerate the paragraphs
	Dim oPar 'The enumerated paragraph
	REM Enumerate the paragraphs.
	REM Tables are enumerated along with paragraphs
	oParEnum = ThisComponent.getText().createEnumeration()
	Do While oParEnum.hasMoreElements()
		oPar = oParEnum.nextElement()
		REM This avoids the tables. Add an else statement if you want to
		REM process the tables.
		If oPar.supportsService("com.sun.star.text.Paragraph") Then
			st= TRIM(oPar.getString())
			if left(st,1) = "{" and right(st,1) ="}" then
				a= instr(1,st,":")
				if a then
					stylename = trim(mid(st,2,a-2))


					if stylename = "c" then stylename = "callout"

					st=TRIM(mid(st,a+1,len(st) - a-1))
					oPar.parastylename = stylename
					oPar.string = st

				end if
			end if

			'ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
			'Print "I found a TextTable"
			'Else
			'Print "What did I find?"
		End If
Loop
End Sub



Re: Convert text in {} to paragraph style

Posted: Fri Jun 15, 2018 2:49 am
by vector
wow,
ok give me some time to try and understand these replies and ill get back to you.
Very much appreciated

Re: Convert text in {} to paragraph style

Posted: Mon Jun 18, 2018 1:02 am
by vector
I am overwhelmed by the amzing help out here!
many thanks to all
Kai who created the chordprotransposer got in touch with me and has also created a new extension for me , well for chordpro users ;) doing just this.
Kai and I are currently testing the extension Ill let you know if he releases it public. So far it working fantastic.


https://extensions.libreoffice.org/exte ... oser/1-0.6

Re: Convert text in {} to paragraph style

Posted: Mon Jun 18, 2018 8:05 am
by Hagar Delest
I remember that one also: [Solved] Taking note and tagging format.