Source code highlighting in Drupal

It is nice to be able to blog about software. For instance, a few years ago I wrote an article about web development using C++. I still get a comment or two about it almost every week, despite the fact that not a single line of code has been written for the project, unfortunately. However, if I ever took the initiative and started working on it, I would, no doubt, write some interesting progress reports here as well. As things go, it would be very neat to show off some code in the process. By using the Geshi source code highligher/formatter, it is much easier with Drupal nowadays. Just install the module, install the library and off you go!

Don't forget to enable/disable additional languages in the settings of the module.

Here is a nice example from the open source C++ library boost, taken from here:

  1. class image;
  3. class animation
  4. {
  5. public:
  7.     void advance(int ms);
  8.     bool inactive() const;
  9.     void render(image & target) const;
  10. };
  12. std::vector<animation> anims;
  14. template<class C, class P> void erase_if(C & c, P pred)
  15. {
  16.     c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
  17. }
  19. void update(int ms)
  20. {
  21.     std::for_each(anims.begin(), anims.end(), boost::bind(&animation::advance, _1, ms));
  22.     erase_if(anims, boost::mem_fn(&animation::inactive));
  23. }
  25. void render(image & target)
  26. {
  27.     std::for_each(anims.begin(), anims.end(), boost::bind(&animation::render, _1, boost::ref(target)));
  28. }

Add new comment