Exemplo n.º 1
0
 /**
  * The abstract process method to be called whenever file needs to be handled by this pipe.
  *
  * @param $asset which should be processed by this pipe
  * @param array $options to be applied on asset
  * @param string describing errors during file location process
  *
  * @return string containing the processed file's content
  */
 public function process($asset, $options = [], $errors = '')
 {
     $html = null;
     $file = $this->_assetDirectory . DIRECTORY_SEPARATOR . $asset;
     $html = \MarkdownExtended\MarkdownExtended::parse($file)->getContent();
     return $html;
 }
    public function testCreate()
    {
        // simple code
        $md = 'my text with `some code` for test ...';
        $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), 'my text with <code>some code</code> for test ...', '[parsing] test of code span');
        // code blocks
        $md = <<<MSG
para1

    My code here

para2
MSG;
        $this->assertEquals($this->stripWhitespaces((string) MarkdownExtended::parse($md, array('template' => false))), '<p>para1</p><pre>My code here</pre><p>para2</p>', '[parsing] test of code block');
        // fenced code blocks
        $md = <<<MSG
~~~~
My code here
~~~~
MSG;
        $this->assertEquals($this->stripWhitespaces((string) MarkdownExtended::parse($md, array('template' => false))), '<pre>My code here
</pre>', '[parsing] test of fenced code block');
        // fenced code blocks with language
        $md = <<<MSG

~~~~html
My code here
~~~~

MSG;
        $this->assertEquals($this->stripWhitespaces((string) MarkdownExtended::parse($md, array('template' => false))), '<pre class="language-html">My code here
</pre>', '[parsing] test of fenced code block with language info');
    }
Exemplo n.º 3
0
    /**
     * Initialized the command
     */
    public function __construct()
    {
        parent::__construct();
        $script = basename($this->script_path);
        $link = MarkdownExtended::LINK;
        $this->setName(MarkdownExtended::NAME)->setShortname(MarkdownExtended::SHORTNAME)->setVersion(MarkdownExtended::VERSION)->setDescription(MarkdownExtended::DESC)->setUsage(<<<DESC
This program converts markdown-extended syntax text(s) source(s) from specified file(s)
(or STDIN). The rendering can be the full parsed content or just a part of this content.
By default, result is written through STDOUT in HTML format.

To transform a file content, write its path as script argument. To process a list of input
files, just write the concerned paths as arguments, separated by a space.

To transform a string read from STDIN, write it as last argument between double-quotes or EOF.
To process a list of input strings, just write them as arguments, separated by a space.
You can also use the output of a previous command with the pipe notation.

Examples:
    {$script} [options ...] input_filename [input_filename] [...]
    {$script} [options ...] "markdown string read from STDIN"
    echo "*Markdown* __content__" | {$script} [options ...]

Additionally, you can call a special task running: `{$script} <task_name>`
Available tasks are:
    license         : read the full LICENSE of the application
    manifest        : read the full application manifest
    config-list     : dump current configuration settings list
    filters-list    : list runtime filters for current configuration

More information at <{$link}>.
DESC
)->setSynopsis($script . ' [options] "**markdown** _string_" [... string / file path]')->setShortVersionString(MarkdownExtended::getAppInfo(true))->setLongVersionString(implode(PHP_EOL, MarkdownExtended::getAppInfo()))->addCliOption('output', array('shortcut' => 'o', 'argument' => UserInput::ARG_REQUIRED, 'type' => UserInput::TYPE_STRING, 'negate' => true, 'description' => 'Write the result in concerned path(s).'))->addCliOption('config', array('shortcut' => 'c', 'argument' => UserInput::ARG_REQUIRED, 'type' => UserInput::TYPE_STRING, 'description' => 'Define a configuration file to over-write defaults.'))->addCliOption('format', array('shortcut' => 'f', 'argument' => UserInput::ARG_REQUIRED, 'type' => UserInput::TYPE_STRING, 'default' => 'html', 'description' => 'Define the final format to use ("HTML" by default).'))->addCliOption('extract', array('shortcut' => 'e', 'argument' => UserInput::ARG_OPTIONAL, 'type' => UserInput::TYPE_STRING, 'default' => false, 'default_arg' => 'metadata', 'description' => 'Extract only a part of parsed content ("metadata" by default).'))->addCliOption('template', array('shortcut' => 't', 'argument' => UserInput::ARG_OPTIONAL, 'type' => UserInput::TYPE_BOOL | UserInput::TYPE_PATH, 'negate' => true, 'default' => 'auto', 'default_arg' => true, 'description' => 'Define a template to load parsed content in (without argument, the default template will be used).'))->addCliOption('response', array('shortcut' => 'r', 'argument' => UserInput::ARG_REQUIRED, 'type' => UserInput::TYPE_STRING | UserInput::TYPE_LISTITEM, 'default' => 'plain', 'list' => array('plain', 'json', 'php'), 'description' => 'Define the response format in "plain" (default), "json" or "php".'))->addCliOption('force', array('argument' => UserInput::ARG_NULL, 'description' => 'Force some actions (i.e. does not create file backup).'));
        $this->initCommonOptions()->parseOptions();
    }
Exemplo n.º 4
0
 public static function getConfigOrDefault($var)
 {
     $cfg_val = MarkdownExtended::getConfig($var);
     if (empty($cfg_val)) {
         $cfg_val = self::$_defaults[$var];
     }
     return $cfg_val;
 }
 public function testCreate()
 {
     // autolink
     $md = '<http://getcomposer.org/>';
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), '<a href="http://getcomposer.org/" title="See online http://getcomposer.org/">http://getcomposer.org/</a>', '[parsing] test of autolink');
     // autolink email
     $md = '<*****@*****.**>';
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), '<a href="&#109;&#97;&#x69;&#x6c;&#116;&#111;&#58;&#x70;&#x69;&#101;&#114;&#x6f;&#x2e;w&#98;&#109;&#x73;&#x74;&#114;&#64;&#x67;&#x6d;a&#105;&#108;&#x2e;&#x63;&#111;&#109;" title="Contact &#x70;&#x69;&#101;&#114;&#x6f;&#x2e;w&#98;&#109;&#x73;&#x74;&#114;&#64;&#x67;&#x6d;a&#105;&#108;&#x2e;&#x63;&#111;&#109;">&#x70;&#x69;&#101;&#114;&#x6f;&#x2e;w&#98;&#109;&#x73;&#x74;&#114;&#64;&#x67;&#x6d;a&#105;&#108;&#x2e;&#x63;&#111;&#109;</a>', '[parsing] test of email autolink');
 }
 public function testCreate()
 {
     // classic link
     $md = '[Composer](http://getcomposer.org/)';
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), '<a href="http://getcomposer.org/" title="See online http://getcomposer.org/">Composer</a>', '[parsing] test of simple links');
     // link with a title
     $md = '[Composer](http://getcomposer.org/ "My title")';
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), '<a href="http://getcomposer.org/" title="My title">Composer</a>', '[parsing] test of links with title');
 }
    public function testCreate()
    {
        $md = <<<MSG
This is a definition with two paragraphs. Lorem ipsum
dolor sit amet, consectetuer adipiscing elit. Aliquam
hendrerit mi posuere lectus.

Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus.
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<p>This is a definition with two paragraphs. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.</p><p>Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>', '[parsing] test of hard break');
    }
    public function testCreate()
    {
        $md = <<<MSG
This is a definition with two paragraphs. Lorem ipsum
dolor sit amet, consectetuer adipiscing elit. Aliquam
hendrerit mi posuere lectus.
![Alt text](http://upload.wikimedia.org/wikipedia/commons/7/70/Example.png 'Optional image title')

Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus.
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<p>This is a definition with two paragraphs. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. <img alt="Alt text" src="http://upload.wikimedia.org/wikipedia/commons/7/70/Example.png" title="Optional image title" /></p><p>Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>', '[parsing] test of image');
    }
    public function testCreate()
    {
        $md = <<<MSG
Term 1
:   This is a definition with two paragraphs. Lorem ipsum
    dolor sit amet, consectetuer adipiscing elit. Aliquam
    hendrerit mi posuere lectus.

    Vestibulum enim wisi, viverra nec, fringilla in, laoreet
    vitae, risus.

:   Second definition for term 1, also wrapped in a paragraph
    because of the blank line preceding it.
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<dl><dt>Term 1</dt><dd><p>This is a definition with two paragraphs. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.</p><p>Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p></dd><dd><p>Second definition for term 1, also wrapped in a paragraph because of the blank line preceding it.</p></dd></dl>', '[parsing] test of definitions list');
    }
Exemplo n.º 10
0
    public function testCreate()
    {
        // unordered list
        $md = <<<MSG
-   first item
*   second item
    - first sub-item
    * second sub-item
-   third item
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<ul><li>first item</li><li>second item  <ul><li>first sub-item</li><li>second sub-item</li></ul></li><li>third item</li></ul>', '[parsing] test of unordered list');
        // ordered list
        $md = <<<MSG
1.   first item
1.   second item
    1. first sub-item
    2. second sub-item
5.   third item
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<ol><li>first item</li><li>second item  <ol><li>first sub-item</li><li>second sub-item</li></ol></li><li>third item</li></ol>', '[parsing] test of ordered list');
    }
Exemplo n.º 11
0
 public function buildTitle($text, array $attributes = array())
 {
     if (!isset($attributes['id']) || empty($attributes['id'])) {
         $attributes['id'] = uniqid();
     } else {
         $attributes['id'] = Helper::getSafeIdString($attributes['id']);
     }
     if (!isset($attributes['name']) || empty($attributes['name'])) {
         $attributes['name'] = $attributes['id'];
     }
     if (isset($attributes['level'])) {
         $tag = 'h' . $attributes['level'];
         unset($attributes['level']);
     } else {
         $tag = 'h' . MarkdownExtended::getVar('baseheaderlevel');
     }
     if (!isset($attributes['no-addon']) || $attributes['no-addon'] !== true) {
         $text = $this->addTitleAddon($text, $attributes);
     }
     if (isset($attributes['no-addon'])) {
         unset($attributes['no-addon']);
     }
     $_ttl = $this->getTagString($text, $tag, $attributes);
     return $_ttl;
 }
Exemplo n.º 12
0
 /**
  * Resets options to defaults
  *
  * @return $this
  *
  * @see \MarkdownExtended\MarkdownExtended::getDefaults()
  */
 public function resetOptions()
 {
     $this->getKernel()->set('config', new Registry(MarkdownExtended::getDefaults()));
     return $this;
 }
 public function testCreate()
 {
     $md = '#Hello World';
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), '<h1 id="hello-world">Hello World</h1>', '[parsing] test of header');
 }
 public function testCreate()
 {
     $md = '**Hello** _World_';
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), '<strong>Hello</strong> <em>World</em>', '[parsing] test of emphasis');
 }
Exemplo n.º 15
0
    public function testCreate()
    {
        // simple table
        $md = <<<MSG
| First Header  | Second Header |
| ------------- | ------------: |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<table><thead><tr><th>First Header</th><th style="text-align:right;">Second Header</th></tr></thead><tbody><tr><td>Content Cell</td><td style="text-align:right;">Content Cell</td></tr><tr><td>Content Cell</td><td style="text-align:right;">Content Cell</td></tr></tbody></table>', '[parsing] test of simple table');
        // simple table with no leading pipe
        $md = <<<MSG
First Header  | Second Header |
------------- | ------------: |
Content Cell  | Content Cell  |
Content Cell  | Content Cell  |
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<table><thead><tr><th>First Header</th><th style="text-align:right;">Second Header</th></tr></thead><tbody><tr><td>Content Cell</td><td style="text-align:right;">Content Cell</td></tr><tr><td>Content Cell</td><td style="text-align:right;">Content Cell</td></tr></tbody></table>', '[parsing] test of simple table with no leading pipe');
        // simple table with not constant spacing
        $md = <<<MSG
| First Header | Second Header |
| ------------ | ------------: |
| Cell | Cell |
| Cell | Cell |
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<table><thead><tr><th>First Header</th><th style="text-align:right;">Second Header</th></tr></thead><tbody><tr><td>Cell</td><td style="text-align:right;">Cell</td></tr><tr><td>Cell</td><td style="text-align:right;">Cell</td></tr></tbody></table>', '[parsing] test of simple table with not constant spaced cells');
        // table with multiple headers and label before
        $md = <<<MSG
[prototype *table*]
|             | Grouping                    ||
First Header  | Second Header | Third header |
First comment  | Second comment | Third comment |
------------- | ------------: | :----------: |
Content Cell  |  *Long Cell*                ||
Content Cell  | **Cell**      | **Cell**     |
New section   |   More        |         Data |
And more      |           And more          ||
And more                     || And more     |
MSG;
        $this->assertEquals($this->stripWhitespaceAndNewLines((string) MarkdownExtended::parse($md, array('template' => false))), '<table><caption id="prototype-table">[prototype <em>table</em>]</caption><thead><tr><th></th><th style="text-align:right;" colspan="2">Grouping</th></tr><tr><th>First Header</th><th style="text-align:right;">Second Header</th><th style="text-align:center;">Third header</th></tr><tr><th>First comment</th><th style="text-align:right;">Second comment</th><th style="text-align:center;">Third comment</th></tr></thead><tbody><tr><td>Content Cell</td><td style="text-align:right;" colspan="2"><em>Long Cell</em></td></tr><tr><td>Content Cell</td><td style="text-align:right;"><strong>Cell</strong></td><td style="text-align:center;"><strong>Cell</strong></td></tr><tr><td>New section</td><td style="text-align:right;">More</td><td style="text-align:center;">Data</td></tr><tr><td>And more</td><td style="text-align:right;" colspan="2">And more</td></tr><tr><td colspan="2">And more</td><td style="text-align:center;">And more</td></tr></tbody></table>', '[parsing] test of complex table with multiple headers and a caption above');
        // table with multiple headers and label after
        /*/
                $md = <<<MSG
        |             | Grouping                    ||
        First Header  | Second Header | Third header |
        First comment  | Second comment | Third comment |
        ------------- | ------------: | :----------: |
        Content Cell  |  *Long Cell*                ||
        Content Cell  | **Cell**      | **Cell**     |
        New section   |   More        |         Data |
        And more      |           And more          ||
        And more                     || And more     |
        [prototype *table*]
        MSG;
                $this->assertEquals(
                    $this->stripWhitespaceAndNewLines(
                        (string) MarkdownExtended::parse($md, array('template'=>false))
                    ),
                    '<table><caption id="prototype_table">[prototype <em>table</em>]</caption><thead><tr><th></th><th style="text-align:right;" colspan="2">Grouping</th></tr><tr><th>First Header</th><th style="text-align:right;">Second Header</th><th style="text-align:center;">Third header</th></tr><tr><th>First comment</th><th style="text-align:right;">Second comment</th><th style="text-align:center;">Third comment</th></tr></thead><tbody><tr><td>Content Cell</td><td style="text-align:right;" colspan="2"><em>Long Cell</em></td></tr><tr><td>Content Cell</td><td style="text-align:right;"><strong>Cell</strong></td><td style="text-align:center;"><strong>Cell</strong></td></tr><tr><td>New section</td><td style="text-align:right;">More</td><td style="text-align:center;">Data</td></tr><tr><td>And more</td><td style="text-align:right;" colspan="2">And more</td></tr><tr><td colspan="2">And more</td><td style="text-align:center;">And more</td></tr></tbody></table>',
                    '[parsing] test of a complex table with multiple headers and a caption below'
                );
        //*/
        // table with multiple bodies
        /*/
                $md = <<<MSG
        |             | Grouping                    ||
        First Header  | Second Header | Third header |
        ------------- | ------------: | :----------: |
        Content Cell  |  *Long Cell*                ||
        Content Cell  | **Cell**      | **Cell**     |
        
        New section   |   More        |         Data |
        MSG;
                $this->assertEquals(
                    $this->stripWhitespaceAndNewLines(
                        (string) MarkdownExtended::parse($md, array('template'=>false))
                    ),
                    '<table><thead><tr><th></th><th style="text-align:right;" colspan="2">Grouping</th></tr><tr><th>First Header</th><th style="text-align:right;">Second Header</th><th style="text-align:center;">Third header</th></tr></thead><tbody><tr><td>Content Cell</td><td style="text-align:right;" colspan="2"><em>Long Cell</em></td></tr><tr><td>Content Cell</td><td style="text-align:right;"><strong>Cell</strong></td><td style="text-align:center;"><strong>Cell</strong></td></tr></tbody><tbody><tr><td>New section</td><td style="text-align:right;">More</td><td style="text-align:center;">Data</td></tr><tr><td>And more</td><td style="text-align:right;" colspan="2">And more</td></tr></tbody></table>',
                    '[parsing] test of complex table with multiple bodies'
                );
        //*/
    }
Exemplo n.º 16
0
 /**
  * Parse the request
  * @return self
  * @api
  */
 public function distribute()
 {
     try {
         Container::get('request')->parse();
     } catch (\Exception $e) {
         $this->error($e->getMessage(), Response::STATUS_BAD_REQUEST);
     }
     if (!Container::get('request')->isMethod('get') && !Container::get('request')->isMethod('post') && !Container::get('request')->isMethod('head')) {
         $this->error(sprintf('Request method "%s" is not allowed!', Container::get('request')->getMethod()), Response::STATUS_METHOD_NOT_ALLOWED);
     }
     // analyze request headers
     foreach (Container::get('request')->getHeaders() as $name => $value) {
         switch ($name) {
             case 'Time-Zone':
                 if (in_array($value, timezone_identifiers_list())) {
                     date_default_timezone_set($value);
                 } else {
                     $this->warning(sprintf('The "%s" timezone defined is not valid!', $value));
                 }
                 break;
             default:
                 break;
         }
     }
     // user files
     $type = Container::get('request')->getData('source_type', 'data_input');
     $this->setSourceType($type);
     if ($this->getSourceType() == 'file') {
         $files = Container::get('request')->getFiles();
         if (!empty($files)) {
             foreach ($files as $name => $path) {
                 $this->addSource(file_get_contents($path), $name);
             }
         }
     }
     // any test to launch
     $test = Container::get('request')->getData('test');
     if (!empty($test)) {
         $method = 'testAction_' . $test;
         if (method_exists($this, $method)) {
             call_user_func(array($this, $method));
         } else {
             $this->warning(sprintf('Test method "%s" not found!', $test));
         }
     }
     // end here if no 'source' or 'sources' post data
     $source = Container::get('request')->getData('source');
     $sources = Container::get('request')->getData('sources');
     $_sources = $this->getSources();
     if (empty($source) && empty($sources) && empty($_sources)) {
         $this->warning('No source to parse!')->serve();
     } else {
         if (!empty($sources)) {
             $this->setSources(array_merge($_sources, $sources));
         }
         if (!empty($source)) {
             $this->addSource($source);
         }
     }
     // debug mode on?
     $this->setDebug(Container::get('request')->getData('debug', false));
     // load the MDE parser
     if (!class_exists('\\MarkdownExtended\\MarkdownExtended')) {
         $this->error('Class "\\MarkdownExtended\\MarkdownExtended" not found!');
     }
     Container::set('mde_parser', \MarkdownExtended\MarkdownExtended::create());
     return $this;
 }
Exemplo n.º 17
0
 public function getMarkdownParser()
 {
     if (empty($this->markdown_parser)) {
         // creating the Markdown parser
         $emd_config = $this->registry->getConfig('markdown', array(), 'docbook');
         $emd_config_strs = $this->registry->getConfig('markdown_i18n', array(), 'docbook');
         if (!empty($emd_config_strs) && is_array($emd_config_strs) && count($emd_config_strs) == 1 && isset($emd_config_strs['markdown_i18n'])) {
             $emd_config_strs = $emd_config_strs['markdown_i18n'];
         }
         if (empty($emd_config)) {
             $emd_config = array();
         }
         $translator = I18n::getInstance();
         foreach ($emd_config_strs as $_str) {
             $emd_config[$_str] = $translator->translate($_str);
         }
         $this->setMarkdownParser(MarkdownExtended::create($emd_config));
     }
     return $this->markdown_parser;
 }
Exemplo n.º 18
0
/**
 * Transform an input file name source by the MarkdownExtended
 *
 * @param   string  $file_name
 * @param   mixed   $options
 * @return  \MarkdownExtended\API\ContentInterface
 */
function MarkdownFromSource($file_name, $options = null)
{
    return \MarkdownExtended\MarkdownExtended::parseSource($file_name, $options);
}
 public function testCreate()
 {
     $md = "\nA text whit HTML expression.\n\n*[HTML]: Hyper Text Markup Language\n        ";
     $this->assertEquals((string) MarkdownExtended::parse($md, array('template' => false)), 'A text whit <abbr title="Hyper Text Markup Language">HTML</abbr> expression.', '[parsing] Test of abbreviation');
 }
 public function testCreate()
 {
     $md = "\n> My citation\n>\n> With a paragraph and some `code`\n>\n>     and even a preformatted string\n        ";
     $this->assertEquals($this->stripWhitespaces((string) MarkdownExtended::parse($md, array('template' => false))), '<blockquote><p>My citation</p><p>With a paragraph and some <code>code</code></p><pre>and even a preformatted string</pre></blockquote>', '[parsing] test of blockquote');
 }
Exemplo n.º 21
0
 /**
  * @param string $file_path
  * @return void
  * @throws \WebServices\NotFoundException if the `$usage_filepath` property is set in the controller but
  *          the file can't be found
  */
 public function parseUsageFilepath($file_path)
 {
     if (file_exists($file_path)) {
         if (substr($file_path, -3) === '.md') {
             \MarkdownExtended\MarkdownExtended::transformSource($file_path);
             $ctt = \MarkdownExtended\MarkdownExtended::getFullContent();
         } elseif (substr($file_path, -7) === '.md.php' || substr($file_path, -4) === '.php') {
             ob_start();
             extract($this->getUsageParams(), EXTR_OVERWRITE);
             include $file_path;
             $file_ctt = ob_get_contents();
             ob_end_clean();
             if (substr($file_path, -7) === '.md.php') {
                 \MarkdownExtended\MarkdownExtended::transformString($file_ctt);
                 $ctt = \MarkdownExtended\MarkdownExtended::getFullContent();
             } else {
                 $ctt = $file_ctt;
             }
         } else {
             $ctt = @file_get_contents($file_path);
         }
         return $ctt;
     } else {
         throw new NotFoundException(sprintf("Usage file '%s' not found!", $file_path));
     }
     return null;
 }