Ejemplo n.º 1
0
 function init()
 {
     parent::init();
     $this->addField('name');
     $this->addField('title');
     $this->addField('data')->caption('Markdown Template');
     $this->addField('rendered')->caption('HTML render cache');
     $this->addHook('afterLoad', function ($m) {
         if (is_null($m['rendered'])) {
             $markdown = $m['data'];
             if (!$m->hook('prepareTemplate', array(&$markdown))) {
                 $markdown = $this->prepareTemplate($markdown);
             }
             $m['rendered'] = \ParsedownExtra::instance()->parse($markdown);
             // $this->prepareTemplate());
         }
         if (is_null($m['title'])) {
             $x = null;
             preg_match('/^# (.*)$/m', $m['data'], $x);
             $m['title'] = $x[1];
             //v
             //$m['rendered'] = \ParsedownExtra::instance()->parse($markdown);
             // $this->prepareTemplate());
         }
     });
 }
Ejemplo n.º 2
0
 /**
  * ContentGetter constructor.
  *
  * @param string $version
  * @param string $slug
  */
 public function __construct($version, $slug = null)
 {
     if ($version === 'local') {
         $this->basepath = dirname(__DIR__);
     } else {
         $this->basepath = sprintf('%s/version/%s/', dirname(__DIR__), $version);
     }
     if ($slug !== null) {
         $sourceFile = sprintf('%s/source_docs/%s.md', $this->basepath, $slug);
         if (!is_readable($sourceFile)) {
             return;
         }
         $this->source = \ParsedownExtra::instance()->text(file_get_contents($sourceFile));
     }
     $this->slug = $slug;
     $this->version = $version;
 }
Ejemplo n.º 3
0
---- */
// Markup for <h1> and <h2>..
$source = preg_replace_callback("/<h([234])>(.*)<\\/h([234])>/i", function ($matches) {
    $output = sprintf("<h%s id='%s'>%s<a href='#%s' class='anchor'>¶</a></h%s>", $matches[1], makeSlug($matches[2]), $matches[2], makeSlug($matches[2]), $matches[1]);
    return $output;
}, $source);
$loader = new Twig_Loader_Filesystem('./view');
$twig = new Twig_Environment($loader, array());
// Add Dumper function to twig.
$dumper = new Twig_SimpleFunction('dump', function ($var) {
    return dump($var);
}, array('is_safe' => array('html')));
$twig->addFunction($dumper);
// Add markdown to twig.
$markdown = new Twig_SimpleFilter('markdown', function ($content) {
    return \ParsedownExtra::instance()->text($content);
}, array('is_safe' => array('html')));
$twig->addFilter($markdown);
// Add slug filter to twig.
$slug = new Twig_SimpleFilter('slug', function ($name) {
    return \URLify::filter($name);
}, array('is_safe' => array('html')));
$twig->addFilter($slug);
echo $twig->render('index.twig', array('title' => $maintitle, 'source' => $source, 'menu' => $menu, 'submenu' => $submenu, 'current' => $request, 'version' => $version, 'requested_page' => $request, 'prefix' => $prefix == "/" ? "" : $prefix));
// ----------
/**
 * Modify a string, so that we can use it for slugs. Like
 * safeString, but using hyphens instead of underscores.
 *
 * @param string $str
 * @param string $type
Ejemplo n.º 4
0
 /**
  * Formats the given string as Markdown in HTML.
  *
  * @param string $content
  *
  * @return string Markdown output
  */
 public function markdown($content)
 {
     // Parse the field as Markdown, return HTML
     $output = \ParsedownExtra::instance()->text($content);
     $config = $this->app['config']->get('general/htmlcleaner');
     $allowed_tags = !empty($config['allowed_tags']) ? $config['allowed_tags'] : array('div', 'p', 'br', 'hr', 's', 'u', 'strong', 'em', 'i', 'b', 'li', 'ul', 'ol', 'blockquote', 'pre', 'code', 'tt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd', 'dl', 'dh', 'table', 'tbody', 'thead', 'tfoot', 'th', 'td', 'tr', 'a', 'img');
     $allowed_attributes = !empty($config['allowed_attributes']) ? $config['allowed_attributes'] : array('id', 'class', 'name', 'value', 'href', 'src');
     // Sanitize/clean the HTML.
     $maid = new \Maid\Maid(array('output-format' => 'html', 'allowed-tags' => $allowed_tags, 'allowed-attribs' => $allowed_attributes));
     $output = $maid->clean($output);
     return $output;
 }
Ejemplo n.º 5
0
 public function readme($filename, Silex\Application $app, Request $request)
 {
     $paths = $app['resources']->getPaths();
     $filename = $paths['extensionspath'] . '/vendor/' . $filename;
     // don't allow viewing of anything but "readme.md" files.
     if (strtolower(basename($filename)) != 'readme.md') {
         die('Not allowed');
     }
     if (!is_readable($filename)) {
         die('Not readable');
     }
     $readme = file_get_contents($filename);
     // Parse the field as Markdown, return HTML
     $html = \ParsedownExtra::instance()->text($readme);
     return new Response($html, 200, array('Cache-Control' => 's-maxage=180, public'));
 }
Ejemplo n.º 6
0
 /**
  * symlinks the localized readme file, if existant
  */
 public function getLocalizedReadme()
 {
     $filename = __DIR__ . "/locales/readme_" . substr($this->app['locale'], 0, 2) . ".md";
     $fallback = __DIR__ . "/locales/readme_en.md";
     if (!($readme = @file_get_contents($filename))) {
         $readme = file_get_contents($fallback);
     }
     // Parse the field as Markdown, return HTML
     return preg_replace("~h1~", "h3", \ParsedownExtra::instance()->text($readme));
 }
Ejemplo n.º 7
0
// API

$this->module("cockpit")->extend([

    "assets" => function($assets, $key=null, $cache=0, $cache_folder=null) use($app) {

        $key          = $key ? $key : md5(serialize($assets));
        $cache_folder = $cache_folder ? $cache_folder : $app->path("cache:assets");

        $app("assets")->style_and_script($assets, $key, $cache_folder, $cache);
    },

    "markdown" => function($content, $extra = false) use($app) {

        return $extra ? \ParsedownExtra::instance()->text($content) : \Parsedown::instance()->text($content);
    },

    "get_registry" => function($key, $default=null) use($app) {

        return $app->memory->hget("cockpit.api.registry", $key, $default);
    },

    "clearCache" => function() use($app) {

        $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($app->path("cache:")), \RecursiveIteratorIterator::SELF_FIRST);

        foreach ($files as $file) {

            if (!$file->isFile()) continue;
            if (preg_match('/(.gitkeep|index\.html)$/', $file)) continue;
Ejemplo n.º 8
0
 /**
  * Get the decoded version of a value of the current object.
  *
  * @param  string $name name of the value to get
  * @return mixed  decoded value or null when no value available
  */
 public function getDecodedValue($name)
 {
     $value = null;
     if (isset($this->values[$name])) {
         $fieldtype = $this->fieldtype($name);
         $fieldinfo = $this->fieldinfo($name);
         $allowtwig = !empty($fieldinfo['allowtwig']);
         switch ($fieldtype) {
             case 'markdown':
                 $value = $this->preParse($this->values[$name], $allowtwig);
                 // Parse the field as Markdown, return HTML
                 $value = \ParsedownExtra::instance()->text($value);
                 // Sanitize/clean the HTML.
                 $maid = new \Maid\Maid(array('output-format' => 'html', 'allowed-tags' => array('html', 'head', 'body', 'section', 'div', 'p', 'br', 'hr', 's', 'u', 'strong', 'em', 'i', 'b', 'li', 'ul', 'ol', 'menu', 'blockquote', 'pre', 'code', 'tt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd', 'dl', 'dh', 'table', 'tbody', 'thead', 'tfoot', 'th', 'td', 'tr', 'a', 'img'), 'allowed-attribs' => array('id', 'class', 'name', 'value', 'href', 'src')));
                 $value = $maid->clean($value);
                 $value = new \Twig_Markup($value, 'UTF-8');
                 break;
             case 'html':
             case 'text':
             case 'textarea':
                 $value = $this->preParse($this->values[$name], $allowtwig);
                 $value = new \Twig_Markup($value, 'UTF-8');
                 break;
             case 'imagelist':
             case 'filelist':
                 if (is_string($this->values[$name])) {
                     // Parse the field as JSON, return the array
                     $value = json_decode($this->values[$name]);
                 } else {
                     // Already an array, do nothing.
                     $value = $this->values[$name];
                 }
                 break;
             case 'image':
                 if (is_array($this->values[$name]) && isset($this->values[$name]['file'])) {
                     $value = $this->values[$name]['file'];
                 } else {
                     $value = $this->values[$name];
                 }
                 break;
             default:
                 $value = $this->values[$name];
                 break;
         }
     }
     return $value;
 }
Ejemplo n.º 9
0
 /**
  * Formats the given string as Markdown in HTML
  *
  * @param  string $content
  * @return string Markdown output
  */
 public function markdown($content)
 {
     // Parse the field as Markdown, return HTML
     $output = \ParsedownExtra::instance()->text($content);
     // Sanitize/clean the HTML.
     $maid = new \Maid\Maid(array('output-format' => 'html', 'allowed-tags' => array('html', 'head', 'body', 'section', 'div', 'p', 'br', 'hr', 's', 'u', 'strong', 'em', 'i', 'b', 'li', 'ul', 'ol', 'menu', 'blockquote', 'pre', 'code', 'tt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd', 'dl', 'dh', 'table', 'tbody', 'thead', 'tfoot', 'th', 'td', 'tr', 'a', 'img'), 'allowed-attribs' => array('id', 'class', 'name', 'value', 'href', 'src')));
     $output = $maid->clean($output);
     return $output;
 }
Ejemplo n.º 10
0
 /**
  * Display the API Documentation in Html (parsed from markdown)
  *
  * @since version 9.1
  *
  * @param      string  $file   relative path of documentation file
  */
 public function inlineDocumentation($file)
 {
     global $CFG_GLPI;
     self::header(true, __("API Documentation"));
     echo Html::css($CFG_GLPI['root_doc'] . "/lib/prism/prism.css");
     echo Html::script($CFG_GLPI['root_doc'] . "/lib/prism/prism.js");
     echo "<div class='documentation'>";
     $documentation = file_get_contents(GLPI_ROOT . '/' . $file);
     echo ParsedownExtra::instance()->text($documentation);
     echo "</div>";
     Html::nullFooter();
 }
Ejemplo n.º 11
0
 public function markdown($str)
 {
     return \ParsedownExtra::instance()->text($str);
 }