[In this reprinted #altdevblogaday in-depth piece, BitSquid co-founder Niklas Frykholm shares his company's documentation system, and explains why the team created its own market system.]
In a previous article, I talked a bit about our documentation system. It has now solidified into something interesting enough to be worth sharing.
The system consists of a collection of Ruby files that read input files (with extension .bsdoc) written in a simple markup language:
and converts them to HTML:
We then use the HTML Help Compiler to convert the help files to .chm.
You can find the repository at:
Motivation
Why have we created our own markup system instead of just using an existing one? (Markdown, Textile, RDoc, POD, Restructured Text, Doxygen, BBDoc, Wikimedia, Docbook, etc.)
For two reasons. First, none of these existing systems work exactly the way that we want.
An example. A large part of our documentation consists of Lua interface documentation. To make that as easy to possible to write, we use a special tag @api to enter an API documentation mode. In that mode, each unindented line documents a new Lua function. The indented lines that follow contain the documentation for the function.
The documentation system recognizes the Lua function definitions and formats them appropriately. It also creates index entries for the functions in the .chm file. In addition, it can create cross-references between classes and functions (with the %r marker).
No out-of-the-box system can provide the same level of convenience.
In any documentation system, the documentation files are the most valuable resource. What really matters is that documentation is easy to write and easy to modify. In particular, my main concerns are:
The generator will add tags as appropriate to ensure that the line is printed in the right context:
When several lines are printed, the generator only opens and closes the minimum number of tags that are necessary to give each line the right context. It does this by matching the list of contexts for neighboring lines:
This:
ends up as:
Note the trick of writing nil to explicitly close a scope.
Since I really, really hate badly formatted HTML documents, I've made sure that the output from the generator looks (almost) as good as hand-written HTML.
Using contexts in this way gets rid of a lot of the complexities of HTML generation. When we write our rules we don't have to think about opening and closing tags, we just have to make sure that we use an appropriate context for each line.
Nested scopes
The final idea is to automatically handle nested markup by applying the rules recursively. Consider this input document:
I don't have any special parsing rules for dealing with nested lists. Instead, the first line of this document creates a scope with the context %w(ul li). That scope is applied to all indented lines that follow it. The system strips the indentation from the line, processes it using the normal rule set, and then prepends %w(ul li) to its context. When it reaches a line without indentation, it drops the scope. Scopes can be stacked for multiple levels of nesting.
This way we can deal with arbitrarily complex nested structures (a code sample in a list in a blockquote) without any special processing rules.
[This piece was reprinted from #AltDevBlogADay, a shared blog initiative started by @mike_acton devoted to giving game developers of all disciplines a place to motivate each other to write regularly about their personal game development passions.]
# Header Some text. * And * A list
<h1>Header</h1> <p>Some text.</p> <ul> <li><p>And</p></li> <li><p>A list</p></li> </ul>
## Application (singleton) Interface to access global application functionality. Note that since the application is a singleton (there is only one application), you don't need to pass any %r Application object to the application functions. All the functions operate on the application singleton. @api resolution() : width, height Returns the screen resolution. argv() : arg1, arg2, arg3, ... Returns the command line arguments supplied to the application.
- Preserving semantic information.
- Avoiding unnecessary markup and clutter.
- paragraph_parser.rb
- Parses the paragraphs of a document into block-level HTML code.
- span_parser.rb
- Does span-level parsing inside a HTML block.
- generator.rb
- Generates the output HTML.
- toc.rb
- Adds section numbering and a table of contents to an HTML file.
env.write(%w(ul li p), "Hi!")
<ul><li><p>Hi!</p></li></ul>
env.write(%w(ul li p), "First item!") env.write(%w(ul li p), "First paragraph!") env.write(%w(ul li), nil) env.write(%w(ul li p), "First item, second paragraph!") env.write(%w(ul), nil) env.write(%w(ul li p), "Second item!")
<ul> <li> <p> First item! First paragraph! <p> <p>First item, second paragraph!</p> </li> <li><p>Second item!</p></li> </ul>
* Caught in the jungle * By a bear * By a lion * By something else * Caught in the woods