Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Returning markdown in Page

Started by Claud Rusnac 9 years ago · 1 replies · 968 views
9 years ago

I am a plugin newb, but got most of the way there. I am trying to build a simple webservice pluggin that fetches github release content and displays in on a page. In general the pluggin should query the GITHUB API, return JSON (Which includes Markdown text), Convert JSON to an array, process the array using a foreach, send the results to the page. The Markdown should be processed when displayed to the user.

I am using the https://learn.getgrav.org/cookbook/plugin-recipes#output-some-php-code-result-in-a-twig-template code as a starter. Everything is working as expected, but my only challenge is the output is not being formatted using markdown. Below is my code:

PHP
<?php
namespace Grav\Plugin;

class ExampleTwigExtension extends \Twig_Extension
{
    public function getName()
    {
        return 'ExampleTwigExtension';
    }
    public function getFunctions()
    {
       return [
           new \Twig_SimpleFunction('example', [$this, 'exampleFunction'])
        ];
    }

public function exampleFunction()
{
    $text = "";

    //Set Variables
    $token = "TOKEN";
    $url = "GITHUB URL";

    $decode_token = base64_decode($token);

    //Init Curl
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FAILONERROR,true);
    curl_setopt($ch, CURLOPT_USERPWD, "$decode_token");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //Set as variable to be re-used.
    $data = curl_exec($ch);

    $returned_content = $data;

    //Convert JSON data to an ARRAY so that it can be parsed.
    $releases = json_decode($returned_content, true);

    if(curl_error($ch)) {

        $text.= '<h3>There has been an error!</h3>';
        $text.= '<p>
                I am unable to get release information.  Please contact <a href="mailto:[email protected]?subject=NISP Release Error: ' .curl_error($ch).' ">CIS Governance</a> for more assistance
               </p>';
        $text.= 'Ref:'. curl_error($ch);

        }else{

            foreach ($releases as $release) {
                $text.= '<div class="page-header">';
                $text.= '<h2>';
                $text.= $release['name'];
                $text.= '</h2>';
                $text.= '</div>';       
                $text.= '<div class="list-group">';
                $text.= "\n\n". $release['body'];
                $text.= '</div>';

            }

    }

     // Return the Processed Text <<<< THIS IS Where I suspect I am doing something wrong...
     return $text;

    }
}
last edited 10/25/17 by Claud Rusnac
9 years ago

I found a work-around as I couldn't figure out how to use the markdown class to return already processed markdown. I ended up using an alternative markdown processor. (https://github.com/michelf/php-markdown) See the new code below:

PHP
<?php
namespace Grav\Plugin;

use \Michelf\Markdown;

class ChangelogExtension extends \Twig_Extension
{
    public function getName(){
        return 'ChangelogExtension';
    }

    public function getFunctions(){
        return [
            new \Twig_SimpleFunction('changelog', [$this, 'changelogFunction'])
        ];
    }

    public function changelogFunction(){

        //Include Markdown Parser
        require_once 'vendor/php-markdown-1.7.0/Michelf/Markdown.inc.php';  

        //Set or Initialize Variables
        $text = "";
        $token = "TOKEN";
        $url = "GITHUB URL";
        $timeout = 5;
        $decode_token = base64_decode($token);

        //Init Curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR,true);
        curl_setopt($ch, CURLOPT_USERPWD, "$decode_token");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //Set as variable to be re-used.
        $data = curl_exec($ch);

        $returned_content = $data;

        //Convert JSON data to an ARRAY so that it can be parsed.
        $releases = json_decode($returned_content, true);

        if(curl_error($ch)) {

            $text.= '<h3>There has been an error!</h3>';
            $text.= '<p>
                    I am unable to get release information.  Please contact <a href="[email protected]?subject=DC Tool Release Error: ' .curl_error($ch).' ">CONTACT</a> for more assistance
                   </p>';
            $text.= 'Ref:'. curl_error($ch);

            }else{

                foreach ($releases as $release) {
                    $text.= '<div class="page-header">';
                    $text.= '<h2>';
                    $text.= $release['name'];
                    $text.= '</h2>';
                    $text.= '</div>';       
                    $text.= '<div class="list-group">';
                    $text.= "\n";
                    $text.= $release['body'];
                    $text.= '</div>';

                }

        }

         // Process the MarkDown
         $mdtext = Markdown::defaultTransform($text);

         //Return the converted content
         return $mdtext;
        }
}

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 72 2 months ago