summaryrefslogtreecommitdiff
path: root/wiki_upload_test.py
blob: 2c256b4f5d911fafd39aa371832a5977c89e22e7 (plain)
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
def get_wiki(path):                                                             
    import imp, os.path                                                         
    return imp.load_module('wiki', open(os.path.expanduser(path)), path,        
            [s for s in imp.get_suffixes() if s[0] == '.py'][0])                
                                                                                

wiki = get_wiki('~/bin/viwiki')                                             


import re

try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO


class RawWikiWrapper(wiki.WikiAPI):

    def __init__(self, profile):
        super(RawWikiWrapper, self).__init__(*wiki.get_credentials(profile))

    def list_pages_by_prefix(self, prefix):
        return self("getAllPagesEx", { "prefix": prefix })

    def list_attachments(self, page):
        return self("listAttachments", page)

    def get_attachment(self, page, attachment):
        return self("getAttachment", page, attachment)

    def get_page(self, page):
        return self("getPage", page)

    def get_page_info(self, page):
        return self("getPageInfo", page)

    def get_processing_instruction(self, page, pi):
        return self("getProcessingInstruction", page, pi)


class WikiWrapper(RawWikiWrapper):

    def __init__(self, *args):
        super(WikiWrapper, self).__init__(*args)
        self._api = super(WikiWrapper, self)

    def list_pages_by_prefix(self, prefix):
        return [
            WikiPage(name, self._api)
            for name in super(WikiWrapper, self).list_pages_by_prefix(prefix)
        ]

    def get_page(self, page):
        return WikiPage(page, self._api)


class WikiPage(object):

    def __init__(self, name, api, contents=None):
        self.name = name
        self._api = api
        self._contents = contents
        self._meta = None
        self._pis = None

    def __repr__(self):
        return "{}({!r}, {!r})".format(
            self.__class__.__name__, self.name, self._api)

    def attachments(self):
        return [
            WikiAttachment(self.name, filename, self)
            for filename in self._api.list_attachments(self.name)
        ]

    @staticmethod
    def _parse_pi(line):
        line = line[1:].strip()
        type, args = line.split(" ", 1)

        if type == "pragma":
            return type, args
        elif type == "#":
            return None
        else:
            return type, args.split(" ")

    def _parse(self, contents):
        buffer = StringIO()
        have_body = False
        pis = {}

        for line in contents.split("\n"):
            if not have_body and line.startswith("#"):
                res = self._parse_pi(line)
                if res:
                    pis[res[0]] = res[1]
            elif not have_body and not line.startswith("#"):
                self.have_body = True
                buffer.write(u"{}\n".format(line))
            else:
                buffer.write(u"{}\n".format(line))

        return buffer.getvalue(), pis

    @property
    def contents(self):
        if not self._contents:
            self._contents , self._pis = self._parse(
                self._api.get_page(self.name))
        return self._contents

    @property
    def meta(self):
        if not self._meta:
            self._meta = self._api.get_page_info(self.name)
        return self._meta

    @property
    def last_modified(self):
        return self.meta["lastModified"]

    @property
    def version(self):
        return self.meta["version"]

    @property
    def author(self):
        return self.meta["author"].split(":")[-1]

    @property
    def format(self):
        return self._pis.get("format", ["rst"])[0]


class WikiAttachment(object):

    def __init__(self, page, filename, api):
        self.page = page
        self.filename = filename
        self._api = api

    def __repr__(self):
        return "{}({!r}, {!r}, {!r})".format(
            self.__class__.__name__, self.page, self.filename, self._api)

    def get_contents(self):
        return self._api.get_attachment(self.page, self.filename)


class PageTree(object):

    def __init__(self, root_path):
        self.root_path = root_path


class PageNode(object):

    def __init__(self, page=None):
        self.page = page
        self.children = []

    def file_name(self, root_path):
        return "/".join([
            re.sub("(.)([A-Z]+)", r"\1-\2", p).lower()
            for p in self.page.name[len(root_path):].split("/")
        ])

# Metadata:
# Site Title
# Footer Link (n)
# Date
# Author
# Tag
# Template (filename)
# Renderer (Page, Blog)

# Page tree:
# Keep metadata attributes from parents
# Add robots (/robots.txt)
# Add sitemap (/sitemap.xml) (https://www.sitemaps.org/protocol.html)

# Site:
# Load template
# Render page
# Render text-only page
# Set copyright date

# Home Page:
# Extract bottom site links
# Extract page header
# Extract site title

# All pages:
# Handle attachments
# Embed images
# Rewrite links
# Extract title
# Extract author
# Extract date
# Extract tags
# Extract lat updated date for footer

# Blog:
# Full post list page (/blog)
# By date page (/archive)
# Tags page (/tags)
# Atom feed (/feed, /atom)
# Rss feed (/rss)


if __name__ == "__main__":
    api = WikiWrapper("crute")

    #print(api.get_page("MikeCruteWebsite"))
    #print(api.list_pages_by_prefix("MikeCruteWebsite/"))
    #print(api.list_attachments("MikeCruteWebsite/Talks/ClePyAST"))
    #print(api.get_attachment("MikeCruteWebsite/Talks/ClePyAST", "clepy-python-ast.pdf"))

    page = api.get_page("MikeCruteWebsite/Talks/ClepyAst")

    import pdb; pdb.set_trace(); print("")