• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Render "GFM"-likely HTML in Sphinx with MyST-Parser

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

is Sphinx extension to parse Markdown document sources.
We can write high structured document using it even if we don't know syntax reStructuredText.

MyST-Parser supports GFM, but ...


MyST-Parser has some options for behavior. 1
There is myst_gfm_only in options. This is to run parse GitHub Flavored Markdown (GFM) that support some extended syntax from Commonmark.

But, when this option is True, it does not inject <br> into linefeed of sources.

Example


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

This markdown source has line feed each sentence.

When Sphinx converts it to HTML by sphinx-build.


<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

But GitHub render from this source. 2


<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<br />
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
<br />
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<br />
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

Why it happened?

GFM is not matched between spec and behavior.


GFM spec is put on

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

.
There are

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

and

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

about spec of line breaks.

Spec of soft line break:

A regular line break (not in a code span or HTML tag) that is not preceded by two or more spaces or a backslash is parsed as a softbreak. (A softbreak may be rendered in HTML either as a line ending or as a space. The result will be the same in browsers. In the examples here, a line ending will be used.)
We expect that it renders nothing when there are soft line breaks.
But actually, GitHub.com renders <br> tag from line feed.

MyST-Parser doesn't have options for line breaks.

It is difficult that to adjust for implementation of GitHub?
No, you can resolve it by hack class definition.

How to hack for realize "actual" GFM style line breaks.

Code


Please write it code into conf.py of your document.


def setup(app):
# If you already defined setup, write these code into before `return`.
from myst_parser.mdit_to_docutils.base import DocutilsRenderer

DocutilsRenderer.render_softbreak = DocutilsRenderer.render_hardbreak
Description


When Sphinx with MyST-Parser convert from Markdown to HTML, generates intermediate object called "doctree".

  • MyST-Parser parse Markdown and create doctree.
  • Writer process of Sphinx generates HTML from doctree.

To create doctree from Markdown, MyST-Parser defines

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

.
This class have methods that works each AST nodes of Markdown.

DocutilsRenderer.render_softbreak is method to put linefeed code when process visits "soft line breaks".
DocutilsRenderer.render_hardbreak is method to put HTML when process visits "hard line breaks".

In Python, we can override methods of classes by reassign.
Resolve code is to override render_softbreak by render_hardbreak.

Therefore, when parser found all line breaks, it runs as "hard line breaks".



  1. Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

    ↩


  2. Actually, <br> tag puts on single line, it added tail of lines. ↩


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу