#python3
def FindBlock(S, Opening, Ending, StartingPoint = 0):
    """
    Find the first start index and the end index of the bound by
    Opening and Ending strings

    Example:
    S = "aa11xxxx11"
    FindBlock(S, "11", "11")
    returns (2, 8) .

    Returns (-1,-1) if not found or error
    """
    StartIndex = S.find(Opening, StartingPoint)
    if StartIndex == -1:
        return (-1, -1)

    EndIndex = S.find(Ending, StartIndex + 1)
    if EndIndex == -1:
        return (-1,-1)
    return (StartIndex, EndIndex)
def GetTheLineBeforeCommentBlocks(S, CommentStart, CommentEnd):
    Result = []
    (a, b) = FindBlock(S, CommentStart, CommentEnd, 0)
    while(a != -1):
        #print("-"*10 +  " Block "+ str(BlockIndex) +  "-"*20)
        end_of_previou_line  = S.rfind("\n", 0, a)
        start_of_previou_line = S.rfind("\n", 0, end_of_previou_line)
        Result +=  [
                    S[  start_of_previou_line  + len("\ndef "):
                        end_of_previou_line - len(":")]
                    ]
        (a, b) = FindBlock(S, CommentStart, CommentEnd, b + len(CommentEnd))
    return Result
def GetCommentBlocks(S, CommentStart, CommentEnd):
    Result = []
    (a, b) = FindBlock(S, CommentStart, CommentEnd, 0)
    BlockIndex = 0
    while(a != -1):
        #print("-"*10 +  " Block "+ str(BlockIndex) +  "-"*20)
        Block = S[a + len(CommentStart) : b]
        Result += [Block]
        BlockIndex += 1
        (a, b) = FindBlock(S, CommentStart, CommentEnd, b + len(CommentEnd))
    return Result
def GetExampleBlocks(S, ExampleStart, ExampleEnd, trim):
    Result = []
    (a, b) = FindBlock(S, ExampleStart, ExampleEnd)
    BlockIndex = 0
    while(a != -1):
        #print("-"*10 +  " Block "+ str(BlockIndex) +  "-"*20)
        Block = S[a + len(ExampleStart) : b]
        if trim:
            Block = Block[trim:]
            Block = Block.replace("\n"+ " "*trim, "\n")
        Result += [Block]
        BlockIndex += 1
        (a, b) = FindBlock(S, ExampleStart, ExampleEnd, b + len(ExampleEnd))
    return Result

def go(TestFile, InitScript, trim = 0,
        CommentStart = "\"\"\"",
        CommentEnd = "\"\"\"",
        ExampleStart = "~>\n",
        ExampleEnd = "# End of Example\n",
        def_name = "eg"):
    Output = ""
    Output += InitScript
    f = open(TestFile, "r")
    Content = f.read()

    Functions = GetTheLineBeforeCommentBlocks(Content, CommentStart, CommentEnd)
    Comments = GetCommentBlocks(Content, CommentStart, CommentEnd)
    NumberOfExample = 0
    i = 0
    for comment in Comments:
        examples = GetExampleBlocks(comment, ExampleStart, ExampleEnd, trim)
        if len(examples):
            for eg in examples:
                Output +=   "\n"\
                            +"# Example "\
                            + str(NumberOfExample)\
                            + " in "\
                            + Functions[i]\
                            + "\n"\
                            + "def "\
                            + def_name\
                            + "_"\
                            + str(NumberOfExample)\
                            + "():\n"\
                            + eg
                NumberOfExample += 1
        i+=1
    #Write down all the functions
    Output += "\n"
    for egnum in range(NumberOfExample):
        Output += "eg_" + str(egnum) + "()\n"
    f.close()
    return Output

TestFile = "lukis.py"
OutputFile = "Testme.py"
InitScript = """
#!python3
# Codes generated by GenTestCode.py to test
# the example listesd in the Testfile.

from lukis import *
"""
g = open(OutputFile, "w")
g.write(
    go(TestFile, InitScript)
        )

g.close()
print("Finish. :)")
