使用 YARD 进行 Ruby 文档的内部评论?

发布于 2024-12-26 13:04:25 字数 531 浏览 4 评论 0原文

我正在将我维护的 ruby​​gem 从 RDoc 切换到 YARD 文档。但是,代码中有一些关键注释只需要保留在代码中,而不应该出现在文档中。例如:

##
# SomeClass documentation here.
#--
# CRITICAL comment that should be in the code but not in the documentation,
#          and must be at this particular spot in the code.
#++
# more documentation that follows the critical comment block, but this part 
# should be in the generated documentation
class SomeClass
    ...
end

RDoc 支持 #--#++ 门,但 YARD 不支持。在 YARD 标记中执行类似操作的语法(如果存在)是什么?

I'm in the process of switching over a rubygem I maintain from RDoc to YARD documentation. However, there are some critical comments in the code that need to remain only in the code and should not show up in the documentation. For instance:

##
# SomeClass documentation here.
#--
# CRITICAL comment that should be in the code but not in the documentation,
#          and must be at this particular spot in the code.
#++
# more documentation that follows the critical comment block, but this part 
# should be in the generated documentation
class SomeClass
    ...
end

RDoc honors the #-- and #++ gates, but YARD does not. What (if it exists) is the syntax for doing an analogous thing in YARD's markup?

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

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

发布评论

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

评论(3

卸妝后依然美 2025-01-02 13:04:25

好吧,以最简单、快速和肮脏的形式,解决方案很容易 -
只需使用任何自定义(码未知)标签名称。例如:

##
# SomeClass documentation here.
#
# @internal_note CRITICAL
#   comment that should be in the code but not in the documentation,
#   and must be at this particular spot in the code.
#
# more documentation that follows the critical comment block, but this part 
# should be in the generated documentation

这里唯一的问题是yard会警告您每次出现@internal_note:

[warn]: Unknown tag @internal_note in file ... near line xxx
[warn]: Unknown tag @internal_note in file ... near line yyy
...

我真的认为应该有官方方法来抑制不需要的警告,但不幸的是我找不到它。不过,您可以尝试以下操作之一:

  1. yardoc -q # Problem: will subpurged有用的信息
  2. 您可以创建文件 yardinit.rb,其中包含以下内容:

    YARD::Tags::Library.define_tag('内部注释', :internal_note)
    

    然后生成文档

    yardoc -e './yardinit.rb'
    
  3. 生成文档

    有一个yard插件可以抑制所有未知标签警告https://github.com/rubyworks/yard-shutup

    它看起来不太活跃,gem installyard-shutup不起作用,但你可以手动安装它并尝试一下

Well, in its simplest, quick and dirty form, solution is easy -
just use any custom (unknown to yard) tag name. For example:

##
# SomeClass documentation here.
#
# @internal_note CRITICAL
#   comment that should be in the code but not in the documentation,
#   and must be at this particular spot in the code.
#
# more documentation that follows the critical comment block, but this part 
# should be in the generated documentation

The only issue here is that yard will warn you about each occurrence of @internal_note:

[warn]: Unknown tag @internal_note in file ... near line xxx
[warn]: Unknown tag @internal_note in file ... near line yyy
...

I really think there should be official way to suppress the undesirable warnings, but unfortunately I couldn't find it. Nevertheless, you may try one of the following:

  1. yardoc -q # problem: will suppress useful info too
  2. you may create file yardinit.rb, with the following content:

    YARD::Tags::Library.define_tag('INTERNAL NOTE', :internal_note)
    

    and then generate docs with

    yardoc -e './yardinit.rb'
    
  3. there is a yard plugin to suppress all unknown tag warning https://github.com/rubyworks/yard-shutup

    it doesn't look very alive, and gem install yard-shutup doesn't work, but you may install it by hand and give it a try

执笏见 2025-01-02 13:04:25

您可以编写

# @comment TODO: This will not be seen
def method(*args)
  ...
end

并在命令行上运行(或将其放入您的 .yardopts 中)

$ yard doc --tag comment --hide-tag comment

You can write

# @comment TODO: This will not be seen
def method(*args)
  ...
end

And run on the command line (or put it into your .yardopts)

$ yard doc --tag comment --hide-tag comment
空宴 2025-01-02 13:04:25

您可以使用标识在码注释中隐藏或转换为“注释”:

示例 1:

# Show Text1
# Show Text2
# Show Text3

结果:

Show Text1
Show Text2
Show Text3

示例 2:

# Show Text1
  # Show Text2
  # Show Text3

结果:

Show Text2
Show Text3

示例 3:

  # Show Text1
# Show Text2
# Show Text3

结果:

Show Text2
Show Text3

示例 4:

  # Show Text1
# Show Text2
  # Show Text3

结果:

Show Text3

示例 5:

# Show Text2
  # Show Text1
# Show Text3

结果:

Show Text3

示例 6:

  # Show Text1
#
  # Show Text3

结果:

Show Text3

示例 7:

# Show Text2
  #
# Show Text3

结果:

Show Text3

You can use identation to hide or convert to "comment" in yard comment:

Example 1:

# Show Text1
# Show Text2
# Show Text3

Result:

Show Text1
Show Text2
Show Text3

Example 2:

# Show Text1
  # Show Text2
  # Show Text3

Result:

Show Text2
Show Text3

Example 3:

  # Show Text1
# Show Text2
# Show Text3

Result:

Show Text2
Show Text3

Example 4:

  # Show Text1
# Show Text2
  # Show Text3

Result:

Show Text3

Example 5:

# Show Text2
  # Show Text1
# Show Text3

Result:

Show Text3

Example 6:

  # Show Text1
#
  # Show Text3

Result:

Show Text3

Example 7:

# Show Text2
  #
# Show Text3

Result:

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