Howto: Writing own macro, a guide on example "ImageMap"

Introduction

Scanning through the MoinMoin page, I realized that there is plenty of documentation on almost any level of detail. But I have not found example-driven coding introductions for newbies.

If I am wrong, feel free to list such docs here! Or tell me where to move/link this document...

All those gurus seem to think that Python is rather easy to learn and self-explanatory, and so should be all written code, too. I agree with the Python one, but with every larger programming project, the latter becomes more and more false. At least without proper introduction.

But the gurus cannot experience this fact by themselves because they have grown into the project and even with growing project size they still know the deeper dependencies. Even with the best of will, they also can't see the real probelms that beginners encounter. So they are willing to patiently answer the many reasonable questions that arise practically. But this tends to grow to splattered piecemeal hints on this and that aspect.

Sure - everyone beginning coding on MoinMoin has a unique story, but I think there will be much in common. Hopefully this document helps on such common introduction difficulties.

With some support of the many others, this document can be integrated into already existing/to be written pages.

For whom is this?

Then read on...

Prior steps

This Howto will guide you through the steps to get your own macro working. Several versions, each with improved code quality and functionality. After you got through, you should be able to understand and write your own extensions.

First let's assume you have read the MoinDev pages (about MoinDev/PluginConcept, MoinDev/CodeStructure etc...) Maybe you have not understood all in detail, but you have got some basic grasp. But you do not just wish to read on forever - you want to get on, step by step...

So you already know you should put your own macro in .../share/mywikiinstance/data/plugin/macro/. You have also tried your first own test macro Helloworld.py (Well, if you have not done it yet, why not try it NOW? :) ), which you did by stealing from http://moinmoin.wikiwikiweb.de/HelpOnMacros. And it seemed so easy!

If you dare, you also could look into the MoinMoin CVS repository yourself (where all the core developers put their newest magic MoinMoin code), e.g.: MoinMoin/wikiaction.py.

But slowly you feel: you should enter the scene with some own stuff... and learn online while doing...

O.K. I think we simply can go on with these...

The guided example: macro ImageMap

Well, I have something to code: An ImageMap inlined into pages. And some parameters to allow for some flexible areas. Here are the details: ./ImageMap

This ImageMap macro shall be about displaying attachments as images and overlay it with a linked map. This feature is well-known from HTML...

For this job I have to review some sources of the MoinMoin distribution .../lib/pythonX.Y/site-packages/MoinMoin/

Let's look in file "wikiaction.py": What I learn from the code comments:

It's always good to look for existing macros/code, to start with (as a first codebase). So I find macro/Action.py and action/AttachFile.py. There I can find some more hints on the internals.

You also may look into my review of the code - here you find my comments to better understand the code... later...

O.K. from reading MoinDev I know that I better try to use the "formatter" to generate the needed HTML output instead of hacking my own HTML tags straight into my code. So lets keep this in mind: this shall be my long term coding goal. But first I definitely am lucky if my code does anyhow do some useful stuff, without deep understanding of all existing code.

Admitted: After some first steps, then I really should rewind myself and learn how to more intensively use the existing MoinMoin code, like formatter/base.py

Then I was told by a good MoinMoin soul AlexanderSchremmer, that attachment: is a "pseudo-macro", I should look into the parser. Huh?

Then I find in parser/wiki.py some hints: "class Parser: Object that turns Wiki markup into HTML".

O.K. after a quick overview I know: a "pseudo-macro" is macro stuff in pages that does not get executed as a macro but somewhere else. And as we saw: [[attachment:]] is an "action", that also can be inlined into pages.

More coming..

MoinMoin: AndreasHüwel/CodingOnMoinMoin (last edited 2007-10-29 19:18:07 by localhost)