Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

!INCLUDE with shift will shift lines inside code blocks #66

Open
sinh3ck opened this issue Nov 14, 2018 · 0 comments
Open

!INCLUDE with shift will shift lines inside code blocks #66

sinh3ck opened this issue Nov 14, 2018 · 0 comments

Comments

@sinh3ck
Copy link

sinh3ck commented Nov 14, 2018

For example using the include statement !INCLUDE "path/to/file.md", 2 on a file containing the following code block,

# uname -a
Linux mymachine 4.18.0-0.bpo.1-amd64 #1 SMP Debian 4.18.6-1~bpo9+1 (2018-09-13) x86_64 GNU/Linux

the resulting file will contain the following,

### uname -a
Linux mymachine 4.18.0-0.bpo.1-amd64 #1 SMP Debian 4.18.6-1~bpo9+1 (2018-09-13) x86_64 GNU/Linux

This is because the regex on the file MarkdownPP/Modules/Include.py does not detect if it is inside a code block. I did a dirty fix as shown below (lines with ## HERE).

MarkdownPP/Modules/Include.py:

    ...

    # matches title lines in Markdown files
    titlere = re.compile(r"^(:?#+.*|={3,}|-{3,})$")
    
    # matches code block start and stop
    codeblockre = re.compile(r"```")  ## HERE

    # includes should happen before anything else
    priority = 0

    ...

    def include_file(self, filename, pwd="", shift=0):
        try:
            f = open(filename, "r")
            data = f.readlines()
            f.close()
            in_codeblock = False

            # line by line, apply shift and recursively include file data
            linenum = 0
            for line in data:
                match = self.includere.search(line)

                if self.codeblockre.search(line):  ## HERE
                    in_codeblock = not in_codeblock  ## HERE

                if match:
                    dirname = path.dirname(filename)
                    data[linenum:linenum+1] = self.include(match, dirname)
                    # Update line so that we won't miss a shift if
                    # heading is on the 1st line.
                    line = data[linenum]

                if shift:

                    titlematch = self.titlere.search(line)
                    if titlematch and not in_codeblock:  ## HERE
                        to_del = []
                        for _ in range(shift):
                            if data[linenum][0] == '#':
                                data[linenum] = "#" + data[linenum]
                            elif data[linenum][0] == '=':
                                data[linenum] = data[linenum].replace("=", '-')
                            elif data[linenum][0] == '-':
                                data[linenum] = '### ' + data[linenum - 1]
                                to_del.append(linenum - 1)
                        for l in to_del:
                            del data[l]


                linenum += 1

            return data

        except (IOError, OSError) as exc:
            print(exc)

        return []

    ...

Note: I'm sorry, I'm not very Github savvy.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants