Menu

[r23]: / trunk / docscript / DocScript / Export.xba  Maximize  Restore  History

Download this file

200 lines (169 with data), 7.7 kB

<?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  *****

&apos; DocScript export script

&apos; Copyright (C) 2009
&apos; Andreas Harnack (ah8 at freenet dot de)

&apos; This software is distributed in the hope that it will be useful,
&apos; but WITHOUT ANY WARRANTY; without even the implied warranty of
&apos; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
&apos; GNU General Public License for more details.

&apos; You should have received a copy of the GNU General Public License along
&apos; with this library; see the file COPYING.  If not, write to the Free
&apos; Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
&apos; USA.

&apos; As a special exception, you may use this file as part of a free software
&apos; library without restriction.  Specifically, if other files instantiate
&apos; templates or use macros or inline functions from this file, or you compile
&apos; this file and link it with other files to produce an executable, this
&apos; file does not by itself cause the resulting executable to be covered by
&apos; the GNU General Public License.  This exception does not however
&apos; invalidate any other reasons why the executable file might be covered by
&apos; the GNU General Public License.


Function quoteStr(str as String) as String
    quoteStr = &quot;&apos;&quot; &amp;  join(split(str, &quot;&apos;&quot;), &quot;&apos;&quot;&amp;CHR(34)&amp;&quot;&apos;&quot;&amp;CHR(34)&amp;&quot;&apos;&quot;) &amp; &quot;&apos;&quot;
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 &lt;&gt; &quot;&quot; Then
        REM check for style name first
        sStyle = &quot; &apos;&quot; + join(split(oText.CharStyleName),&quot;_&quot;) +&quot;&apos;&quot;
    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 = &quot;&quot;
        If oText.CharPosture=iItalic And oPara.CharPosture &lt;&gt; iItalic Then 
            sStyle = sStyle + &quot; italic&quot;
        End If
        If oText.CharWeight =iBold And oPara.CharWeight &lt;&gt; iBold Then
            sStyle = sStyle + &quot; bold&quot;
        End If
        If  oText.CharFontPitch = iFixed And oPara.CharFontPitch &lt;&gt; iFixed Then
            sStyle = sStyle + &quot; fixedfont&quot;
        End If
    End If
    charStyle = sStyle
End Function


Sub exportText(iFile%, oPara as Object, oText as Object, sShift$)
    If len( oText.getString()) &gt; 0 Then
        Dim sText, sAttributes as String
        sText = quoteStr(oText.getString())
        sAttributes = charStyle(oText, oPara)
        print #iFile sShift &amp; &quot;text&quot; &amp; sAttributes &amp; &quot; &apos;&apos; &quot; &amp; 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 = &quot;text&quot; Then 
            exportText(iFile, oPara, oText, sShift)
        Else
            print #iFile &quot;debug &quot; + stype + &quot; &lt;&lt; _END_&quot;
            print #iFile join(oText.SupportedServiceNames, CHR(10))
            print #iFile &quot;_END_&quot;
            print #iFile
        End If
    Loop
End Sub


Function paragraphStyle(oPara as Object) as String
    Dim oOptions as String
    Dim oStyles, oStyle as Object

    oOptions = &quot;&apos;&quot; + join(split(oPara.ParaStyleName),&quot;_&quot;) + &quot;&apos;&quot;
    oStyles = ThisComponent.StyleFamilies.getByName(&quot;ParagraphStyles&quot;)
    oStyle = oStyles.getByName(oPara.ParaStyleName)
    If oPara.ParaAdjust &lt;&gt; oStyle.ParaAdjust Then
        If oPara.ParaAdjust = com.sun.star.style.ParagraphAdjust.CENTER Then
            oOptions = oOptions + &quot; `align center`&quot;
        ElseIf oPara.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT Then
            oOptions = oOptions + &quot; `align left`&quot;
        ElseIf oPara.ParaAdjust = com.sun.star.style.ParagraphAdjust.RIGHT Then
            oOptions = oOptions + &quot; `align right`&quot;
        End If
    End If
    paragraphStyle = oOptions
End Function


Sub exportParagraph(iFile%, oPara as Object, sShift$)
    If len(trim(oPara.getString())) &gt; 0 Then
        print #iFile sShift &amp; &quot;(&quot;
        exportParagraphContent(iFile, oPara, sShift + &quot;	&quot;)
        print #iFile
        print #iFile sShift &amp; &quot;) | paragraph &quot; + 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 &amp; &quot;( :&quot;
    For i = 0 to iRows-1
        print #iFile sShift &amp; &quot;	( :&quot;
        For j = 0 to iColumns-1
            print #iFile sShift &amp; &quot;		( :&quot;
            exportContent(iFile, oTable.getCellByPosition(j,i), sShift &amp; &quot;			&quot;)
            print #iFile sShift &amp; &quot;		) | column&quot;
        Next
        print #iFile sShift &amp; &quot;	) | row&quot;
    Next
    print #iFile sShift &amp; &quot;) | table&quot;
    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(&quot;com.sun.star.text.Paragraph&quot;) Then
            REM normal paragraphs
            exportParagraph(iFile, oPara, sShift)
        ElseIf oPara.supportsService(&quot;com.sun.star.text.TextTable&quot;) Then
            REM Tables 
            exportTable(iFile, oPara, sShift)
        Else
            REM anything else, should not happen
            MsgBox &quot;Unsupported Text Element&quot;
        End If
    Loop
End Sub


Sub exportDocument(iFile%, oDoc as Object)
    print #iFile &quot;#!/bin/bash&quot;
    print #iFile
    print #iFile &quot;source oo2html.styles&quot;
    print #iFile

    REM export document content
    exportContent(iFile, oDoc, &quot;&quot;)
    print #iFile
End Sub


Sub Main
    Dim sDocName, sFileName as String
    Dim oDialog as Object
    DialogLibraries.LoadLibrary(&quot;DocScript&quot;)
    sDocName = convertFromURL(ThisComponent.URL)
    If InStr(sDocName, &quot;.&quot;) &gt; 1 Then
        sFileName = Left$(sDocName, InStr(sDocName, &quot;.&quot;) - 1) + &quot;.sh&quot;
    Else
        sFileName = sDocName + &quot;.sh&quot;
    End If
    oDialog = createUnoDialog(DialogLibraries.DocScript.FileOpen)
    oDialog.getControl(&quot;FileName&quot;).text = sFileName
    If oDialog.execute() = 1 Then
        Dim iFile as Integer
        iFile = FreeFile
        sFileName = oDialog.getControl(&quot;FileName&quot;).text
        Open sFileName for Output as #iFile
        exportDocument(iFile, ThisComponent)
        Close #iFile
    End If
End Sub

</script:module>
MongoDB Logo MongoDB