1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
9 from vstorage import DocumentNotFound
10 from wiki import settings, constants
11 from django.utils.translation import ugettext_lazy as _
13 from django.http import Http404
16 logger = logging.getLogger("fnp.wiki")
19 # _PCHARS_DICT = dict(zip((ord(x) for x in u"ĄĆĘŁŃÓŚŻŹąćęłńóśżź "), u"ACELNOSZZacelnoszz_"))
20 _PCHARS_DICT = dict(zip((ord(x) for x in u" "), u"_"))
22 # I know this is barbaric, but I didn't find a better solution ;(
24 parts = name.translate(_PCHARS_DICT).split('__')
27 def join_name(*parts, **kwargs):
28 name = u'__'.join(p.translate(_PCHARS_DICT) for p in parts)
29 logger.info("JOIN %r -> %r", parts, name)
32 def normalize_name(name):
34 >>> normalize_name("gąska".decode('utf-8'))
37 return name.translate(_PCHARS_DICT).lower()
39 STAGE_TAGS_RE = re.compile(r'^#stage-finished: (.*)$', re.MULTILINE)
42 class DocumentStorage(object):
43 def __init__(self, path):
44 self.vstorage = vstorage.VersionedStorage(path)
46 def get(self, name, revision=None):
47 text, rev = self.vstorage.page_text(name, revision)
48 return Document(self, name=name, text=text, revision=rev)
50 def get_by_tag(self, name, tag):
51 text, rev = self.vstorage.page_text_by_tag(name, tag)
52 return Document(self, name=name, text=text, revision=rev)
54 def revert(self, name, revision):
55 text, rev = self.vstorage.revert(name, revision)
56 return Document(self, name=name, text=text, revision=rev)
58 def get_or_404(self, *args, **kwargs):
60 return self.get(*args, **kwargs)
61 except DocumentNotFound:
64 def put(self, document, author, comment, parent=None):
65 self.vstorage.save_text(
74 def create_document(self, text, name):
75 title = u', '.join(p.title for p in split_name(name))
80 document = Document(self, name=name, text=text, title=title)
81 return self.put(document, u"<wiki>", u"Document created.")
83 def delete(self, name, author, comment):
84 self.vstorage.delete_page(name, author, comment)
87 return list(self.vstorage.all_pages())
89 def history(self, title):
90 def stage_desc(match):
91 stage = match.group(1)
92 return _("Finished stage: %s") % constants.DOCUMENT_STAGES_DICT[stage]
94 for changeset in self.vstorage.page_history(title):
95 changeset['description'] = STAGE_TAGS_RE.sub(stage_desc, changeset['description'])
100 class Document(object):
101 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
103 def __init__(self, storage, **kwargs):
104 self.storage = storage
105 for attr, value in kwargs.iteritems():
106 setattr(self, attr, value)
108 def add_tag(self, tag, revision, author):
109 """ Add document specific tag """
110 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
111 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
114 def plain_text(self):
115 return re.sub(self.META_REGEX, '', self.text, 1)
120 m = re.match(self.META_REGEX, self.text)
122 for line in m.group(1).split('\n'):
124 k, v = line.split(':', 1)
125 result[k.strip()] = v.strip()
129 gallery = result.get('gallery', self.name.replace(' ', '_'))
131 if gallery.startswith('/'):
132 gallery = os.path.basename(gallery)
134 result['gallery'] = gallery
138 return self.storage.vstorage.page_meta(self.name, self.revision)
141 return DocumentStorage(settings.REPOSITORY_PATH)