views:

68

answers:

4

It's possible to interleave a C++ source file with an HTML document:

//<![IGNORE[
/*]]>

<html>
<head>
<title>Example</title>
</head>
<body>
<p>Example</p>
</body>
</html>

<![IGNORE[*/

#include <iostream>

int main(int argc, char** argv) {

    std::cout << "Hello, world!\n";

}

/*]]>
<![IGNORE[*/
//]]>

This leaves a stray // at the start of the HTML document, which can be covered up by wrapping the body content in a <div>:

...
<body>
<div id="main">
</div>
</body>
...

Which is styled to fill the content area with an opaque background:

#main {
    background: #ffffff;
          left: 0;
      position: absolute;
           top: 0;
}

Is it a good idea to document code this way? It's more flexibile than, say, Doxygen, but still allows quite structured formats to be used, such as DocBook. It also has the advantage that the source file not only contains the documentation, but is the documentation. Renaming or linking the source file to a filename ending in .htm is enough to make a Web browser read it as HTML.

The only problem is that stray C++ comment at the start of the XML document, which (correct me if I'm wrong) ought to prevent it from validating. Is there any workaround for that?

Edit: Changed to use <![IGNORE[...]]> instead of <!--...--> to avoid clashes with --.

+1  A: 

This requires a lot of boilerplate (and, in my opinion, is really, really ugly).

I'd think it would be better to use a tool designed for this purpose, like Doxygen. The less you have to type the better, right?

James McNellis
Oh, it's hideous! But some ugly things are known to be useful. Just curious what the general reaction would be.
Jon Purdy
+2  A: 

This is a bad idea. When ever you write -- in your code it ends the comment.

You also won't be able to use the --&gt; operator. ;-)

Mark Byers
Very good point. Perhaps `<![IGNORE[...]]>` would be better?
Jon Purdy
+1  A: 

I also think this is really ugly.

Your source files should remain source files. How are you going to name them ? .cpp, .html ? What will they be ? And why one over the other ?

Doxygen is really flexible, probably more than you seem to believe.

ereOn
The source files should remain named `.cpp` and `.h`. On Unix and Linux, at least, it's trivial to make a hardlink or symlink named `main.htm` pointing to `main.cpp`, which will work exactly as expected. I do know that Doxygen is really, very, terribly flexible and useful, and I use it for all of my projects. I just stumbled across this and thought it interesting.
Jon Purdy
+3  A: 

Mixing languages (C++ and HTML in this case) is never a good thing, and should be minimized whenever possible. It clutters the real code and makes it less readable (quite a lot, in fact). Unless it’s not possible due to company policies, I’d question the use of inline documentation at all. Code should be written to be readable and easily understandable, standing on its own. As a developer, I never want to use a help file parallel to the code itself; that’s just tedious.

There are exceptions, of course, such as when you’re developing a closed source library. In those cases, the library users cannot read the code so they need a substitute. I would use something like doxygen, but only for the public API. Any internal code wouldn’t be documented that way; there shouldn’t be a need for it, or else it should be refactored.

I would never mix raw HTML and code. It’s just too ugly. If you think raw HTML would be easier for your use case, I’d suggest keeping it in a parallel file to the source code, e.g., Foo.cpp and Foo.html. That way, it won't interfer with reading the code, and it will still be easy to maintain.

cjberg
I am persuaded.
Jon Purdy