Easy Amazon Links
If you're lazy like me, you'd like to link to Amazon items and put your associate ID in there, but who remembers that URL format? With publishing systems like Movable Type, you shouldn't have to remember, so let's make MT do the work for us.
Ingredients:
- 1 MT-Macro plugin
- 1 MT-IfEmpty plugin
- 1 MT-Regex plugin
Technique:
Define a pattern to match the Amazon URL (today they use the www.amazon.com/exec/obidos/tg/detail/-/ASINCODE/... So we use this to transform the regular Amazon link into an associate link:
<$MTSetVar name="amazon_id" value="your_amazon_associate_id"$>
<MTRegexDefine
name="amazon_fixup1">s
{^http://(www\.)?amazon\.com/exec/obidos/tg/detail/-/([a-zA-Z0-9]+)/.+$}
{http://www.amazon.com/exec/obidos/ASIN/$2/<$MTGetVar name="amazon_id"$>/ref=nosim/}
</MTRegexDefine>
<MTRegexDefine
name="amazon_fixup2">s
{^http://(www\.)?amazon\.com/exec/obidos/ASIN/([a-zA-Z0-9]+)/.+$}
{http://www.amazon.com/exec/obidos/ASIN/$2/<$MTGetVar name="amazon_id"$>/ref=nosim/}
</MTRegexDefine>
The next thing we need to do is create a macro that processes the 'a' tags in your entries, looking for hrefs that match that Amazon URL pattern:
<MTMacroDefine ctag="a" name="body_amazonfixup">
<MTIfMatches
expr="[MTMacroAttr name='href']"
pattern="m|^http://(www\.)?amazon\.com/exec/obidos/.*|">
<MTMacroAttr
name="href"
value="[MTMacroAttr name='href' regex='m/^amazon_fixup.*/']">
</MTIfMatches>
<MTMacroTag rebuild="1"><MTMacroContent></a>
</MTMacroDefine>
(Note that for each of these examples, you should remove any extra spaces inbetween the 'MT' tags so that you don't get a lot of unwanted gaps in your HTML.)
In order to run this on your entries, just add the above markup to your index and/or archive templates -- wherever you want them to function. A better idea would be to put all that into a custom template module and use the <MTInclude> tag to pull it in where it's needed.
Finally, use the <MTMacroApply> tag around your entry body and entry more tags so that they are processed:
<MTMacroApply macro="body_amazonfixup"> <MTEntryBody> <MTEntryMore> </MTMacroApply>
That should do it. Maybe it'll bump your Amazon referrals up a bit.
(For purposes of full disclosure, I have a meager investment in Amazon.com.)
Brad, for some reason your <pre> style doesn't work in IE5/Mac. Nothing between <pre> and </pre> shows up at all... just FYI.
Well, it appears to have been the 'overflow: auto' rule. It keeps the content of my 'pre' blocks from spilling out of the content div and worked great on everything I tried it on, but I guess I didn't check IE5/Mac. Sigh. Maybe I should automate the 'pre' tag to produce textarea blocks...
More info on this bug and others is available here.
I have tried to use this, but I get a rebuild error:
Build error in template 'Main Index': Error in tag: Error in tag: Error building macro tag a: Error in tag: Error compiling pattern: m|^http://www\.)?amazon\.com/exec/obidos/tg/detail/-/[a-zA-Z0-9]+/|
I have tried to use this, but I get a rebuild error:
Build error in template 'Main Index': Error in tag: Error in tag: Error building macro tag a: Error in tag: Error compiling pattern: m|^http://www\.)?amazon\.com/exec/obidos/tg/detail/-/[a-zA-Z0-9]+/|
This doesn't seem to work. No compilation errors, but nothing happens to my links. They stay the way they are.
P.S. could the fact that I use textile affect this in any way?