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

Process linebreaks and convert them to br element.#162

Merged
ericholscher merged 2 commits into
readthedocs:masterfrom
RaptorCZ:master
Aug 9, 2019
Merged

Process linebreaks and convert them to br element.#162
ericholscher merged 2 commits into
readthedocs:masterfrom
RaptorCZ:master

Conversation

@RaptorCZ

Copy link
Copy Markdown
Contributor

No description provided.

@humitos

humitos commented Jul 1, 2019

Copy link
Copy Markdown
Member

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

RaptorCZ commented Jul 1, 2019

Copy link
Copy Markdown
Contributor Author

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

humitos commented Jul 1, 2019

Copy link
Copy Markdown
Member

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

RaptorCZ commented Jul 1, 2019

Copy link
Copy Markdown
Contributor Author

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'))

@humitos humitos left a comment

Copy link
Copy Markdown
Member

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

grazius commented Jul 15, 2019

Copy link
Copy Markdown

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