Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
db Add isort as a companion to black (#5132) Nov 28, 2018 Reformat the code with black (#3367) May 24, 2018 Update black from 18.5b0 to 18.5b1 (#4096) May 30, 2018

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utils for rendering and updating package descriptions (READMEs)."""

import cgi

import pkg_resources
import readme_renderer.markdown
import readme_renderer.rst
import readme_renderer.txt

    None: readme_renderer.rst,  # Default if description_content_type is None
    "": readme_renderer.rst,  # Default if description_content_type is None
    "text/plain": readme_renderer.txt,
    "text/x-rst": readme_renderer.rst,
    "text/markdown": readme_renderer.markdown,

def render(value, content_type=None, use_fallback=True):
    if value is None:
        return value

    content_type, parameters = cgi.parse_header(content_type or "")

    # Get the appropriate renderer
    renderer = _RENDERERS.get(content_type, readme_renderer.txt)

    # Actually render the given value, this will not only render the value, but
    # also ensure that it's had any disallowed markup removed.
    rendered = renderer.render(value, **parameters)

    # If the content was not rendered, we'll render as plaintext instead. The
    # reason it's necessary to do this instead of just accepting plaintext is
    # that readme_renderer will deal with sanitizing the content.
    # Skip the fallback option when validating that rendered output is ok.
    if use_fallback and rendered is None:
        rendered = readme_renderer.txt.render(value)

    return rendered

def renderer_version():
    return pkg_resources.get_distribution("readme-renderer").version