|
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: '.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
|
|
|
$this->process($input, $output, $filename, $formatter); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function content($path) |
|
63
|
|
|
{ |
|
64
|
|
|
if(!($file = @fopen($path, 'r'))) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$content = ''; |
|
69
|
|
|
while(!feof($file)) { |
|
70
|
|
|
$content .= fgets($file); |
|
71
|
|
|
} |
|
72
|
|
|
fclose($file); |
|
73
|
|
|
|
|
74
|
|
|
return $content; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function mergeApplicationDefinition($arguments = true) |
|
78
|
|
|
{ |
|
79
|
|
|
// Some ugly hack, because symfony/console is not very extensible :( |
|
80
|
|
|
parent::mergeApplicationDefinition($this->getApplication()->explicit); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param InputInterface $input |
|
85
|
|
|
* @param OutputInterface $output |
|
86
|
|
|
* @param $filename |
|
87
|
|
|
* @param $formatter |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function process(InputInterface $input, OutputInterface $output, $filename, $formatter) |
|
90
|
|
|
{ |
|
91
|
|
|
$language = $input->getOption('language') |
|
92
|
|
|
? Language::byName($input->getOption('language')) |
|
93
|
|
|
: Language::byFilename($filename); |
|
94
|
|
|
|
|
95
|
|
|
if (!($source = $this->content($filename))) { |
|
96
|
|
|
throw new InvalidArgumentException(sprintf('Specified file %s doesn\'t exist, check if given path is correct.', |
|
97
|
|
|
$filename)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($output->isVerbose()) { |
|
101
|
|
|
$output->writeln(sprintf( |
|
102
|
|
|
"Used file: <path>%s</path>, Language: <language>%s</language>, Formatter: <formatter>%s</formatter>", |
|
103
|
|
|
$filename, $language->getFQN(), get_class($formatter) |
|
104
|
|
|
)); |
|
105
|
|
|
|
|
106
|
|
|
$verbose = new VerboseOutput($output, $input, $language, $formatter, $source); |
|
107
|
|
|
$formatted = $verbose->process(); |
|
108
|
|
|
} else { |
|
109
|
|
|
$formatted = KeyLighter::get()->highlight($source, $language, $formatter); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!$input->getOption('no-output')) { |
|
113
|
|
|
$output->writeln($formatted); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|