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:
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.)