' Created: 2004-12-08 ' Last modified: 2005-04-01 ' MoinMoin-Replacements: 2005-07-26 'Creating a new document that contains the current document in MoinMoin format '***************************************************************************** ' Copyright © 2005 Ian Laurenson ' Anyone may run this code. ' If you wish to modify or distribute this code, then ' you are granted a license to do so under the terms ' of the Gnu Lesser General Public License. ' See: http://www.gnu.org/licenses/lgpl.html ' ' Improvements and replacements for MoinMoinWikiEngine: 2005 KlausHeinisch ' See: http://moinmoin.wikiwikiweb.de/FeatureRequests/OpenOfficeConversion '***************************************************************************** const sBOLDSTART = "'''" const sBOLDEND = "'''" const sUNDERSTART = "__" const sUNDEREND = "__" const sITALICSTART = "''" const sITALICEND = "''" const sMONOSTART = "`" const sMONOEND = "`" const sSUPERSTART = "^" const sSUPEREND = "^" const sSUBSTART = ",," const sSUBEND = ",," const sDELSTART = "" const sDELEND = "" const sFOOTSTART = "[[FootNote(^" const sFOOTEND = "^)]]" const sHEADCHAR = "=" const sHORIZLINE = "----" const sTABLESEP = "||" const sTABLEHEADSEP = "||" const sNEWLINE = "[[BR]]" const sLITERALSTART = "{{{" const sLITERALEND = "}}}" const sORDEREDLIST = "1. " const sUNORDEREDLIST = "* " const sHYPERSTART = "[" const sHYPERSEP = " " const sHYPEREND = "]" const sPICTURESTART = "{{wiki:" const sPICTUREEND = "}}" const sCODESTART = "###" const sCODEEND = "###" const sBLANK = " " const sDEFAULTCODE = "oobas" private sLineEnd as string private s as string private oVC private bInCode as boolean private mCodes 'This routine processes all html files in a directory tree. sub Folders dim mTextFile(0) mTextFile(0) = createUnoStruct("com.sun.star.beans.PropertyValue") mTextFile(0).Name = "FilterName" mTextFile(0).Value = "Text" basicLibraries.loadLibrary("Tools") 'ReadDirectories(ByVal AnchorDir As String, bRecursive as Boolean, bcheckFileType as Boolean, bGetByTitle as Boolean, Optional sFileContent(), Optional sExtension as String) mFiles = ReadDirectories("/var/www/html/dokuwiki/data/ref", true, false, false,,"html") 'sStart = "file:///var/www/html/dokuwiki/data/" 'nStart = len(sStart) + 1 for i = 0 to uBound(mfiles) sFile = mFiles(i) if right(sFile,5) = ".html" then oSourceDoc = StarDesktop.loadComponentFromURL(sFile, "_blank", 0, Array()) oText = oSourceDoc.text oDestDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() ) oDestText = oDestDoc.getText() ' oCursor = oDestText.createTextCursor() oVC = oDestDoc.currentController.viewCursor subDokuWiki(oText) ' oVC.text.insertString(oVC.text.end, "[[http://api.openoffice.org/docs/common/" & mid(sFile, nStart) & "]]", false) oDestDoc.storeAsUrl(left(sFile,len(sFile)-4) & "txt", mTextFile()) oDestDoc.close(true) oSourceDoc.close(false) ' kill sFile 'Uncomment this line to remove the original file end if next end sub 'This is the main routine to run on the currently open document sub DokuWiki oSourceDoc = thisComponent oDestDoc= fnDokuWiki(oSourceDoc) 'oDestDoc.close(false) end sub function fnDokuWiki(oSourceDoc) oText = oSourceDoc.text 'Create a new document oNewDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() ) oNewText = oNewDoc.getText() oCursor = oNewText.createTextCursor() 'oNewText.insertString(oCursor,sDokuWiki , true) oVC = oNewDoc.currentController.viewCursor mCodes = array("actionscript", "ada", "apache", "asm", "asp", "bash", "caddcl", "cadlisp", "c_mac", "cpp", "csharp", _ "css", "delphi", "html4strict", "javascript", "java", "lisp", "lua", "nsis", "objc", "oobas", "pascal", "perl", _ "php-brief", "php", "python", "qbasic", "smarty", "sql", "vbnet", "vb", "visualfoxpro", "xml", "c") subDokuWiki(oText) 'Copy to clipboard 'There is a way of doing this via the API but this will do oVC.gotoStart(false) oVC.gotoEnd(true) oFrame = oNewDoc.CurrentController.Frame oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper") oDispatcher.executeDispatch(oFrame, ".uno:Copy", "", 0, array()) fnDokuWiki = oNewDoc end function sub subDokuWiki(oText) sLineEnd = chr(10) 'If not oDoc.supportsService("com.sun.star.text.TextDocument") then ' msgBox "Sorry - I can only create DokuWiki documents out of text documents", 16, "Error" ' exit function 'end if s = "" oEnum1 = oText.createEnumeration ' loop over all paragraphs while oEnum1.hasMoreElements oTextElement = oEnum1.nextElement if oTextElement.supportsService("com.sun.star.text.Paragraph") Then subParagraph(oTextElement) elseif oTextElement.supportsService("com.sun.star.text.TextTable") Then subTable(oTextElement) end if wend if bIncode then subAddString(sCODEEND & sLineEnd) end if end sub sub subParagraph(oTextElement) nHeadLevel = oTextElement.ParaChapterNumberingLevel + 1 bInlist = false sCode = oTextElement.paraStyleName sCode = fnInCode(sCode) if bIncode and sCode = "" then subAddString(sCODEEND & sLineEnd) bInCode = false end if if nHeadLevel >= 1 and nHeadLevel <= 5 then sHead = string(nHeadLevel, sHEADCHAR) subAddString(sLineEnd & sHead & sBLANK & oTextElement.string & sBLANK & sHead & sLineEnd) elseif sCode <> "" then if not bInCode then subAddString(sCODESTART & sCode & sCODEEND & sLineEnd) bInCode = true end if subAddString(oTextElement.string & sLineEnd) else if oTextElement.NumberingIsNumber then 'if not isEmpty(oTextElement.NumberingLevel) then ' xray.xray oTextElement bInlist = true subAddString(string((oTextElement.NumberingLevel + 1) * 2, " ")) if instr("0123456789", left(oTextElement.string, 1)) > 0 then subAddString(sORDEREDLIST) else subAddString(sUNORDEREDLIST) end if end if oEnum2 = oTextElement.createEnumeration ' loop over all text portions while oEnum2.hasMoreElements oTextPortion = oEnum2.nextElement subAddString(fnTextPortion(oTextPortion, false)) wend subAddString(sLineEnd) if oTextElement.bottomBorder.OuterLineWidth > 0 then subAddString(sHORIZLINE & sLineEnd) else if not bInList then subAddString(sLineEnd) end if end if end sub sub subTable(oTable) mCellnames = oTable.getCellNames nMaxCols = 0 for i = 0 to uBound(mCellNames) sCellName = mCellNames(i) nCol = 0 for j = 1 to len(sCellName) ch = mid(sCellName, j, 1) select case ch case "A" to "Z" nCol = nCol * 26 + (asc(ch) - asc("A")) case "0" to "9" 'nRow = val(mid(sCellName, j)) - 1 exit for end select next if nCol > nMaxCols then nMaxCols = nCol next nRows = oTable.rows.count - 1 if nMaxCols = 0 then for i = 0 to nRows oCell = oTable.getCellByPosition(0, i) oCellEnum = oCell.createEnumeration while oCellEnum.hasMoreElements subParagraph(oCellEnum.nextElement) wend next exit sub end if dim mCells(nRows, nMaxCols) 'for i = 0 to nRows ' for j = 0 to nmaxCols - 1 ' mCells(i, j) = "" ' next 'next for i = 0 to uBound(mCellNames) sCellName = mCellNames(i) nCol = 0 for j = 1 to len(sCellName) ch = mid(sCellName, j, 1) select case ch case "A" to "Z" nCol = nCol * 26 + (asc(ch) - asc("A")) case "0" to "9" nRow = val(mid(sCellName, j)) - 1 exit for end select next oCell = oTable.getCellByName(sCellName) sCell = " " oCellEnum = oCell.createEnumeration while oCellEnum.hasMoreElements oTextElement = oCellEnum.nextElement if sCell <> " " then sCell = sCell & sNEWLINE end if if oTextElement.ParaStyleName = "Table Heading" then sSep = sTABLEHEADSEP else sSep = sTABLESEP end if if oTextElement.NumberingIsNumber then if instr("0123456789", left(oTextElement.string, 1)) > 0 then sCell = sCell & str(val(oTextElement.string)) else sCell = sCell & "* " end if end if oPortionEnum = oTextElement.createEnumeration while oPortionEnum.hasMoreElements oTextPortion = oPortionEnum.nextElement sCell = sCell & fnTextPortion(oTextPortion, true) wend wend sCell =trim(sCell) select case oTextElement.paraAdjust case com.sun.star.style.ParagraphAdjust.CENTER sCell = " " & sCell & " " case com.sun.star.style.ParagraphAdjust.RIGHT sCell = " " & sCell end select mCells(nRow, nCol) = sCell next for i = 0 to nRows sRow = "" sRow = sRow & sTableSep bHeading = false for j = 0 to nmaxCols if mCells(i, j) <> "" then if j = 0 and instr(mCells(i, j), chr(10)) = 0 then bHeading = true else bHeading = false end if end if sRow = sRow & mCells(i, j) & sTableSep next if bHeading then if i = 0 then sRow = string(6, sHEADCHAR) & mCells(i, 0) & string(6, sHEADCHAR) & sLineEnd & sLineEnd else sRow = string(5, sHEADCHAR) & mCells(i, 0) & string(5, sHEADCHAR) & sLineEnd & sLineEnd end if else sRow = sRow & sLineEnd end if subAddString(sRow) next subAddString(sLineEnd) end sub function fnTextPortion(oTextPortion, bInTable as boolean) if not isNull(oTextPortion.footnote) then fnTextPortion = sFOOTStart & oTextPortion.footnote.string & sFOOTEND elseif oTextPortion.hyperlinkURL <> "" then 'NB: If the link has separate text portions (i.e. formats inside it) this will repeat the link :( fnTextPortion = sHYPERSTART & fnHyperConvert(oTextPortion.hyperlinkURL) & sHYPERSEP & oTextPortion.string & sHYPEREND elseif oTextPortion.TextPortionType = "Frame" then 'The above condition may need to be tightened sName = oTextPortion.createContentEnumeration("com.sun.star.text.TextContent").nextElement.name if sName <> "" then fnTextPortion = sPICTURESTART & sName & sPICTUREEND end if else sPortion = oTextPortion.string if sPortion = "" then fnTextPortion = "" exit function end if if bInTable then 'In case the separators are actually in the text of the table mSplits = split(sPortion, sTABLESEP) sPortion = join(mSplits, sLITERALSTART & sTABLESEP & sLITERALEND) mSplits = split(sPortion, sTABLEHEADSEP) sPortion = join(mSplits, sLITERALSTART & sTABLEHEADSEP & sLITERALEND) else 'In the unlikley event of a paragraph starting and finishing with separator characters. sFirstChar = left(sPortion, 1) if sFirstChar = sTABLESEP or sFirstChar = sTABLEHEADSEP then sPortion = sLITERALSTART & sFirstChar & sLITERALEND & mid(sPortion,2) end if end if 'Convert smart quotes mSplits = split(sPortion, "“") sPortion = join(mSplits, chr(34)) mSplits = split(sPortion, "”") sPortion = join(mSplits, chr(34)) mSplits = split(sPortion, "‘") sPortion = join(mSplits, "'") mSplits = split(sPortion, "’") sPortion = join(mSplits, "'") 'Convert em dashes mSplits = split(sPortion, "–") sPortion = join(mSplits, "-") 'In case the text holds any of the formatting charaters make sure that they are treated literally mSplits = split(sPortion, sBOLDSTART) sPortion = join(mSplits, sLITERALSTART & sBOLDSTART & sLITERALEND) mSplits = split(sPortion, sUNDERSTART) sPortion = join(mSplits, sLITERALSTART & sUNDERSTART & sLITERALEND) mSplits = split(sPortion, sITALICSTART) sPortion = join(mSplits, sLITERALSTART & sITALICSTART & sLITERALEND) mSplits = split(sPortion, sMONOSTART) sPortion = join(mSplits, sLITERALSTART & sMONOSTART & sLITERALEND) mSplits = split(sPortion, sSUPERSTART) sPortion = join(mSplits, sLITERALSTART & sSUPERSTART & sLITERALEND) mSplits = split(sPortion, sSUBSTART) sPortion = join(mSplits, sLITERALSTART & sSUBSTART & sLITERALEND) mSplits = split(sPortion, sDELSTART) sPortion = join(mSplits, sLITERALSTART & sDELSTART & sLITERALEND) mSplits = split(sPortion, sFOOTSTART) sPortion = join(mSplits, sLITERALSTART & sFOOTSTART & sLITERALEND) mSplits = split(sPortion, sPICTURESTART) sPortion = join(mSplits, sLITERALSTART & sPICTURESTART & sLITERALEND) mSplits = split(sPortion, sCODESTART) sPortion = join(mSplits, sLITERALSTART & sCODESTART & sLITERALEND) mSplits = split(sPortion, sLineEnd) sPortion = join(mSplits, sNEWLINE & " ") 'This is not very elegant as it will produce **bold**//**bold & italic**// 'rather than **bold//bold & italic//** if oTextPortion.charWeight > 100 then sPortion = sBOLDSTART & sPortion & sBOLDEND if oTextPortion.charPosture > 0 then sPortion = sITALICSTART & sPortion & sITALICEND if oTextPortion.charUnderline > 0 then sPortion = sUNDERSTART & sPortion & sUNDEREND if oTextPortion.charFontPitch = com.sun.star.awt.FontPitch.FIXED then sPortion = sMONOSTART & sPortion & sMONOEND if oTextPortion.charEscapement > 0 then sPortion = sSUPERSTART & sPortion & sSUPEREND if oTextPortion.charEscapement < 0 then sPortion = sSUBSTART & sPortion & sSUBEND if oTextPortion.charStrikeOut > 0 then sPortion = sDELSTART & sPortion & sDELEND fnTextPortion = sPortion end if end function function fnHyperConvert(sURL as string) as string const sINTLINK = "doku.php?id=" 'sIDL = "vnd.sun.star.help://sbasic/text/sbasic/shared/" 'sGuide = "file:///var/www/html/dokuwiki/DevelopersGuide" 'nIDL = len(sIDL) 'nGuide = len(sGuide) nStartInternalLink = instr(sUrl, sINTLINK) if nStartInternalLink > 0 then nStartInternalLink = nStartInternalLink + len(sINTLINK) fnHyperConvert = mid(sURL, nStartInternalLink) 'elseif sIDL = left(sURL, nIDL) then ' sTemp = mid(sURL, nIDL + 1) ' sTemp = left(stemp,(instr(sTemp, ".xhp")-1)) ' mSplits = split(sTemp, ".html") ' sTemp = join(mSplits, "") ' mSplits = split(sTemp, "/") fnHyperConvert = sTemp 'join(mSplits, ":") 'elseif left(sURL, nGuide) = sGuide then ' sTemp = "http://api.openoffice.org/docs/DevelopersGuide" & mid(sURL, nGuide + 1) ' mSplits = split(sTemp, ".xhtml") ' fnHyperConvert = join(mSplits, ".htm") else fnHyperConvert = sURL end if end function function fnInCode(sParaStyleName) nCode = instr(1, sParaStyleName, "Code") if nCode > 0 then nCode = nCode + 4 for i = 0 to ubound(mCodes) if instr(nCode, sParaStyleName, mCodes(i)) > 0 then fnInCode = mCodes(i) exit for end if next elseif sParaStyleName = "Preformatted Text" then fnInCode = sDEFAULTCODE else fnInCode = "" end if end function sub subAddString(sAdd as string) if sAdd = sLineEnd then 'A paragraph can't be > 64k therefore this hack makes sure of some paragraph breaks oVC.text.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) else oVC.text.insertString(oVC, sAdd , false) end if end sub