107 |
107 |
108 @adapter_config(name='rest', context=(str, IRequest), provides=IHTMLRenderer) |
108 @adapter_config(name='rest', context=(str, IRequest), provides=IHTMLRenderer) |
109 class ReStructuredTextRenderer(BaseHTMLRenderer): |
109 class ReStructuredTextRenderer(BaseHTMLRenderer): |
110 """reStructuredText HTML renderer |
110 """reStructuredText HTML renderer |
111 |
111 |
112 This renderer is using *docutils* to render HTML output. |
112 This renderer is using *docutils* to convert text to HTML output. |
113 """ |
113 """ |
114 |
114 |
115 def render(self, **kwargs): |
115 def render(self, **kwargs): |
116 """Render reStructuredText to HTML""" |
116 """Render reStructuredText to HTML""" |
117 overrides = { |
117 overrides = { |
130 |
130 |
131 @adapter_config(name='markdown', context=(str, IRequest), provides=IHTMLRenderer) |
131 @adapter_config(name='markdown', context=(str, IRequest), provides=IHTMLRenderer) |
132 class MarkdownTextRenderer(BaseHTMLRenderer): |
132 class MarkdownTextRenderer(BaseHTMLRenderer): |
133 """Markdown HTML renderer |
133 """Markdown HTML renderer |
134 |
134 |
135 This renderer is using *Markdown* HTML output. |
135 This renderer is converting *Markdown* formatted text to HTML. |
136 """ |
136 """ |
137 |
137 |
138 def render(self, **kwargs): |
138 def render(self, **kwargs): |
139 """Renderer Markdown code to HTML""" |
139 """Render Markdown code to HTML""" |
140 return markdown(self.context) |
140 return markdown(self.context) |
141 |
141 |
142 |
142 |
143 def text_to_html(text, renderer='text'): |
143 def text_to_html(text, renderer='text'): |
144 """Convert text to HTML using the given renderer |
144 """Convert text to HTML using the given renderer |
145 |
145 |
146 Renderer name can be any registered HTML renderer adapter |
146 Renderer name can be any registered HTML renderer adapter. |
|
147 |
|
148 You can provide several renderers by giving their names separated by semicolon; renderers will then |
|
149 act as in a pipe, each renderer transforming output of the previous one. |
147 """ |
150 """ |
148 request = check_request() |
151 request = check_request() |
149 registry = request.registry |
152 registry = request.registry |
150 for renderer_name in renderer.split(';'): |
153 for renderer_name in renderer.split(';'): |
151 renderer = registry.queryMultiAdapter((text, request), IHTMLRenderer, name=renderer_name) |
154 renderer = registry.queryMultiAdapter((text, request), IHTMLRenderer, name=renderer_name) |
163 |
166 |
164 If first *context* argument of the renderer is an object for which an :py:class:`IHTMLRenderer` |
167 If first *context* argument of the renderer is an object for which an :py:class:`IHTMLRenderer` |
165 adapter can be found, this adapter is used to render the context to HTML; if *context* is a string, |
168 adapter can be found, this adapter is used to render the context to HTML; if *context* is a string, |
166 it is converted to HTML using the renderer defined as second parameter; otherwise, context is just |
169 it is converted to HTML using the renderer defined as second parameter; otherwise, context is just |
167 converted to string using the :py:func:`str` function. |
170 converted to string using the :py:func:`str` function. |
|
171 |
|
172 You can provide several renderers by giving their names separated by semicolon; renderers will then |
|
173 act as in a pipe, each renderer transforming output of the previous one. |
168 """ |
174 """ |
169 |
175 |
170 def render(self, context=empty_marker, renderer='text'): |
176 def render(self, context=empty_marker, renderer='text'): |
171 if context is empty_marker: |
177 if context is empty_marker: |
172 context = self.context |
178 context = self.context |