DCsunset / pandoc-include

A pandoc filter to allow file and header inclusion
MIT License
63 stars 14 forks source link

newlines in src do not work #37

Open towi opened 1 year ago

towi commented 1 year ago

When my included source code contained a newline I got a backtrace:

Traceback (most recent call last):
  File "/usr/bin/pandoc-include", line 8, in <module>
    sys.exit(main())
  File "/usr/lib/python3.10/site-packages/pandoc_include/main.py", line 333, in main
    return pf.run_filter(action, doc=doc)
...
  File "/usr/lib/python3.10/site-packages/pandoc_include/main.py", line 309, in action
    codes.append(read_file(fn, config))
  File "/usr/lib/python3.10/site-packages/pandoc_include/main.py", line 160, in read_file
    content = "\n".join(dedent(content, config["dedent"]))
TypeError: sequence item 4: expected str instance, NoneType found
Error running filter pandoc-include:
Filter returned error status 1

I fixed this by adding a check in removeLeadingWhitespaces():

def removeLeadingWhitespaces(s, num):
    if not s.strip():  # <<< empty lines didn't work
        return s
    regex = re.compile(r"[^\s]")
    m = regex.search(s)
    if m == None:
        return
    pos = m.span()[0]
    if num < 0:
        return s[pos:]
    else:
        return s[min(pos, num):]
DCsunset commented 1 year ago

Hi, I'm using a cpp file with only a newline in it and include it in another file simply like the following:

```cpp
!include empty.cpp
```

But I'm unable to reproduce this error. Could you provide a minimum test case?