Close already opened list if needed.

    This will try to see if it is needed to close already opened list.

    :Args:
      - ctx (:class:`Context`): Context object 
      - root (Element): lxml element representing current position.

    :Returns:
      lxml element where future content should be placed.

def close_list(ctx, root):
    """Close already opened list if needed.

    This will try to see if it is needed to close already opened list.

    :Args:
      - ctx (:class:`Context`): Context object 
      - root (Element): lxml element representing current position.

    :Returns:
      lxml element where future content should be placed.
    """

    try:
        n = len(ctx.in_list)
        if n <= 0:
            return root

        elem = root

        while n > 0:
            while True:
                if elem.tag in ['ul', 'ol', 'td']:                    
                    elem = elem.getparent()
                    break

                elem = elem.getparent()

            n -= 1

        ctx.in_list = []

        return elem
    except:
        return None