Edycja paragrafów.
[redakcja.git] / apps / wiki / models.py
1 import re
2 import vstorage
3 from vstorage import DocumentNotFound
4 from wiki import settings
5
6
7 class DocumentStorage(object):
8     def __init__(self, path):
9         self.vstorage = vstorage.VersionedStorage(path)
10     
11     def get(self, name, revision=None):
12         if revision is None:
13             text = self.vstorage.page_text(name)
14         else:
15             text = self.vstorage.revision_text(name, revision)
16         return Document(self, name=name, text=text)
17     
18     def put(self, document, author, comment, parent):
19         self.vstorage.save_text(document.name, document.text, author, comment, parent)
20
21     def delete(self, name, author, comment):
22         self.vstorage.delete_page(name, author, comment)
23
24     def all(self):
25         return list(self.vstorage.all_pages())
26
27     def _info(self, name):
28         return self.vstorage.page_meta(name)
29
30
31 class Document(object):
32     META_REGEX = re.compile(r'\s*<!--\s(.*?)\s-->', re.DOTALL | re.MULTILINE)
33     
34     def __init__(self, storage, **kwargs):
35         self.storage = storage
36         for attr, value in kwargs.iteritems():
37             setattr(self, attr, value)
38             
39     def revision(self):
40         try:
41             return self.storage._info(self.name)[0]
42         except DocumentNotFound:
43             return -1
44
45     def plain_text(self):
46         return re.sub(self.META_REGEX, '', self.text, 1)
47     
48     def meta(self):
49         result = {}
50         
51         m = re.match(self.META_REGEX, self.text)
52         if m:
53             for line in m.group(1).split('\n'):
54                 try:
55                     k, v = line.split(':', 1)
56                     result[k.strip()] = v.strip()
57                 except ValueError:
58                     continue
59         
60         return result
61
62
63 storage = DocumentStorage(settings.REPOSITORY_PATH)
64