Takes low state data and returns a dict of proccessed data
    that is by default used in a jinja template when rendering a markdown highstate_doc.

    This `lowstate_item_markdown` given a lowstate item, returns a dict like:

    .. code-block:: yaml

        vars:       # the raw lowstate_item that was proccessed
        id:         # the 'id' of the state.
        id_full:    # combo of the state type and id "state: id"
        state:      # name of the salt state module
        function:   # name of the state function
        name:       # value of 'name:' passed to the salt state module
        state_function:    # the state name and function name
        markdown:          # text data to describe a state
            requisites:    # requisite like [watch_in, require_in]
            details:       # state name, parameters and other details like file contents

def proccesser_markdown(lowstate_item, config, **kwargs):
    '''
    Takes low state data and returns a dict of proccessed data
    that is by default used in a jinja template when rendering a markdown highstate_doc.

    This `lowstate_item_markdown` given a lowstate item, returns a dict like:

    .. code-block:: yaml

        vars:       # the raw lowstate_item that was proccessed
        id:         # the 'id' of the state.
        id_full:    # combo of the state type and id "state: id"
        state:      # name of the salt state module
        function:   # name of the state function
        name:       # value of 'name:' passed to the salt state module
        state_function:    # the state name and function name
        markdown:          # text data to describe a state
            requisites:    # requisite like [watch_in, require_in]
            details:       # state name, parameters and other details like file contents

    '''
    # TODO: switch or ... ext call.
    s = lowstate_item
    state_function = '{0}.{1}'.format(s['state'], s['fun'])
    id_full = '{0}: {1}'.format(s['state'], s['__id__'])

    # TODO: use salt defined STATE_REQUISITE_IN_KEYWORDS
    requisites = ''
    if s.get('watch'):
        requisites += 'run or update after changes in:\n'
        for w in s.get('watch', []):
            requisites += _format_markdown_requisite(w.items()[0][0], w.items()[0][1])
        requisites += '\n'
    if s.get('watch_in'):
        requisites += 'after changes, run or update:\n'
        for w in s.get('watch_in', []):
            requisites += _format_markdown_requisite(w.items()[0][0], w.items()[0][1])
        requisites += '\n'
    if s.get('require') and s.get('require'):
        requisites += 'require:\n'
        for w in s.get('require', []):
            requisites += _format_markdown_requisite(w.items()[0][0], w.items()[0][1])
        requisites += '\n'
    if s.get('require_in'):
        requisites += 'required in:\n'
        for w in s.get('require_in', []):
            requisites += _format_markdown_requisite(w.items()[0][0], w.items()[0][1])
        requisites += '\n'

    details = ''

    if state_function == 'highstate_doc.note':
        if 'contents' in s:
            details += '\n{0}\n'.format(s['contents'])
        if 'source' in s:
            text = __salt__['cp.get_file_str'](s['source'])
            if text:
                details += '\n{0}\n'.format(text)
            else:
                details += '\n{0}\n'.format('ERROR: opening {0}'.format(s['source']))

    if state_function == 'pkg.installed':
        pkgs = s.get('pkgs', s.get('name'))
        details += '\n```\ninstall: {0}\n```\n'.format(pkgs)

    if state_function == 'file.recurse':
        details += '''recurse copy of files\n'''
        y = _state_data_to_yaml_string(s)
        if y:
            details += '```\n{0}\n```\n'.format(y)
        if '!doc_recurse' in id_full:
            findfiles = __salt__['file.find'](path=s.get('name'), type='f')
            if len(findfiles) < 10 or '!doc_recurse_force' in id_full:
                for f in findfiles:
                    details += _format_markdown_system_file(f, config)
            else:
                details += ''' > Skipping because more than 10 files to display.\n'''
                details += ''' > HINT: to force include !doc_recurse_force in state id.\n'''
        else:
            details += ''' > For more details review logs and Salt state files.\n\n'''
            details += ''' > HINT: for improved docs use multiple file.managed states or file.archive, git.latest. etc.\n'''
            details += ''' > HINT: to force doc to show all files in path add !doc_recurse .\n'''

    if state_function == 'file.blockreplace':
        if s.get('content'):
            details += 'ensure block of content is in file\n```\n{0}\n```\n'.format(_md_fix(s['content']))
        if s.get('source'):
            text = '** source: ' + s.get('source')
            details += 'ensure block of content is in file\n```\n{0}\n```\n'.format(_md_fix(text))

    if state_function == 'file.managed':
        details += _format_markdown_system_file(s['name'], config)

    # if no state doc is created use default state as yaml
    if not details:
        y = _state_data_to_yaml_string(s)
        if y:
            details += '```\n{0}```\n'.format(y)

    r = {
        'vars': lowstate_item,
        'state': s['state'],
        'name': s['name'],
        'function': s['fun'],
        'id': s['__id__'],
        'id_full': id_full,
        'state_function': state_function,
        'markdown': {
            'requisites': requisites.decode('utf-8'),
            'details': details.decode('utf-8')
        }
    }
    return r