DEVELOPERS.rdoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. = Developer Introduction
  2. So you want to write a generator, fix a bug, or otherwise work with RDoc. This
  3. document provides an overview of how RDoc works from parsing options to
  4. generating output. Most of the documentation can be found in the specific
  5. classes for each feature.
  6. == Bugs
  7. If you think you found a bug, file a ticket on the {issues
  8. tracker}[https://github.com/rdoc/rdoc/issues] on github.
  9. To get your bug fixed as fast as possible please include a sample file that
  10. illustrates the problem or link to a repository and include steps to reproduce
  11. the issue. Here are some examples of good issues:
  12. * https://github.com/rdoc/rdoc/issues/55
  13. * https://github.com/rdoc/rdoc/issues/61
  14. == Plugins
  15. When 'rdoc/rdoc' is loaded RDoc looks for 'rdoc/discover' files in your
  16. installed gems. This can be used to load parsers, alternate generators, or
  17. additional preprocessor directives. An rdoc plugin layout should look
  18. something like this:
  19. lib/rdoc/discover.rb
  20. lib/my/rdoc/plugin.rb
  21. # etc.
  22. In your rdoc/discover.rb file you will want to wrap the loading of your plugin
  23. in an RDoc version check like this:
  24. begin
  25. gem 'rdoc', '~> 3'
  26. require 'my/rdoc/plugin'
  27. rescue Gem::LoadError
  28. end
  29. === Plugin Types
  30. In RDoc you can change the following behaviors:
  31. * Add a parser for a new file format
  32. * Add a new output generator
  33. * Add a new markup directive
  34. * Add a new type of documentation markup
  35. * Add a new type of formatter
  36. All of these are described below
  37. == Option Parsing
  38. Option parsing is handled by RDoc::Options. When you're writing a generator
  39. you can provide the user with extra options by providing a class method
  40. +setup_options+. The option parser will call this after your generator is
  41. loaded. See RDoc::Generator for details.
  42. == File Parsing
  43. After options are parsed, RDoc parses files from the files and directories in
  44. ARGV. RDoc compares the filename against what each parser claims it can parse
  45. via RDoc::Parser#parse_files_matching. For example, RDoc::Parser::C can parse
  46. C files, C headers, C++ files, C++ headers and yacc grammars.
  47. Once a matching parser class is found it is instantiated and +scan+ is called.
  48. The parser needs to extract documentation from the file and add it to the RDoc
  49. document tree. Usually this involves starting at the root and adding a class
  50. or a module (RDoc::TopLevel#add_class and RDoc::TopLevel#add_module) and
  51. proceeding to add classes, modules and methods to each nested item.
  52. When the parsers are finished the document tree is cleaned up to remove
  53. dangling references to aliases and includes that were not found (and may exist
  54. in a separate library) through RDoc::ClassModule#complete.
  55. To write your own parser for a new file format see RDoc::Parser.
  56. === Documentation Tree
  57. The parsers build a documentation tree that is composed of RDoc::CodeObject and
  58. its subclasses. There are various methods to walk the tree to extract
  59. information, see RDoc::Context and its subclasses.
  60. Within a class or module, attributes, methods and constants are divided into
  61. sections. The section represents a functional grouping of parts of the class.
  62. TomDoc uses the sections "Public", "Internal" and "Deprecated". The sections
  63. can be enumerated using RDoc::Context#each_section.
  64. == Output Generation
  65. An RDoc generator turns the documentation tree into some other kind of output.
  66. RDoc comes with an HTML generator (RDoc::Generator::Darkfish) and an RI
  67. database generator (RDoc::Generator::RI). The output a generator creates does
  68. not have to be human-readable.
  69. To create your own generator see RDoc::Generator.
  70. === Comments
  71. In RDoc 3.10 and newer the comment on an RDoc::CodeObject is now an
  72. RDoc::Comment object instead of a String. This is to support various
  73. documentation markup formats like rdoc, TomDoc and rd. The comments are
  74. normalized to remove comment markers and remove indentation then parsed lazily
  75. via RDoc::Comment#document to create a generic markup tree that can be
  76. processed by a formatter.
  77. To add your own markup format see RDoc::Markup@Other+directives
  78. ==== Formatters
  79. To transform a comment into some form of output an RDoc::Markup::Formatter
  80. subclass is used like RDoc::Markup::ToHtml. A formatter is a visitor that
  81. walks a parsed comment tree (an RDoc::Markup::Document) of any format. To help
  82. write a formatter RDoc::Markup::FormatterTestCase exists for generic parsers,
  83. and RDoc::Markup::TextFormatterTestCase which contains extra test cases for
  84. text-type output (like +ri+ output).
  85. RDoc ships with formatters that will turn a comment into HTML, rdoc-markup-like
  86. text, ANSI or terminal backspace highlighted text, HTML, cross-referenced HTML,
  87. an HTML snippet free of most markup, an HTML label for use in id attributes, a
  88. table-of-contents page, and text with only code blocks.
  89. The output of the formatter does not need to be text or text-like.
  90. RDoc::Markup::ToLabel creates an HTML-safe label for use in an HTML id
  91. attribute. A formatter could count the number of words and the average word
  92. length for a comment, for example.
  93. ==== Directives
  94. For comments in markup you can add new directives (:nodoc: is a directive).
  95. Directives may replace text or store it off for later use.
  96. See RDoc::Markup::PreProcess::register for details.
  97. === JSONIndex
  98. RDoc contains a special generator, RDoc::Generator::JSONIndex, which creates a
  99. JSON-based search index and includes a search engine for use with HTML output.
  100. This generator can be used to add searching to any HTML output and is designed
  101. to be called from inside an HTML generator.