Quote bookmarklet plugin

19 views
Skip to first unread message

Michael C. Harris

unread,
Jan 9, 2008, 11:59:07 PM1/9/08
to Habari Dev
I often quote web sites on my blog. In WP this was made easy with the
bookmarklet on post-new - just highlight some text and click the
bookmarklet and you've got a new formatted post with the quoted text.

Attached is a plugin that provides this functionality for Habari.

Issues:
* Currently only tested in Firefox. Any tips on how to do browser
capability testing so I can dynamically return an appropriate
bookmarklet?
* You need to have an actively logged in session or you get 'Whooops!'
* It's probably crap code. Suggestions for improvement are very
welcome.

Thanks to ringmaster and BigJibby who helped me with this.


--
Michael C. Harris, School of CS&IT, RMIT University
http://twofishcreative.com/michael/blog

publish_quote.plugin.php

Michael Harris

unread,
Jan 10, 2008, 12:06:55 AM1/10/08
to habar...@googlegroups.com
publish_quote.plugin.php

Michael C. Harris

unread,
Jan 10, 2008, 1:17:07 AM1/10/08
to habar...@googlegroups.com
Attached is a version that doesn't break your site when there _isn't_
a quote :)
publish_quote.plugin.php

Ali B.

unread,
Jan 10, 2008, 2:48:52 AM1/10/08
to habar...@googlegroups.com
Very handy Michael! Nice Job.

<?php

class PublishQuote extends Plugin {
 /**
   * Required Plugin Information
   **/
 function info() {
   return array(
     'name' => 'Publish Quote',
     'license' => 'Apache License 2.0',
     'url' => 'http://twofishcreative.com/publish_quote/',
     'author' => 'Michael C. Harris',
     'authorurl' => 'http://twofishcreative.com/michael/blog/',
     'version' => '0.1alpha',
     'description' => 'Allows users to post quotes with a bookmarklet',
     'copyright' => '2008'
   );
 }

 /**
   * Create the bookmarklet that is appropriate for the client's User Agent
   * @return array The array of actions to attach to the specified $plugin_id
   **/
 private function get_bookmarklet() {
   $admin_url= Site::get_url('admin');
   $link_name= Options::get('title');
   // TODO: Only handles Firefox. How do I detect browser capabilities?
   $bookmarklet= "<a href=\"javascript:location.href='{$admin_url}/publish?quote='+encodeURIComponent(document.selection?document.selection.createRange().text:document.getSelection())+'&url='+encodeURIComponent( location.href)+'&title='+encodeURIComponent(document.title);\">Quote on {$link_name}</a>";
   return $bookmarklet;
 }

 public function updated_config($ui) {
   return false;
 }

 /**
   * Add actions to the plugin page for this plugin
   * @param array $actions An array of actions that apply to this plugin
   * @param string $plugin_id The string id of a plugin, generated by the system
   * @return array The array of actions to attach to the specified $plugin_id
   **/
 public function filter_plugin_config($actions, $plugin_id) {
   if ( $plugin_id == $this->plugin_id() ) {
     $actions[]= 'Create Bookmarklet';
   }

   return $actions;
 }

 /**
   * Respond to the user selecting an action on the plugin page
   * @param string $plugin_id The string id of the acted-upon plugin
   * @param string $action The action string supplied via the filter_plugin_config hook
   **/
 public function action_plugin_ui($plugin_id, $action) {
   if ( $plugin_id == $this->plugin_id() ) {
     switch ( $action ) {
       case 'Create Bookmarklet' :
         echo "Bookmark this link: ";
         echo $this->get_bookmarklet();
         break;
     }
   }
 }

 public function filter_publish_controls($controls) {
   // Using filter_publish_controls because it's in a convenient spot
   // If quote has been sent in the URL, add a filter to insert text in the post
   $this->handler_vars= Controller::get_handler_vars();
   if ( array_key_exists('quote', $this->handler_vars) ) {
     Plugins::register( array( $this, 'add_quote_to_post'), 'filter', 'post_content', 1 );
   }
   // Return $controls unmolested
   return $controls;
 }

 public function add_quote_to_post($content, $post) {
   $quote= $this->handler_vars['quote'];
   $url= $this->handler_vars['url'];
   $title= $this->handler_vars['title'];
   // TODO: I'd like the user to be able to change this HTML without hacking the code
   $content= <<< QUOTE
<blockquote>$quote</blockquote>
<a href="$url" title="$title">$title</a>
QUOTE;
   return $content;
 }

 public function action_update_check() {
   Update::add( 'Publish Quote', 'ac3d6784-57c2-4dfa-9c24-3a09d0296a6f',  $this->info->version );
 }
}

?>




--
Ali B.

Michael C. Harris

unread,
Jan 16, 2008, 2:53:14 AM1/16/08
to habar...@googlegroups.com
I've now released the plugin to quote web sites properly.

http://www.twofishcreative.com/michael/blog/2008/01/16/quoting-web-pages-in-habari

cheers, Michael

Reply all
Reply to author
Forward
0 new messages