1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Highlighter |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\bin\Commands; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Kadet\Highlighter\bin\VerboseOutput; |
20
|
|
|
use Kadet\Highlighter\KeyLighter; |
21
|
|
|
use Kadet\Highlighter\Language\Language; |
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
27
|
|
|
use Symfony\Component\Console\Output\Output; |
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
29
|
|
|
|
30
|
|
|
class HighlightCommand extends Command |
31
|
|
|
{ |
32
|
|
|
protected $_debug = ['time', 'detailed-time', 'count', 'tree-before', 'tree-after', 'density']; |
33
|
|
|
|
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this->setName('highlight') |
37
|
|
|
->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File(s) to highlight') |
38
|
|
|
->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'Source Language to highlight, see <comment>list-languages</comment> command') |
39
|
|
|
->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format, see <comment>list-languages</comment> command', 'cli') |
40
|
|
|
->addOption( |
41
|
|
|
'debug', 'd', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
42
|
|
|
'Debug features (only in verbose): '.implode(', ', array_map(function($f) { return "<info>{$f}</info>"; }, $this->_debug)) |
43
|
|
|
) |
44
|
|
|
->setDescription('<comment>[DEFAULT]</comment> Highlights given file') |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
if(!empty($input->getOption('debug')) && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { |
51
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$output->writeln($this->getApplication()->getLongVersion()."\n", Output::VERBOSITY_VERBOSE); |
55
|
|
|
$formatter = KeyLighter::get()->getFormatter($input->getOption('format')) ?: KeyLighter::get()->getDefaultFormatter(); |
56
|
|
|
|
57
|
|
|
foreach($input->getArgument('path') as $filename) { |
58
|
|
|
$language = $input->getOption('language') |
59
|
|
|
? Language::byName($input->getOption('language')) |
60
|
|
|
: Language::byFilename($filename); |
61
|
|
|
|
62
|
|
|
if(!($source = $this->content($filename))) { |
63
|
|
|
throw new InvalidArgumentException(sprintf('Specified file %s doesn\'t exist, check if given path is correct.', $filename)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if($output->isVerbose()) { |
67
|
|
|
$output->writeln(sprintf( |
68
|
|
|
"Used file: <path>%s</path>, Language: <language>%s</language>, Formatter: <formatter>%s</formatter>", |
69
|
|
|
$filename, $language->getFQN(), get_class($formatter) |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
$verbose = new VerboseOutput($output, $input, $language, $formatter, $source); |
73
|
|
|
$formatted = $verbose->process(); |
74
|
|
|
} else { |
75
|
|
|
$formatted = KeyLighter::get()->highlight($source, $language, $formatter); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if(!$input->getOption('no-output')) { |
79
|
|
|
$output->writeln($formatted); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function content($path) |
85
|
|
|
{ |
86
|
|
|
if(!($file = @fopen($path, 'r'))) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$content = ''; |
91
|
|
|
while(!feof($file)) { |
92
|
|
|
$content .= fgets($file); |
93
|
|
|
} |
94
|
|
|
fclose($file); |
95
|
|
|
|
96
|
|
|
return $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function mergeApplicationDefinition($arguments = true) |
100
|
|
|
{ |
101
|
|
|
parent::mergeApplicationDefinition($this->getApplication()->explicit); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|