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 shift(sShift as String) as String
shift = sShift & " "
End Function
Function quoteStr(str as String) as String
quoteStr = "'" & join(split(str, "'"), "'"&CHR(34)&"'"&CHR(34)&"'") & "'"
End Function
Function ooVersion() As String
'Retrieves the running OOo version
'Author : Laurent Godard
'e-mail : listes.godard@laposte.net
Dim oSet, oConfigProvider
Dim sProvider$, sAccess$
Dim oParm(0) As New com.sun.star.beans.PropertyValue
sProvider = "com.sun.star.configuration.ConfigurationProvider"
sAccess = "com.sun.star.configuration.ConfigurationAccess"
oParm(0).Name = "nodepath"
oParm(0).Value = "/org.openoffice.Setup/Product"
oConfigProvider = createUnoService(sProvider)
oSet = oConfigProvider.createInstanceWithArguments(sAccess, oParm())
ooVersion=oSet.getByName("ooSetupVersion")
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 listType(iType as Integer) as String
Select Case iType
Case com.sun.star.style.NumberingType.ARABIC:
listType = "arabic"
Case com.sun.star.style.NumberingType.ROMAN_UPPER:
listType = "roman_upper"
Case com.sun.star.style.NumberingType.ROMAN_LOWER:
listType = "roman_lower"
Case com.sun.star.style.NumberingType.CHARS_UPPER_LETTER:
listType = "letter_upper"
Case com.sun.star.style.NumberingType.CHARS_LOWER_LETTER:
listType = "letter_lower"
Case com.sun.star.style.NumberingType.CHAR_SPECIAL:
listType = "char_special"
Case com.sun.star.style.NumberingType.BITMAP:
listType = "bitmap"
Case Else
listType = str(iType)
End Select
End Function
Function paragraphListType(oPara as Object) as Integer
paragraphListType = 0
If oPara.supportsService("com.sun.star.text.Paragraph") Then
If Not isEmpty(oPara.NumberingRules) Then
Dim oRules
oRules = oPara.NumberingRules
If Not oRules.NumberingIsOutline Then
Dim oRule()
Dim i As Integer
oRule() = oRules.getByIndex(oPara.NumberingLevel )
For i = LBound(oRule()) To Ubound(oRule())
If oRule(i).Name = "NumberingType" Then
paragraphListType = oRule(i).Value
End If
Next
End If
End If
End If
End Function
Function paragraphListLevel(oPara as Object, iListType as Integer) as Integer
If ( iListType > 0 ) Then
paragraphListLevel = oPara.NumberingLevel + 1
Else
paragraphListLevel = 0
End If
End Function
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, shift(sShift))
print #iFile
print #iFile sShift & ") | paragraph " + paragraphStyle(oPara)
print #iFile
End If
End Sub
Sub exportTable(iFile%, oTable as Object, sShift0$)
iRows% = oTable.getRows().getCount()
iColumns% = oTable.getColumns().getCount()
sShift1$ = shift(sShift0)
sShift2$ = shift(sShift1)
sShift3$ = shift(sShift2)
print #iFile sShift0 & "( :"
For i = 0 to iRows-1
print #iFile sShift1 & "( :"
For j = 0 to iColumns-1
print #iFile sShift2 & "( :"
exportContent(iFile, oTable.getCellByPosition(j,i), sShift3)
print #iFile sShift2 & ") | column"
Next
print #iFile sShift1 & ") | row"
Next
print #iFile sShift0 & ") | table"
print #iFile
End Sub
Sub exportContent(iFile%, oContent as Object, sShift$)
Dim oParaEnum, oPara as Object
oParaEnum = oContent.getText().createEnumeration()
Dim iListLevel as Integer
Dim iListType(32) as Integer
Dim sListShift(32) as String
Dim sListType as String
REM iterate through all paragraphs
iListLevel = 0
sListShift(0) = sShift
Do While oParaEnum.hasMoreElements()
Dim iParagarphListLevel, iParagraphListType as Integer
oPara = oParaEnum.nextElement()
iParagraphListType = paragraphListType(oPara)
iParagarphListLevel = paragraphListLevel(oPara, iParagraphListType)
While iListLevel < iParagarphListLevel 'open a new list
print #iFile sListShift(iListLevel) & "("
iListType(iListLevel) = iParagraphListType
iListLevel = iListLevel + 1
sListShift(iListLevel) = shift(sListShift(iListLevel-1))
Wend
While iListLevel > iParagarphListLevel 'close current list
iListLevel = iListLevel - 1
sListType = listType(iListType(iListLevel))
print #iFile sListShift(iListLevel) & ") | list " + sListType
print #iFile
Wend
If iListLevel > 0 Then 'we are still in a list
If iListType(iListLevel-1) <> iParagraphListType Then
sListType = listType(iListType(iListLevel-1))
print #iFile sListShift(iListLevel-1) & ") | list " & sListType
iListType(iListLevel-1) = iParagraphListType
print #iFile sListShift(iListLevel-1) & "("
End If
print #iFile sShift & sListShift(iListLevel) & "("
exportParagraphContent(iFile, oPara, shift(sListShift(iListLevel)))
print #iFile sShift & sListShift(iListLevel) & ") | item"
ElseIf 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.styles"
print #iFile
print #iFile "export OPENOFFICE_SOLAR_VERSION=" + GetSolarVersion()
print #iFile "export OPENOFFICE_VERSION=" + ooVersion()
print #iFile "export OPENOFFICE_GUI=" + GetGUIType()
print #iFile "export LANG=" + Environ("LANG")
print #iFile
REM export document content
exportContent(iFile, oDoc, "")
print #iFile
End Sub
Sub exportToFile(sFileName as String, oDoc as Object)
Dim iFile as Integer
iFileNumber = FreeFile
Open sFileName for Output as #iFileNumber
exportDocument(iFileNumber, oDoc)
Close #iFileNumber
End Sub
Function fileName(sDocName as String, sExtension as String) as String
Dim vPath as Variant
Dim vName as Variant
vPath = split(sDocName, "/")
vName = split(vPath(UBound(vPath())), ".")
If LBound(vName) < UBound(vName) Then
vName(UBound(vName)) = sExtension
Else
vName(LBound(vName)) = vName(LBound(vName)) & sExtension
End If
vPath(UBound(vPath())) = join(vName, ".")
fileName = join(vPath, "/")
End Function
Sub Batch(sDocName as String)
Dim sURL as String
Dim oDoc as Object
sURL = ConvertToURL(sDocName)
oDoc = StarDesktop.LoadComponentFromURL(sURL, "_blank", 0, Array())
exportToFile(fileName(sDocName, "sh"), oDoc)
oDoc.close(true)
End Sub
Sub Main
Dim sDocName, sFileName as String
Dim oDialog as Object
DialogLibraries.LoadLibrary("DocScript")
sDocName = convertFromURL(ThisComponent.URL)
if len(sDocName) > 0 then
sFileName = fileName(sDocName, "sh")
end if
oDialog = createUnoDialog(DialogLibraries.DocScript.FileOpen)
oDialog.getControl("FileName").text = sFileName
If oDialog.execute() = 1 Then
exportToFile(oDialog.getControl("FileName").text, ThisComponent)
End If
End Sub
</script:module>