comparison rhodecode/lib/markup_renderer.py @ 1818:cf51bbfb120e beta

auto white-space removal
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 29 Dec 2011 07:35:51 +0200
parents 025f3333c769
children 89efedac4e6c
comparison
equal deleted inserted replaced
1817:523b1011a625 1818:cf51bbfb120e
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 """ 2 """
3 rhodecode.lib.markup_renderer 3 rhodecode.lib.markup_renderer
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6
7 Renderer for markup languages with ability to parse using rst or markdown 7 Renderer for markup languages with ability to parse using rst or markdown
8 8
9 :created_on: Oct 27, 2011 9 :created_on: Oct 27, 2011
10 :author: marcink 10 :author: marcink
11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> 11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
12 :license: GPLv3, see COPYING for more details. 12 :license: GPLv3, see COPYING for more details.
13 """ 13 """
31 31
32 log = logging.getLogger(__name__) 32 log = logging.getLogger(__name__)
33 33
34 class MarkupRenderer(object): 34 class MarkupRenderer(object):
35 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw'] 35 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
36 36
37 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown',re.IGNORECASE) 37 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown',re.IGNORECASE)
38 RST_PAT = re.compile(r're?st',re.IGNORECASE) 38 RST_PAT = re.compile(r're?st',re.IGNORECASE)
39 PLAIN_PAT = re.compile(r'readme',re.IGNORECASE) 39 PLAIN_PAT = re.compile(r'readme',re.IGNORECASE)
40 40
41 def __detect_renderer(self, source, filename=None): 41 def __detect_renderer(self, source, filename=None):
42 """ 42 """
43 runs detection of what renderer should be used for generating html 43 runs detection of what renderer should be used for generating html
44 from a markup language 44 from a markup language
45 45
46 filename can be also explicitly a renderer name 46 filename can be also explicitly a renderer name
47 47
48 :param source: 48 :param source:
49 :param filename: 49 :param filename:
50 """ 50 """
51 51
52 if MarkupRenderer.MARKDOWN_PAT.findall(filename): 52 if MarkupRenderer.MARKDOWN_PAT.findall(filename):
64 def render(self, source, filename=None): 64 def render(self, source, filename=None):
65 """ 65 """
66 Renders a given filename using detected renderer 66 Renders a given filename using detected renderer
67 it detects renderers based on file extension or mimetype. 67 it detects renderers based on file extension or mimetype.
68 At last it will just do a simple html replacing new lines with <br/> 68 At last it will just do a simple html replacing new lines with <br/>
69 69
70 :param file_name: 70 :param file_name:
71 :param source: 71 :param source:
72 """ 72 """
73 73
74 renderer = self.__detect_renderer(source, filename) 74 renderer = self.__detect_renderer(source, filename)
128 return cls.plain(source) 128 return cls.plain(source)
129 129
130 @classmethod 130 @classmethod
131 def rst_with_mentions(cls, source): 131 def rst_with_mentions(cls, source):
132 mention_pat = re.compile(r'(?:^@|\s@)(\w+)') 132 mention_pat = re.compile(r'(?:^@|\s@)(\w+)')
133 133
134 def wrapp(match_obj): 134 def wrapp(match_obj):
135 uname = match_obj.groups()[0] 135 uname = match_obj.groups()[0]
136 return ' **@%(uname)s** ' % {'uname':uname} 136 return ' **@%(uname)s** ' % {'uname':uname}
137 mention_hl = mention_pat.sub(wrapp, source).strip() 137 mention_hl = mention_pat.sub(wrapp, source).strip()
138 return cls.rst(mention_hl) 138 return cls.rst(mention_hl)
139