import re


def convert_rst(changelog: str):
    """
    Generic RST conversion

    Xarray:
    ignore everything until the first version
    headers:
    v0.12.2 (29 June 2019)
    ----------------------

    New functions/methods
    ~~~~~~~~~~~~~~~~~~~~~

    Xonsh headers:
    **Header:**
    """
    # ignore rst blocks:
    changelog = re.sub(r"^\.\. .*$", "", changelog, flags=re.MULTILINE)

    # xarray preface until first verion
    changelog = re.sub(r"^(.*?)\nv", "v", changelog, flags=re.DOTALL)

    # first level headers
    changelog = re.sub(r"^\nhistory\n-+$", "", changelog, flags=re.MULTILINE | re.IGNORECASE)

    # normal list entries
    # Are ~ headers used?
    if re.search('[a-z0-9]\n~+\n', changelog, flags=re.MULTILINE | re.IGNORECASE):
        changelog = re.sub("^  ", "    ", changelog, flags=re.MULTILINE)
        changelog = re.sub(r"^[-\*] (.*)$", r"  - \1", changelog, flags=re.MULTILINE)
    else:
        changelog = re.sub("^ ", "  ", changelog, flags=re.MULTILINE)
        changelog = re.sub(r"^[-\*] (.*)$", r" - \1", changelog, flags=re.MULTILINE)

    # versions, with or without date
    # following line may be a '+' line
    changelog = re.sub(r"^v?([0-9\.]+)( \(.*?\))?(\n[+]+)?$", r"- update to version \1:", changelog, flags=re.MULTILINE)

    # sections
    changelog = re.sub(r"^\*?\*?([A-Za-z]+.*?):?\*?\*?$", r" - \1:", changelog, flags=re.MULTILINE)

    return changelog