2022-12-05 - md2gemini eats newlines in code blocks in item lists

Well, I was going to get a good night’s sleep, but I really wanted to publish my stuff. And so TIL there’s a bug in the handling of list items in the python md2gemini. I spent at least three hours trying really dumb hacks of applying sed expressions, reformatting my markdown, and trying different pandoc options… I went reading about how some others have moved from various markdown converters until they went off and created their own… you know who you are, and you’ve done impressive things, but it doesn’t do the things I like about md2gemini. No other converters were as close to what I wanted as md2gemini is. So I cloned the project off github, made liberal use of import pdb and pdb.set_trace() and I found the spot where things go poorly.

    def list_item(self, text, level):
        items = [item.strip() for item in text.splitlines()]
        text = functools.reduce(lambda x, y: x + " " + y, items)
        return text + NEWLINE

This is a decent chunk of code and it’s pretty quick, but it naively rips out all the newlines from the list item that’s being parsed. In the troubled article I had a list of steps that included pre-formatted or code blocks of commands to execute. This stanza will cheerfully squash the entire code block into one line and cause me to stare tearfully at my monitor and consider different career paths.

I wrote some really quick code as a hack to just get it working so I can publish. I’m going to shamelessly post it here, but I’ll definitely need to clean it up if anyone else is going to use it.

    def list_item(self, text, level):
        new_text = ''
        in_fence = False
        text = text.replace(PARAGRAPH_DELIM, PARAGRAPH_DELIM+'\r\n')
        for item in text.splitlines():
            was_in_fence = in_fence
            in_fence = not in_fence if item.lstrip().startswith('```') else in_fence
            if in_fence:
                new_text += LINEBREAK + item
            else:
                if was_in_fence:
                    new_text += LINEBREAK + item + LINEBREAK + LINEBREAK
                else:
                    new_text += ' ' + item
        return new_text + NEWLINE

github

the troubled article

Tags

#index

Navigation

index

tags

prev ⏰

⏰ next

updated: 2022-12-05 01:51:43

generated: 2023-11-11