import re


def to_markdown(data):

    preamble = """# {tests}
|  |       before       |    after  |       ratio | benchmark  |
|- |-: |-: |-|-|
||     {base_commit}    |    {head_commit} | | |
||     `{base_branch}`        |   `{head_branch}` | ||\n""".format(
        **data
    )

    full_table = preamble + "\n".join(
        "|" + "|".join(row) + "|" for row in data["comparisons"]
    )

    return full_table


def parse(asv_input):

    result = re.match(
        r"^\n(?P<tests>.*?)\n       before           after         ratio\n     \[(?P<base_commit>.+)\]       \[(?P<head_commit>.+)\]\n     <(?P<base_branch>.+)>           <(?P<head_branch>.+)>\n(?P<raw_comparisons>.*)",
        asv_input,
        re.DOTALL,
    )

    test_details = result.groupdict()

    raw_comparisons = test_details.pop("raw_comparisons").splitlines()
    test_details["comparisons"] = [
        [row[:2].strip()] + row[2:].split(maxsplit=3) for row in raw_comparisons
    ]
    return test_details


if __name__ == "__main__":
    import sys

    print("hello", file=sys.stderr)
    asv_input = sys.stdin.read()
    print(asv_input, file=sys.stderr)

    print(to_markdown(parse(asv_input)))

@@ -1,24 +1,303 @@
 import re%0A%0A%0A
+def format(element):%0A%0A    if is_float(element):%0A%0A        f = float(element)%0A%0A        return %22%7B0:.3%7D%22.format(f)%0A%0A    else:%0A        return element%0A%0A%0Adef is_float(element):%0A%0A    try:%0A        float(element)%0A    except ValueError:%0A        return False%0A    else:%0A        return True%0A%0A%0A
 def to_markd
@@ -1121,22 +1121,8 @@
     
-test_details%5B%22
 comp
@@ -1132,14 +1132,12 @@
 sons
-%22%5D
  = 
-%5B
+(
 %0A   
@@ -1214,16 +1214,216 @@
 arisons%0A
+    )%0A    print(comparisons)%0A    test_details%5B%22comparisons%22%5D = %5B%0A        %5Bindicator, format(value_a), format(value_b), ratio, test%5D%0A        for indicator, value_a, value_b, ratio, test in comparisons%0A
     %5D%0A  

format floats