tzador / makedown

Organise your shell scripts within executable markdown files
MIT License
248 stars 9 forks source link

Markdown parse error: "IndexError: list index out of range" #32

Open Mizuho32 opened 4 days ago

Mizuho32 commented 4 days ago

When I run the demo,

# my_scripts.md

Here are a few examples of commands:

## [hello]() Prints "hello" using bash

```bash
echo "hello"

[world]() Prints "world" using python

This is a more detailed description of the command.

#!/usr/bin/env python

print("world")

makedown.py says.
```bash
    script = parts[1].strip() + "\n"
IndexError: list index out of range

This is actually because "#" of "#!/usr/bin/env python" cheat the code of

source = re.split(
    r"^#", "\n".join(lines[line_number:]), flags=re.MULTILINE
)[1].strip()

My naive implementation of parsing source works.

try:
    state = dict(in_code = False)
    def not_next_para(l):
        if not len(l):
            return True
        in_code = state["in_code"]
        if l[0] == "`": # code
            state["in_code"] = not in_code
        elif not in_code and l[0] == "#": # next paragraph
            return False
        return True

    source2 = []
    for ln2 in range(line_number, len(lines)):
        source2.append(lines[ln2])

        l = lines[ln2+1]
        if not (not len(l) or not_next_para(l)): # not len(l) => not next is para
            break

except IndexError as ex:
    pass

source = "\n".join(source2)
tzador commented 4 days ago

Thank you. Taking a look

On Fri, Oct 18, 2024, 09:07 Mizuho32 @.***> wrote:

When I run the demo,

my_scripts.md

Here are a few examples of commands:

[hello]() Prints "hello" using bash

bashecho "hello"## [world]() Prints "world" using python

This is a more detailed description of the command.


print("world")```

makedown.py says.

    script = parts[1].strip() + "\n"
IndexError: list index out of range

This is actually because "#" of "#!/usr/bin/env python" cheat the code of

source = re.split(
    r"^#", "\n".join(lines[line_number:]), flags=re.MULTILINE
)[1].strip()

My naive implementation of parsing source works.

try:
    state = dict(in_code = False)
    def not_next_para(l):
        if not len(l):
            return True
        in_code = state["in_code"]
        if l[0] == "`": # code
            state["in_code"] = not in_code
        elif not in_code and l[0] == "#": # next paragraph
            return False
        return True

    source2 = []
    for ln2 in range(line_number, len(lines)):
        source2.append(lines[ln2])

        l = lines[ln2+1]
        if not (not len(l) or not_next_para(l)): # not len(l) => not next is para
            break
except IndexError as ex:
    pass
source = "\n".join(source2)

—
Reply to this email directly, view it on GitHub
<https://github.com/tzador/makedown/issues/32>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AXFJKJA5ROAT2DMCUHI2SW3Z4CXUXAVCNFSM6AAAAABQFJMOYWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGU4TMNRTG4YTSNI>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>