-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrgFileExporter.py
496 lines (418 loc) · 18.2 KB
/
OrgFileExporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# -*- encoding: utf-8 -*-
##############################################################################
#
# OrgFileExporter, a python module exporter for exporting WikidPad files to
# orgfiles.
# Copyright (c) 2012 Josep Mones Teixidor
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the <ORGANIZATION> nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
##############################################################################
"""
OrgFileExporter.py
https://github.com/jmones/wikidpad_orgfile_exporter
DESCRIPTION
WikidPad exporter to emacs org-mode files (http://orgmode.org).
FEATURES
This exporter lacks a lot of features. It's just a quick hack to export some data
from WikidPad. Feel free to improved. Current supported features:
* Exporting data to a unique file, each word in a node.
* It uses WikidPad parser classes to get WikidPad data.
* It uses PyOrgMode library to generate org files.
* It set ups links from wiki words in pages to actual nodes (inserting CUSTOM_ID properties).
* It processes bold and italics.
* It processes tables (only simple ones).
* It processes horizontal rules.
* It processes unordered and ordered lists.
However this features known to be missing:
* Does not support footnotes.
* Does not support insertion.
* Does not support roman lists.
* Does not support alpha lists.
* Does not support wikidpad anchors in text.
* Only strategy available to layout nodes is "one word, one node".
* Doesn't have a clever way to solve presence of headings in words.
REQUIREMENTS
* WikidPad version >= 2.2.
* PyOrgMode (included).
INSTALLATION
1. If user_extensions/ folder in WikidPad installation doesn't exist, create it as a sibling of extensions/
2. Copy OrgFileExporter.py to user_extensions/
3. Copy PyOrgMode.py to user_extensions/
USAGE
1. Select Extra/Export
2. Select "Org mode file" in "Export to:" dropdown.
3. Select destination file (it will create a single file).
4. Adjust all other settings as desired.
5. Press OK.
AUTHOR
Josep Mones Teixidor < jmones at gmail dot com >
"""
from pwiki.StringOps import *
from pwiki.Exporters import AbstractExporter
from pwiki.WikiPyparsing import SyntaxNode
import PyOrgMode
import string
import re
from copy import copy
WIKIDPAD_PLUGIN = (("Exporters", 1),)
LOG = False
def describeExportersV01(mainControl):
return (OrgFileExporter,)
class OrgFileExporter(AbstractExporter):
"""
Exports org mode files from WikidPad.
"""
def __init__(self, mainControl):
AbstractExporter.__init__(self, mainControl)
self.wordList = None
self.exportDest = None
self.currentContent = []
self.currentLine = ""
self.currentIndent = 2
self.currentWord = ""
self.niceTitles = {}
self.listItems = []
@staticmethod
def getExportTypes(mainControl, continuousExport=False):
"""
Return sequence of tuples with the description of export types provided
by this object. A tuple has the form (<exp. type>,
<human readable description>)
All exporters must provide this as a static method (which can be called
without constructing an object first.
mainControl -- PersonalWikiFrame object
continuousExport -- If True, only types with support for continuous export
are listed.
"""
if continuousExport:
# Continuous export not supported
return ()
return (
(u"org_mode", _(u'Org mode file')),
)
def getAddOptPanelsForTypes(self, guiparent, exportTypes):
"""
Construct all necessary GUI panels for additional options
for the types contained in exportTypes.
Returns sequence of tuples (<exp. type>, <panel for add. options or None>)
The panels should use guiparent as parent.
If the same panel is used for multiple export types the function can
and should include all export types for this panel even if some of
them weren't requested. Panel objects must not be shared by different
exporter classes.
"""
if not u"org_mode" in exportTypes:
return ()
return (
(u"org_mode", None),
)
def getExportDestinationWildcards(self, exportType):
"""
If an export type is intended to go to a file, this function
returns a (possibly empty) sequence of tuples
(wildcard description, wildcard filepattern).
If an export type goes to a directory, None is returned
"""
if exportType == u"org_mode":
return ((_(u"Org mode file (*.org)"), "*.org"),)
return None
def getAddOptVersion(self):
"""
Returns the version of the additional options information returned
by getAddOpt(). If the return value is -1, the version info can't
be stored between application sessions.
Otherwise, the addopt information can be stored between sessions
and can later handled back to the export method of the object
without previously showing the export dialog.
"""
return -1
def getAddOpt(self, addoptpanel):
"""
Reads additional options from panel addoptpanel.
If getAddOptVersion() > -1, the return value must be a sequence
of simple string and/or numeric objects. Otherwise, any object
can be returned (normally the addoptpanel itself)
"""
return (1,)
def setAddOpt(self, addOpt, addoptpanel):
"""
Shows content of addOpt in the addoptpanel (must not be None).
This function is only called if getAddOptVersion() != -1.
"""
pass
def flushLine(self, force=False):
if force or len(self.currentLine) > 0:
line = (" "*self.currentIndent) + self.currentLine + "\n"
self.currentContent.append(line.encode("utf-8"))
self.currentLine = ""
def shouldExport(self, wikiWord, wikiPage=None):
if not wikiPage:
try:
wikiPage = self.wikiDocument.getWikiPage(wikiWord)
except WikiWordNotFoundException:
return False
return strToBool(wikiPage.getAttributes().get("export", ("True",))[-1])
def getLinkForWikiWord(self, word, default = None):
relUnAlias = self.wikiDocument.getWikiPageNameForLinkTerm(word)
if relUnAlias is None:
return default
if not self.shouldExport(word):
return default
return relUnAlias
def processWikiWord(self, astNodeOrWord, fullContent=None):
if isinstance(astNodeOrWord, SyntaxNode):
wikiWord = astNodeOrWord.wikiWord
titleNode = astNodeOrWord.titleNode
else:
wikiWord = astNodeOrWord
titleNode = None
if titleNode == None:
title = self.niceTitles.get(wikiWord, None)
link = self.getLinkForWikiWord(wikiWord)
if link:
if titleNode is not None:
self.currentLine += u"[[#%s][" % link
self.processAst(fullContent, titleNode)
self.currentLine += u"]]"
else:
if title is None:
self.currentLine += u"[[#%s]]" % (link)
else:
self.currentLine += u"[[#%s][%s]]" % (link, title)
else:
if titleNode is not None:
self.processAst(fullContent, titleNode)
else:
if isinstance(astNodeOrWord, SyntaxNode):
self.currentLine += astNodeOrWord.getString()
else:
self.currentLine += astNodeOrWord
def processUrlLink(self, fullContent, astNode):
link = astNode.url
self.currentLine += u"[[%s][" % link
if astNode.titleNode is not None:
self.processAst(fullContent, astNode.titleNode)
else:
self.currentLine += astNode.coreNode.getString()
self.currentLine += "]]"
def processTable(self, content, astNode):
"""
Write out content of a table as HTML code.
astNode -- node of type "table"
"""
self.flushLine()
table = PyOrgMode.OrgTable.Element()
table.content = []
for row in astNode.iterFlatByName("tableRow"):
orgRow = []
for cell in row.iterFlatByName("tableCell"):
orgRow.append(cell.getString().encode("utf-8"))
table.content.append(orgRow)
self.currentContent.append(table)
def processAst(self, content, pageAst):
"""
Actual token to org-mode converter. May be called recursively.
"""
for node in pageAst.iterFlatNamed():
tname = node.name
# self.currentLine += "{" + tname + "}"
if tname is None:
continue
elif tname == "plainText":
if self.removePlainText:
# This it the text for the first title in a wikiword,
# we use it as a nice title
pass
else:
self.currentLine += node.getString()
elif tname == "lineBreak":
self.flushLine(True)
elif tname == "newParagraph":
self.flushLine()
self.flushLine(True)
elif tname == "whitespace":
self.currentLine += " "
elif tname == "indentedText":
self.flushLine()
self.currentIndent += 2
self.processAst(content, node)
elif tname == "orderedList":
self.flushLine()
self.processAst(content, node)
self.flushLine()
elif tname == "unorderedList":
self.flushLine()
self.listItems.append(0)
self.processAst(content, node)
self.listItems.pop()
self.flushLine()
elif tname == "romanList":
self.flushLine()
print "[ERROR: romanList is not implemented]"
self.processAst(content, node)
self.flushLine()
elif tname == "alphaList":
self.flushLine()
print "[ERROR: alphaList is not implemented]"
self.processAst(content, node)
self.flushLine()
elif tname == "bullet":
self.currentLine += "- ";
elif tname == "number":
self.listItems[-1] += 1
self.currentLine += "%d. " % self.listItems[-1];
elif tname == "roman":
print "[ERROR: roman is not implemented]"
elif tname == "alpha":
print "[ERROR: alpha is not implemented]"
elif tname == "italics":
self.currentLine += "/"
self.processAst(content, node)
self.currentLine += "/"
elif tname == "bold":
self.currentLine += "*"
self.processAst(content, node)
self.currentLine += "*"
elif tname == "htmlTag" or tname == "htmlEntity":
self.currentLine += node.getString()
elif tname == "heading":
# we ignore the heading, it doesn't fit very well in the
# exporting model we are using (every wikiword is a node)
self.flushLine()
# we use the first heading as a friendly title for the node
if self.itemsProcessed == 0:
self.removePlainText = True
self.processAst(content, node.contentNode)
self.removePlainText = False
else:
self.processAst(content, node.contentNode)
elif tname == "horizontalLine":
self.flushLine()
self.currentLine += "-----"
self.flushLine()
elif tname == "preBlock":
self.flushLine()
self.currentLine += "#+BEGIN_EXAMPLE"
self.flushLine()
for line in string.split(node.findFlatByName("preText").getString(), "\n"):
self.currentLine += line
self.flushLine()
self.currentLine += "#+END_EXAMPLE"
elif tname == "todoEntry":
# we should create nodes but it's difficult to fit in current "each wiki word is a node scheme"
self.flushLine()
self.currentLine += "TODO: %s%s" % (node.key, node.delimiter)
self.processAst(content, node.valueNode)
self.flushLine()
elif tname == "script":
pass # Hide scripts
elif tname == "noExport":
pass # Hide no export areas
elif tname == "anchorDef":
self.currentLine += u"[ERROR: We can't process anchors]"
elif tname == "wikiWord":
self.processWikiWord(node, content)
elif tname == "table":
self.processTable(content, node)
elif tname == "footnote":
self.flushLine()
self.currentLine += u"[ERROR: We can't process footnotes]"
self.flushLine()
elif tname == "urlLink":
self.processUrlLink(content, node)
elif tname == "stringEnd":
pass
else:
self.flushLine()
self.currentLine += u'[Unknown parser node with name "%s" found]' % tname
self.flushLine()
self.itemsProcessed += 1
# if we have a line to flush do it now
self.flushLine()
def updateNiceTitle(self, content, word, pageAst):
"""
This gets Nice title
"""
item = pageAst.iterFlatNamed().next()
if item.name == 'heading':
item = item.contentNode.iterFlatNamed().next()
if item.name == 'plainText':
self.niceTitles[word] = item.getString()
def export(self, wikiDocument, wordList, exportType, exportDest,
compatFilenames, addopt, progressHandler):
"""
Run export operation.
wikiDocument -- WikiDocument object
wordList -- Sequence of wiki words to export
exportType -- string tag to identify how to export
exportDest -- Path to destination directory or file to export to
compatFilenames -- Should the filenames be encoded to be lowest
level compatible
addopt -- additional options returned by getAddOpt()
"""
self.wikiDocument = wikiDocument
self.wordList = wordList
self.exportDest = exportDest
try:
org = PyOrgMode.OrgDataStructure()
# capture nice titles
for word in self.wordList:
wikiPage = self.wikiDocument.getWikiPage(word)
word = wikiPage.getWikiWord()
content = wikiPage.getLiveText()
basePageAst = wikiPage.getLivePageAst()
# set default setting
self.niceTitles[word] = word
self.updateNiceTitle(content, word, basePageAst)
for word in self.wordList:
wikiPage = self.wikiDocument.getWikiPage(word)
word = wikiPage.getWikiWord()
formatDetails = wikiPage.getFormatDetails()
content = wikiPage.getLiveText()
basePageAst = wikiPage.getLivePageAst()
self.currentContent = []
self.currentWord = word
self.currentLine = ""
self.itemsProcessed = 0
self.removePlainText = False
self.currentIndent = 2
self.listItems = []
self.processAst(content, basePageAst)
node = PyOrgMode.OrgNode.Element()
node.level = 1
node.heading = self.niceTitles[word].encode("utf-8")
drawer = PyOrgMode.OrgDrawer.Element("PROPERTIES")
customId = ":CUSTOM_ID: " + word
drawer.content.append(customId.encode("utf-8"))
node.content.append(drawer)
node.content.extend(self.currentContent)
org.root.append_clean(node)
org.save_to_file(self.exportDest)
except:
traceback.print_exc()