Archiv des LibreOffice- und OpenOffice.org-Wiki

[ÜberSicht] [TitelIndex] [WortIndex] [SeiteFinden

Besondere Lizenzbedingungen für Programmcode

This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

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

<!> Die Darstellung auf dieser Seite kann je nach der Schriftgröße, die im Browser eingestellt wurde, unerwünschte Zeilenumbrüche enthalten.


KategorieMakro


LizenzBedingungen | AnbieterKennzeichnung | DatenschutzErklärung | Stand: 2013-04-28