User:TJFrazier/WikiBasic

From Apache OpenOffice Wiki
< User:TJFrazier
Revision as of 16:56, 9 September 2009 by TJFrazier (Talk | contribs)

Jump to: navigation, search

Code to post-process the output of MediaWiki Export Filter

REM  *****  BASIC  *****
 
Option Explicit
 
Sub TipNoteCautionCleanup
' Cleans up Tips, Notes, and Cautions after "Save to MediaWiki".
'   Converts wiki tables to wiki template calls.
 
Dim oCursor as Variant	'text cursor
Dim oDoc as Object
Dim enum1 as Variant	'paragraph enumeration
Dim enum2 as Variant	'portion enumeration
Dim thisPara as Variant
Dim thisPortion as Variant
Dim bBegin as Boolean	'True if looking for table start
' counters for items found.
Dim iCaution as Integer
Dim iNote as Integer
Dim iTip as Integer
Dim iPara as Integer
Dim iPor as Integer
Dim iTable as Integer	'non-T|N|C tables
 
Dim aNames(2) as String	'names to search for
Const kNote = 0			'subscript enumeration list
Const kTip = 1
Const kCaution = 2
 
Dim sFind as String		'sentinel sought
Dim sISO as String		'language code
Dim sRep as String		'replacement
Dim sText as String		'scratch
Dim sV as String		'release version
'Dim vLang as Variant	'array of Locale structs
 
sV = "0.1.1"
REM Version notes:
'V 0.1.1: Localized for DE. Also necessary to allow for different
'	structure of Caution/Achtung table (no images in DE).
'V 0.1.0: Released 2009-09-04 to wiki page. All MsgBox constants removed.
'V 0.0.2: More code cleanup. MsgBox constants now for module.
'V 0.0.1: Remove useless error msg for other table. Code cleanup.
'	Tally "other tables", & display.
'	Display version in tally header.
'V 0.0.0 released 2009-01-22 to authors list & JHW.
 
 
oDoc = ThisComponent
'Localize searches
aNames(kNote) = "Note"
aNames(kTip) = "Tip"
aNames(kCaution) = "Caution"
 
'Language locales are not available in txt files ...
'vLang = oDoc.getDocumentLanguages(1, 2)
'sISO = vLang(0).Country
sISO = InputBox( "Please input ISO language code", "T/N/C Cleanup ver " & sV, "US")
If sISO = "DE" then
  aNames(kNote) = "Anmerkung"
  aNames(kTip) = "TIPP"
  aNames(kCaution) = "Achtung"
End If 'DE
 
enum1 = oDoc.Text.createEnumeration
bBegin = True
 
While enum1.hasMoreElements
  thisPara = enum1.nextElement
  iPara = iPara + 1
  enum2 = thisPara.createEnumeration
  While enum2.hasMoreElements
    thisPortion = enum2.nextElement
    iPor = iPor + 1
    sText = thisPortion.getString()
    sFind = IIf( bBegin, "{|", "|}" )	'start/end wiki table
    If Mid(sText, 1, len(sFind)) = sFind Then	'found a sentinel
      oCursor = thisPortion.Text.createTextCursorByRange(thisPortion.Anchor)
      If bBegin Then	'looking for opening delimiter   
        bBegin = False	'next loop, look for closing delimiter
        ' expand selection to include next paragraph and its mark.
        oCursor.goRight( 1, True )	'-> next paragraph
        oCursor.gotoEndOfParagraph( True )
        oCursor.goRight( 1, True )	'include the para mark
        ' the selected string should hold one of the 3 target names.
        ' Set sRep to generate wiki-template names "{{Documentation/whatever"
        ' Code included for possible future localized templates.
        If InStr( oCursor.String, aNames(kNote) ) Then
'	      sRep = aNames(kNote)
	      sRep = "Note"
	      iNote = iNote + 1
        ElseIf InStr( oCursor.String, aNames(kTip) ) Then
'	      sRep = aNames(kTip)
	      sRep = "Tip"
	      iTip = iTip + 1
	ElseIf InStr( oCursor.String, aNames(kCaution) ) Then
'	      sRep = aNames(kCaution)
	      sRep = "Caution"
	      iCaution = iCaution + 1
	      If sISO = "DE" Then
	        ' no special action required
	      Else
                'kill generated image-related text
                oCursor.goRight( 2, True)				'empty para and into next
                oCursor.gotoEndOfParagraph( True )	'img: <center> para
                oCursor.goRight( 1, True )			'include the para mark
              End If 'Language
        Else	'different kind of wiki table
	      sRep = ""
	      iTable = iTable + 1
              bBegin = True	'keep looking for beginning, after other table
        EndIf 'NTC or other table found
        If sRep <> "" Then
             thisPortion.Text.insertString( oCursor, "{{Documentation/" & sRep, True )
        End If 'sRep not empty
      Else 'bBegin = False, found ending delimiter
        oCursor.collapseToEnd
        oCursor.goLeft( 4, True )	'include two para marks
        'replace table-end delimiter with template call delimiter
        thisPortion.Text.insertString( oCursor, "}}", True )
        bBegin = True
      EndIf 'bBegin
    End If 'hit
  Wend			'loop for all portions in this paragraph
Wend		'loop for all paragraphs in document
 
MsgBox "Language code " &sISO & " Processing Complete!" & chr(13) & chr(13) _
  & "Paragraphs - " & str(iPara) & chr(13) _
  & "Portions - " & str(iPor) & chr(13) _
  & "Cautions - " & str(iCaution) & chr(13) _
  & "Notes - " & str(iNote) & chr(13) _
  & "Tips - " & str(iTip) & chr(13) _
  & "Other tables - " & str(iTable) & chr(13) _
  ,, "T|N|C ver " & sV
 
End Sub 'TipNoteCautionCleanup
 
REM V0.0.0 released with V0.0.1 of TNC. No toolbar or buttons yet.
'	Entry points for bold, italics, nowiki, source, and tt.
 
Sub wikiServSub( byVal sBegin as String, Optional byVal sEnd as String )
REM Service routine for entry points which define the brackets.
' If the end bracket is not given, it is the same as the begin.
 
Dim oDoc as Object
Dim oViewCursor as Object
Dim bEmpty as Boolean
Dim iLF as Integer
Dim sTarget as String
 
If IsMissing( sEnd ) Then sEnd = sBegin
 
oDoc = ThisComponent
' get the current cursor position in the GUI.
oViewCursor = oDoc.getCurrentController().getViewCursor()
sTarget = oViewCursor.getString()	'fetch user selection, if any
bEmpty = (sTarget = "")
If bEmpty Then sTarget = "   "
iLF = InStr( sTarget, chr(10) )	'Remove artifact line feeds
Do While iLF
  Mid( sTarget, iLF, 1, "" )
  iLF = InStr( sTarget, chr(10) )
Loop 'for all line feeds
oViewCursor.setString( sBegin & sTarget & sEnd )		'set result
' un-select the insertion.
If bEmpty Then	'select the spaces
  oViewCursor.collapseToStart
  oViewCursor.goRight( len(sBegin), False )
  oViewCursor.goRight( len(sTarget), True )
Else	'select nothing
  oViewCursor.collapseToEnd
End If 'user selection was empty
End Sub 'wikiServSub
 
'--------------------------------
' Entry points for wiki editing.
 
Sub WikiBold
 call wikiServSub( "'''" )
End Sub 'WikiBold
 
Sub WikiItalics
 call wikiServSub( "''" )
End Sub 'WikiItalics
 
Sub WikiNowiki
 call wikiServSub( "<nowiki>", "</nowiki>" )
End Sub 'WikiNowiki
 
Sub WikiSource
' call wikiServSub( "\n<" & "source lang=oobas>\n", "\n<" & "/source>\n" )	'fails, "\n" ignored
 call wikiServSub( chr(13) & "<" & "source lang=""oobas"">" & chr(13), chr(13) & "<" & "/source>" & chr(13) )
' call wikiServSub( "<" & "source lang=""oobas"">", "<" & "/source>" )
End Sub 'WikiSource
 
Sub WikiTT
 call wikiServSub( "<tt>", "</tt>" )
End Sub 'WikiTT
Personal tools