|
10 | 10 | import grp |
11 | 11 | except ImportError: |
12 | 12 | grp = None |
| 13 | +try: |
| 14 | + import docutils.core |
| 15 | + import docutils.writers.html4css1 |
| 16 | +except ImportError: |
| 17 | + pass |
13 | 18 | import errno |
14 | 19 | import urlparse |
15 | 20 | from tempfile import NamedTemporaryFile |
16 | | -from logging import getLogger |
17 | | -log = getLogger(__name__) |
| 21 | +import logging |
| 22 | +log = logging.getLogger(__name__) |
18 | 23 |
|
19 | 24 | BUFFER_SIZE = 4096 |
| 25 | +DEFAULT_ENCODING = os.environ.get('GALAXY_DEFAULT_ENCODING', 'utf-8') |
20 | 26 |
|
21 | 27 |
|
22 | 28 | def enum(**enums): |
@@ -130,6 +136,8 @@ def xml_text(root, name=None): |
130 | 136 | # asbool implementation pulled from PasteDeploy |
131 | 137 | truthy = frozenset(['true', 'yes', 'on', 'y', 't', '1']) |
132 | 138 | falsy = frozenset(['false', 'no', 'off', 'n', 'f', '0']) |
| 139 | + |
| 140 | + |
133 | 141 | def asbool(obj): |
134 | 142 | if isinstance(obj, basestring): |
135 | 143 | obj = obj.strip().lower() |
@@ -193,3 +201,29 @@ def mask_password_from_url( url ): |
193 | 201 | split._replace(netloc=split.netloc.replace("%s:%s" % (split.username, split.password), '%s:********' % split.username)) |
194 | 202 | url = urlparse.urlunsplit(split) |
195 | 203 | return url |
| 204 | + |
| 205 | + |
| 206 | +def unicodify( value, encoding=DEFAULT_ENCODING, error='replace', default=None ): |
| 207 | + """ |
| 208 | + Returns a unicode string or None |
| 209 | + """ |
| 210 | + |
| 211 | + if isinstance( value, unicode ): |
| 212 | + return value |
| 213 | + try: |
| 214 | + return unicode( str( value ), encoding, error ) |
| 215 | + except: |
| 216 | + return default |
| 217 | + |
| 218 | + |
| 219 | +def rst_to_html( s ): |
| 220 | + """Convert a blob of reStructuredText to HTML""" |
| 221 | + log = logging.getLogger( "docutils" ) |
| 222 | + |
| 223 | + class FakeStream( object ): |
| 224 | + def write( self, str ): |
| 225 | + if len( str ) > 0 and not str.isspace(): |
| 226 | + log.warn( str ) |
| 227 | + return unicodify( docutils.core.publish_string( s, |
| 228 | + writer=docutils.writers.html4css1.Writer(), |
| 229 | + settings_overrides={ "embed_stylesheet": False, "template": os.path.join(os.path.dirname(__file__), "docutils_template.txt"), "warning_stream": FakeStream() } ) ) |
0 commit comments