summaryrefslogtreecommitdiff
path: root/wikitrans/wikidump.py
diff options
context:
space:
mode:
Diffstat (limited to 'wikitrans/wikidump.py')
-rw-r--r--wikitrans/wikidump.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/wikitrans/wikidump.py b/wikitrans/wikidump.py
new file mode 100644
index 0000000..d5f651c
--- /dev/null
+++ b/wikitrans/wikidump.py
@@ -0,0 +1,77 @@
1# Wiki "dump" format. -*- coding: utf-8 -*-
2# Copyright (C) 2015-2018 Sergey Poznyakoff
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3, or (at your option)
7# any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17"""
18Print Wiki parse tree as JSON.
19
20Classes:
21
22DumpWikiMarkup
23
24"""
25
26from __future__ import print_function
27from wikitrans.wikitoken import *
28import json
29from wikitrans.wikimarkup import WikiMarkup
30
31class DumpReferences(object):
32 idx = 0
33 def __len__(self):
34 return self.idx + 1
35 def append(self, obj):
36 self.idx += 1
37
38class DumpWikiMarkup(WikiMarkup):
39 """Produce a JSON dump of the Wiki markup parse tree.
40
41 Usage:
42
43 x = DumpWikiMarkup(file="input.wiki")
44 # Parse the input:
45 x.parse()
46 # Print a JSON dump of the parse tree
47 print(str(x))
48
49 """
50
51 indent = None
52 references = DumpReferences()
53 def __init__(self, **kwarg):
54 """Create a DumpWikiMarkup object.
55
56 Arguments:
57
58 filename=FILE
59 Read Wiki material from the file named FILE.
60 file=FD
61 Read Wiki material from file object FD.
62 text=STRING
63 Read Wiki material from STRING.
64 indent=N
65 Basic indent offset for JSON objects.
66 """
67
68 n = kwarg.pop('indent', None)
69 if n != None:
70 self.indent = int(n)
71 super(DumpWikiMarkup,self).__init__(self, **kwarg)
72 def __str__(self):
73 return json.dumps(self.tree,
74 cls=WikiNodeEncoder,
75 indent=self.indent,
76 separators=(',',': '),
77 sort_keys=True)

Return to:

Send suggestions and report system problems to the System administrator.