Make code link/reference creation more orderly and rational

Migrated issue

Reported by: joachim

As I discovered a while back when working on the issue to link "module_invoke($module, 'hook')" code, the current system we have for making links has some limitations.

Briefly, it currently works by running the code through a bunch of regular expressions one by one. Each one uses preg_split() on the code. Each matched chunk is sent off to one callback that makes the link, each unmatched chunk is left as it.

The big drawback is that this can only link what it matches. We can do a bit of wizardry with regex lookbehinds, but as those can't be variable lengths that still limits us. We can only match "module_invoke($module, 'hook')" if we fix the "$module" part, so any module that uses another variable name there fails.

I don't think the preg_split approach is serving us well here. I don't think we need a strict divide-and-recurse approach here because we're not parsing code, we're just scanning it for links.

I think there's a better way of doing this. We let each regex run through the code and keep a list of the string offsets where it wants to act, the matched text, and the replacement text. Then we go through the original code in one go and make all the replacements from everything.

Here is a proof-of-concept patch that does a few neat tricks. It doesn't do everything and it doesn't make the links properly yet because api_link_name() is rather hard to work with.

Try it on these sample functions:

- node_menu() to see menu callbacks turned into links
- help_page() for non-standard forms of module_invoke()
- _theme_test_suggestion() for calls to theme() that pass an array of theme function names

Making this work would be a major refactoring, hence I'm posting this proof of concept to see what people think of it before taking it any further :)

Pre-migration files

Edited by drupalbot