Difference between revisions of "User:TJFrazier/WikiBasic"
From Apache OpenOffice Wiki
(v 0.1.1 with some localization) |
(→Code to post-process the output of MediaWiki Export Filter: V0.1.2) |
||
Line 1: | Line 1: | ||
== Code to post-process the output of MediaWiki Export Filter == | == Code to post-process the output of MediaWiki Export Filter == | ||
+ | Version information is embedded in the code, a little way down. If you need an earlier version, use the history tab to find it. | ||
+ | |||
<source lang="oobas"> | <source lang="oobas"> | ||
REM ***** BASIC ***** | REM ***** BASIC ***** | ||
Line 36: | Line 38: | ||
'Dim vLang as Variant 'array of Locale structs | 'Dim vLang as Variant 'array of Locale structs | ||
− | sV = "0.1. | + | sV = "0.1.2" |
REM Version notes: | REM Version notes: | ||
+ | 'V 0.1.2: Add file-type check for ".txt" input. | ||
+ | ' Remove all tabs, so wiki version aligns properly. | ||
+ | ' Localize tally message. | ||
+ | ' Make user constant localization easy. | ||
'V 0.1.1: Localized for DE. Also necessary to allow for different | 'V 0.1.1: Localized for DE. Also necessary to allow for different | ||
' structure of Caution/Achtung table (no images in DE). | ' structure of Caution/Achtung table (no images in DE). | ||
Line 49: | Line 55: | ||
oDoc = ThisComponent | oDoc = ThisComponent | ||
+ | 'oDoc = StarDesktop.CurrentComponent 'Testing bad-file, gives IDE | ||
+ | If TNCBadFile(oDoc, ".txt") Then Exit Sub | ||
'Localize searches | 'Localize searches | ||
aNames(kNote) = "Note" | aNames(kNote) = "Note" | ||
Line 57: | Line 65: | ||
'vLang = oDoc.getDocumentLanguages(1, 2) | 'vLang = oDoc.getDocumentLanguages(1, 2) | ||
'sISO = vLang(0).Country | 'sISO = vLang(0).Country | ||
− | sISO = InputBox( "Please input ISO language code", "T/N/C Cleanup ver " & sV, "US") | + | |
+ | ' USER LOCALIZE: only one of the following group of lines should be active (not commented out). | ||
+ | ' Pick whichever you find convenient. | ||
+ | sISO = InputBox( "Please input ISO language code", "T/N/C Cleanup ver " & sV, "US") 'ask language | ||
+ | 'sISO = "US" 'always US | ||
+ | 'sISO = "DE" 'always DE | ||
+ | ' End group. | ||
+ | |||
If sISO = "DE" then | If sISO = "DE" then | ||
aNames(kNote) = "Anmerkung" | aNames(kNote) = "Anmerkung" | ||
Line 63: | Line 78: | ||
aNames(kCaution) = "Achtung" | aNames(kCaution) = "Achtung" | ||
End If 'DE | End If 'DE | ||
+ | ' Other languages can be added here, in similar fashion. | ||
+ | ' Careful! Some code may be needed to parse differences in the tables. | ||
+ | ' See other references to variable sISO. | ||
enum1 = oDoc.Text.createEnumeration | enum1 = oDoc.Text.createEnumeration | ||
Line 95: | Line 113: | ||
sRep = "Tip" | sRep = "Tip" | ||
iTip = iTip + 1 | iTip = iTip + 1 | ||
− | + | ElseIf InStr( oCursor.String, aNames(kCaution) ) Then | |
' sRep = aNames(kCaution) | ' 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 | 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 | EndIf 'NTC or other table found | ||
If sRep <> "" Then | If sRep <> "" Then | ||
− | + | thisPortion.Text.insertString( oCursor, "{{Documentation/" & sRep, True ) | |
End If 'sRep not empty | End If 'sRep not empty | ||
Else 'bBegin = False, found ending delimiter | Else 'bBegin = False, found ending delimiter | ||
Line 129: | Line 147: | ||
& "Paragraphs - " & str(iPara) & chr(13) _ | & "Paragraphs - " & str(iPara) & chr(13) _ | ||
& "Portions - " & str(iPor) & chr(13) _ | & "Portions - " & str(iPor) & chr(13) _ | ||
− | & " | + | & aNames(kCaution) & " - " & str(iCaution) & chr(13) _ |
− | & " | + | & aNames(kNote) & " - " & str(iNote) & chr(13) _ |
− | & " | + | & aNames(kTip) & " - " & str(iTip) & chr(13) _ |
& "Other tables - " & str(iTable) & chr(13) _ | & "Other tables - " & str(iTable) & chr(13) _ | ||
,, "T|N|C ver " & sV | ,, "T|N|C ver " & sV | ||
Line 137: | Line 155: | ||
End Sub 'TipNoteCautionCleanup | End Sub 'TipNoteCautionCleanup | ||
+ | Function TNCBadFile(oDoc as Object, Optional sSuffix as String) as Boolean | ||
+ | Dim sName as String | ||
+ | Dim sURL as String | ||
+ | |||
+ | On local error GoTo BadFile | ||
+ | sName = oDoc.ImplementationName | ||
+ | sURL = oDoc.URL | ||
+ | If IsMissing(sSuffix) Then sSuffix = "" | ||
+ | If len(sSuffix) = 0 Then Exit Function | ||
+ | If Right(sUrl, len(sSuffix)) = sSuffix Then Exit Function | ||
+ | |||
+ | BadFile: | ||
+ | On error GoTo 0 | ||
+ | Dim iAnswer as Integer | ||
+ | iAnswer = MsgBox( "Found this target file:" & chr(13) _ | ||
+ | & "Name: " & sName & chr(13) _ | ||
+ | & "URL: " & sURL & chr(13) _ | ||
+ | & "You probably want to bring the proper file to the foreground, and run the macro again." _ | ||
+ | & chr(13) & chr(13) & "Process this file anyway?" _ | ||
+ | ,MB_YESNO + MB_DEFBUTTON2 + MB_ICONEXCLAMATION, "WRONG FILE?" ) | ||
+ | TNCBadFile = (iAnswer <> IDYES) | ||
+ | End Function 'TNCBadFile | ||
+ | |||
+ | REM V0.0.1 with V0.1.2 of TNC. "Source" bracket split up for Syntax Highlighter. | ||
REM V0.0.0 released with V0.0.1 of TNC. No toolbar or buttons yet. | 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. | ' Entry points for bold, italics, nowiki, source, and tt. | ||
Line 178: | Line 220: | ||
Sub WikiBold | Sub WikiBold | ||
− | + | call wikiServSub( "'''" ) | |
End Sub 'WikiBold | End Sub 'WikiBold | ||
Sub WikiItalics | Sub WikiItalics | ||
− | + | call wikiServSub( "''" ) | |
End Sub 'WikiItalics | End Sub 'WikiItalics | ||
Sub WikiNowiki | Sub WikiNowiki | ||
− | + | call wikiServSub( "<nowiki>", "</nowiki>" ) | |
End Sub 'WikiNowiki | End Sub 'WikiNowiki | ||
Sub WikiSource | Sub WikiSource | ||
− | ' call wikiServSub( "\n<" & "source lang=oobas>\n", "\n<" & "/source>\n" ) 'fails, "\n" ignored | + | ' 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) ) | ||
End Sub 'WikiSource | End Sub 'WikiSource | ||
Sub WikiTT | Sub WikiTT | ||
− | + | call wikiServSub( "<tt>", "</tt>" ) | |
End Sub 'WikiTT | End Sub 'WikiTT | ||
</source> | </source> |
Revision as of 19:48, 18 September 2009
Code to post-process the output of MediaWiki Export Filter
Version information is embedded in the code, a little way down. If you need an earlier version, use the history tab to find it.
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.2" REM Version notes: 'V 0.1.2: Add file-type check for ".txt" input. ' Remove all tabs, so wiki version aligns properly. ' Localize tally message. ' Make user constant localization easy. '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 'oDoc = StarDesktop.CurrentComponent 'Testing bad-file, gives IDE If TNCBadFile(oDoc, ".txt") Then Exit Sub '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 ' USER LOCALIZE: only one of the following group of lines should be active (not commented out). ' Pick whichever you find convenient. sISO = InputBox( "Please input ISO language code", "T/N/C Cleanup ver " & sV, "US") 'ask language 'sISO = "US" 'always US 'sISO = "DE" 'always DE ' End group. If sISO = "DE" then aNames(kNote) = "Anmerkung" aNames(kTip) = "TIPP" aNames(kCaution) = "Achtung" End If 'DE ' Other languages can be added here, in similar fashion. ' Careful! Some code may be needed to parse differences in the tables. ' See other references to variable sISO. 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) _ & aNames(kCaution) & " - " & str(iCaution) & chr(13) _ & aNames(kNote) & " - " & str(iNote) & chr(13) _ & aNames(kTip) & " - " & str(iTip) & chr(13) _ & "Other tables - " & str(iTable) & chr(13) _ ,, "T|N|C ver " & sV End Sub 'TipNoteCautionCleanup Function TNCBadFile(oDoc as Object, Optional sSuffix as String) as Boolean Dim sName as String Dim sURL as String On local error GoTo BadFile sName = oDoc.ImplementationName sURL = oDoc.URL If IsMissing(sSuffix) Then sSuffix = "" If len(sSuffix) = 0 Then Exit Function If Right(sUrl, len(sSuffix)) = sSuffix Then Exit Function BadFile: On error GoTo 0 Dim iAnswer as Integer iAnswer = MsgBox( "Found this target file:" & chr(13) _ & "Name: " & sName & chr(13) _ & "URL: " & sURL & chr(13) _ & "You probably want to bring the proper file to the foreground, and run the macro again." _ & chr(13) & chr(13) & "Process this file anyway?" _ ,MB_YESNO + MB_DEFBUTTON2 + MB_ICONEXCLAMATION, "WRONG FILE?" ) TNCBadFile = (iAnswer <> IDYES) End Function 'TNCBadFile REM V0.0.1 with V0.1.2 of TNC. "Source" bracket split up for Syntax Highlighter. 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) ) End Sub 'WikiSource Sub WikiTT call wikiServSub( "<tt>", "</tt>" ) End Sub 'WikiTT