简体   繁体   English

如何在 Sphinx 和 DocString 中创建常规文本

[英]How to create regular text in Sphinx and DocString

I added Sphinx auto-documenting to my vanilla Django project.我将 Sphinx 自动记录添加到我的 vanilla Django 项目中。

The vanilla Django project has a DocString that I want to keep: vanilla Django 项目有一个我想保留的 DocString:

"""URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

However, Sphinx is trying to process it and gives me these errors:但是,Sphinx 正在尝试处理它并给我以下错误:

/usr/src/app/porter/urls.py:docstring of porter.urls:5: WARNING: Definition list ends without a blank line; unexpected unindent.
/usr/src/app/porter/urls.py:docstring of porter.urls:7: WARNING: Unexpected indentation.
/usr/src/app/porter/urls.py:docstring of porter.urls:9: WARNING: Block quote ends without a blank line; unexpected unindent.

How can I tell Sphinx to just render the text as-is (just a very long description) and don't process it?我怎么能告诉 Sphinx 只按原样呈现文本(只是一个很长的描述)而不处理它?

I guess there's no way of just having the text as-is.我想没有办法让文本保持原样。

For anyone coming from markdown and not quite used to Sphinx and DocStrings, this is how the extra lines needed to be added in:对于任何来自 Markdown 并且不太习惯 Sphinx 和 DocStrings 的人,这是需要添加额外行的方式:

"""porter URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/

Examples:

Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

Thank you for the confirmation that there's not other, easier way to avoid the warnings.感谢您确认没有其他更简单的方法可以避免警告。

The concept of "regular" text, or "plain" or "as is", is poorly defined. “常规”文本或“普通”或“原样”的概念定义不明确。 Text is, if you will, a rather abstract concept.如果您愿意,文本是一个相当抽象的概念。 Let's think about that… Even when we write something by hand, text is rendered to the page.让我们考虑一下……即使我们手写内容,文本也会呈现到页面上。 A text editor processes its input too: It usually renders it in a monospaced font, may apply syntax highlighting, and will either preserve explicit line breaks or soft-wrap paragraphs.文本编辑器也会处理它的输入:它通常以等宽字体呈现它,可以应用语法突出显示,并且将保留显式换行符或软换行段落。 Word processors do even more.文字处理器做得更多。 As does a browser.浏览器也是如此。

Sphinx also processes text, but only performs an intermediate step. Sphinx 也处理文本,但只执行一个中间步骤。 It ultimately hands over its output to a rendering back-end, such as the browser for HTML documentation, or LaTeX and then ultimately the PDF viewer.它最终将其输出移交给渲染后端,例如 HTML 文档的浏览器或 LaTeX,然后最终移交给 PDF 查看器。

We can tell Sphinx not to do any processing, thanks to the raw directive.由于raw指令,我们可以告诉 Sphinx 不要做任何处理。 But the result in this case will not be very appealing.但这种情况下的结果不会很有吸引力。 We can copy that doc-string from the question, paste it into a newly created file, and open that file in the browser.我们可以从问题中复制该文档字符串,将其粘贴到新创建的文件中,然后在浏览器中打开该文件。 It will look terrible, something like this:它看起来很糟糕,像这样:

URL Configuration The `urlpatterns` list routes URLs to views. URL 配置 `urlpatterns` 列表将 URL 路由到视图。 For more information please see: httрs://docs.djangoproject.com/en/3.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') […]更多信息请参见:httрs://docs.djangoproject.com/en/3.1/topics/http/urls/ 示例:函数视图 1. 添加一个导入:from my_app 导入视图 2. 添加一个 URL 到 urlpatterns: path( '', views.home, name='home') […]

This where we usually give in and give Sphinx what it wants: reStructuredText as input.这是我们通常让步并给 Sphinx 想要的东西的地方:reStructuredText 作为输入。 That often means adding blank lines before and after blocks, such as lists .这通常意味着在块之前和之后添加空行,例如列表

I never liked that about reStructuredText.我从不喜欢 reStructuredText。 It's supposed to be "minimal mark-up", but whenever I want to introduce a list, like so它应该是“最小标记”,但每当我想引入一个列表时,就像这样

Here is a list:
* item 1
* item 2
(and maybe even continuing here)

I have to add extra lines, like that:我必须添加额外的行,如下所示:

Here is a list:

* item 1
* item 2

(and then the next paragraph)

In the past, I even went so far as to customize Sphinx's processing, just so that I don't have to change my doc-strings.过去,我什至自定义了 Sphinx 的处理,这样我就不必更改我的文档字符串。 Sphinx provides the autodoc-process-docstring event to facilitate that. Sphinx 提供了autodoc-process-docstring事件来促进这一点。 But eventually that was too much trouble and now I'm just using Markdown for the doc-strings .但最终这太麻烦了,现在我只是将 Markdown 用于 doc-strings Markdown also has some rules, of course, such as for lists.当然,Markdown 也有一些规则,比如对于列表。 But they usually interfere less with my aesthetic sensibility.但它们通常对我的审美敏感性的干扰较少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM