1
|
"""This script exports the wiki pages from Redmine.
|
2
|
|
3
|
All wiki pages on Redmine (https://cocomud.plan.io) are saved in the
|
4
|
'doc' directory.
|
5
|
|
6
|
Requirements:
|
7
|
This script needs 'python-redmine', which you can obtain with
|
8
|
pip install python-redmine
|
9
|
This script also needs BeautifulSoup:
|
10
|
pip install BeautifulSoup
|
11
|
|
12
|
"""
|
13
|
|
14
|
import argparse
|
15
|
import os
|
16
|
import sys
|
17
|
import urllib2
|
18
|
|
19
|
from BeautifulSoup import BeautifulSoup
|
20
|
from redmine import Redmine
|
21
|
|
22
|
|
23
|
parser = argparse.ArgumentParser(
|
24
|
description="export wiki pages in various formats")
|
25
|
parser.add_argument("lang", help="the language code (en, fr, es...)",
|
26
|
nargs='?', choices=["en", "fr"], default="en")
|
27
|
group = parser.add_mutually_exclusive_group()
|
28
|
group.add_argument("-t", "--txt", help="export in txt format",
|
29
|
action="store_true")
|
30
|
group.add_argument("-l", "--html", help="export in html format",
|
31
|
action="store_true")
|
32
|
args = parser.parse_args()
|
33
|
|
34
|
|
35
|
lang = args.lang
|
36
|
if args.txt:
|
37
|
format = "txt"
|
38
|
elif args.html:
|
39
|
format = "html"
|
40
|
else:
|
41
|
print "You must specify the format in which to export."
|
42
|
sys.exit(1)
|
43
|
|
44
|
|
45
|
redmine = Redmine("https://cocomud.plan.io")
|
46
|
|
47
|
|
48
|
|
49
|
|
50
|
|
51
|
|
52
|
if lang == "en":
|
53
|
project_id = "cocomud-client"
|
54
|
else:
|
55
|
project_id = lang
|
56
|
|
57
|
|
58
|
if format == "txt":
|
59
|
doc = "../doctext/{lang}".format(lang=lang)
|
60
|
else:
|
61
|
doc = "../doc/{lang}".format(lang=lang)
|
62
|
|
63
|
pages = redmine.wiki_page.filter(project_id=project_id)
|
64
|
for page in pages:
|
65
|
|
66
|
url = "https://cocomud.plan.io/projects/{id}/wiki/{title}.html".format(
|
67
|
id=project_id, title=page.title)
|
68
|
if format == "txt":
|
69
|
exported = page.text
|
70
|
elif format == "html":
|
71
|
response = urllib2.urlopen(url)
|
72
|
content = response.read()
|
73
|
soup = BeautifulSoup(content)
|
74
|
|
75
|
|
76
|
for link in soup.findAll("a"):
|
77
|
try:
|
78
|
href = link["href"]
|
79
|
except KeyError:
|
80
|
continue
|
81
|
|
82
|
if href.startswith("/projects/{}/wiki/".format(project_id)):
|
83
|
link["href"] = href[16 + len(project_id):] + ".html"
|
84
|
elif href.startswith("/"):
|
85
|
link["href"] = "https://cocomud.plan.io" + href
|
86
|
|
87
|
exported = str(soup)
|
88
|
else:
|
89
|
raise ValueError("unknown format {}".format(format))
|
90
|
|
91
|
|
92
|
path = os.path.join(doc, page.title + "." + format)
|
93
|
|
94
|
|
95
|
version = None
|
96
|
if os.path.exists(path):
|
97
|
file = open(path, "r")
|
98
|
content = file.read()
|
99
|
file.close()
|
100
|
lines = content.splitlines()
|
101
|
if lines[0].isdigit():
|
102
|
version = int(lines[0])
|
103
|
|
104
|
print "Writing", page.title, "in", path, "version", version
|
105
|
exported = exported.decode("utf-8").encode("latin-1").replace("\r", "")
|
106
|
file = open(path, "w")
|
107
|
if version is not None:
|
108
|
file.write(str(version) + "\n")
|
109
|
file.write(exported)
|
110
|
file.close()
|
111
|
|
112
|
|
113
|
if format == "html":
|
114
|
print "Downloading the complete HTML export."
|
115
|
url = "https://cocomud.plan.io/projects/{id}/wiki/export.html".format(
|
116
|
id=project_id)
|
117
|
response = urllib2.urlopen(url)
|
118
|
content = response.read()
|
119
|
path = os.path.join(doc, "index.html")
|
120
|
file = open(path, "w")
|
121
|
file.write(content)
|
122
|
file.close()
|