官术网_书友最值得收藏!

Documenting Dart code with dartdoc

Documentation is a critical part of software development, and to encourage good documentation of packages, Dart has the tool dartdoc (https://github.com/dart-lang/dartdoc/), which creates static HTML documentation based on specially formatted code comments. Previous documentation tools for Dart were part of the SDK, while dartdoc is a separate project developed by the Dart team.

It can be installed using pub at the command line:

pub global activate dartdoc

We will take a look at a Dart package project to explore the features of dartdoc. Open the project quakerecord in the WebStorm Editor, and take a look at the file quakerecord.dart:

library quakerecord;
export 'src/quakerecord_base.dart';

This package exports a single file, quakerecord_base.dart, and in this file, the class Processor is declared:

/// A lightweight object to process a raw earthquake feature.
///
/// * Must have valid JSON.
/// * Must be less than 128MB.
///
class Processor {

  // Store for the JSON.
  String _feature;

  /// The state of processing
  bool get processed => true;

  /// The default constructor.
  ///     Processor processor;
  ///     processor = new Processor('{}');
  Processor(String feature) {}

  /// Returns a converted object based on the feature passed into
  /// the constructor [Processor].
  object convert() {
    // A normal comment.
    return new Object();
  }

  /// Changes feature based on partial data.
  /// *BETA QUALITY*
  ///
  /// 1. Estimates end points.
  /// 2. Transforms polarity.
  ///
  void experimentalNewMethod() {}
}

Note the use of the /// triple slash comment style—this is what dartdoc is looking for. The class declaration comments contain an unordered list *, the constructor has a section of sample code, the convert method has a link [] declared to the constructor, and experimentalNewMethod has a numbered list and formatting.

Tip

For full details of Dart comments and documentation guidelines, see:

https://www.dartlang.org/articles/doc-comment-guidelines/

There is also an additional class called ProcessHelper in recordhelper.dart:

/// A helper class to deal with quake feature settings.
class ProcessHelper {

  ///Swaps the data structure.
  void reversePolar() {}
}

To transform the comments into a web page, run dartdoc on the folder containing pubspec.yaml:

 ~/quakerecord/$ dartdoc
Generating documentation for 'quakerecord' into quakerecord/doc/api/
parsing lib/quakerecord.dart...
Parsed 1 file in 15.6 seconds.
generating docs for library quakerecord from quakerecord.dart...
Documented 1 library in 20.0 seconds.
Success! Open quakerecord/doc/api/index.html

A doc sub-folder is created in the project containing a folder named api that holds the documentation. Open the page in your favorite web browser and take a look at the output for the Processor class. Let's have a look at the following screenshot:

Documenting Dart code with dartdoc

Finally, you may be wondering where the class ProcessHelper from recordhelper.dart is located on the generated page. It is not part of the documentation as it is not exported from the package, and because of this, is considered to be a private implementation detail that the consumer of the package does not need to know about.

主站蜘蛛池模板: 江北区| 宁津县| 庆安县| 万年县| 云南省| 拉孜县| 蓬莱市| 安泽县| 海晏县| 涟源市| 宁武县| 突泉县| 八宿县| 永泰县| 伊通| 广东省| 商洛市| 铜陵市| 西充县| 凌源市| 北碚区| 昔阳县| 安溪县| 常德市| 个旧市| 阿克苏市| 建宁县| 昭平县| 临武县| 九龙坡区| 巴彦淖尔市| 宜城市| 通山县| 那坡县| 连城县| 襄垣县| 保靖县| 公主岭市| 随州市| 华安县| 买车|