createCommonMarkEnvironment() public static method

public static createCommonMarkEnvironment ( ) : Environment
return Environment
Beispiel #1
0
 /**
  * Create and return a CommonMark environment
  *
  * @return Environment CommonMark environment
  */
 protected function environment()
 {
     // Obtain a pre-configured Environment with all the CommonMark parsers/renderers ready-to-go
     $environment = Environment::createCommonMarkEnvironment();
     // Custom environment initialization
     return $this->initializeEnvironment($environment);
 }
 /**
  * @param string $markdown
  * @param string $expected
  * @dataProvider e2eProvider
  */
 public function testFullRender($markdown, $expected)
 {
     $factory = new CliRendererFactory();
     $renderer = $factory->__invoke();
     $parser = new DocParser(Environment::createCommonMarkEnvironment());
     $this->assertEquals($expected, $renderer->renderBlock($parser->parse($markdown)));
 }
 public function testExerciseRendererSetsCurrentExerciseAndRendersExercise()
 {
     $menu = $this->getMockBuilder(CliMenu::class)->disableOriginalConstructor()->getMock();
     $item = $this->getMock(MenuItemInterface::class);
     $item->expects($this->any())->method('getText')->will($this->returnValue('Exercise 2'));
     $menu->expects($this->once())->method('getSelectedItem')->will($this->returnValue($item));
     $menu->expects($this->once())->method('close');
     $exercise1 = $this->getMock(ExerciseInterface::class);
     $exercise2 = $this->getMock(ExerciseInterface::class);
     $exercises = [$exercise1, $exercise2];
     $exerciseRepository = $this->getMockBuilder(ExerciseRepository::class)->disableOriginalConstructor()->getMock();
     $userState = $this->getMock(UserState::class);
     $userStateSerializer = $this->getMockBuilder(UserStateSerializer::class)->disableOriginalConstructor()->getMock();
     $exerciseRepository->expects($this->once())->method('findByName')->with('Exercise 2')->will($this->returnValue($exercise2));
     $exerciseRepository->expects($this->once())->method('findAll')->will($this->returnValue($exercises));
     $userState->expects($this->once())->method('setCurrentExercise')->with('Exercise 2');
     $userStateSerializer->expects($this->once())->method('serialize')->with($userState);
     $problemFile = sprintf('%s/%s/problem.md', sys_get_temp_dir(), $this->getName());
     $exercise2->expects($this->once())->method('getProblem')->will($this->returnValue($problemFile));
     if (!is_dir(dirname($problemFile))) {
         mkdir(dirname($problemFile), 0775, true);
     }
     file_put_contents($problemFile, '### Exercise Content');
     $markdownRenderer = new MarkdownRenderer(new DocParser(Environment::createCommonMarkEnvironment()), (new CliRendererFactory())->__invoke());
     $color = new Color();
     $color->setForceStyle(true);
     $exerciseRenderer = new ExerciseRenderer('phpschool', $exerciseRepository, $userState, $userStateSerializer, $markdownRenderer, $color, new StdOutput($color, $this->getMock(TerminalInterface::class)));
     $this->expectOutputString(file_get_contents(__DIR__ . '/res/exercise-help-expected.txt'));
     $exerciseRenderer->__invoke($menu);
     unlink($problemFile);
 }
 public function setCommonMarkEnvironment(Environment $environment = null)
 {
     if (is_null($environment)) {
         $environment = Environment::createCommonMarkEnvironment();
     }
     $this->_cmEnvironment = $environment;
 }
Beispiel #5
0
 public function setUp()
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->addInlineParser(new MentionParser());
     $parser = new DocParser($environment);
     $renderer = new HtmlRenderer($environment);
     $this->service = new CommonMarkParser($environment, $parser, $renderer);
 }
Beispiel #6
0
 /**
  * Convert body to html
  */
 public function markdown($description)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $parser = new DocParser($environment);
     $htmlRenderer = new HtmlRenderer($environment);
     $markdown = $this->description;
     $documentAST = $parser->parse($markdown);
     return $htmlRenderer->renderBlock($documentAST);
 }
 public function testRender()
 {
     $docParser = new DocParser(Environment::createCommonMarkEnvironment());
     $cliRenderer = (new CliRendererFactory())->__invoke();
     $renderer = new MarkdownRenderer($docParser, $cliRenderer);
     $markdown = "### HONEY BADGER DON'T CARE";
     $expected = "\n### HONEY BADGER DON'T CARE\n\n";
     $this->assertSame($expected, $renderer->render($markdown));
 }
Beispiel #8
0
 public function getArticle($uri)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->addExtension(new TableExtension());
     $converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
     $contents = \Storage::get($uri . '.md');
     preg_match('/^.*$/m', $contents, $matches);
     return view('portal', array('body' => $converter->convertToHtml($contents), 'title' => substr($matches[0], 2)));
 }
Beispiel #9
0
 private function parseCode($string)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->addInlineParser(new MentionParser());
     $parser = new DocParser($environment);
     $renderer = new HtmlRenderer($environment);
     $this->service = new CommonMarkParser($environment, $parser, $renderer);
     return $this->service->render($string);
 }
 protected function newCommonMarkConverter(RootConfig $config)
 {
     $environment = Environment::createCommonMarkEnvironment();
     foreach ($config->getCommonMarkExtensions() as $extension) {
         if (!class_exists($extension)) {
             throw new \RuntimeException(sprintf('CommonMark extension class "%s" does not exists. You must use a FCQN!', $extension));
         }
         $environment->addExtension(new $extension());
     }
     return new \League\CommonMark\Converter(new DocParser($environment), new HtmlRenderer($environment));
 }
Beispiel #11
0
 /**
  * Creates a new article object from its values.
  */
 public function __construct(array $metatdata, string $text, string $slug)
 {
     $this->metatdata = $metatdata;
     $this->date = new Carbon($this->metatdata['date'] ?? 'now');
     $this->slug = $slug;
     // Parse the Markdown document.
     $environment = Environment::createCommonMarkEnvironment();
     $parser = new DocParser($environment);
     $this->renderer = new HtmlRenderer($environment);
     $this->document = $parser->parse($text);
 }
Beispiel #12
0
 public function toHtml($content)
 {
     $environment = \League\CommonMark\Environment::createCommonMarkEnvironment();
     $environment->mergeConfig([]);
     $environment->addExtension(new \Webuni\CommonMark\TableExtension\TableExtension());
     $converter = new \League\CommonMark\Converter(new \League\CommonMark\DocParser($environment), new \League\CommonMark\HtmlRenderer($environment));
     $content = $converter->convertToHtml($content);
     // strip comment marks
     $markdownContent = preg_replace("/\n\\-\\-[\\ ]*/s", "\n", $content);
     return $markdownContent;
 }
 public function htmlIntroduction($before = '', $after = '')
 {
     $introduction = trim($this->introduction);
     if (empty($introduction)) {
         return;
     }
     $environment = Environment::createCommonMarkEnvironment();
     $parser = new DocParser($environment);
     $htmlRenderer = new HtmlRenderer($environment);
     $text = $parser->parse($introduction);
     return $before . $htmlRenderer->renderBlock($text) . $after;
 }
 /**
  * Create a new commonmark converter instance.
  *
  * @param array $config
  */
 public function __construct(array $config = array())
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->mergeConfig($config);
     $environment->addExtension(new TableExtension());
     $this->extendEnvironment($environment);
     if (array_key_exists('processor_instance', $config['daux'])) {
         $config['daux']['processor_instance']->extendCommonMarkEnvironment($environment);
     }
     $this->docParser = new DocParser($environment);
     $this->htmlRenderer = new HtmlRenderer($environment);
 }
 /**
  * @param string $string
  * @param string $expected
  * @return void
  *
  * @dataProvider dataForIntegrationTest
  */
 public function testStrikethrough($string, $expected)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->addInlineParser(new StrikethroughParser());
     $environment->addInlineRenderer('CommonMarkExt\\Strikethrough\\Strikethrough', new StrikethroughRenderer());
     $parser = new DocParser($environment);
     $renderer = new HtmlRenderer($environment);
     $document = $parser->parse($string);
     $html = $renderer->renderBlock($document);
     //
     //        $converter = new CommonMarkConverter($environment->getConfig());
     //        $html = $converter->convertToHtml($string);
     $this->assertSame($expected, $html);
 }
Beispiel #16
0
 /**
  * Returns rendered view
  *
  * @return string
  */
 public function render()
 {
     if ($this->isMarkdown()) {
         $environment = Environment::createCommonMarkEnvironment();
         $environment->addInlineParser(new TwitterHandleParser());
         $parser = new DocParser($environment);
         $htmlRenderer = new HtmlRenderer($environment);
         $document = $parser->parse(file_get_contents($this->filename));
         return $htmlRenderer->renderBlock($document);
     }
     ob_start();
     extract($this->vars);
     require $this->filename;
     return ob_get_clean();
 }
 /**
  * Register the environment class.
  *
  * @return void
  */
 protected function registerEnvironment()
 {
     $this->app->singleton('markdown.environment', function (Container $app) {
         $environment = Environment::createCommonMarkEnvironment();
         $environment->addExtension(new TableExtension());
         $environment->addExtension(new AttributesExtension());
         $config = $app->config->get('markdown');
         $environment->mergeConfig(array_except($config, ['extensions', 'views']));
         foreach ((array) array_get($config, 'extensions') as $extension) {
             $environment->addExtension($app->make($extension));
         }
         return $environment;
     });
     $this->app->alias('markdown.environment', Environment::class);
 }
Beispiel #18
0
 public function transform($markdownText)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $beforeParseEvent = Croogo::dispatchEvent('Helper.Markdown.beforeMarkdownParse', $this->_View, array('environment' => $environment, 'markdown' => $markdownText));
     $markdownText = $beforeParseEvent->data['markdown'];
     $environment = $beforeParseEvent->data['environment'];
     $parser = new DocParser($environment);
     $htmlRenderer = new HtmlRenderer($environment);
     $documentAST = $parser->parse($markdownText);
     $beforeRenderEvent = Croogo::dispatchEvent('Helper.Markdown.beforeMarkdownRender', $this->_View, array('ast' => $documentAST));
     $documentAST = $beforeRenderEvent->data['ast'];
     $rendered = $htmlRenderer->renderBlock($documentAST);
     $afterRenderEvent = Croogo::dispatchEvent('Helper.Markdown.afterMarkdownRender', $this->_View, array('rendered' => $rendered));
     return $afterRenderEvent->data['rendered'];
 }
 /**
  * Register the CommonMark Environment.
  *
  * @return void
  */
 protected function registerMarkdownEnvironment()
 {
     $app = $this->app;
     $app->singleton('commonmark.environment', function ($app) {
         $config = $app['config']['markdown'];
         $environment = Environment::createCommonMarkEnvironment();
         if ($config['configurations']) {
             $environment->mergeConfig($config['configurations']);
         }
         foreach ($config['extensions'] as $extension) {
             if (class_exists($extension)) {
                 $environment->addExtension(new $extension());
             }
         }
         return $environment;
     });
     $app->alias('commonmark.environment', Environment::class);
 }
 public function init()
 {
     $this->markdown = new CommonMarkConverter(apply_filters('cws_markdown_config', []), apply_filters('cws_markdown_environment', Environment::createCommonMarkEnvironment()));
     load_plugin_textdomain('markdown-on-save', NULL, basename(dirname(__FILE__)));
     add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10, 2);
     // add_action( 'do_meta_boxes', array( $this, 'do_meta_boxes' ), 20, 2 );
     add_action('post_submitbox_misc_actions', array($this, 'submitbox_actions'));
     add_filter('edit_post_content', array($this, 'edit_post_content'), 10, 2);
     add_filter('edit_post_content_filtered', array($this, 'edit_post_content_filtered'), 10, 2);
     add_action('load-post.php', array($this, 'load'));
     add_action('load-post.php', array($this, 'enqueue'));
     add_action('load-post-new.php', array($this, 'enqueue'));
     add_action('xmlrpc_call', array($this, 'xmlrpc_actions'));
     add_action('init', array($this, 'maybe_remove_kses'), 99);
     add_action('set_current_user', array($this, 'maybe_remove_kses'), 99);
     add_action('wp_insert_post', array($this, 'wp_insert_post'));
     add_action('wp_restore_post_revision', array($this, 'wp_restore_post_revision'), 10, 2);
     add_filter('_wp_post_revision_fields', array($this, '_wp_post_revision_fields'));
 }
Beispiel #21
0
 private function createEnvironment()
 {
     $this->environment = Environment::createCommonMarkEnvironment();
     $this->addInlineRenderers();
 }
Beispiel #22
0
}, RealPathListener::class => object(), FileExistsCheck::class => object(), PhpLintCheck::class => object(), CodeParseCheck::class => function (ContainerInterface $c) {
    return new CodeParseCheck($c->get(Parser::class));
}, FunctionRequirementsCheck::class => function (ContainerInterface $c) {
    return new FunctionRequirementsCheck($c->get(Parser::class));
}, DatabaseCheck::class => object(), ComposerCheck::class => object(), Filesystem::class => object(), Parser::class => function () {
    $parserFactory = new ParserFactory();
    return $parserFactory->create(ParserFactory::PREFER_PHP7);
}, CodePatcher::class => function (ContainerInterface $c) {
    $patch = (new Patch())->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'ini_set("display_errors", 1);'))->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'error_reporting(E_ALL);'))->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'date_default_timezone_set("Europe/London");'));
    return new CodePatcher($c->get(Parser::class), new Standard(), $patch);
}, FakerGenerator::class => function () {
    return FakerFactory::create();
}, RequestRenderer::class => object(), TerminalInterface::class => factory([TerminalFactory::class, 'fromSystem']), 'menu' => factory(MenuFactory::class), MenuFactory::class => object(), ExerciseRenderer::class => function (ContainerInterface $c) {
    return new ExerciseRenderer($c->get('appName'), $c->get(ExerciseRepository::class), $c->get(UserState::class), $c->get(UserStateSerializer::class), $c->get(MarkdownRenderer::class), $c->get(Color::class), $c->get(OutputInterface::class));
}, MarkdownRenderer::class => function (ContainerInterface $c) {
    $docParser = new DocParser(Environment::createCommonMarkEnvironment());
    $cliRenderer = (new MarkdownCliRendererFactory())->__invoke($c);
    return new MarkdownRenderer($docParser, $cliRenderer);
}, UserStateSerializer::class => function (ContainerInterface $c) {
    return new UserStateSerializer(getenv('HOME'), $c->get('workshopTitle'), $c->get(ExerciseRepository::class));
}, UserState::class => function (ContainerInterface $c) {
    return $c->get(UserStateSerializer::class)->deSerialize();
}, SyntaxHighlighter::class => factory(PsxFactory::class), PsxFactory::class => object(), ResetProgress::class => function (ContainerInterface $c) {
    return new ResetProgress($c->get(UserStateSerializer::class));
}, ResultRendererFactory::class => function (ContainerInterface $c) {
    $factory = new ResultRendererFactory();
    $factory->registerRenderer(FunctionRequirementsFailure::class, FunctionRequirementsFailureRenderer::class);
    $factory->registerRenderer(Failure::class, FailureRenderer::class);
    $factory->registerRenderer(CgiResult::class, CgiResultRenderer::class, function (CgiResult $result) use($c) {
        return new CgiResultRenderer($result, $c->get(RequestRenderer::class));
    });
Beispiel #23
0
 /**
  * Create a new instance of commonMark with some custom parser rules
  * 
  * @param  string $mediaRoot
  * @return \League\CommonMark\Environment
  */
 protected function initializeCommonMark($mediaRoot)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->addInlineParser(new MediaParser($mediaRoot));
     return $environment;
 }
Beispiel #24
0
 /**
  * @return DocParser
  */
 protected static function getParser()
 {
     if (static::$parser === null) {
         $environment = Environment::createCommonMarkEnvironment();
         $environment->addExtension(new TableExtension());
         static::$parser = new DocParser($environment);
     }
     return static::$parser;
 }
 /**
  * Register the environment class.
  *
  * @param \Illuminate\Contracts\Foundation\Application $app
  *
  * @return void
  */
 protected function registerEnvironment(Application $app)
 {
     $app->singleton('markdown.environment', function ($app) {
         $environment = Environment::createCommonMarkEnvironment();
         $config = array_only($app->config->get('markdown'), ['renderer', 'enable_em', 'enable_strong', 'use_asterisk', 'use_underscore', 'safe']);
         $environment->mergeConfig($config);
         return $environment;
     });
     $app->alias('markdown.environment', Environment::class);
 }
Beispiel #26
0
 * Builds static pages from README and code samples.
 */
use Aura\Cli\CliFactory;
require_once 'vendor/autoload.php';
//cli setup
$factory = new CliFactory();
$cli = $factory->newContext($GLOBALS);
$io = $factory->newStdio();
$getop = $cli->getopt(['b,build:', 'p,path:', 'e,env:']);
//config
$buildDir = $getop->get('--build', __DIR__ . '/_build');
$webPath = $getop->get('--path', '/Quickstarts');
$environment = $getop->get('--env', 'development');
//markdown
$io->out('setting up markdown...');
$env = \League\CommonMark\Environment::createCommonMarkEnvironment();
$exampleParser = new ExampleParser();
$exampleRenderer = new ExampleRenderer();
$linkRenderer = new LinkRenderer(['nexmo.github.io', 'nexmo.com', 'docs.nexmo.com', 'dashboard.nexmo.com', 'help.nexmo.com']);
$io->out('adding custom parsers/renderers...');
$env->addBlockParser($exampleParser);
$env->addBlockRenderer('ExampleElement', $exampleRenderer);
$env->addInlineRenderer('League\\CommonMark\\Inline\\Element\\Link', $linkRenderer);
$parser = new \League\CommonMark\DocParser($env);
$renderer = new \League\CommonMark\HtmlRenderer($env);
$io->outln('done');
//mustache
$io->out('setting up mustache template loader...');
$mustache = new Mustache_Engine(['loader' => new Mustache_Loader_FilesystemLoader(__DIR__ . '/_templates')]);
$io->outln('done');
$io->out('checking for index README.md...');
 /**
  * Register the environment class.
  *
  * @param \Illuminate\Contracts\Foundation\Application $app
  *
  * @return void
  */
 protected function registerEnvironment(Application $app)
 {
     $app->singleton('markdown.environment', function ($app) {
         return Environment::createCommonMarkEnvironment();
     });
     $app->alias('markdown.environment', Environment::class);
 }
 /**
  * Process the payload of an object
  *
  * @param string $payload Payload
  * @return string Processed payload
  */
 public function processPayload($payload)
 {
     // Reset all relevant relations
     $this->resetRefersToRelations();
     $this->resetEmbedsRelations();
     $env = Environment::createCommonMarkEnvironment();
     $env->addDocumentProcessor($this);
     // Parse and process the object payload
     /** @var DocParser $docParser */
     $docParser = Kernel::create(DocParser::class, [$env]);
     $docParser->parse($payload);
     return $payload;
 }
Beispiel #29
0
 /**
  * Extract the title and abstract out of the payload
  *
  * @param string $markdownPayload Markdown payload
  * @return array Titel and abstract
  */
 protected function extractTitleAndAbstract($markdownPayload)
 {
     $abstract = trim($markdownPayload);
     if (preg_match('%^(.+?)\\R%', $markdownPayload, $firstParagraph)) {
         $abstract = trim($firstParagraph[1]);
     }
     // Strip formatting
     if (strlen($abstract)) {
         $environment = Environment::createCommonMarkEnvironment();
         /** @var CommonMarkConverter $converter */
         $converter = Kernel::create(CommonMarkConverter::class, [[], $environment]);
         $abstract = trim(strip_tags($converter->convertToHtml($abstract)));
     }
     $title = $abstract;
     return [$title, $abstract];
 }