首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Sphinx自动摘要中使用第一段而不是第一行

在Sphinx自动摘要中使用第一段而不是第一行
EN

Stack Overflow用户
提问于 2012-05-23 19:18:13
回答 1查看 428关注 0票数 3

我正在使用Sphinx自动汇总指令来记录一个类,但是我遇到了这样的问题:自动汇总只严格显示自动汇总表中文档字符串的第一行。例如,

代码语言:javascript
复制
.. currentmodule:: logging
.. autosummary::
  ~Logger.manager
  ~Logger.root

生成一个表,该表具有:

代码语言:javascript
复制
manager   There is [under normal circumstances] just one Manager instance, which
root      A root logger is not that different to any other logger, except that

我可以理解为什么这是默认的,但是有没有办法让它显示第一句话或第一段呢?

EN

回答 1

Stack Overflow用户

发布于 2012-06-04 02:39:35

您的文档字符串显然来自标准库logging模块。它们看起来像这样:

代码语言:javascript
复制
class Manager(object):
    """
    There is [under normal circumstances] just one Manager instance, which
    holds the hierarchy of loggers.
    """

代码语言:javascript
复制
class RootLogger(Logger):
    """
    A root logger is not that different to any other logger, except that
    it must have a logging level and there is only one instance of it in
    the hierarchy.
    """

这是返回自动汇总字符串(autosummary/__init__.py)的代码:

代码语言:javascript
复制
m = re.search(r"^([A-Z][^A-Z]*?\.\s)", " ".join(doc).strip())
if m:
    summary = m.group(1).strip()
elif doc:
    summary = doc[0].strip()
else:
    summary = '':

doc是行列表形式的文档字符串。

自动汇总字符串应该是the first sentence of the docstring。但是,正则表达式存在一些问题:

  1. 在最初的大写字母后,句子不能包含其他大写字母。
  2. 句点后应为空格字符。

这意味着正则表达式不会与上面的任何文档字符串匹配。如果模式更改为

代码语言:javascript
复制
^([A-Z].*?\.\s?)

然后,它将匹配两个文档字符串,并且完整的第一个句子将出现在输出中。(这可能不是最优的正则表达式,但至少在这种情况下它可以工作。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10718846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档