This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_export.py
189 lines (152 loc) · 5.96 KB
/
space_export.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
"""
This is intended to work on a Confluence Space export, converting the contents into Markdown files.
Format and title conversion remains a problem.
"""
import xml.etree.ElementTree
from dataclasses import dataclass
import confluence_object_defs as cod
import urllib.parse
enc = urllib.parse.quote_plus
import yaml
import pypandoc
# The path to the unzipped Confluence space export
# This should probably be a path object instead or something
export_dir = "ARC_Export"
# Directory to dump all the output files into
# Similarly, should probably use path objects
dump_dir = "dump_dir"
def main():
space = parse_space(export_dir + "/entities.xml")
page_list_out = open("page_list.txt", 'w')
for page in space["Page"]:
#print(space["Page"][page].LowerTitle)
#print(enc(space["Page"][page].Title))
page_title = space["Page"][page].Title
page_title_safe = safe_title(page_title)
page_list_out.write(f"{page_title}\n")
page_list_out.write(f"{page_title_safe}\n")
p = space["Page"][page]
with open(dump_dir + "/" + p.Id + ".yaml", 'w') as one_output_page_file:
one_output_page_file.write(dump_page(space,p))
if (space["Page"][page].ParentId == None) and (space["Page"][page].OriginalVersionId == None):
print(f"Found root page: id={p.Id} title={p.Title}")
# Debug: if you just want to dump the root page to see
# print(f"BodyContents: {p.BodyContentsIds}")
# print(dump_page(space,p))
def dump_page(space, page):
# Might replace this with a Mako template or something
header = yaml.dump(page)
page_content = space["BodyContent"][page.BodyContentsIds[0]].Body
if page_content == None:
page_content = ""
# Having some problems with passing nested <[[CDATA blocks through this at the moment >:/
#page_content = page_to_markdown(page_content)
return header + "---\n" + page_content
def page_to_markdown(content):
import wiki2storage as w
view_format = w.s2v(content)
md = pypandoc.convert_text(view_format, 'gfm', format='html')
return md
def parse_space(entity_file = "entities.xml"):
space_dump_tree = xml.etree.ElementTree.ElementTree(file = entity_file)
root = space_dump_tree.getroot()
logf = open("log", 'a')
objects = dict()
failures = dict()
for child in root:
# These classes weren't in my existing list or didn't parse the same way as the others.
if child.attrib["class"] == "BucketPropertySetItem":
# These have an awkward composite ID and I don't think we have any use for them anyway
continue
if child.attrib["class"] == "Notification":
# I don't see us doing anything useful with these
continue
if child.attrib["class"] == "LikeEntity":
# I don't know what these are and I am choosing to ignore them (probably "likes" on pages)
continue
if child.attrib["class"] == "CustomContentEntityObject":
# I don't know what these are and I should probably find out
# I think they're usually those special comments that go in a place off to the side instead of down at the bottom.
# Maybe it's an extensible object used for things Confluence extensions need to store
continue
if child.attrib["class"] not in objects:
objects[child.attrib["class"]] = dict()
fields = get_fields(child)
try:
objects[child.attrib["class"]][get_id(child)] = cod.__dict__[child.attrib["class"]](**fields)
except TypeError as e:
# These TypeErrors occur when a field we didn't know was optional turns out to be optional.
# If they come up, it usually means you just have to add a default field value in the object defs
# and re-run. (I've used None as the default value in those cases.)
logf.write(str(e) + "\n")
if child.attrib["class"] not in failures:
failures[child.attrib["class"]] = 1
else:
failures[child.attrib["class"]] += 1
for className in objects.keys():
print(f"Got {className}: {len(objects[className])}")
for className in failures.keys():
print(f"Failed {className}: {failures[className]}")
return objects
def get_id(obj):
"""
Extract just the ID field of a Confluence object.
"""
for prop in obj:
if prop.tag == "id":
return prop.text
raise KeyError
def get_fields(obj):
"""
Gets all the fields of a Confluence exported object, assembling in a dictionary.
"""
props = dict()
for prop in obj:
if prop.tag == "id":
props["Id"] = prop.text
if prop.tag == "property":
if prop.find("id") != None:
props[cap_first(prop.attrib["name"])+"Id"] = prop.find("id").text
else:
props[cap_first(prop.attrib["name"])] = prop.text
if prop.tag == "collection":
l = list()
for element in prop:
l.append(element.find("id").text)
props[cap_first(prop.attrib["name"])+"Ids"] = l
return props
def cap_first(s):
"""
Capitalises just the first letter of a string, since the usual
str.capitalize method lowercases the entire rest of the string as well.
"""
return s[0].upper() + s[1:]
# Yes, yes, this is a horrible textual crime.
def safe_title(t):
ratio_character = "∶"
fullwidth_solidus = "/"
fullwidth_colon = ":"
t = t.replace(":", fullwidth_colon)
t = t.replace("/", fullwidth_solidus)
return t
## Known classes of object
# Attachment
# BodyContent
# BucketPropertySetItem
# Comment
# ConfluenceBandanaRecord
# ConfluenceUserImpl
# ContentProperty
# CustomContentEntityObject
# Label
# Labelling
# LikeEntity
# Notification
# OutgoingLink
# Page
# Space
# SpaceDescription
# SpacePermission
# User2ContentRelationEntity
if __name__ == "__main__":
main()