# Path: fabfile/helpers.py
# def true(value):
#     '''Since fabric task inputs are always text, we need a way to convert it
#     to a boolean.
#     '''
#     if isinstance(value, bool):
#         return value
#     elif isinstance(value, basestring):
#         return value.lower()[0] in ['y', 't', '1']
# 
#     # Use the default representation
#     return bool(value)
# 
# def ex(command, cwd=None, shell=None, raise_on_nonzero=True):
#     """Execute a command and return the output.  This will raise an Exception if
#     the return code is non-zero.
# 
#     :param str/list command: Either a string or a list of arguments.
#     :param bool shell: If None, "shell" will be determined by the type of
#         `command` (str -> shell=True; list -> shell=False)
#     :param bool raise_on_nonzero: Raise an exception if the return code of the
#         process is not zero.
#     :returns: tuple(text, returncode)
#     """
#     shell = shell if (shell is not None) else (type(command) is not list)
# 
#     p = subprocess.Popen(
#         command, shell=shell, cwd=cwd, stderr=subprocess.STDOUT,
#         stdout=subprocess.PIPE)
# 
#     output, _ = p.communicate()
# 
#     if p.returncode and raise_on_nonzero:
#         raise Exception("command failed: %s" % output)
# 
#     return (output, p.returncode)
# 
# Path: fabfile/constants.py
# RELEASE_NOTES_FILE = os.path.join(LIB_DIR, 'release-notes.md')
# 
# Path: fabfile/ver.py
# def get_version():
#     '''Gets the current version'''
#     text = open(VER_FILE).read()
#     match = RE_VERSION.search(text)
# 
#     if not match:
#         raise Exception("Could not get version")
# 
#     return match.group('version')


import os
import re
from markdown import markdown
from fabric.api import env, task
from fabric.colors import red
from .helpers import true, ex
from .constants import RELEASE_NOTES_FILE
from .ver import get_version


See Fabric documentation for more info: http://docs.fabfile.org/en/1.10/index.html
"""

# Imports #####################################################################
from __future__ import print_function

# Internal

# Metadata ####################################################################
__author__ = "Timothy McFadden"
__creationDate__ = "07-JUL-2017"


# Fabric environment setup ####################################################
env.colorize_errors = True
###############################################################################


def _get(version=None, path=RELEASE_NOTES_FILE, html='n', display='n'):
    '''Return only the release notes for the specified version'''
    version = version or get_version()
    text = open(path).read()

    match = re.search('^(?P<notes># v?{0}.*?)^\*+$\n^$'.format(version), text, re.MULTILINE | re.DOTALL)
    if match:
        notes = match.group('notes').strip()
        if true(html):
            notes = markdown(notes, extensions=['markdown.extensions.fenced_code'])


# Path: fabfile/helpers.py
# def true(value):
#     '''Since fabric task inputs are always text, we need a way to convert it
#     to a boolean.
#     '''
#     if isinstance(value, bool):
#         return value
#     elif isinstance(value, basestring):
#         return value.lower()[0] in ['y', 't', '1']
# 
#     # Use the default representation
#     return bool(value)
# 
# def ex(command, cwd=None, shell=None, raise_on_nonzero=True):
#     """Execute a command and return the output.  This will raise an Exception if
#     the return code is non-zero.
# 
#     :param str/list command: Either a string or a list of arguments.
#     :param bool shell: If None, "shell" will be determined by the type of
#         `command` (str -> shell=True; list -> shell=False)
#     :param bool raise_on_nonzero: Raise an exception if the return code of the
#         process is not zero.
#     :returns: tuple(text, returncode)
#     """
#     shell = shell if (shell is not None) else (type(command) is not list)
# 
#     p = subprocess.Popen(
#         command, shell=shell, cwd=cwd, stderr=subprocess.STDOUT,
#         stdout=subprocess.PIPE)
# 
#     output, _ = p.communicate()
# 
#     if p.returncode and raise_on_nonzero:
#         raise Exception("command failed: %s" % output)
# 
#     return (output, p.returncode)
# 
# Path: fabfile/constants.py
# RELEASE_NOTES_FILE = os.path.join(LIB_DIR, 'release-notes.md')
# 
# Path: fabfile/ver.py
# def get_version():
#     '''Gets the current version'''
#     text = open(VER_FILE).read()
#     match = RE_VERSION.search(text)
# 
#     if not match:
#         raise Exception("Could not get version")
# 
#     return match.group('version')


# Path: fabfile/relnote.py
import os
import re
from markdown import markdown
from fabric.api import env, task
from fabric.colors import red
from .helpers import true, ex
from .constants import RELEASE_NOTES_FILE
from .ver import get_version


See Fabric documentation for more info: http://docs.fabfile.org/en/1.10/index.html
"""

# Imports #####################################################################
from __future__ import print_function

# Internal

# Metadata ####################################################################
__author__ = "Timothy McFadden"
__creationDate__ = "07-JUL-2017"


# Fabric environment setup ####################################################
env.colorize_errors = True
###############################################################################


def _get(version=None, path=RELEASE_NOTES_FILE, html='n', display='n'):
    '''Return only the release notes for the specified version'''
    version = version or get_version()
    text = open(path).read()

    match = re.search('^(?P<notes># v?{0}.*?)^\*+$\n^$'.format(version), text, re.MULTILINE | re.DOTALL)
    if match:
        notes = match.group('notes').strip()
        if true(html):
            notes = markdown(notes, extensions=['markdown.extensions.fenced_code'])


        if true(display):
