Skip to content

Instantly share code, notes, and snippets.

@Neunerlei
Last active January 19, 2018 19:35
Show Gist options
  • Save Neunerlei/9991f24875ed5421415ff0643233b4ab to your computer and use it in GitHub Desktop.
Save Neunerlei/9991f24875ed5421415ff0643233b4ab to your computer and use it in GitHub Desktop.
Tiny helper to convert fontastic stylesheets into less / sass variables
<?php
/**
* User: Martin Neundorfer
* Date: 19.01.2018
* Time: 13:28
* Vendor: LABOR.digital
*/
/**
* Converts a fontastic css file into a list of variables
*/
if (isset($_POST['set']))
{
// Check for config
$asSass = isset($_POST['asSass']);
$removePrefix = isset($_POST['removePrefix']);
$customPrefix = isset($_POST['customPrefix']) ? $_POST['customPrefix'] : 'icon-';
// Convert to rows
echo '<pre>';
$rows = explode(PHP_EOL, $_POST['set']);
$var = null;
$prefix = null;
$preParsed = $parsed = [];
foreach($rows as $c => $row){
$row = trim($row);
// Handle variable value
if(!empty($var)){
// Validate line
if(stripos($row, 'content:')){
die('Maleformed content in line: '.$c. ' a line like content: "\e001"; was expected!');
}
// Extract value
$value = substr($row, 10, -2);
// Store value
$preParsed[$var] = $value;
unset($var);
unset($value);
}
// Handle variable definition
if(!isset($row[0]) || $row[0] !== '.') continue;
if(($endPos = stripos($row, ':before')) === false) continue;
$parsedRow = substr($row, 1, $endPos - 1);
$var = $parsedRow;
// Parse for prefix
if($prefix === null || $removePrefix === false) {
$prefix = $parsedRow;
} else {
// Check if the prefix matches
if(stripos($parsedRow, $prefix) === 0) continue;
// Find a more fitting prefix
for($i = 1; $i <= strlen($parsedRow); $i++){
$prefixSearch = substr($parsedRow, 0, -$i);
if(stripos($prefix, $prefixSearch) === 0){
$prefix = $prefixSearch;
break;
}
}
// Check for empty prefix
if(empty($prefixSearch)){
$prefix = '';
}
}
}
// Check if we should remove the prefix
if($removePrefix === false){
// Ignore prefix
$parsed = $preParsed;
} else {
// Remove prefix
// Check if we found a prefix
$prefixLength = strlen($prefix);
$hasPrefix = $prefixLength > 0;
// Remove prefix and clean up values
foreach($preParsed as $k => $v){
if($hasPrefix) $k = substr($k, $prefixLength);
$k = trim(trim(trim($k), '.,-_\\/'));
$v = trim($v);
$parsed[$k] = $v;
}
}
// Build the output
echo '@charset "UTF-8";'.PHP_EOL.PHP_EOL;
foreach($parsed as $k => $v){
if($asSass){
echo '$'.$customPrefix.$k.': "'.trim($v).'"'.PHP_EOL;
} else {
echo '@'.$customPrefix.$k.': "'.trim($v).'";'.PHP_EOL;
}
}
// Done
exit();
}
?>
<html>
<head>
<title>Convert icon CSS to icon variables</title>
</head>
<body>
<h1>Convert icon CSS to icon variables</h1>
<p>Put the content of your fontastic styles.css file in the textfield below to convert the classes to a list of less variables.</p>
<form action="" method="post" enctype="multipart/form-data">
<label><input type="checkbox" value="on" name="asSass">Output the result as sass?</label><br/>
<label><input type="checkbox" value="on" name="removePrefix" checked>Remove existing prefix?</label><br/>
<label>Custom prefix <input value="icon-" name="customPrefix"></label><br/>
<textarea name="set" cols="80" rows="10"></textarea><br/>
<button type="submit">Submit</button>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment