This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added:

* function get Data From github
* function to take only a few lines of entire page
Fixed:
* Class name
  • Loading branch information...
fititnt committed Jul 15, 2011
1 parent 9077323 commit b1e63d095e2d6cafb06295a1f786209507ffe457
Showing with 111 additions and 46 deletions.
  1. +83 −20 getgithubcode.php
  2. +14 −24 getgithubcode.xml
  3. +7 −1 language/en-GB/en-GB.plg_getgithubcode.ini
  4. +7 −1 language/en-GB/en-GB.plg_getgithubcode.sys.ini
View
@@ -13,51 +13,114 @@
*
* @since 1.6
*/
-class plgContentExample extends JPlugin
+class plgContentGetgithubcode extends JPlugin
{
/**
- * Example before display content method
+ * Example prepare content method
*
- * Method is called by the view and the results are imploded and displayed in a placeholder
+ * Method is called by the view
*
- * @param string The context for the content passed to the plugin.
- * @param object The content object. Note $article->text is also available
- * @param object The content params
- * @param int The 'page' number
- * @return string
+ * @param string The context of the content being passed to the plugin.
+ * @param object The content object. Note $article->text is also available
+ * @param object The content params
+ * @param int The 'page' number
* @since 1.6
*/
- public function onContentBeforeDisplay($context, &$article, &$params, $limitstart)
+ public function onContentPrepare($context, &$article, &$params, $limitstart)
{
$app = JFactory::getApplication();
// simple performance check to determine whether bot should process further
- if (strpos($article->text, 'github') === false) {
+ $tagname = $this->get('tagname', 'github');
+ if (strpos($article->text, $tagname) === false) {
return true;
}
// expression to search for
- $regex = '/{github\s+(.*?)}/i'; //@todo: change to real regex later
+ $regex = '/{'.$tagname.'\s+(.*?)}/i'; //@todo: change to real regex later
$matches = array();
// find all instances of plugin and put in $matches
preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
- // $match[0] is full pattern match, $match[1] is the position
- $output = $this->_getGithubRawCode($match[1]);
+ // $match[0] is full pattern match, $match[1] is the url
+ $code = $this->_getGithubCode($match[1]);
// We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
- $article->text = preg_replace("|$match[0]|", $output, $article->text, 1);
+ $article->text = preg_replace("|$match[0]|", $code, $article->text, 1);
}
-
-
-
- return '';
+ //echo _getUrlContents('https://raw.github.com/fititnt/plg_getgithubcode/master/getgithubcode.php');
+ //str_replace('https://raw.github.com/fititnt/plg_getgithubcode/master/getgithubcode.php', _getUrlContents('https://raw.github.com/fititnt/plg_getgithubcode/master/getgithubcode.php'), $article->text);
+ //echo 'teste';
+ //$article->text = 'ronaldo';
+ //var_dump($article); die();
+ return '';
}
- protected function _getGithubRawCode($url){
+
+ protected function _getGithubCode($url, $start = FALSE, $end = FALSE, $certificate = FALSE){
+ //Get Page
+ $page = $this->_getUrlContents($url, $certificate);
+ //Get only desired lines
+ if($start !== FALSE || $end !== FALSE){
+ $page = $this->_getStringLines($page, $start, $end);
+ }
+ //Clean up special chars
+ $github = htmlspecialchars($page);
+ //Get start and end tags and apply
+ $tagstart = $this->get('tagstart', '<pre>');
+ $tagstart = str_replace('&lt;', '<', $tagstart);
+ $tagstart = str_replace('&gt;', '>', $tagstart);
+ $tagsend = $this->get('tagend', '</pre>');
+ $tagsend = str_replace('&lt;', '<', $tagsend);
+ $tagsend = str_replace('&gt;', '>', $tagsend);
+ $github = $tagstart . $github . $tagsend;
- return $code;
+ return $github;
+ }
+
+
+ /*
+ * Return contents of url
+ * @author Emerson Rocha Luiz
+ * @var string $url
+ * @var string $certificate path to certificate if is https URL
+ * @return string
+ */
+ protected function _getUrlContents($url, $certificate = FALSE){
+ //$page = file_get_contents($url);
+ $ch = curl_init(); //Inicializar a sessao
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Retorne os dados em vez de imprimir em tela
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $certificate);//Check certificate if is SSL, default FALSE
+ curl_setopt($ch, CURLOPT_URL, $url);//Setar URL
+ $content = curl_exec($ch);//Execute
+ curl_close($ch);//Feche
+
+ return $content;
+ }
+
+ /* Return just lines betwen betwen start and end lines
+ * @author Emerson Rocha Luiz
+ * @var string $string: the string to edit
+ * @var int $start: initial line
+ * @var int $end: end line
+ * @return string
+ */
+ protected function _getStringLines($string, $start, $end){
+
+ $stringArray = explode("\n", $string);
+ $nLines = count($stringArray)-1;
+
+ //Handle a few errors
+ if( $end < $start || $end > $nLines){
+ //return FALSE;
+ }
+
+ $result = '';
+ for( $i=($start-1); $i<=$end ; $i++ ){
+ $result .= $stringArray[$i] . "\n";
+ }
+ return $result;
}
}
View
@@ -7,18 +7,12 @@
<license>GPL3</license>
<authorEmail>emerson@webdesign.eng.br</authorEmail>
<authorUrl>http://fititnt.org</authorUrl>
- <version>0.1</version>
- <description>PLG_GETGITHUBCODE_XML_DESCRIPTION</description>
+ <version>0.2</version>
+ <description>PLG_GETGITHUBCODE_DESCRIPTION</description>
<files>
<filename plugin="getgithubcode">getgithubcode.php</filename>
<filename>index.html</filename>
- <!--
- if you want to install the language files in the plugin folder.
- Create a "language" folder at the root of the pack i.e
- language/en-GB/en-GB.plg_content_example.ini
- language/en-GB/en-GB.plg_content_example.sys.ini
- -->
<folder>language</folder>
</files>
<languages folder="language">
@@ -33,26 +27,22 @@
<fields name="params">
<fieldset name="basic">
- <field name="field1" type="text"
- description="Example field 1"
- label="Example field 1"
+ <field name="tagname" type="text"
+ description="PLG_GETGITHUBCODE_TAGSNAME_DESC"
+ label="PLG_GETGITHUBCODE_TAGSNAME_LABEL"
+ default="github"
/>
- </fieldset>
-
- <fieldset name="advanced">
- <field name="field2" type="text"
- description="Example field 2"
- label="Example field 2"
+ <field name="tagstart" type="text"
+ description="PLG_GETGITHUBCODE_TAGSTART_DESC"
+ label="PLG_GETGITHUBCODE_TAGSTART_LABEL"
+ default="&lt;pre&gt;"
/>
- </fieldset>
-
- <fieldset name="example">
- <field name="field3" type="text"
- description="Example field 3"
- label="Example field 3"
+ <field name="tagend" type="text"
+ description="PLG_GETGITHUBCODE_TAGEND_DESC"
+ label="PLG_GETGITHUBCODE_TAGEND_LABEL"
+ default="&lt;/pre&gt;"
/>
</fieldset>
-
</fields>
</config>
</extension>
@@ -3,4 +3,10 @@
; GPL3
; Note : All ini files need to be saved as UTF-8
-PLG_GETGITHUBCODE_XML_DESCRIPTION="Joomla plugin that import GitHub.com code to your articles, by using {github}https://github.com/fititnt/codename{/github}"
+PLG_GETGITHUBCODE_DESCRIPTION="Joomla plugin that import GitHub.com code to your articles, by using {github}https://github.com/fititnt/codename{/github}"
+PLG_GETGITHUBCODE_TAGSNAME_LABEL="Tag Name"
+PLG_GETGITHUBCODE_TAGSNAME_DESC="Tag Name. Default is github for [github}"
+PLG_GETGITHUBCODE_TAGSTART_LABEL="Start Tag"
+PLG_GETGITHUBCODE_TAGSTART_DESC="Tag to add before Github Code. You cand add special code that can be parsed by anoter plugin after this one like {code}"
+PLG_GETGITHUBCODE_TAGEND_LABEL="End Tag"
+PLG_GETGITHUBCODE_TAGEND_DESC="Tag to add after Github code. You cand add special code that can be parsed by anoter plugin after this one like {/code}"
@@ -3,4 +3,10 @@
; GPL3
; Note : All ini files need to be saved as UTF-8
-PLG_GETGITHUBCODE_XML_DESCRIPTION="Joomla plugin that import GitHub.com code to your articles, by using {github}https://github.com/fititnt/codename{/github}"
+PLG_GETGITHUBCODE_DESCRIPTION="Joomla plugin that import GitHub.com code to your articles, by using {github}https://github.com/fititnt/codename{/github}"
+PLG_GETGITHUBCODE_TAGSNAME_LABEL="Tag Name"
+PLG_GETGITHUBCODE_TAGSNAME_DESC="Tag Name. Default is github for [github}"
+PLG_GETGITHUBCODE_TAGSTART_LABEL="Start Tag"
+PLG_GETGITHUBCODE_TAGSTART_DESC="Tag to add before Github Code. You cand add special code that can be parsed by anoter plugin after this one like {code}"
+PLG_GETGITHUBCODE_TAGEND_LABEL="End Tag"
+PLG_GETGITHUBCODE_TAGEND_DESC="Tag to add after Github code. You cand add special code that can be parsed by anoter plugin after this one like {/code}"

0 comments on commit b1e63d0

Please sign in to comment.