<commit_before><?php
namespace NyroDev\UtilityBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Finder\Finder;

/**
 * Symfony2 command to convert CSS file from rem to px
 */
class rem2pxCommand extends ContainerAwareCommand {
	
	/**
	 * Configure the command
	 */
	protected function configure() {
		$this
			->setName('nyrodev:rem2px')
			->setDescription('Convert CSS files into px')
			->addArgument('src', InputArgument::REQUIRED, 'Which files to parse?')
			->addArgument('dst', InputArgument::REQUIRED, 'Where to save the file?')
			->addOption('append', 'a', InputOption::VALUE_NONE, 'Append to dst file?');
	}
	
	/**
	 * Executes the command
	 *
	 * @param InputInterface $input
	 * @param OutputInterface $output 
	 */
	protected function execute(InputInterface $input, OutputInterface $output) {
		$src = $input->getArgument('src');
		$dst = $input->getArgument('dst');
		$append = $input->getOption('append');
		
		// Create CSS dest object
		$dstCss = new \Sabberworm\CSS\Parser($append && file_exists($dst) ? file_get_contents($dst) : null);
		$dstCssDoc = $dstCss->parse();
		
		$finder = new Finder();
		$resources = $finder
					->files()
					->depth(0)
					->in($src)
					->name('*.css');
		
		foreach($resources as $res) {
			$output->writeln('CSS Found: '.$res->getRealPath());
			$css = new \Sabberworm\CSS\Parser(file_get_contents($res->getRealPath()));
			$cssDoc = $css->parse();
			foreach($cssDoc->getContents() as $block) {
				if ($block instanceof \Sabberworm\CSS\RuleSet\DeclarationBlock) {
					/* @var $ruleset \Sabberworm\CSS\RuleSet\DeclarationBlock */
					// Keep only the root CSS (ignore media queries)
					$keepRules = array();
					foreach($block->getRules() as $rule) {
						/* @var $rule \Sabberworm\CSS\Rule\Rule */
						$value = $rule->getValue();
						if (is_object($value)) {
							// keep only object with size rem
							$keepValue = false;
							switch(get_class($value)) {
								case 'Sabberworm\CSS\Value\Size':
									/* @var $value \Sabberworm\CSS\Value\Size */
									if ($value->getUnit() == 'rem') {
										$value->setSize($value->getSize() * 10);
										$value->setUnit('px');
										$keepValue = true;
									}
									break;
								case 'Sabberworm\CSS\Value\RuleValueList':
									/* @var $value \Sabberworm\CSS\Value\RuleValueList */
									foreach($value->getListComponents() as $c) {
										if ($c instanceof \Sabberworm\CSS\Value\Size)
											if ($c->getUnit() == 'rem') {
												$c->setSize($c->getSize() * 10);
												$c->setUnit('px');
												$keepValue = true;
											}
									}
									break;
							}
							if ($keepValue)
								$keepRules[] = $rule;
						}
					}
					
					if (count($keepRules)) {
						$oItem = new \Sabberworm\CSS\RuleSet\DeclarationBlock();
						$oItem->setSelectors($block->getSelectors());
						foreach($keepRules as $rule)
							$oItem->addRule($rule);
						
						$dstCssDoc->append($oItem);
					}
				}
			}
		}
		
		$output->writeln('Write '.$dst);
		file_put_contents($dst, $dstCssDoc->render(\Sabberworm\CSS\OutputFormat::createPretty()));
	}
}<commit_msg>Update rem2px command to add a selector option<commit_after><?php
namespace NyroDev\UtilityBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Finder\Finder;

/**
 * Symfony2 command to convert CSS file from rem to px
 */
class rem2pxCommand extends ContainerAwareCommand {
	
	/**
	 * Configure the command
	 */
	protected function configure() {
		$this
			->setName('nyrodev:rem2px')
			->setDescription('Convert CSS files into px')
			->addArgument('src', InputArgument::REQUIRED, 'Which files to parse?')
			->addArgument('dst', InputArgument::REQUIRED, 'Where to save the file?')
			->addArgument('selector', InputArgument::OPTIONAL, 'Selector for files to parse?', '*')
			->addOption('append', 'a', InputOption::VALUE_NONE, 'Append to dst file?');
	}
	
	/**
	 * Executes the command
	 *
	 * @param InputInterface $input
	 * @param OutputInterface $output 
	 */
	protected function execute(InputInterface $input, OutputInterface $output) {
		$src = $input->getArgument('src');
		$dst = $input->getArgument('dst');
		$selector = $input->getArgument('selector');
		$append = $input->getOption('append');
		
		// Create CSS dest object
		$dstCss = new \Sabberworm\CSS\Parser($append && file_exists($dst) ? file_get_contents($dst) : null);
		$dstCssDoc = $dstCss->parse();
		
		$finder = new Finder();
		$resources = $finder
					->files()
					->depth(0)
					->in($src)
					->name($selector.'.css');
		
		foreach($resources as $res) {
			$output->writeln('CSS Found: '.$res->getRealPath());
			$css = new \Sabberworm\CSS\Parser(file_get_contents($res->getRealPath()));
			$cssDoc = $css->parse();
			foreach($cssDoc->getContents() as $block) {
				if ($block instanceof \Sabberworm\CSS\RuleSet\DeclarationBlock) {
					/* @var $ruleset \Sabberworm\CSS\RuleSet\DeclarationBlock */
					// Keep only the root CSS (ignore media queries)
					$keepRules = array();
					foreach($block->getRules() as $rule) {
						/* @var $rule \Sabberworm\CSS\Rule\Rule */
						$value = $rule->getValue();
						if (is_object($value)) {
							// keep only object with size rem
							$keepValue = false;
							switch(get_class($value)) {
								case 'Sabberworm\CSS\Value\Size':
									/* @var $value \Sabberworm\CSS\Value\Size */
									if ($value->getUnit() == 'rem') {
										$value->setSize($value->getSize() * 10);
										$value->setUnit('px');
										$keepValue = true;
									}
									break;
								case 'Sabberworm\CSS\Value\RuleValueList':
									/* @var $value \Sabberworm\CSS\Value\RuleValueList */
									foreach($value->getListComponents() as $c) {
										if ($c instanceof \Sabberworm\CSS\Value\Size)
											if ($c->getUnit() == 'rem') {
												$c->setSize($c->getSize() * 10);
												$c->setUnit('px');
												$keepValue = true;
											}
									}
									break;
							}
							if ($keepValue)
								$keepRules[] = $rule;
						}
					}
					
					if (count($keepRules)) {
						$oItem = new \Sabberworm\CSS\RuleSet\DeclarationBlock();
						$oItem->setSelectors($block->getSelectors());
						foreach($keepRules as $rule)
							$oItem->addRule($rule);
						
						$dstCssDoc->append($oItem);
					}
				}
			}
		}
		
		$output->writeln('Write '.$dst);
		file_put_contents($dst, $dstCssDoc->render(\Sabberworm\CSS\OutputFormat::createPretty()));
	}
}
{'fraction_non_alphanumeric': 0.16326530612244897, 'fraction_numerical': 0.0024432308134521414, 'mean_word_length': 2.9697661152310326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1845864', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2196, 'n_words': 432}