Created
September 24, 2019 14:51
-
-
Save earth3300/2e19158fd94555b6289ae5c2472da6ea to your computer and use it in GitHub Desktop.
Creates a set of CSS files from standardized data files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* EC01 CSS From Data Standardized Printer | |
* | |
* Generates valid CSS from a set of data files. | |
* This version uses the standardized data files created from a | |
* complete set of HTML elements and element types, plus a select | |
* list of attributes. | |
* | |
* @package Earth3300\EC01 | |
* @since 1.0.1 | |
* @author Clarence J. Bos <cbos@tnoep.ca> | |
* @copyright Copyright (c) 2018, Clarence J. Bos | |
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL v3.0 | |
* @link https://gist.github.com/earth3300/78d5e020899782efd6f160553de08ef3 | |
* | |
* File: printer.css-from-dat-standardized.php | |
* Created: 2019-09-16 | |
* Updated: 2019-09-24 | |
* Time: 09:30 EDT | |
* ID: ENG-ON-001 | |
*/ | |
namespace Earth3300\EC01; | |
if( ! defined( 'NDA' ) ) | |
{ | |
define('NDA',true); | |
} | |
/** | |
* Prints a CSS file. | |
*/ | |
class CssPrinter | |
{ | |
/** @var array Default options. */ | |
protected $opts = [ | |
'this' => [ | |
'file' => 'printer.css-from-dat-standardized.php', | |
'title' => 'EC01 CSS From Data Printer', | |
'date' => [ | |
'created' => '2019-09-07', | |
], | |
], | |
'dir' => [ | |
'config' => '/0/t/css', | |
'input' => '/0/t/css/dats', | |
'output' => '/0/t/css/dats/src', | |
], | |
'file' => [ | |
'config' => '/css.data.php', | |
'output' => '/style.dats.css', | |
'overwrite' => 1, | |
], | |
'files' => [ | |
'include' => 'all', | |
'exclude' => 'none', | |
], | |
'max' => [ | |
'len' => 3000000, // Bytes | |
], | |
'write' => [ | |
'root' => 1, | |
], | |
'message' => [ | |
'na' => 'Not available.', | |
'write' => [ | |
'denied' => 'Write permission denied.' , | |
'success' => 'Write operation succeeded.', | |
'failure' => 'Write operation failed.', | |
], | |
], | |
]; | |
/** | |
* Init | |
*/ | |
public function init() | |
{ | |
if( $this->security() ) | |
{ | |
$this->authorize(); | |
} | |
} | |
/** | |
* Security | |
*/ | |
private function security() | |
{ | |
if ( '127.0.0.1' == $_SERVER['REMOTE_ADDR'] | |
&& file_exists( __DIR__ . '/.security' ) ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* Authorize | |
*/ | |
private function authorize() | |
{ | |
if( isset( $_GET['unlock'] ) ) | |
{ | |
echo $this->run(); | |
} | |
else | |
{ | |
echo '<pre>Unauthorized.</pre>' . PHP_EOL; | |
} | |
} | |
/** | |
* Run | |
* | |
* @return string | |
*/ | |
private function run() | |
{ | |
if( isset( $_GET['run'] ) ) | |
{ | |
$file = []; | |
$file = $this->getDesc( $file ); | |
$file = $this->getPaths( $file ); | |
$file = $this->getFiles( $file ); | |
$file = $this->getInfo( $file ); | |
$html = $this->getPageHTML( $file ); | |
return $html; | |
} | |
else | |
{ | |
echo '<pre>You wish to run the script?</pre>' . PHP_EOL; | |
} | |
} | |
/** | |
* Get the Description (Purpose) of This File | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function getDesc( $file ) | |
{ | |
$file['text']['desc'] = ''; | |
$file['text']['desc'] = '<div class="description">'; | |
$file['text']['desc'] .= '<p>Converts CSS saved as a data file to valid CSS.</p>'; | |
$file['text']['desc'] .= '</div>' . PHP_EOL; | |
$file['text']['desc'] .= '<div class="clear"></div>' . PHP_EOL; | |
$file['text']['desc'] .= PHP_EOL; | |
return $file; | |
} | |
/** | |
* Get Info | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function getInfo( $file ) | |
{ | |
$file['text']['info'] = null; | |
$str = ''; | |
if( is_array( $file ) ) | |
{ | |
// Input path | |
$str .= isset( $file['path']['input'] ) ? | |
sprintf( '<br>Input Path: %s<br>%s', $file['path']['input'], PHP_EOL ) : ''; | |
// Count of the files. | |
$str .= isset( $file['cnt'] ) ? | |
sprintf( '<br>Files: %s<br>%s', $file['cnt'], PHP_EOL ) : ''; | |
// Output path | |
$str .= isset( $file['path']['output'] ) ? | |
sprintf( '<br>Output Path: %s<br><br>%s', $file['path']['output'], PHP_EOL ) : ''; | |
} | |
$file['text']['info'] = $str; | |
return $file; | |
} | |
/** | |
* Get the Paths | |
* | |
* @return array | |
*/ | |
private function getPaths( $file ) | |
{ | |
$path = []; | |
$path['root'] = rtrim( $_SERVER['DOCUMENT_ROOT'], '/' ); | |
$path['path']['input'] = $path['root']; | |
$path['path']['config'] = $path['root']; | |
$path['path']['input'] .= $this->opts['dir']['input']; | |
$path['path']['config'] .= $this->opts['dir']['config']; | |
$path['path']['output'] = $path['root']; | |
$path['path']['output'] .= $this->opts['dir']['output']; | |
$path['file']['config'] = $path['path']['config']; | |
$path['file']['config'] .= $this->opts['file']['config']; | |
return $path; | |
} | |
/** | |
* Get the Files | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function getFiles( $file ) | |
{ | |
if( is_dir( $file['path']['input'] ) ) | |
{ | |
$match = $file['path']['input'] . '/*.php'; | |
$items = glob( $match ); | |
// Loop through the files | |
foreach( $items as $cnt => $item ) | |
{ | |
// load the file. | |
if( file_exists( $item ) && require_once( $item ) ) | |
{ | |
$this->prcItem( $file, $item ); | |
} | |
} | |
} | |
return $file; | |
} | |
/** | |
* Process Item Based on HTML Element Type | |
* | |
* Need the function names | |
*/ | |
private function prcItem( $file, $item ) | |
{ | |
if( isset( $file['file']['output'] ) ) | |
{ | |
// Have to get the function, and know what the function is!!! | |
if( strpos( $item, 'sectioning' ) !== false ) | |
{ | |
$item = get_sectioning_data(); | |
} | |
elseif( strpos( $item, 'content' ) !== false ) | |
{ | |
$item = get_content_data(); | |
} | |
elseif( strpos( $item, 'structural' ) !== false ) | |
{ | |
$item = get_structural_data(); | |
} | |
elseif( strpos( $item, 'semantics' ) !== false ) | |
{ | |
$item = get_semantics_data(); | |
} | |
elseif( strpos( $item, 'media' ) !== false ) | |
{ | |
$item = get_media_data(); | |
} | |
elseif( strpos( $item, 'embedded' ) !== false ) | |
{ | |
$item = get_embedded_data(); | |
} | |
elseif( strpos( $item, 'script' ) !== false ) | |
{ | |
$item = get_script_data(); | |
} | |
elseif( strpos( $item, 'edits' ) !== false ) | |
{ | |
$item = get_edits_data(); | |
} | |
elseif( strpos( $item, 'table' ) !== false ) | |
{ | |
$item = get_table_data(); | |
} | |
elseif( strpos( $item, 'forms' ) !== false ) | |
{ | |
$item = get_forms_data(); | |
} | |
elseif( strpos( $item, 'interactive' ) !== false ) | |
{ | |
$item = get_interactive_data(); | |
} | |
elseif( strpos( $item, 'components' ) !== false ) | |
{ | |
$item = get_components_data(); | |
} | |
else | |
{ | |
// Do nothing. | |
} | |
} | |
else | |
{ | |
printf( "<pre>Houston, please check $file['file']['output']" ); | |
} | |
$name = pathinfo( $item['file']['ouptput'], PATHINFO_BASENAME ); | |
$file['file']['output'] .= '/' . str_replace( '.php', '.css', $name ); | |
$file = $this->genFile( $file, $item, $name ); | |
} | |
/** | |
* Generates the File | |
* | |
* @param array $file | |
* @param array $item | |
* | |
* @return array | |
*/ | |
private function genFile( $file, $item, $name ) | |
{ | |
if( is_array( $file ) && is_array( $item ) ) | |
{ | |
$file['str'] = ''; | |
$file = $this->getFileHeader( $file, $item, $name ); | |
$file = $this->genCSS( $file, $item ); | |
$file = $this->getFileSize( $file, $item ); | |
$file = $this->getFileFooter( $file, $item ); | |
$file['text']['resp'] .= $this->printFile( $file, $item ); | |
return $file; | |
} | |
else | |
{ | |
echo '<pre>Not an array.</pre>' . PHP_EOL; | |
} | |
} | |
/** | |
* Generate CSS | |
* | |
* Generates valid CSS as a string and returns in an array. | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function genCSS( $file, $item ) | |
{ | |
$str = ''; | |
$strValue = ''; | |
if( is_array( $item ) && count( $item ) > 0 ) | |
{ | |
foreach( $item as $selector => $properties ) | |
{ | |
if( 'type' == $selector || 'use' == $selector ) | |
{ | |
continue; | |
} | |
// always use a media selector to block out sections. | |
if( 'media' == $selector ) | |
{ | |
$str .= sprintf( '@media %s {%s', $properties, PHP_EOL ); | |
} | |
else | |
{ | |
if( is_array( $properties ) | |
&& count( $properties ) > 0 ) | |
{ | |
$str .= sprintf( ' %s {%s', $selector, PHP_EOL ); | |
foreach( $properties as $key => $value ) | |
{ | |
if( strlen( $value ) > 0 ) | |
{ | |
$str .= sprintf( ' %s: %s;%s', $key, $value, PHP_EOL ); | |
} | |
} | |
$str .= ' }' . PHP_EOL; | |
$str .= PHP_EOL; | |
} | |
} | |
} | |
// Close media selector. | |
$str .= '}' . PHP_EOL; | |
} | |
$file['str'] .= $str; | |
return $file; | |
} | |
/** | |
* Get File Size | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function getFileSize( $file ) | |
{ | |
$file['size'] = null; | |
if( isset( $file['str'] ) ) | |
{ | |
$file['size'] = strlen( $file['str'] ); | |
} | |
return $file; | |
} | |
/** | |
* Print the File | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function printFile( $file, $item ) | |
{ | |
$str = ''; | |
if ( isset( $_GET['print'] ) ) | |
{ | |
if ( isset( $_POST['safety'] ) ) | |
{ | |
if( isset( $item['file']['output'] ) && isset( $item['str'] ) ) | |
{ | |
if( ! file_exists( $item['file']['output'] ) | |
|| $this->opts['file']['overwrite'] ) | |
{ | |
$resp = file_put_contents( $item['file']['output'], $item['str'] ); | |
$str = sprintf( | |
'File: %s (%s B)<br>%s', $item['file']['output'], $resp, PHP_EOL ); | |
} | |
else | |
{ | |
$str = sprintf( | |
'File: %s (exists)<br>%s', $item['file']['output'], PHP_EOL ); | |
} | |
} | |
} | |
} | |
return $str; | |
} | |
/** | |
* File Header | |
* | |
* @return string | |
*/ | |
private function getFileHeader( $file, $item, $name ) | |
{ | |
// Initialize | |
$str = ''; | |
$str .= '/*' . PHP_EOL; | |
$str .= sprintf( 'File: %s%s', $name, PHP_EOL ); | |
$str .= sprintf( 'Generated by: %s%s', | |
$this->opts['this']['file'], PHP_EOL ); | |
$str .= PHP_EOL; | |
$str .= $this->getTimeStamp(); | |
$str .= $this->getIDStamp(); | |
$str .= '*/' . PHP_EOL; | |
$str .= PHP_EOL; | |
$file['str'] = $str; | |
return $file; | |
} | |
/** | |
* Time Stamp (Including Created Date) | |
* | |
* @return string | |
*/ | |
private function getTimeStamp() | |
{ | |
$str = ''; | |
$str .= sprintf( 'Created: %s%s', | |
$this->opts['this']['date']['created'], PHP_EOL ); | |
$str .= sprintf( 'Updated: %s%s', date( 'Y-m-d' ), PHP_EOL ); | |
$str .= sprintf( 'Time: %s%s', date( 'H:i:s T' ), PHP_EOL ); | |
return $str; | |
} | |
/** | |
* ID Stamp | |
* | |
* @return string | |
*/ | |
private function getIDStamp() | |
{ | |
$str = ''; | |
$str .= 'ID: ENG-ON-001' . PHP_EOL; | |
return $str; | |
} | |
/** | |
* File Footer | |
* | |
* For use at the end of the file as needed. | |
*/ | |
private function getFileFooter( $file, $item ) | |
{ | |
// Initialize | |
$str = PHP_EOL; | |
$str .= '/*'; | |
$str .= ' End of File ('; | |
$str .= $file['size'] . ' B).'; | |
$str .= ' */' . PHP_EOL; | |
$str .= PHP_EOL; | |
// Add this string to the end... | |
$file['str'] .= $str; | |
return $file; | |
} | |
/** | |
* Get Form | |
* | |
* @return string | |
*/ | |
private function getForm() | |
{ | |
if( isset( $_GET['ctrls'] ) ) | |
{ | |
// Initialize | |
$str = ''; | |
$str .= '<form action="" method="post">' . PHP_EOL; | |
$str .= '<div>' . PHP_EOL; | |
$str .= '<input type="checkbox" name="safety" id="safety" required> ' . PHP_EOL; | |
$str .= '<input type="submit" class="btn btn-primary" value="Run">' . PHP_EOL; | |
$str .= '</div>' . PHP_EOL; | |
$str .= '</form>' . PHP_EOL; | |
return $str; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* Wrap the HTML in Page HTML `<!DOCTYPE html>`, etc. | |
* | |
* Use basic settings. Assume no SEO necessary. Bootstrap CSS only. | |
* | |
* @param string $html | |
* | |
* @return string|bool | |
*/ | |
private function getPageHTML( $file ) | |
{ | |
$str = '<!DOCTYPE html>' . PHP_EOL; | |
$str .= '<html lang="en-CA" class="theme-dark">' . PHP_EOL; | |
$str .= '<head>' . PHP_EOL; | |
$str .= '<meta charset="UTF-8">' . PHP_EOL; | |
$str .= '<meta name="viewport" content="width=device-width, initial-scale=1"/>' . PHP_EOL; | |
$str .= sprintf( '<title>%s</title>%s', | |
$this->opts['this']['title'], PHP_EOL ); | |
$str .= '<meta name="robots" content="noindex,nofollow" />' . PHP_EOL; | |
$str .= '<link rel=stylesheet href="/style.min.css">' . PHP_EOL; | |
$str .= '</head>' . PHP_EOL; | |
$str .= '<body>' . PHP_EOL; | |
$str .= '<main>' . PHP_EOL; | |
$str .= sprintf( '<h1>%s</h1>%s', $this->opts['this']['title'], PHP_EOL ); | |
$str .= isset( $file['text']['desc'] ) ? $file['text']['desc'] : ''; | |
$str .= $this->getForm(); | |
$str .= isset( $file['text']['info'] ) ? $file['text']['info'] : ''; | |
$str .= isset( $file['text']['resp'] ) ? $file['text']['resp'] : ''; | |
if( isset( $_GET['display'] ) ) | |
{ | |
if( isset( $file['str'] ) ) | |
{ | |
$str .= '<pre>' . PHP_EOL; | |
$str .= $file['str']; | |
$str .= '</pre>' . PHP_EOL; | |
} | |
} | |
$str .= '</main>' . PHP_EOL; | |
$str .= '</html>' . PHP_EOL; | |
return $str; | |
} | |
} // End Class | |
if ( ! function_exists( 'pre_dump' ) ) { | |
/** Dumps a formatted string or array. */ | |
function pre_dump( $arr, $continue = 1 ) { | |
if ( isset( $_GET['debug'] ) ) { | |
if (is_string( $arr ) ) { | |
$arr = str_replace( ['<','>'], ['<','>'], $arr ); | |
} | |
echo "<pre>" . PHP_EOL; | |
var_dump( $arr ); | |
if ( isset( $_GET['exit'] ) || ! $continue ) { | |
exit( '<br/>' . 'Done.' ); | |
} | |
echo "</pre>" . PHP_EOL; | |
} | |
} | |
} | |
function ec01_css_printer() | |
{ | |
$printer = new CssPrinter(); | |
return $printer->init(); | |
} | |
ec01_css_printer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment