Browse Source

first functional version (text only) of the lightweight social buttons

Fernando Ribeiro 9 years ago
parent
commit
edc4742d0c
1 changed files with 81 additions and 5 deletions
  1. 81 5
      syntax/button.php

+ 81 - 5
syntax/button.php

@@ -14,19 +14,19 @@ class syntax_plugin_lsb_button extends DokuWiki_Syntax_Plugin {
      * @return string Syntax mode type
      */
     public function getType() {
-        return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
+        return 'substition';
     }
     /**
      * @return string Paragraph type
      */
     public function getPType() {
-        return 'FIXME: normal|block|stack';
+        return 'normal';
     }
     /**
      * @return int Sort order - Low numbers go before high numbers
      */
     public function getSort() {
-        return FIXME;
+        return 999;
     }
 
     /**
@@ -35,7 +35,7 @@ class syntax_plugin_lsb_button extends DokuWiki_Syntax_Plugin {
      * @param string $mode Parser mode
      */
     public function connectTo($mode) {
-        $this->Lexer->addSpecialPattern('<FIXME>',$mode,'plugin_lsb_button');
+        $this->Lexer->addSpecialPattern('~~LSB\b.*?~~',$mode,'plugin_lsb_button');
 //        $this->Lexer->addEntryPattern('<FIXME>',$mode,'plugin_lsb_button');
     }
 
@@ -53,8 +53,13 @@ class syntax_plugin_lsb_button extends DokuWiki_Syntax_Plugin {
      * @return array Data for the renderer
      */
     public function handle($match, $state, $pos, Doku_Handler &$handler){
-        $data = array();
+        $match = trim(substr($match, 5, -2)); // strip markup
 
+        if (!empty($match)) {
+            $data = explode(' ', $match);
+        } else {
+            $data = array('twitter', 'facebook', 'google+', 'linkedin', 'pinterest', 'tumblr', 'reddit');
+        }
         return $data;
     }
 
@@ -67,10 +72,81 @@ class syntax_plugin_lsb_button extends DokuWiki_Syntax_Plugin {
      * @return bool If rendering was successful.
      */
     public function render($mode, Doku_Renderer &$renderer, $data) {
+
         if($mode != 'xhtml') return false;
 
+        // valid list of available social networks
+        $protected = array('twitter', 'facebook', 'google+', 'linkedin', 'pinterest', 'tumblr', 'reddit');
+
+        $renderer->doc .= '<ul class="lsb">';
+
+        foreach ($data as $network) {
+            if (in_array($network, $protected)) {
+                $renderer->doc .= $this->lsb_button($network);
+            }
+        }
+        $renderer->doc .= '</ul>';
+
         return true;
     }
+
+    /**
+     * Render xhtml output for facebook share button
+     *
+     * @param string $network The social network to render the button to
+     * @return string Xhtml code for button.
+     */
+    protected function lsb_button ($network) {
+        global $ID;
+        global $INFO;
+
+        $xhtml = '';
+
+        $url      = rawurlencode(wl($ID,'',true));
+        $title    = hsc(($INFO['meta']['title']) ? $INFO['meta']['title'] : $meta);
+        $abstract = hsc($INFO['meta']['description']['abstract']);
+
+        $xhtml = '<li class="lsb-item">';
+
+        // see: https://github.com/cferdinandi/social-sharing
+        // <a href="https://twitter.com/intent/tweet?text=YOUR-TITLE&url=YOUR-URL&via=TWITTER-HANDLE">Tweet</a>
+        // <a href="https://www.facebook.com/sharer/sharer.php?u=YOUR-URL">Share on Facebook</a>
+        // <a href="https://plus.google.com/share?url=YOUR-URL">Plus on Google+</a>
+        // <a href="https://www.linkedin.com/shareArticle?mini=true&url=YOUR-URL&title=YOUR-TITLE&summary=YOUR-SUMMARY&source=YOUR-URL">Share on LinkedIn</a>
+        // <a href="https://pinterest.com/pin/create/button/?url=YOUR-URL&description=YOUR-DESCRIPTION&media=YOUR-IMAGE-SRC">Pin on Pinterest</a>
+        // <a href="https://vk.com/share.php?url=YOUR-URL&title=YOUR-TITLE&description=YOUR-DESCRIPTION&image=YOUR-IMAGE-SRC&noparse=true">Share on VK</a>
+        // <a href="https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url=YOUR-URL">Share on Xing</a>
+        // <a href="http://www.tumblr.com/share/link?url=YOUR-URL&description=YOUR-DESCRIPTION">Share on Tumblr</a>
+        // <a href="http://www.reddit.com/submit?url=YOUR_URL&title=YOUR_TITLE">Share on Reddit</a>
+
+        switch ($network) {
+            case 'twitter':
+                $xhtml .= '<a href="https://twitter.com/intent/tweet?text=' . $title . '&url='. $url .'&via=TWITTER-HANDLE">Tweet</a>';
+                break;
+            case 'facebook':
+                $xhtml .= '<a href="http://www.facebook.com/sharer.php?u='. $url .'">Share on Facebook</a>';
+                break;
+            case 'google+':
+                $xhtml .= '<a href="https://plus.facebook.com/share?url='. $url .'">Plus on Google+</a>';
+                break;
+            case 'linkedin':
+                $xhtml .= '<a href="https://www.linkedin.com/shareArticle?mini=true&url='. $url .'&title=' . $title . '&summary=' . $abstract . '&source=YOUR-URL">Share on LinkedIn</a>';
+                break;
+            case 'pinterest':
+                $xhtml .= '<a href="https://pinterest.com/pin/create/button/?url='. $url .'&description=' . $abstract . '&media=YOUR-IMAGE-SRC">Pin on Pinterest</a>';
+                break;
+            case 'tumblr':
+                $xhtml .= '<a href="http://www.tumblr.com/share/link?url='. $url .'&description=' . $abstract . '">Share on Tumblr</a>';
+                break;
+            case 'reddit':
+                $xhtml .= '<a href="http://www.reddit.com/submit?url='. $url .'&title=' . $title . '">Share on Reddit</a>';
+                break;
+        }
+
+        $xhtml .= '</li>';
+
+        return $xhtml;
+    }
 }
 
 // vim:ts=4:sw=4:et: