Sphinx 的 RESTful Web 服务 API 文档

发布于 2024-10-09 07:00:28 字数 1539 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-10-16 07:00:28

Sphinx Contrib 项目似乎还有一个用于记录 RESTful HTTP API 的 HTTP 域 包。您可以在 Python 包 站点上找到其文档。我无法谈论它的适用性:我才刚刚开始研究 Sphinx,并且我还需要记录 RESTful API,并注意到了这个贡献的包。

The Sphinx Contrib project also seems to have an HTTP Domain package for documenting RESTful HTTP APIs. You can find its documentation on the Python packages site. I can't speak to its fitness: I'm only just starting to look into Sphinx, and I have a need to document RESTful APIs as well, and noticed this contributed package.

南城旧梦 2024-10-16 07:00:28

由于似乎没有任何现有的解决方案,我实现了一个非常简单的 HTTP 域,我可以用它来标记 API 方法:

import re

from docutils import nodes

from sphinx import addnodes
from sphinx.locale import l_, _
from sphinx.domains import Domain, ObjType
from sphinx.directives import ObjectDescription


http_method_sig_re = re.compile(r'^(GET|POST|PUT|DELETE)?\s?(\S+)(.*)

允许我标记这样的方法,并且它们的样式在视觉上会很好看:

.. http:method:: GET /api/foo/bar/:id/:slug

   :param id: An id
   :param slug: A slug

   Retrieve list of foobars matching given id.

它 这是我第一次尝试 Sphinx 和 Python,所以这应该被认为是非常基本的代码。如果有人有兴趣充实这个项目,请在 Github 上分叉这个项目

) class HTTPMethod(ObjectDescription): def handle_signature(self, sig, signode): m = http_method_sig_re.match(sig) if m is None: raise ValueError verb, url, query = m.groups() if verb is None: verb = 'GET' signode += addnodes.desc_addname(verb, verb) signode += addnodes.desc_name(url, url) if query: params = query.strip().split() for param in params: signode += addnodes.desc_optional(param, param) return url class HTTPDomain(Domain): """HTTP language domain.""" name = 'http' label = 'HTTP' object_types = { 'method': ObjType(l_('method'), 'method') } directives = { 'method': HTTPMethod } def setup(app): app.add_domain(HTTPDomain)

允许我标记这样的方法,并且它们的样式在视觉上会很好看:

它 这是我第一次尝试 Sphinx 和 Python,所以这应该被认为是非常基本的代码。如果有人有兴趣充实这个项目,请在 Github 上分叉这个项目

Since there doesn't appear to be any existing solution, I have implemented a very simple HTTP domain that I can use to mark up API methods:

import re

from docutils import nodes

from sphinx import addnodes
from sphinx.locale import l_, _
from sphinx.domains import Domain, ObjType
from sphinx.directives import ObjectDescription


http_method_sig_re = re.compile(r'^(GET|POST|PUT|DELETE)?\s?(\S+)(.*)

It allows me to mark up methods like this and they'll be styled somewhat visually nicely:

.. http:method:: GET /api/foo/bar/:id/:slug

   :param id: An id
   :param slug: A slug

   Retrieve list of foobars matching given id.

This was my first foray into both Sphinx and Python, so this should be considered very rudimentary code. If anybody is interested in fleshing this out, please fork this project on Github!

) class HTTPMethod(ObjectDescription): def handle_signature(self, sig, signode): m = http_method_sig_re.match(sig) if m is None: raise ValueError verb, url, query = m.groups() if verb is None: verb = 'GET' signode += addnodes.desc_addname(verb, verb) signode += addnodes.desc_name(url, url) if query: params = query.strip().split() for param in params: signode += addnodes.desc_optional(param, param) return url class HTTPDomain(Domain): """HTTP language domain.""" name = 'http' label = 'HTTP' object_types = { 'method': ObjType(l_('method'), 'method') } directives = { 'method': HTTPMethod } def setup(app): app.add_domain(HTTPDomain)

It allows me to mark up methods like this and they'll be styled somewhat visually nicely:

This was my first foray into both Sphinx and Python, so this should be considered very rudimentary code. If anybody is interested in fleshing this out, please fork this project on Github!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文