Last active
September 26, 2019 15:31
-
-
Save earth3300/29c4bca5fb0f63f48a85a6070ce1bb63 to your computer and use it in GitHub Desktop.
Generates valid CSS from a set of data files using the standardized data.
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/29c4bca5fb0f63f48a85a6070ce1bb63 | |
* | |
* File: printer.css-from-dat-standardized.php | |
* Created: 2019-09-16 | |
* Updated: 2019-09-26 | |
* Time: 08:21 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/cfg', | |
'input' => '/0/t/css/dats', | |
'output' => '/0/t/css/dats/src', | |
], | |
'file' => [ | |
'config' => '/css.data.php', | |
'prefilled' => '/css.prefilled.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 ); | |
if( $file = $this->getCfgFiles( $file ) ) | |
{ | |
$file = $this->prcCfgData( $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 CSS Configuration File(s) | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function getCfgFiles( $file ) | |
{ | |
if( is_dir( $file['path']['config'] ) ) | |
{ | |
if( file_exists( $file['path']['config'] . $this->opts['file']['config'] ) ) | |
{ | |
require_once( $file['path']['config'] . $this->opts['file']['config'] ); | |
} | |
else | |
{ | |
pre_dump( 'CSS config file does not exist.' ); | |
} | |
if( file_exists( $file['path']['config'] . $this->opts['file']['prefilled'] ) ) | |
{ | |
require_once( $file['path']['config'] . $this->opts['file']['prefilled'] ); | |
} | |
else | |
{ | |
pre_dump( 'CSS config file does not exist.' ); | |
} | |
} | |
else | |
{ | |
pre_dump( 'CSS config directory does not exist.' ); | |
} | |
return $file; | |
} | |
/** | |
* Process Item Based on HTML Element Type | |
* | |
* Need the function names | |
* | |
* @param array $item | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function prcCfgData( $file ) | |
{ | |
if( isset( $file['path']['output'] ) ) | |
{ | |
$css = new CssData; | |
$items = $css->getTypes(); | |
foreach ( $items as $item ) | |
{ | |
// Have to get the function, and know what the function is!!! | |
if( strpos( $item, 'sectioning' ) !== false ) | |
{ | |
$item = get_sectioning_data(); | |
$item = $this->getFileInfo( $file, $item ); | |
} | |
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 | |
{ | |
pre_dump( "<pre>Houston, please check file['path']['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['text']['css'] = ''; | |
$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['text']['css'] .= $str; | |
return $file; | |
} | |
/** | |
* Get File Size | |
* | |
* @param array $file | |
* | |
* @return array | |
*/ | |
private function getFileSize( $file ) | |
{ | |
$file['size'] = null; | |
if( isset( $file['text']['css'] ) ) | |
{ | |
$file['size'] = strlen( $file['text']['css'] ); | |
} | |
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['text']['css'] ) ) | |
{ | |
if( ! file_exists( $item['file']['output'] ) | |
|| $this->opts['file']['overwrite'] ) | |
{ | |
$resp = file_put_contents( $item['file']['output'], $item['text']['css'] ); | |
$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['text']['css'] = $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['text']['css'] .= $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['text']['css'] ) ) | |
{ | |
$str .= '<pre>' . PHP_EOL; | |
$str .= $file['text']['css']; | |
$str .= '</pre>' . PHP_EOL; | |
} | |
} | |
$str .= '</main>' . PHP_EOL; | |
$str .= '</html>' . PHP_EOL; | |
return $str; | |
} | |
/** | |
* 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 ) ) | |
{ | |
$item = getFileInfo( $file, $item ); | |
pre_dump( $item, 0 ); | |
$this->prcCfgData( $file, $item ); | |
} | |
} | |
} | |
return $file; | |
} | |
/** | |
* Get File Info | |
* | |
* File Level Info | |
* | |
* Generate the name of the file to which the data is going. | |
* | |
* path, full name (.ext), and base name (no ext). | |
* $files['files'][$cnt] = $this->getFilesInfo( $file, $item ); | |
* | |
* @param $file | |
* @param $items | |
* | |
* @return $item | |
*/ | |
private function getFileInfo( $file, $item ) | |
{ | |
if( isset( $item['path']['input'] ) ) | |
{ | |
$path = pathinfo( $item['file']['input'] ); | |
// name with ext. | |
$item['file']['name'] = $path["basename"]; | |
// name without ext. | |
$item['file']['base'] = $path["filename"]; | |
return $item; | |
} | |
else | |
{ | |
pre_dump( "384: Not set: item['file']['input'] " ); | |
} | |
} | |
} // 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