Skip to content

Commit

Permalink
added --lint|-l (syntax check) option to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
kzykhys committed Aug 7, 2013
1 parent f4646a2 commit f45b429
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ Or using pipe (On Windows in does't work):
--gfm Activate Gfm extensions
--compress (-c) Remove whitespace between HTML tags
--format (-f) Output format (html|xhtml) (default: "html")
--lint (-l) Syntax check only (lint)
```

### Where is the script?
Expand Down
37 changes: 35 additions & 2 deletions src/Ciconia/Console/Command/CiconiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Ciconia\Console\Command;

use Ciconia\Ciconia;
use Ciconia\Exception\SyntaxError;
use Ciconia\Extension\Gfm\FencedCodeBlockExtension;
use Ciconia\Extension\Gfm\InlineStyleExtension;
use Ciconia\Extension\Gfm\TableExtension;
use Ciconia\Extension\Gfm\TaskListExtension;
use Ciconia\Extension\Gfm\WhiteSpaceExtension;
use Ciconia\Renderer\XhtmlRenderer;
Expand Down Expand Up @@ -41,6 +43,7 @@ protected function configure()
->addOption('compress', 'c', InputOption::VALUE_NONE, 'Remove whitespace between HTML tags')
//->addOption('profile', null, InputOption::VALUE_NONE, 'Display events and extensions information')
->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format (html|xhtml)', 'html')
->addOption('lint', 'l', InputOption::VALUE_NONE, 'Syntax check only (lint)')
->setHelp($this->getHelpContent())
;
}
Expand Down Expand Up @@ -70,10 +73,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
new FencedCodeBlockExtension(),
new InlineStyleExtension(),
new TaskListExtension(),
new WhiteSpaceExtension()
new WhiteSpaceExtension(),
new TableExtension()
));
}

if ($input->getOption('lint')) {
return $this->lint($output, $ciconia, $content);
}

$html = $ciconia->render($content);

if ($input->getOption('compress')) {
Expand All @@ -92,8 +100,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
*
* @param InputInterface $input
*
* @return string
* @throws \InvalidArgumentException
*
* @return string
*/
protected function handleInput(InputInterface $input)
{
Expand Down Expand Up @@ -139,6 +148,30 @@ protected function runHelp(InputInterface $input, OutputInterface $output)
$help->run($input, $output);
}

/**
* Lints the content
*
* @param OutputInterface $output
* @param Ciconia $ciconia
* @param $content
*
* @return int
*/
protected function lint(OutputInterface $output, Ciconia $ciconia, $content)
{
try {
$ciconia->render($content, array('strict' => true));
$output->writeln('No syntax errors detected.');

return 0;
} catch (SyntaxError $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');

return 1;
}
}


/**
* --help
*
Expand Down
19 changes: 17 additions & 2 deletions test/Ciconia/Console/Command/CiconiaCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php


use Ciconia\Console\Application;
use Ciconia\Console\Command\CiconiaCommand;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @author Kazuyuki Hayashi <>
* @author Kazuyuki Hayashi <hayashi@valnur.net>
*/
class CiconiaCommandTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -89,6 +88,22 @@ public function testCommandWithCompressOption()
$this->assertEquals($expected, $tester->getDisplay(true));
}

/**
*
*/
public function testCommandWithLintOption()
{
$file = __DIR__ . '/../../Resources/core/strict/table-invalid-body.md';
$tester = $this->createCommandTester();
$return = $tester->execute(array('file' => $file, '--lint' => true, '--gfm' => true));
$this->assertEquals(1, $return);

$file = __DIR__ . '/../../Resources/gfm/table-simple.md';
$tester = $this->createCommandTester();
$return = $tester->execute(array('file' => $file, '--lint' => true, '--gfm' => true));
$this->assertEquals(0, $return);
}

/**
* @return CommandTester
*/
Expand Down

0 comments on commit f45b429

Please sign in to comment.