Joomla plugin to display github code into articles

731 Views Asked by At

Is there any plugin available for joomla 2.5 to embed code from github repository. For word press there is a plugin to achieve the same (http://wordpress.org/extend/plugins/github-code-viewer-2/). I wanted to do include code from my github repository into my joomla/k2 articles using something like

{github url='https://github.com/jamescarr/spring-integration/blob/master/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractFileListFilter.java'} 

By looking at the wp plugin I thought of writing my own joomla plugin but WP plugin is using *wp_remote_fopen* function and i did not find the same kind of function in joomla and read some articles about vulnerabilities using such remote_open functions. Here is what WP plugin is doing

function getGitHubFile($url, $ttl = null){
        self::__loadCache($url, $ttl);

        if (isset(self::$cache[$url])) {
            $code = self::$cache[$url];
        } else {
            $code = wp_remote_fopen($url . '?raw=true');
            if ($code == '') {
                return 'You need cURL installed to use GitHub_Code_Viewer';
            }
            $code = str_replace('<', '&lt;', $code);
            self::__setCache($url, $code);
        }

        return $code;
    }  
2

There are 2 best solutions below

0
Jean-Marie Favre On

You might want to have a try with http://darcyclarke.me/dev/repojs/ I've managed to include it in a joomla article (by including the javascript code directly in the page) so that one can browse a github repository from within a joomla article.

0
Stuart Cardall On

EDIT: I've fixed the problems mentioned below & published a new plugin on github for Joomla 2.5 & 3.0 - the plugin author should be updating JED soon.


There is a Github Repo plugin for Joomla 2.5 which uses the repojs mentioned by @Jean-Marie Favre.

To get this working on Joomla 3 you need to edit githubrepo.php & change:

   if ( version_compare( JVERSION, '3.0', '<' ) == 1) { 
        if($jquery){
                $document->addScript('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
        }
   } else {
        JHtml::_('jquery.framework');
   }

To just JHtml::_('jquery.framework');

Depending on your server setup you may also see the following errors in firebug:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://raw.github.com/darcyclarke/Repo.js/master/fonts/repo.woff.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://raw.github.com/darcyclarke/Repo.js/master/fonts/repo.ttf.

Enabling Cross Domain Requests did not work for me so I fixed these by uploading all the fonts in repo.js to my webserver & editing the 4 paths in repo.js.