【问题标题】:Sphinx documentation and links to MarkdownSphinx 文档和指向 Markdown 的链接
【发布时间】:2018-09-20 16:34:13
【问题描述】:

我正在尝试使用 Sphinx 从 Markdown 源构建一些文档。我的conf.py如下...

conf.py

from recommonmark.parser import CommonMarkParser
project = 'DS'
copyright = '2018, DS'
author = 'DS, Work'

version = ''
release = ''

extensions = []

templates_path = ['_templates']

source_suffix = ['.rst', '.md']

master_doc = 'index'

language = None
exclude_patterns = []
pygments_style = 'sphinx'
html_theme = 'classic'
html_static_path = ['_static']
source_parsers = {
   '.md': CommonMarkParser,
}
htmlhelp_basename = 'DSDocumentationdoc'
latex_elements = {
}
latex_documents = [
    (master_doc, 'DSDocumentation.tex', 'DS Documentation',
     'DS, Work', 'manual'),
]
man_pages = [
    (master_doc, 'dsdocumentation', 'DS Documentation',
     [author], 1)
]
texinfo_documents = [
    (master_doc, 'DSDocumentation', 'DS Documentation',
     author, 'DSDocumentation', 'One line description ofproject.',
 'Miscellaneous'),
]

index.rst

Welcome to DS Documentation!
======================================

The following documentation is produced and maintained by the Data Science team.

Contents:

.. toctree::
   :maxdepth: 2
   :glob:

   README.md
   documentation.md
   getting_started/*
   how-tos/*
   statistics_data_visualisation.md

生成了文档构建和html输出,但是README.md在两个子目录中有指向其他markdown文档的链接,例如以下...

... [this document](./getting_started/setting_your_machine_up.md)...

...在已翻译的README.html 文档中,目标尚未转换为已翻译的HTML 目标,因为它被识别为reference external...

...<a class="reference external" href="./getting_started/setting_your_machine_up.md">this document</a>...

...我有一半的期望/希望它会输出为reference internal 并适当地转换文件扩展名...

...<a class="reference internal" href="./getting_started/setting_your_machine_up.html">this document</a>...

...这样链接的工作方式与侧边栏中的目录相同。

任何关于这是否可以实现的建议将不胜感激。

干杯。

编辑

尝试@waylan 建议的解决方案我已将以下内容添加到conf.pyenable_auto_doc_ref...

def setup(app):
    app.add_config_value('recommonmark_config', {
        'enable_auto_doc_ref': True,
    }, True)
    app.add_transform(AutoStructify)

...在运行make html 时出现以下错误.....

❱ cat /tmp/sphinx-err-57rejer3.log 
# Sphinx version: 1.8.0
# Python version: 3.6.6 (CPython)
# Docutils version: 0.14 
# Jinja2 version: 2.10
# Last messages:
#   building [mo]: targets for 0 po files that are out of date
#   
#   building [html]: targets for 16 source files that are out of date
#   
#   updating environment:
#   
#   16 added, 0 changed, 0 removed
#   
#   reading sources... [  6%] README
#   
# Loaded extensions:
#   sphinx.ext.mathjax (1.8.0) from /home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/ext/math
jax.py                                                                                                                                
#   alabaster (0.7.11) from /home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/alabaster/__init__.py
Traceback (most recent call last):
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/cmd/build.py", line 304, in build_ma
in                                                                                                                                    
    app.build(args.force_all, filenames)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/application.py", line 341, in build
    self.builder.build_update()
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 347, in 
build_update                                                                                                                          
    len(to_build))
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 360, in 
build                                                                                                                                 
    updated_docnames = set(self.read())
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 468, in 
read                                                                                                                                  
    self._read_serial(docnames)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 490, in 
_read_serial                                                                                                                          
    self.read_doc(docname)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 534, in 
read_doc                                                                                                                              
    doctree = read_doc(self.app, self.env, self.env.doc2path(docname))
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/io.py", line 318, in read_doc
    pub.publish()
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/docutils/core.py", line 218, in publish
    self.apply_transforms()
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/docutils/core.py", line 199, in apply_trans
forms                                                                                                                                 
    self.document.transformer.apply_transforms()
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/sphinx/transforms/__init__.py", line 90, in
 apply_transforms                                                                                                                     
    Transformer.apply_transforms(self)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/docutils/transforms/__init__.py", line 171,
 in apply_transforms                                                                                                                  
    transform.apply(**kwargs)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 325, in ap
ply                                                                                                                                   
    self.traverse(self.document)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 297, in tr
averse                                                                                                                                
    self.traverse(child)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 297, in tr
averse                                                                                                                                
    self.traverse(child)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 297, in tr
averse                                                                                                                                
    self.traverse(child)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 287, in tr
averse                                                                                                                                
    newnode = self.find_replace(c)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 267, in fi
nd_replace                                                                                                                            
    newnode = self.auto_doc_ref(node)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/transform.py", line 175, in au
to_doc_ref                                                                                                                            
    return self.state_machine.run_role('doc', content=content)
  File "/home/neil.shephard@samba.sheffield.thefloow.com/.local/lib/python3.6/site-packages/recommonmark/states.py", line 134, in run_r
ole                                                                                                                                   
    content=content)
TypeError: 'NoneType' object is not callable

我查看了最后两个电话,我认为这可能归结为 content 未设置,这可能与我的 index.rst 有关,但我在这里真的超出了我的深度。

【问题讨论】:

    标签: python-3.x markdown python-sphinx


    【解决方案1】:

    recommonmark documentation 建议通过将以下内容添加到您的 config.py 文件来启用 AutoStructify

    from recommonmark.transform import AutoStructify
    
    github_doc_root = 'https://github.com/rtfd/recommonmark/tree/master/doc/'
    def setup(app):
        app.add_config_value('recommonmark_config', {
                'url_resolver': lambda url: github_doc_root + url,
                'auto_toc_tree_section': 'Contents',
                }, True)
        app.add_transform(AutoStructify)
    

    这将为您提供以下功能:

    • enable_auto_toc_tree:是否开启Auto Toc Tree功能。
    • auto_toc_tree_section:启用后,自动目录树只会在与标题匹配的部分上启用。
    • enable_auto_doc_ref:是否开启Auto Doc Ref功能。
    • enable_math:是否启用数学公式
    • enable_inline_math:是否启用内联数学
    • enable_eval_rst:是否启用 Embed reStructuredText。
    • url_resolver:将文档中现有的相对位置映射到 http 链接的函数

    值得注意的是Auto Doc Ref 功能:

    在一个文档中引用另一个文档页面是很常见的。我们 通常使用参考来做到这一点。 AutoStructify 将翻译这些 参考块转化为结构化文档参考。例如

    [API Reference](api_ref.md)
    

    将被翻译成以下 reStructuredText 代码的 AST

    :doc:`API Reference </api_ref>`
    

    它会被渲染为 API 参考

    为什么这是必要的?因为,与 Rst 不同,Markdown 不了解给定文档之外的任何内容,也不支持 Rst 样式指令。因此,没有转换 URL 的机制。

    相反,AutoStructify 会等到 recommonmark 桥将 Markdown 转换为 Sphinx 的底层文档结构(docutils 文档对象)之后,然后在其上运行一系列转换器以提供有限的类似 Rst 的功能。即使使用 AutoStructify,在使用 Markdown 时您也永远不会获得完整的功能支持。这需要 Markdown 原生支持指令,这不太可能发生。

    【讨论】:

    • @slackline 我添加了对 AutoStructify 为何必要的解释,并概述了它的工作原理。
    • 那是花时间解释@Waylan 非常感谢的一切(我在开始一份新工作后才使用 Python 4.5 个月)。在尝试您的建议时,我遇到了一个错误,我在上面的原始帖子中已详细说明。
    • 我刚刚编辑了我的答案以包含from recommonmark.transform import AutoStructify。我希望这会解决您的问题。
    • 我在recommonmark 的Autostructify section 上读到AutoStructify 需要显式导入,因此已将其包含在conf.py 的标题中,对于未将其包含在上述编辑中表示歉意。
    • 我再次查看此内容并在 recommonmark 上发现 enable_auto_doc_ref 现在已弃用(请参阅 issue #85,在 GitHub 存储库 README 上也标记为这样)。它建议“markdown 中的链接默认添加为:any: 角色,并由 sphinx 解析。”,所以我将对此进行研究。在这里注意以防其他人遇到此问题。
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多