DocScript SVN
Status: Beta
Brought to you by:
ah8
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Export" script:language="StarBasic">REM ***** BASIC *****
' DocScript export script
' Copyright (C) 2009
' Andreas Harnack (ah8 at freenet dot de)
' This software is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
' You should have received a copy of the GNU General Public License along
' with this library; see the file COPYING. If not, write to the Free
' Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
' USA.
' As a special exception, you may use this file as part of a free software
' library without restriction. Specifically, if other files instantiate
' templates or use macros or inline functions from this file, or you compile
' this file and link it with other files to produce an executable, this
' file does not by itself cause the resulting executable to be covered by
' the GNU General Public License. This exception does not however
' invalidate any other reasons why the executable file might be covered by
' the GNU General Public License.
Function quoteStr(str as String) as String
quoteStr = "'" & join(split(str, "'"), "'"&CHR(34)&"'"&CHR(34)&"'") & "'"
End Function
Function charStyle(oText as Objec, oPara as Object) as String
Dim sStyle as String
REM get the character style properties
If oText.CharStyleName <> "" Then
REM check for style name first
sStyle = " '" + join(split(oText.CharStyleName),"_") +"'"
Else
Dim iItalic, iBold, iFixed as Integer
iItalic = com.sun.star.awt.FontSlant.ITALIC
iBold = com.sun.star.awt.FontWeight.BOLD
iFixed = com.sun.star.awt.FontPitch.FIXED
sStyle = ""
If oText.CharPosture=iItalic And oPara.CharPosture <> iItalic Then
sStyle = sStyle + " italic"
End If
If oText.CharWeight =iBold And oPara.CharWeight <> iBold Then
sStyle = sStyle + " bold"
End If
If oText.CharFontPitch = iFixed And oPara.CharFontPitch <> iFixed Then
sStyle = sStyle + " fixedfont"
End If
End If
charStyle = sStyle
End Function
Sub exportText(iFile%, oPara as Object, oText as Object, sShift$)
If len( oText.getString()) > 0 Then
Dim sText, sAttributes as String
sText = quoteStr(oText.getString())
sAttributes = charStyle(oText, oPara)
print #iFile sShift & "text" & sAttributes & " '' " & sText
End If
End Sub
Sub exportParagraphContent(iFile%, oPara as Object, sShift$)
Dim oTextEnum
otextEnum = oPara.createEnumeration()
REM iterat through all text portions of a paragraph
Do While otextEnum.hasMoreElements()
Dim oText as Object
Dim sType as String
REM get next portion
oText = oTextEnum.nextElement()
REM get portion type
sType = LCase(oText.TextPortionType)
If sType = "text" Then
exportText(iFile, oPara, oText, sShift)
Else
print #iFile "debug " + stype + " << _END_"
print #iFile join(oText.SupportedServiceNames, CHR(10))
print #iFile "_END_"
print #iFile
End If
Loop
End Sub
Function paragraphStyle(oPara as Object) as String
Dim oOptions as String
Dim oStyles, oStyle as Object
oOptions = "'" + join(split(oPara.ParaStyleName),"_") + "'"
oStyles = ThisComponent.StyleFamilies.getByName("ParagraphStyles")
oStyle = oStyles.getByName(oPara.ParaStyleName)
If oPara.ParaAdjust <> oStyle.ParaAdjust Then
If oPara.ParaAdjust = com.sun.star.style.ParagraphAdjust.CENTER Then
oOptions = oOptions + " `align center`"
ElseIf oPara.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT Then
oOptions = oOptions + " `align left`"
ElseIf oPara.ParaAdjust = com.sun.star.style.ParagraphAdjust.RIGHT Then
oOptions = oOptions + " `align right`"
End If
End If
paragraphStyle = oOptions
End Function
Sub exportParagraph(iFile%, oPara as Object, sShift$)
If len(trim(oPara.getString())) > 0 Then
print #iFile sShift & "("
exportParagraphContent(iFile, oPara, sShift + " ")
print #iFile
print #iFile sShift & ") | paragraph " + paragraphStyle(oPara)
print #iFile
End If
End Sub
Sub exportTable(iFile%, oTable as Object, sShift$)
iRows% = oTable.getRows().getCount()
iColumns% = oTable.getColumns().getCount()
print #iFile sShift & "( :"
For i = 0 to iRows-1
print #iFile sShift & " ( :"
For j = 0 to iColumns-1
print #iFile sShift & " ( :"
exportContent(iFile, oTable.getCellByPosition(j,i), sShift & " ")
print #iFile sShift & " ) | column"
Next
print #iFile sShift & " ) | row"
Next
print #iFile sShift & ") | table"
print #iFile
End Sub
Sub exportContent(iFile%, oContent as Object, sShift$)
Dim oParaEnum, oPara as Object
oParaEnum = oContent.getText().createEnumeration()
REM iterate through all paragraphs
Do While oParaEnum.hasMoreElements()
oPara = oParaEnum.nextElement()
If oPara.supportsService("com.sun.star.text.Paragraph") Then
REM normal paragraphs
exportParagraph(iFile, oPara, sShift)
ElseIf oPara.supportsService("com.sun.star.text.TextTable") Then
REM Tables
exportTable(iFile, oPara, sShift)
Else
REM anything else, should not happen
MsgBox "Unsupported Text Element"
End If
Loop
End Sub
Sub exportDocument(iFile%, oDoc as Object)
print #iFile "#!/bin/bash"
print #iFile
print #iFile "source oo2html.include"
print #iFile
REM export document content
exportContent(iFile, oDoc, "")
print #iFile
End Sub
Sub Main
Dim sDocName, sFileName as String
Dim oDialog as Object
DialogLibraries.LoadLibrary("DocScript")
sDocName = convertFromURL(ThisComponent.URL)
If InStr(sDocName, ".") > 1 Then
sFileName = Left$(sDocName, InStr(sDocName, ".") - 1) + ".sh"
Else
sFileName = sDocName + ".sh"
End If
oDialog = createUnoDialog(DialogLibraries.DocScript.FileOpen)
oDialog.getControl("FileName").text = sFileName
If oDialog.execute() = 1 Then
Dim iFile as Integer
iFile = FreeFile
sFileName = oDialog.getControl("FileName").text
Open sFileName for Output as #iFile
exportDocument(iFile, ThisComponent)
Close #iFile
End If
End Sub
</script:module>