1

Is there any way to have Pycco recognize doctests as code and render them appropriately?

E.g. the docstring of the following function

def fib(i):
    """ Fibonacci number

    >>> fib(10)
    55
    """
    if i < 2:
        return i
    else:
        return fib(i-1) + fib(i-2)

Renders In Pycco like the following

Fibonacci number

           fib(10) 5

Clearly the >>> was interpretted as indentation and code highlighting did not take effect. This seems like a common use case. is there a plugin somewhere that I'm missing?

4

1 回答 1

1

Python 不介意doctest套件是否与文档字符串的其余部分相比缩进了额外的四个字符,只要它在内部是一致的,因此以下将通过测试就好(在 2.7.9 和 3.4.2 中测试):

def test():
    """Doctest test

    The following are doctests:

        >>> test()
        'foo'

    That went well.

    """
    return 'foo'

if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True)

这个额外的缩进将被解释为一个代码块pycco,就像它在 SO 上的 Markdown 中一样。上面的测试在 HTML 中呈现如下:

渲染的 pycco HTML 的屏幕抓取

于 2015-01-06T22:17:49.140 回答