Search code examples
rubyshellyard

How to get Yardoc to output to STDOUT?


I've looked for templates that come with the gem (I can only find the default HTML outputter), I've searched the help and online docs for a switch that will redirect to STDOUT. I can't find anything on how I might approach this.

Is there an easy way to do this (perhaps a shell command?) or do I have wade through the source code?


Solution

    • Create the following file as test.rb:

      # This class is cool
      class Test
        # This method is also cool
        def foo
        end
      end
      
    • You must compile the yard documentation before you can output this. But you don't want the doc folder right? So let's omit that.

      yardoc --no-output test.rb
      

      This will only update the documentation inside .yardoc folder which is hidden. The doc folder will not be generated.

    • Now you can see the documentation for specific objects in a few ways:

      > yard display "Test"
      # Outputs:
      ------------------------------------------ Class: Foo < Object
      
          This class is cool
      
      
      
      --------------------------------------------------------------
      
      > yard display "Test#foo"
      # Outputs:
      ------------------------------------------ Method: #meth (Foo)
                                                (Defined in: foo.rb)
      
          foo.meth -> Object
      --------------------------------------------------------------
      
          This method is cool
      

      This is the output that you get. You can get the yard documentation for any class/method using the yard display command.

    • But hey, the output sucks! Let's create a template for it. Create the following templates folder with a few files:

      + your app
        + templates
          + default
            + class
            | + text
            |   + setup.rb
            |       def init
            |         super
            |         sections T('docstring')
            |       end
            + method
            | + text
            |   + setup.rb
            |       def init
            |         super
            |         sections T('docstring')
            |       end
            + method_details
            | + text
            |   + setup.rb
            |       def init
            |         super
            |         sections T('docstring')
            |       end
      

      All the setup.rb files must have the same content like def init, super, sections T('docstring'), end. This will make the text output display only the documentation, without any fancy headers and stuff.

    • Now just run the same yard display command, but let's use our custom templates:

      > yard display Foo --template-path ./templates
      
      This class is cool
      
      > yard display "Foo#meth" --template-path ./templates
      
      This method is cool
      
    • That's it. You may find that the yard doc may have too much of leading/trailing new lines in the output, you can use some other standard linux head/tail commands to fix that.