User:TJFrazier/WikiBasic

From Apache OpenOffice Wiki
< User:TJFrazier
Revision as of 01:09, 5 September 2009 by TJFrazier (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 sFind as String		'sentinel sought
Dim sRep as String		'replacement
Dim sText as String		'scratch
Dim sV as String		'release version
 
oDoc = ThisComponent
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.
        If InStr( oCursor.String, "Note" ) Then
	      sRep = "Note"
	      iNote = iNote + 1
        ElseIf InStr( oCursor.String, "Tip" ) Then
	      sRep = "Tip"
	      iTip = iTip + 1
	    ElseIf InStr( oCursor.String, "Caution" ) Then
	      sRep = "Caution"
	      iCaution = iCaution + 1
	      '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
	    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
sV = "0.1.0"
REM Version notes:
'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.
 
MsgBox "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
 
' Some wikification was needed below, to keep the Syntax Highlighter happy.
Sub WikiSource
 call wikiServSub( chr(13) & "<" & "source lang=""oobas"">" & chr(13), chr(13) & "<" & "/source>" & chr(13) )
End Sub 'WikiSource
 
Sub WikiTT
 call wikiServSub( "<tt>", "</tt>" )
End Sub 'WikiTT
Personal tools