Attachment 'DokuWiki2MoinMoin.txt'
Download 1 ' Created: 2004-12-08
2 ' Last modified: 2005-04-01
3 ' MoinMoin-Replacements: 2005-07-26
4
5 'Creating a new document that contains the current document in MoinMoin format
6
7 '*****************************************************************************
8 ' Copyright © 2005 Ian Laurenson
9 ' Anyone may run this code.
10 ' If you wish to modify or distribute this code, then
11 ' you are granted a license to do so under the terms
12 ' of the Gnu Lesser General Public License.
13 ' See: http://www.gnu.org/licenses/lgpl.html
14 '
15 ' Improvements and replacements for MoinMoinWikiEngine: 2005 KlausHeinisch
16 ' See: http://moinmoin.wikiwikiweb.de/FeatureRequests/OpenOfficeConversion
17 '*****************************************************************************
18 const sBOLDSTART = "'''"
19 const sBOLDEND = "'''"
20 const sUNDERSTART = "__"
21 const sUNDEREND = "__"
22 const sITALICSTART = "''"
23 const sITALICEND = "''"
24 const sMONOSTART = "`"
25 const sMONOEND = "`"
26 const sSUPERSTART = "^"
27 const sSUPEREND = "^"
28 const sSUBSTART = ",,"
29 const sSUBEND = ",,"
30 const sDELSTART = "<del>"
31 const sDELEND = "</del>"
32 const sFOOTSTART = "[[FootNote(^"
33 const sFOOTEND = "^)]]"
34 const sHEADCHAR = "="
35 const sHORIZLINE = "----"
36 const sTABLESEP = "||"
37 const sTABLEHEADSEP = "||"
38 const sNEWLINE = "[[BR]]"
39 const sLITERALSTART = "{{{"
40 const sLITERALEND = "}}}"
41 const sORDEREDLIST = "1. "
42 const sUNORDEREDLIST = "* "
43 const sHYPERSTART = "["
44 const sHYPERSEP = " "
45 const sHYPEREND = "]"
46 const sPICTURESTART = "{{wiki:"
47 const sPICTUREEND = "}}"
48 const sCODESTART = "###"
49 const sCODEEND = "###"
50 const sBLANK = " "
51
52 const sDEFAULTCODE = "oobas"
53
54 private sLineEnd as string
55
56 private s as string
57 private oVC
58 private bInCode as boolean
59 private mCodes
60
61 'This routine processes all html files in a directory tree.
62 sub Folders
63 dim mTextFile(0)
64
65 mTextFile(0) = createUnoStruct("com.sun.star.beans.PropertyValue")
66 mTextFile(0).Name = "FilterName"
67 mTextFile(0).Value = "Text"
68
69 basicLibraries.loadLibrary("Tools")
70 'ReadDirectories(ByVal AnchorDir As String, bRecursive as Boolean, bcheckFileType as Boolean, bGetByTitle as Boolean, Optional sFileContent(), Optional sExtension as String)
71 mFiles = ReadDirectories("/var/www/html/dokuwiki/data/ref", true, false, false,,"html")
72
73 'sStart = "file:///var/www/html/dokuwiki/data/"
74 'nStart = len(sStart) + 1
75
76 for i = 0 to uBound(mfiles)
77 sFile = mFiles(i)
78 if right(sFile,5) = ".html" then
79 oSourceDoc = StarDesktop.loadComponentFromURL(sFile, "_blank", 0, Array())
80 oText = oSourceDoc.text
81
82 oDestDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
83 oDestText = oDestDoc.getText()
84 ' oCursor = oDestText.createTextCursor()
85 oVC = oDestDoc.currentController.viewCursor
86 subDokuWiki(oText)
87
88 ' oVC.text.insertString(oVC.text.end, "[[http://api.openoffice.org/docs/common/" & mid(sFile, nStart) & "]]", false)
89 oDestDoc.storeAsUrl(left(sFile,len(sFile)-4) & "txt", mTextFile())
90 oDestDoc.close(true)
91
92 oSourceDoc.close(false)
93 ' kill sFile 'Uncomment this line to remove the original file
94 end if
95 next
96 end sub
97
98
99 'This is the main routine to run on the currently open document
100 sub DokuWiki
101 oSourceDoc = thisComponent
102 oDestDoc= fnDokuWiki(oSourceDoc)
103 'oDestDoc.close(false)
104 end sub
105
106
107 function fnDokuWiki(oSourceDoc)
108 oText = oSourceDoc.text
109 'Create a new document
110 oNewDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
111 oNewText = oNewDoc.getText()
112 oCursor = oNewText.createTextCursor()
113 'oNewText.insertString(oCursor,sDokuWiki , true)
114 oVC = oNewDoc.currentController.viewCursor
115 mCodes = array("actionscript", "ada", "apache", "asm", "asp", "bash", "caddcl", "cadlisp", "c_mac", "cpp", "csharp", _
116 "css", "delphi", "html4strict", "javascript", "java", "lisp", "lua", "nsis", "objc", "oobas", "pascal", "perl", _
117 "php-brief", "php", "python", "qbasic", "smarty", "sql", "vbnet", "vb", "visualfoxpro", "xml", "c")
118
119 subDokuWiki(oText)
120
121 'Copy to clipboard
122 'There is a way of doing this via the API but this will do
123 oVC.gotoStart(false)
124 oVC.gotoEnd(true)
125 oFrame = oNewDoc.CurrentController.Frame
126 oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
127 oDispatcher.executeDispatch(oFrame, ".uno:Copy", "", 0, array())
128
129
130 fnDokuWiki = oNewDoc
131 end function
132
133
134 sub subDokuWiki(oText)
135 sLineEnd = chr(10)
136
137 'If not oDoc.supportsService("com.sun.star.text.TextDocument") then
138 ' msgBox "Sorry - I can only create DokuWiki documents out of text documents", 16, "Error"
139 ' exit function
140 'end if
141 s = ""
142 oEnum1 = oText.createEnumeration
143 ' loop over all paragraphs
144 while oEnum1.hasMoreElements
145 oTextElement = oEnum1.nextElement
146 if oTextElement.supportsService("com.sun.star.text.Paragraph") Then
147 subParagraph(oTextElement)
148 elseif oTextElement.supportsService("com.sun.star.text.TextTable") Then
149 subTable(oTextElement)
150 end if
151 wend
152 if bIncode then
153 subAddString(sCODEEND & sLineEnd)
154 end if
155 end sub
156
157
158 sub subParagraph(oTextElement)
159 nHeadLevel = oTextElement.ParaChapterNumberingLevel + 1
160 bInlist = false
161 sCode = oTextElement.paraStyleName
162 sCode = fnInCode(sCode)
163 if bIncode and sCode = "" then
164 subAddString(sCODEEND & sLineEnd)
165 bInCode = false
166 end if
167 if nHeadLevel >= 1 and nHeadLevel <= 5 then
168 sHead = string(nHeadLevel, sHEADCHAR)
169 subAddString(sLineEnd & sHead & sBLANK & oTextElement.string & sBLANK & sHead & sLineEnd)
170 elseif sCode <> "" then
171 if not bInCode then
172 subAddString(sCODESTART & sCode & sCODEEND & sLineEnd)
173 bInCode = true
174 end if
175 subAddString(oTextElement.string & sLineEnd)
176 else
177 if oTextElement.NumberingIsNumber then 'if not isEmpty(oTextElement.NumberingLevel) then
178 ' xray.xray oTextElement
179 bInlist = true
180 subAddString(string((oTextElement.NumberingLevel + 1) * 2, " "))
181 if instr("0123456789", left(oTextElement.string, 1)) > 0 then
182 subAddString(sORDEREDLIST)
183 else
184 subAddString(sUNORDEREDLIST)
185 end if
186 end if
187 oEnum2 = oTextElement.createEnumeration
188 ' loop over all text portions
189 while oEnum2.hasMoreElements
190 oTextPortion = oEnum2.nextElement
191 subAddString(fnTextPortion(oTextPortion, false))
192 wend
193 subAddString(sLineEnd)
194 if oTextElement.bottomBorder.OuterLineWidth > 0 then
195 subAddString(sHORIZLINE & sLineEnd)
196 else
197 if not bInList then subAddString(sLineEnd)
198 end if
199 end if
200 end sub
201
202
203 sub subTable(oTable)
204 mCellnames = oTable.getCellNames
205
206 nMaxCols = 0
207 for i = 0 to uBound(mCellNames)
208 sCellName = mCellNames(i)
209 nCol = 0
210 for j = 1 to len(sCellName)
211 ch = mid(sCellName, j, 1)
212 select case ch
213 case "A" to "Z"
214 nCol = nCol * 26 + (asc(ch) - asc("A"))
215 case "0" to "9"
216 'nRow = val(mid(sCellName, j)) - 1
217 exit for
218 end select
219 next
220 if nCol > nMaxCols then nMaxCols = nCol
221 next
222
223 nRows = oTable.rows.count - 1
224
225 if nMaxCols = 0 then
226 for i = 0 to nRows
227 oCell = oTable.getCellByPosition(0, i)
228 oCellEnum = oCell.createEnumeration
229 while oCellEnum.hasMoreElements
230 subParagraph(oCellEnum.nextElement)
231 wend
232 next
233 exit sub
234 end if
235
236 dim mCells(nRows, nMaxCols)
237
238 'for i = 0 to nRows
239 ' for j = 0 to nmaxCols - 1
240 ' mCells(i, j) = ""
241 ' next
242 'next
243
244 for i = 0 to uBound(mCellNames)
245 sCellName = mCellNames(i)
246 nCol = 0
247 for j = 1 to len(sCellName)
248 ch = mid(sCellName, j, 1)
249 select case ch
250 case "A" to "Z"
251 nCol = nCol * 26 + (asc(ch) - asc("A"))
252 case "0" to "9"
253 nRow = val(mid(sCellName, j)) - 1
254 exit for
255 end select
256 next
257 oCell = oTable.getCellByName(sCellName)
258 sCell = " "
259 oCellEnum = oCell.createEnumeration
260 while oCellEnum.hasMoreElements
261 oTextElement = oCellEnum.nextElement
262 if sCell <> " " then
263 sCell = sCell & sNEWLINE
264 end if
265
266 if oTextElement.ParaStyleName = "Table Heading" then
267 sSep = sTABLEHEADSEP
268 else
269 sSep = sTABLESEP
270 end if
271
272 if oTextElement.NumberingIsNumber then
273 if instr("0123456789", left(oTextElement.string, 1)) > 0 then
274 sCell = sCell & str(val(oTextElement.string))
275 else
276 sCell = sCell & "* "
277 end if
278 end if
279
280 oPortionEnum = oTextElement.createEnumeration
281 while oPortionEnum.hasMoreElements
282 oTextPortion = oPortionEnum.nextElement
283 sCell = sCell & fnTextPortion(oTextPortion, true)
284 wend
285 wend
286 sCell =trim(sCell)
287 select case oTextElement.paraAdjust
288 case com.sun.star.style.ParagraphAdjust.CENTER
289 sCell = " " & sCell & " "
290 case com.sun.star.style.ParagraphAdjust.RIGHT
291 sCell = " " & sCell
292 end select
293 mCells(nRow, nCol) = sCell
294 next
295
296 for i = 0 to nRows
297 sRow = ""
298 sRow = sRow & sTableSep
299 bHeading = false
300 for j = 0 to nmaxCols
301 if mCells(i, j) <> "" then
302 if j = 0 and instr(mCells(i, j), chr(10)) = 0 then
303 bHeading = true
304 else
305 bHeading = false
306 end if
307 end if
308 sRow = sRow & mCells(i, j) & sTableSep
309 next
310 if bHeading then
311 if i = 0 then
312 sRow = string(6, sHEADCHAR) & mCells(i, 0) & string(6, sHEADCHAR) & sLineEnd & sLineEnd
313 else
314 sRow = string(5, sHEADCHAR) & mCells(i, 0) & string(5, sHEADCHAR) & sLineEnd & sLineEnd
315 end if
316 else
317 sRow = sRow & sLineEnd
318 end if
319
320 subAddString(sRow)
321 next
322
323 subAddString(sLineEnd)
324 end sub
325
326
327 function fnTextPortion(oTextPortion, bInTable as boolean)
328
329 if not isNull(oTextPortion.footnote) then
330 fnTextPortion = sFOOTStart & oTextPortion.footnote.string & sFOOTEND
331 elseif oTextPortion.hyperlinkURL <> "" then
332 'NB: If the link has separate text portions (i.e. formats inside it) this will repeat the link :(
333 fnTextPortion = sHYPERSTART & fnHyperConvert(oTextPortion.hyperlinkURL) & sHYPERSEP & oTextPortion.string & sHYPEREND
334 elseif oTextPortion.TextPortionType = "Frame" then
335 'The above condition may need to be tightened
336 sName = oTextPortion.createContentEnumeration("com.sun.star.text.TextContent").nextElement.name
337 if sName <> "" then
338 fnTextPortion = sPICTURESTART & sName & sPICTUREEND
339 end if
340 else
341 sPortion = oTextPortion.string
342 if sPortion = "" then
343 fnTextPortion = ""
344 exit function
345 end if
346 if bInTable then
347 'In case the separators are actually in the text of the table
348 mSplits = split(sPortion, sTABLESEP)
349 sPortion = join(mSplits, sLITERALSTART & sTABLESEP & sLITERALEND)
350 mSplits = split(sPortion, sTABLEHEADSEP)
351 sPortion = join(mSplits, sLITERALSTART & sTABLEHEADSEP & sLITERALEND)
352 else
353 'In the unlikley event of a paragraph starting and finishing with separator characters.
354 sFirstChar = left(sPortion, 1)
355 if sFirstChar = sTABLESEP or sFirstChar = sTABLEHEADSEP then
356 sPortion = sLITERALSTART & sFirstChar & sLITERALEND & mid(sPortion,2)
357 end if
358 end if
359 'Convert smart quotes
360 mSplits = split(sPortion, "")
361 sPortion = join(mSplits, chr(34))
362 mSplits = split(sPortion, "")
363 sPortion = join(mSplits, chr(34))
364 mSplits = split(sPortion, "")
365 sPortion = join(mSplits, "'")
366 mSplits = split(sPortion, "")
367 sPortion = join(mSplits, "'")
368 'Convert em dashes
369 mSplits = split(sPortion, "")
370 sPortion = join(mSplits, "-")
371 'In case the text holds any of the formatting charaters make sure that they are treated literally
372 mSplits = split(sPortion, sBOLDSTART)
373 sPortion = join(mSplits, sLITERALSTART & sBOLDSTART & sLITERALEND)
374 mSplits = split(sPortion, sUNDERSTART)
375 sPortion = join(mSplits, sLITERALSTART & sUNDERSTART & sLITERALEND)
376 mSplits = split(sPortion, sITALICSTART)
377 sPortion = join(mSplits, sLITERALSTART & sITALICSTART & sLITERALEND)
378 mSplits = split(sPortion, sMONOSTART)
379 sPortion = join(mSplits, sLITERALSTART & sMONOSTART & sLITERALEND)
380 mSplits = split(sPortion, sSUPERSTART)
381 sPortion = join(mSplits, sLITERALSTART & sSUPERSTART & sLITERALEND)
382 mSplits = split(sPortion, sSUBSTART)
383 sPortion = join(mSplits, sLITERALSTART & sSUBSTART & sLITERALEND)
384 mSplits = split(sPortion, sDELSTART)
385 sPortion = join(mSplits, sLITERALSTART & sDELSTART & sLITERALEND)
386 mSplits = split(sPortion, sFOOTSTART)
387 sPortion = join(mSplits, sLITERALSTART & sFOOTSTART & sLITERALEND)
388 mSplits = split(sPortion, sPICTURESTART)
389 sPortion = join(mSplits, sLITERALSTART & sPICTURESTART & sLITERALEND)
390 mSplits = split(sPortion, sCODESTART)
391 sPortion = join(mSplits, sLITERALSTART & sCODESTART & sLITERALEND)
392 mSplits = split(sPortion, sLineEnd)
393 sPortion = join(mSplits, sNEWLINE & " ")
394
395 'This is not very elegant as it will produce **bold**//**bold & italic**//
396 'rather than **bold//bold & italic//**
397 if oTextPortion.charWeight > 100 then sPortion = sBOLDSTART & sPortion & sBOLDEND
398 if oTextPortion.charPosture > 0 then sPortion = sITALICSTART & sPortion & sITALICEND
399 if oTextPortion.charUnderline > 0 then sPortion = sUNDERSTART & sPortion & sUNDEREND
400 if oTextPortion.charFontPitch = com.sun.star.awt.FontPitch.FIXED then sPortion = sMONOSTART & sPortion & sMONOEND
401 if oTextPortion.charEscapement > 0 then sPortion = sSUPERSTART & sPortion & sSUPEREND
402 if oTextPortion.charEscapement < 0 then sPortion = sSUBSTART & sPortion & sSUBEND
403 if oTextPortion.charStrikeOut > 0 then sPortion = sDELSTART & sPortion & sDELEND
404 fnTextPortion = sPortion
405 end if
406 end function
407
408
409 function fnHyperConvert(sURL as string) as string
410 const sINTLINK = "doku.php?id="
411
412 'sIDL = "vnd.sun.star.help://sbasic/text/sbasic/shared/"
413 'sGuide = "file:///var/www/html/dokuwiki/DevelopersGuide"
414 'nIDL = len(sIDL)
415 'nGuide = len(sGuide)
416 nStartInternalLink = instr(sUrl, sINTLINK)
417 if nStartInternalLink > 0 then
418 nStartInternalLink = nStartInternalLink + len(sINTLINK)
419 fnHyperConvert = mid(sURL, nStartInternalLink)
420 'elseif sIDL = left(sURL, nIDL) then
421 ' sTemp = mid(sURL, nIDL + 1)
422 ' sTemp = left(stemp,(instr(sTemp, ".xhp")-1))
423 ' mSplits = split(sTemp, ".html")
424 ' sTemp = join(mSplits, "")
425 ' mSplits = split(sTemp, "/")
426 fnHyperConvert = sTemp 'join(mSplits, ":")
427 'elseif left(sURL, nGuide) = sGuide then
428 ' sTemp = "http://api.openoffice.org/docs/DevelopersGuide" & mid(sURL, nGuide + 1)
429 ' mSplits = split(sTemp, ".xhtml")
430 ' fnHyperConvert = join(mSplits, ".htm")
431 else
432 fnHyperConvert = sURL
433 end if
434 end function
435
436
437 function fnInCode(sParaStyleName)
438 nCode = instr(1, sParaStyleName, "Code")
439
440 if nCode > 0 then
441 nCode = nCode + 4
442 for i = 0 to ubound(mCodes)
443 if instr(nCode, sParaStyleName, mCodes(i)) > 0 then
444 fnInCode = mCodes(i)
445 exit for
446 end if
447 next
448 elseif sParaStyleName = "Preformatted Text" then
449 fnInCode = sDEFAULTCODE
450 else
451 fnInCode = ""
452 end if
453 end function
454
455
456 sub subAddString(sAdd as string)
457
458 if sAdd = sLineEnd then 'A paragraph can't be > 64k therefore this hack makes sure of some paragraph breaks
459 oVC.text.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
460 else
461 oVC.text.insertString(oVC, sAdd , false)
462 end if
463 end sub
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.