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 get_or_404(self, *args, **kwargs):
56 return self.get(*args, **kwargs)
57 except DocumentNotFound:
60 def put(self, document, author, comment, parent=None):
61 self.vstorage.save_text(
70 def create_document(self, text, name):
71 title = u', '.join(p.title for p in split_name(name))
76 document = Document(self, name=name, text=text, title=title)
77 return self.put(document, u"<wiki>", u"Document created.")
79 def delete(self, name, author, comment):
80 self.vstorage.delete_page(name, author, comment)
83 return list(self.vstorage.all_pages())
85 def history(self, title):
86 def stage_desc(match):
87 stage = match.group(1)
88 return _("Finished stage: %s") % constants.DOCUMENT_STAGES_DICT[stage]
90 for changeset in self.vstorage.page_history(title):
91 changeset['description'] = STAGE_TAGS_RE.sub(stage_desc, changeset['description'])
96 class Document(object):
97 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
99 def __init__(self, storage, **kwargs):
100 self.storage = storage
101 for attr, value in kwargs.iteritems():
102 setattr(self, attr, value)
104 def add_tag(self, tag, revision, author):
105 """ Add document specific tag """
106 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
107 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
110 def plain_text(self):
111 return re.sub(self.META_REGEX, '', self.text, 1)
116 m = re.match(self.META_REGEX, self.text)
118 for line in m.group(1).split('\n'):
120 k, v = line.split(':', 1)
121 result[k.strip()] = v.strip()
125 gallery = result.get('gallery', self.name.replace(' ', '_'))
127 if gallery.startswith('/'):
128 gallery = os.path.basename(gallery)
130 result['gallery'] = gallery
134 return self.storage.vstorage.page_meta(self.name, self.revision)
137 return DocumentStorage(settings.REPOSITORY_PATH)