简体   繁体   English

Sphinx文档和Markdown链接

[英]Sphinx documentation and links to Markdown

I'm trying to use Sphinx to build some documentation from Markdown source. 我正在尝试使用Sphinx从Markdown源构建一些文档。 My conf.py is as follows... 我的conf.py如下...

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

The documents build and html output is generated, however README.md has links to other markdown documents in the two sub-directories such as the following... 文档生成并生成html输出,但是README.md在两个子目录中具有指向其他markdown文档的链接,例如:

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

...which in the translated README.html document the target has not been converted to the translated HTML target as its been recognised as reference external ... ...在翻译的README.html文档中,该目标尚未转换为翻译的HTML目标,因为它已被识别为reference external ...

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

...I was half-expecting/hoping it would output as reference internal and convert the file extension approrpiately... ...我半期待/希望它会作为reference internal输出并适当地转换文件扩展名...

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

...so that links worked in the same vein as the Table of Contents does in the sidebar. ...以使链接的工作方式与目录在侧边栏中的作用相同。

Any suggestions as to whether this can be achieved would be appreciated. 关于是否可以实现的任何建议将不胜感激。

Cheers. 干杯。

EDIT 编辑

Trying out the solution suggested by @waylan I have added the following to by conf.py to enable_auto_doc_ref ... 尝试通过@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)

...and on running make html I get the following error..... ...并且在运行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

I've looked through the last two calls and I think this might be down to content not being set, which may be something to do with my index.rst but I'm really out of my depth here. 我浏览了最后两个电话,我认为这可能index.rst未设置content ,这可能与index.rst但我确实不在这里。

The recommonmark documentation suggests enabling AutoStructify by adding the following to your config.py file: recommonmark文档建议通过将以下内容添加到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)

This will give you the following features: 这将为您提供以下功能:

  • enable_auto_toc_tree : whether enable Auto Toc Tree feature. enable_auto_toc_tree :是否启用自动Toc树功能。
  • auto_toc_tree_section : when enabled, Auto Toc Tree will only be enabled on section that matches the title. auto_toc_tree_section :启用时,仅在与标题匹配的部分上启用“自动Toc树”。
  • enable_auto_doc_ref : whether enable Auto Doc Ref feature. enable_auto_doc_ref :是否启用自动文档参考功能。
  • enable_math : whether enable Math Formula enable_math :是否启用数学公式
  • enable_inline_math : whether enable Inline Math enable_inline_math :是否启用内联数学
  • enable_eval_rst : whether Embed reStructuredText is enabled. enable_eval_rst :是否启用Embed reStructuredText。
  • url_resolver : a function that maps a existing relative position in the document to a http link url_resolver :将文档中现有的相对位置映射到http链接的函数

Of note is the Auto Doc Ref feature: 值得注意的是自动文档引用功能:

It is common to refer to another document page in one document. 通常在一个文档中引用另一文档页面。 We usually use reference to do that. 我们通常使用引用来做到这一点。 AutoStructify will translate these reference block into a structured document reference. AutoStructify会将这些参考块转换为结构化文档参考。 For example 例如

 [API Reference](api_ref.md) 

will be translated to the AST of following reStructuredText code 将转换为以下reStructuredText代码的AST

 :doc:`API Reference </api_ref>` 

And it will be rendered as API Reference 并将其呈现为API参考

Why is this necessary? 为什么这是必要的? Because, unlike Rst, Markdown does not have any knowledge of anything outside of the given document and has no support for Rst style directives. 因为与Rst不同,Markdown对给定文档之外的任何内容都不了解,也不支持Rst样式指令。 Therefore, there is no mechanism to transform a URL. 因此,没有转换URL的机制。

Instead, AutoStructify waits until after the recommonmark bridge converts the Markdown to Sphinx's underlying document structure (docutils document object), then it runs a series of transformers on it to provide limited Rst like functionality. 相反,AutoStructify会等到重新标记桥将Markdown转换为Sphinx的基础文档结构(docutils文档对象)之后,然后在其上运行一系列转换器以提供有限的Rst类似功能。 Even with AutoStructify, you will never get full feature support when using Markdown. 即使使用AutoStructify,使用Markdown时也永远不会获得全部功能支持。 That would require Markdown to have native support for directives, which is not likely to ever happen. 这将要求Markdown具有对指令的本机支持,这是不可能发生的。

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

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