]> git.openstreetmap.org Git - osqa.git/blob - forum/markdownext/mdx_settingsparser.py
Allow for missing content types
[osqa.git] / forum / markdownext / mdx_settingsparser.py
1 from os import linesep
2 from csv import reader, QUOTE_NONE
3 import markdown
4 from markdown import Extension
5 from markdown.preprocessors import Preprocessor
6 import re
7
8 from forum import settings
9
10 class SettingsExtension(markdown.Extension):
11     def __init__(self, configs):
12         self.configs = {} # settings.REP_TO_VOTE_UP}
13         for key, value in configs:
14             self.config[key] = value
15
16         # self.extendMarkdown(markdown.Markdown()., config)
17
18     def reset(self):
19         pass
20
21     def extendMarkdown(self, md, md_globals):
22         md.registerExtension(self)
23         self.parser = md.parser
24         md.preprocessors.add('MinRep', SettingsPre(self), '_begin')
25
26 SETTING_RE = re.compile(r'\|[A-Z_]+\|')
27
28 def setting_rep_callback(m):
29     setting_name = m.group(0).strip('|')
30     if hasattr(settings, setting_name):
31         return unicode(getattr(settings, setting_name))
32     else:
33         return ''
34
35
36 class SettingsPre(Preprocessor):
37     def run(self, lines):
38         new_lines = []
39         for line in lines:
40             new_lines.append(SETTING_RE.sub(setting_rep_callback, line))
41
42         return new_lines
43
44
45 def makeExtension(configs=None) :
46     return SettingsExtension(configs=configs)