Lodahl's blog: Basic
Showing posts with label Basic. Show all posts
Showing posts with label Basic. Show all posts

26 September 2009

Extension help content

I have created a few extensions over the last few years. Some of them even include help content to help the users. One problem is, that the XML syntax are rather dificult to figure out. Even a simple document with a few headers and some paragraphs are rather difficult to create.

It would be rather nice if there where an export filter for xhp-files. I have head that such filter exists but I have never found it.

Expired by this article an Linux Magazine by Dimtri Popov ( http://www.linux-magazine.com/Online/Blogs/Productivity-Sauce-Dmitri-s-open-source-blend-of-productive-computing/Format-Writer-Documents-with-Any-Markup ) I decided to try to make a macro to export a document to xhp. So far I got headers, paragraphs and links. There is still some work to do because tables and pictures.

Also the macro will mess up the original document. It would be nice if it only exported the content leaving the original text untouched.

Here is the macro:


REM ***** BASIC *****

Sub HelpContent
If not ThisComponent.hasLocation Then
MsgBox ("Save document befor export", 0 ,"Export to Help content")
stop
End If


MarkupHeadingsFunc("Text body", "", "")
MarkupHeadingsFunc("Heading 1", "", "")
MarkupHeadingsFunc("Heading 2", "", "")
MarkupHeadingsFunc("Heading 3", "", "")
MarkupTextFunc("CharWeight", com.sun.star.awt.FontWeight.BOLD, "&")
MarkupURLFunc

AddText
ExportTheThing

End Sub

Function MarkupHeadingsFunc (StyleName, StartTag, EndTag)
ThisDoc=ThisComponent
ThisText=ThisDoc.Text
ParaEnum=ThisText.createEnumeration
While ParaEnum.hasmoreElements
Para=ParaEnum.nextElement
PortionEnum = Para.createEnumeration
While PortionEnum.hasMoreElements
Portion=PortionEnum.nextElement
If Portion.paraStyleName = StyleName then
Portion.String = StartTag + Portion.String + EndTag
End if
Wend
Wend
End Function

Function MarkupTextFunc(SearchAttrName, SearchAttrValue, ReplaceStr)
Dim SearchAttributes(0) As New com.sun.star.beans.PropertyValue
ThisDoc=ThisComponent
SearchAttributes(0).Name=SearchAttrName
SearchAttributes(0).Value=SearchAttrValue
ReplaceObj=ThisDoc.createReplaceDescriptor
ReplaceObj.SearchRegularExpression=true
ReplaceObj.searchStyles=false
ReplaceObj.searchAll=true
ReplaceObj.SetSearchAttributes(SearchAttributes)
ReplaceObj.SearchString=".*"
ReplaceObj.ReplaceString=ReplaceStr
ThisDoc.replaceAll(ReplaceObj)
End Function

Sub MarkupURLFunc
ThisDoc=ThisComponent
ThisText=ThisDoc.Text
ParaEnum=ThisText.createEnumeration
While ParaEnum.hasmoreElements
Para=ParaEnum.nextElement
PortionEnum=Para.createEnumeration
While PortionEnum.hasMoreElements
Portion=PortionEnum.nextElement
If Portion.HyperlinkURL <> "" then
Portion.String = "" +Portion.String + ""
End if
Wend
Wend
End Sub

function SetFileName() as String


Dim oDoc
Dim sDocURL
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If

sDocURL = ThisComponent.getURL()
Directory = DirectoryNameoutofPath(sDocURL, "/")
File_Name = FileNameoutofPath(sDocURL, "/")
New_File_name = ConvertFromUrl(Left(File_Name, Len(File_Name)-4))


SetFileName = ""
boInitialized = false

oListener = CreateUnoListener("MyPick01_", "com.sun.star.ui.dialogs.XFilePickerListener")
oFP = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
With oFP
.setMultiSelectionMode(False)

.Initialize( Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE) )
.appendFilter("Help content", "*.xhp" )
.setTitle( "Help content ..." )
.setDisplayDirectory(Directory)
.setDefaultName(New_File_Name & ".xhp")



If .execute() Then OpenFile = .Files(0)

.removeFilePickerListener(oListener)
.Dispose()
End With
SetFileName = OpenFile

If SetFileName = "" Then
Stop
End if


end function

sub AddText


Starttext= "" & CHR$(10) & "" & CHR$(10) & "" & CHR$(10) & "" & CHR$(10) & "write title here" & CHR$(10) & "write filename here" & CHR$(10) & "" & CHR$(10) & "" & CHR$(10) & "" & CHR$(10) & "" & CHR$(10) & "xxx" & CHR$(10) & "xxx; yyy" & CHR$(10) & ""

EndText = CHR$(10) & "" & CHR$(10) & "
"

Dim oText As Object
oText = ThisComponent.Text

REM Insert some simple text at the start
oText.insertString(oText.getStart(), StartText & CHR$(13), False)
REM Append a new paragraph at the end
oText.insertString(oText.getEnd(), EndText & CHR$(13), False)



end sub

sub ExportTheThing
FileName = SetFilename()


Dim args(0) as new com.sun.star.beans.PropertyValue
args(0).Name = "FilterName"
args(0).Value = "Text"
ThisComponent.storeToURL(FileName,args())


end sub

REM ***** END BASIC *****