Quick CSS3 properties in TextMate
A TextMate/e-texteditor snippet for quickly adding browser-specific CSS properties.
else if link contains "/detail/-/" then -- http://www.amazon.com/exec/obidos/tg/detail/-/B00009R6UA/ref=pd_luc_mri?_encoding=UTF8&m=ATVPDKIKX0DER&v=glance set startLength to the offset of "/detail/-/" in link set startLength to startLength + 10 set endCutoff to startLength + 9 set asin to the text startLength thru endCutoff of link as text
public function set${1/./\u$0/}(\$$1) {
\$this->$1 = \$$1;
return true;
} // function set${1/./\u$0/}
${2:public function get${1/./\u$0/}() {
return \$this->$1;
\} // function get${1/./\u$0/}}
I have my snippet bound to "getset" (tab trigger) in textmate, so getset?body will yield the following:
public function setBody($body) {
$this->body = $body;
return true;
} // function setBody
public function getBody() {
return $this->body;
} // function getBody
I also have it configured so that tabbing a second time will highlight the entire getter method, in case you don't want it available for whatever reason (the code I'm writing has a big getAll-type method and individual getters would be highly redundant). Enjoy :)
Edit - sorry, something's screwing up the formatting here - I think it's being a little too aggressive with stripslashes() or something on display, as I see it properly in the editor window. See the attached screenshot of the snippet - keep an eye out for some extra backslashes escaping a few characters. Should be coming in a sec. Wordpress attachments hate me these days. Someday I'll do a proper reinstall and deal with all of the cruft.

EA, I'm writing to inform you that you have permanently lost me as a customer. This is due entirely to the DRM in Spore and your intent to put it in future products. By using this technology you belittle and disrespect me as a customer, and as such have lost all future business with me. I purchased a copy of Spore, which aside from being disappointingly shallow prevents me from using the product as I purchased it. There was no indication of this on the packaging whatsoever, and I would have returned the product as defective had I discovered this prior to opening the game (be thankful for Best Buy's abusive software return policies - they made you $50). Your decision to use DRM - especially a system so invasive as SecuROM - has provoked me to make this move. Not only have you lost all of my future business, but I will be telling everyone I know to avoid all of your products until DRM is no longer used on ANY of your products. My money will be going to developers that are willing to treat me like a customer, not a criminal. I don't expect you to rectify this in any way; the damage has already been done. I just wanted to reiterate what countless others have already said. Sincerely, A former customer.Goodbye, EA. You and your done-to-death rehashing franchises will not be missed.
set link to the clipboard
if link contains "amazon.com/" then
if link contains "/o/" then
set startLength to the offset of "/o/" in link
set startLength to startLength + 8
set endCutoff to startLength + 9
set asin to text startLength thru endCutoff of link as text
else if link contains "/dp/" then
set startLength to the offset of "/dp/" in link
set startLength to startLength + 4
set endCutoff to startLength + 9
set asin to the text startLength thru endCutoff of link as text
else if link contains "/gp/product/?ASIN=" then
set startLength to the offset of "/gp/product/?ASIN=" in link
set startLength to startLength + 18
set endCutoff to startLength + 9
set asin to the text startLength thru endCutoff of link as text
else if link contains "/gp/product/" and link does not contain "?ASIN=" then
set startLength to the offset of "/gp/product/" in link
set startLength to startLength + 12
set endCutoff to startLength + 9
set asin to the text startLength thru endCutoff of link as text
end if
--asin is now set
-- CHANGE YOUR AFFILIATE LINK(S) HERE BEFORE SAVING!
--
-- This can be easily modified to add in additional codes, or remove
-- the whole dialog thing and just set the variable directly if you have
-- only one link (which is probably the case)
-- If you've only got one code, remove from here down to...
display dialog "Which affiliate link should be used?" buttons {"Blog - firsblo0a-20", "OTHERSITE - SOMELINK-20"} default button 1
set the button_pressed to the button returned of the result
if the button_pressed is "Blog - firsblo0a-20" then
set affiliateCode to "firsblo0a-20"
else
set affiliateCode to "OTHERSITE-20"
end if
-- here - stop removing stuff! and just use: set affiliateCode to "yourcode-20"
set amazonLink to "http://www.amazon.com/gp/product/" & asin & "?tag=" & affiliateCode
set shortLink to (do shell script "curl http://is.gd/api.php?longurl=" & amazonLink)
set the clipboard to shortLink
display dialog "Your shortened URL is " & shortLink & " and has been copied to the clipboard." buttons {"Woot!"} default button 1
end if
Of course, you'll want to make sure you change your affiliate link(s) appropriately. Or don't, and let me reap the benefits :) Read the script comments on what to change and where. I know the approach to grabbing the ASIN is kind of gross, but it works.
Made available under an MIT License. Questions? Hit the comments section and I'll answer as best I can.
And yes, you could swap it out easily enough to any of the other myriad url shortening tools. is.gd is probably the shortest out there, and since the API returns nothing other than the link itself (no XML markup, etc), it's also as simple to work with as computationally possible. If you don't care about affiliate links and just want to be able to send out tweets 1% faster, then just take the first line, and the fourth- and third-to-last, and tweak accordingly (it should be obviously enough). You know, for when a bookmarklet is too much work.
Just browse to any Amazon item, copy the URL to the clipboard, and run the script. Goes awesome with Quicksilver.
Now go sell stuff.<div id="someid" class="class1 class2">
content
</div><!-- #someid.class1.class2 -->
I'm not exactly the great RegEx expert that's required to figure out how to make that kind of thing come automagically from the standard TextMate snippet for HTML divs, but quite by accident I found that the h1 snippet does something along those lines, in that it converts any spaces to underscores. Net result?
<div${1: id="${2:someID}"}${3: class="${4:someClass}"}>
${0:$TM_SELECTED_TEXT}
</div> <!-- #${2/[[:alpha:]]+|( )/(?1:#:\L$0)/g}.${4/[[:alpha:]]+|( )/(?1:.:\L$0)/g} -->
Yeah, gross. Just replace the snippet for div with that load of insanity in TextMate's bundle editor and you should be good to go. It's a tiny bit quirky in that it has to lead with the initial # and . for the id and class respectively, so if you don't have a class set on the div you'll still have a comment looking like <!-- #someid. --> instead of <!-- #someid --> (note the extra period), or vice-versa. Close enough for government work.
Obviously you could extend that same horrible regex out to other things where you want automatic comments - most block-level elements would be a decent choice. Note that there's two of them in there - grab the ${someNumber/} bit, and make sure that the number matches the right part of the snippet.
Have fun.function apply_xslt($simplexml, $path_to_xslt) {
$xslt_file = new DOMDocument;
$xslt_file->load($path_to_xslt);
$xml = new DOMDocument;
$xml->loadXML($simplexml->asXML());
$transform = new XSLTProcessor();
$transform->importStyleSheet($xslt_file);
return $transform->transformToXML($xml);
}
Now all you have to do is pass the function a SimpleXML object (see my post on XMLifying DB results) and get on with your life.
$db_results = new SQL_as_XML;
echo apply_xslt($db_results->get_list_of_content_by_author_permalink('eric-stern','1'), 'some_stylesheet.xslt');
And just for reference on the SQL_as_XML class:
class SQL_as_XML {
function get_content_by_permalink($content_permalink, $page = 0) {
$content_permalink = clean($content_permalink);//sanitize input
if (!is_numeric($page)) {
$page = 1; //$page also sanitized }
$query = "SELECT content.publish_date,
content.last_edit_date,
content.title,
content.number_of_pages,
content.permalink AS content_permalink,
authors.display_name,
authors.permalink AS author_permalink,
content_pages.body
FROM content
JOIN authors ON content.author_id = authors.id
JOIN content_pages ON content_pages.content_id = content.id
WHERE content.permalink = '$content_permalink'
AND content_pages.page_number = $page";
return mysql_to_xml(mysql_query($query), 'response', 'content');
}
//other functions structured similarly
}So there you go.
function mysql_to_xml($mysql_result, $root_node_name, $child_node_name) { $xml = new SimpleXMLElement('< ?xml version="1.0" encoding="UTF-8"?>< ' . $root_node_name . '>');
while ($row = mysql_fetch_assoc($mysql_result)) { $newrow = $xml->addChild($child_node_name); foreach ($row as $key => $value ) { $newrow->addChild($key, $value); } }
Updated... Wordpress really made a mess of the first one, and I did an even worse job fixing it.return $xml; }
function mysql_to_xml($mysql_result, $root_node_name = 'result', $child_node_name = 'row') {
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><' . $root_node_name . '></' . $root_node_name . '>');
while ($row = mysql_fetch_assoc($mysql_result)) {
$newrow = $xml->addChild($child_node_name);
foreach ($row as $key => $value ) {
$newrow->addChild($key, $value);
}
}
return $xml;
}
In my SQL functions, I'm able to return an XML object quite easily:
return mysql_to_xml(mysql_query($query), 'response', 'content');
And to get it in the first place:
$sql = new SQL_as_XML;
$xml = $sql->get_content_by_permalink($permalink, $page);
(the SQL_as_XML class is a series of functions that build and run a query, then return the XML using the previous snippet)
Finally, if you want to output the raw XML (after perhaps finagling it into an RSS feed):
echo $xml->asXML();
Questions? Ask. Improvements? Tell. And yes, I already know that I really should set default values for $root_node_name and $child_node_name in the original function. I'll get there.
And yes, you'll have to excuse the blog making a mess of the formatting. :(