Perl script to automatically format docblock text

Events happening in the community are now at Drupal community events on www.drupal.org.
joachim's picture

One of the things that I find really fiddly with writing documentation is making sure lines wrap to 80 characters. I find it really breaks my flow of concentration to be always watching for when to hit return or even go back when a word turns out to be too long and then type a space-star-space (how on earth people did it in the days of manual typewriters... I guess we're just spoilt with technology nowadays!).

So with that in mind, I've written a perl script that takes plain text input with lines just as you'd write them naturally (ie as long as you like) and formats it ready for pasting into a function docblock.

Here's an example, with this input:

Determines whether the current user is in compact mode.

Compact mode shows certain administration pages with less description text, such as the configuration page and the permissions page.

Whether the user is in compact mode is determined by a cookie, which is set for the user by system_admin_compact_page().

If the user does not have the cookie, the default value is given by the system variable 'admin_compact_mode', which itself defaults to FALSE. This does not have a user interface to set it: it is a hidden variable which can be set in the settings.php file.

And the output is:

* Determines whether the current user is in compact mode.
*
* Compact mode shows certain administration pages with less description text,
* such as the configuration page and the permissions page.
*
* Whether the user is in compact mode is determined by a cookie, which is set
* for the user by system_admin_compact_page().
*
* If the user does not have the cookie, the default value is given by the
* system variable 'admin_compact_mode', which itself defaults to FALSE. This
* does not have a user interface to set it: it is a hidden variable which can
* be set in the settings.php file.

Code is available on Github: https://github.com/joachim-n/Docblock

Next step would be to allow it to take text that's already formatted as docblock as input and reflow it.

Hope other people find it useful! :D

Comments

Many editors do this...

jhodgdon's picture

I'm not sure which text editor you use, but Emacs will do this for me. I just have to set the fill-column variable to 80, and then I can use the Meta-Q command to re-format a block of comments correctly.

Probably vim has a similar command, but I wouldn't know...

Documentation

Group categories

Event type

Post type

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week