Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Process linebreaks and convert them to br element. #162

Merged
merged 2 commits into from
Aug 9, 2019

Conversation

RaptorCZ
Copy link
Contributor

No description provided.

@humitos
Copy link
Member

humitos commented Jul 1, 2019

Hi @RaptorCZ, thanks for your contribution!

I think it's not the intended behavior of Markdown. I think that "double breakline" makes a paragraph, and "single breakline" just should do nothing instead of creating a <br>.

Besides, I suppose that the usage of <br> everywhere on the HTML is not a recommended practice.

@RaptorCZ
Copy link
Contributor Author

RaptorCZ commented Jul 1, 2019

Hi,
2 spaces in markdown means "do line break". You can check documentation
https://www.markdownguide.org/basic-syntax/#line-breaks
This is standard way how to wrap line instead of creating paragraphs.

The same in commonmark specs
https://spec.commonmark.org/0.17/#hard-line-breaks

We can discuss if <br> is ok or not in html, but this is how it works :-)

@humitos
Copy link
Member

humitos commented Jul 1, 2019

Good point. You are right.

From what I read there, it only happens when the line ends with "two or more spaces" (and then the breakline) in the source. Does visit_linebreak works like that way only?

@RaptorCZ
Copy link
Contributor Author

RaptorCZ commented Jul 1, 2019

Commonmark's parser has new line detection as

    def parseNewline(self, block):
        """
        Parse a newline.  If it was preceded by two spaces, return a hard
        line break; otherwise a soft line break.
        """
        # assume we're at a \n
        self.pos += 1
        lastc = block.last_child
        if lastc and lastc.t == 'text' and lastc.literal[-1] == ' ':
            linebreak = len(lastc.literal) >= 2 and lastc.literal[-2] == ' '
            lastc.literal = re.sub(reFinalSpace, '', lastc.literal)
            if linebreak:
                node = Node('linebreak', None)
            else:
                node = Node('softbreak', None)
            block.append_child(node)
        else:
            block.append_child(Node('softbreak', None))

        # gobble leading spaces in next line
        self.match(reInitialSpace)
        return True

So it checks if it is softbreak or linebreak.

Current version of recommonmark has softbreak implemented.

    def visit_softbreak(self, _):
        self.current_node.append(nodes.Text('\n'))

So I just added implementation for missing linebreak token as specification says. Render linebreak as <br \>

    def visit_linebreak(self, _):
        self.current_node.append(nodes.raw('', '<br />', format='html'))

Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Thanks for all your comments and explanation ;)

@grazius
Copy link

grazius commented Jul 15, 2019

can be merged for new release ?
i need linebreak support

@ericholscher ericholscher merged commit 59dcaaa into readthedocs:master Aug 9, 2019
Harmon758 added a commit to Harmon758/tweepy that referenced this pull request Apr 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants