/** * Build the scripts. * * @return void * @access public */ public function build() { $project = $this->getProject(); $manifest = $project->getManifestData(); $script_options = $manifest['scripts']; if (isset($script_options['libraries'])) { \Curator\Console::stdout(' Minifying libraries…'); foreach ($script_options['libraries'] as $filename) { $path = $project->getScriptsLibsDirPath() . DS . $filename; $rel_path = str_replace($project->getProjectDirPath() . DS, '', $path); $basename = pathinfo($filename, PATHINFO_FILENAME); $raw_size = filesize($path); $script_data = file_get_contents($path); $handler = HandlerFactory::getHandlerForMediaType('text/javascript'); $script_data = $handler->input($script_data); $hash = hash('sha256', $script_data); $out_path = $project->getPublicScriptsLibsDirPath() . DS . $basename . '-' . $hash . '.js'; $out_rel = str_replace($project->getProjectDirPath() . DS, '', $out_path); $out_url = str_replace($project->getPublicHtmlDirPath(), '', $out_path); if (!file_exists($out_path)) { touch($out_path); } if (!file_put_contents($out_path, $script_data)) { throw new \Exception('Could not save script library: ' . $out_path); } $output_size = filesize($out_path); $a = sprintf('%d', 100 * ($output_size / $raw_size)); \Curator\Console::stdout(' Wrote ' . $out_rel . ' (' . $a . '%)'); $lib_paths[$basename] = $out_url; } \Curator\TemplateData::setValue('scripts', 'libs', $lib_paths); \Curator\Console::stdout(''); } if (isset($script_options['combine'])) { \Curator\Console::stdout(' Minifying scripts…'); $script_data = null; foreach ($script_options['combine'] as $filename) { $path = $project->getScriptsDirPath() . DS . $filename; $rel_path = str_replace($project->getProjectDirPath() . DS, '', $path); $raw_size = filesize($path); $script_data = $script_data . NL . NL . NL . file_get_contents($path); } $handler = \Curator\Handler\Factory::getHandlerForMediaType('text/javascript'); $script_data = $handler->input($script_data); $hash = hash('sha256', $script_data); $out_path = $project->getPublicScriptsDirPath() . DS . 'combined-' . $hash . '.js'; $out_rel = str_replace($project->getProjectDirPath() . DS, '', $out_path); $out_url = str_replace($project->getPublicHtmlDirPath(), '', $out_path); if (!file_exists($out_path)) { touch($out_path); } if (!file_put_contents($out_path, $script_data)) { throw new \Exception('Could not save script file: ' . $out_path); } $output_size = filesize($out_path); $a = sprintf('%d', 100 * ($output_size / $raw_size)); \Curator\Console::stdout(' Wrote ' . $out_rel . ' (' . $a . '%)'); \Curator\TemplateData::setValue('scripts', 'combined', $out_url); } }
/** * Build the stylesheet files. * * @return void * @access public */ public function build() { $project = $this->project; $manifest = $project->getManifestData(); $style_options = $manifest['styles']; $css_data = ''; $raw_size = 0; if (is_array($style_options['combine'])) { foreach ($style_options['combine'] as $filename) { $filepath = $this->project->getStylesDirPath() . DS . $filename; $rel_path = str_replace($this->project->getProjectDirPath() . DS, '', $filepath); $raw_size = $raw_size + filesize($filepath); \Curator\Console::stdout(' Loading ' . $rel_path); $css_data = $css_data . NL . NL . NL . file_get_contents($filepath); } \Curator\Console::stdout(' Minimizing styles…'); $handler = \Curator\Handler\Factory::getHandlerForMediaType('text/css'); $css_data = $handler->input($css_data); $hash = hash('sha256', $css_data); $output_path = $project->getPublicStylesDirPath() . DS . 'combined-' . $hash . '.css'; $rel_output = str_replace($this->project->getProjectDirPath() . DS, '', $output_path); $url_output = str_replace($this->project->getPublicHtmlDirPath(), '', $output_path); if (!file_put_contents($output_path, $css_data)) { throw new \Exception('Could not write CSS to: ' . $output_path); } $output_size = filesize($output_path); $a = sprintf('%d', 100 * ($output_size / $raw_size)); \Curator\Console::stdout(' wrote ' . $rel_output . ' (' . $a . '%)'); \Curator\TemplateData::setValue('styles', 'combined', $url_output); } else { \Curator\Console::stdout(' Nothing to do.'); } }
/** * Build the data files. * * This function goes through the contents of the project's data directory, * and attempts to build any files it encounters. Directories are not * supported, and will be skipped. * * Building a file is the process of loading a source data file, handling * its contents, applying that to a template, then writing the finished * template to disk. * * @access public */ public function build() { // Get our cast of characters. $data_dir = $this->project->getDataDirPath(); $data_files = \Curator\FileSystem::getDirectoryContents($data_dir, array('directories' => false)); $manifest = $this->project->getManifestData(); foreach ($data_files as $data_file) { // Gather our facts here. $data_info = pathinfo($data_file); $data_ext = $data_info['extension']; $data_rel = str_replace($this->project->getProjectDirPath() . DS, '', $data_file); // relative from the project dir. $data_media = \Curator\Handler\Factory::getMediaTypeForFileExtension($data_ext); \Curator\Console::stdout(' Building ' . $data_rel); // load the data file. $handler = \Curator\Handler\Factory::getHandlerForMediaType($data_media); $data = $handler->input($data_file); // Stash the data stuff in the template data. \Curator\TemplateData::setValue('data', 'title', $data['header']['title']); if (isset($data['header']['created'])) { \Curator\TemplateData::setValue('data', 'created', $data['header']['created']); } if (isset($data['header']['next_link'])) { \Curator\TemplateData::setValue('data', 'next_link', $data['header']['next_link']); } if (isset($data['header']['prev_link'])) { \Curator\TemplateData::setValue('data', 'prev_link', $data['header']['prev_link']); } \Curator\TemplateData::setValue('data', 'body', $data['body']); \Curator\TemplateData::setValue('site', 'title', $manifest['site']['title']); \Curator\TemplateData::setValue('site', 'tagline', $manifest['site']['tagline']); \Curator\TemplateData::setValue('site', 'host', $manifest['site']['host']); if (isset($data['header']['analytics'])) { \Curator\TemplateData::setValue('site', 'analytics', $manifest['site']['analytics']); } // Figure out what template to use. If the file has a specific // one, use it. Otherwise, grab the one specified in the // project manifest. if (isset($data['header']['template'])) { $template_name = $data['header']['template']; } else { $template_name = $manifest['data']['template']; } // An overly complex way of doing something simple, I'm sure. // Just getting the path to the template file, the media type // for that file, based on its extension, and the handler for // that media type. $template_path = $this->project->getTemplatesDirPath() . DS . $template_name; $template_media = \Curator\Handler\Factory::getMediaTypeForFileExtension(pathinfo($template_path, PATHINFO_EXTENSION)); $tmpl_handler = \Curator\Handler\Factory::getHandlerForMediaType($template_media); // And a template is useless without things to change. $substitutions = array(); // •• This needs to be defined somehow! $substitutions['styles_combined_url'] = \Curator\TemplateData::getValue('styles', 'combined'); $substitutions['site_title'] = \Curator\TemplateData::getValue('site', 'title'); $substitutions['site_tagline'] = \Curator\TemplateData::getValue('site', 'tagline'); $substitutions['site_host'] = \Curator\TemplateData::getValue('site', 'host'); $substitutions['site_analytics'] = \Curator\TemplateData::getValue('site', 'analytics'); $substitutions['data_title'] = \Curator\TemplateData::getValue('data', 'title'); $substitutions['data_created_date'] = date('l, F j, Y', \Curator\TemplateData::getValue('data', 'created')); $substitutions['data_content'] = \Curator\TemplateData::getValue('data', 'body'); $substitutions['scripts_combined_url'] = \Curator\TemplateData::getValue('scripts', 'combined'); $substitutions['data_next_link'] = ''; $substitutions['data_prev_link'] = ''; $script_libs = \Curator\TemplateData::getValue('scripts', 'libs'); if ($script_libs) { foreach ($script_libs as $script_lib => $url) { $key = 'scripts_libs_' . $script_lib . '_url'; $substitutions[$key] = $url; } } if (\Curator\TemplateData::getValue('scripts', 'libs') !== null) { $libs = \Curator\TemplateData::getValue('scripts', 'libs'); foreach ($libs as $lib => $url) { $substitutions['scripts_libs_' . $lib] = $url; } } if (\Curator\TemplateData::getValue('data', 'next_link') !== null) { $link = '<a href="' . \Curator\TemplateData::getValue('data', 'next_link') . '">Next</a>'; $substitutions['data_next_link'] = $link; } if (\Curator\TemplateData::getValue('data', 'prev_link') !== null) { $link = '<a href="' . \Curator\TemplateData::getValue('data', 'prev_link') . '">Previous</a>'; $substitutions['data_prev_link'] = $link; } $tmpl_data = $tmpl_handler->input($template_path, $substitutions); // See if the file specifies a special name, otherwise hack // together something resembling the data's filename. if (isset($data['header']['url'])) { $filename = $data['header']['url']; } else { $filename = pathinfo($data_file, PATHINFO_FILENAME) . '.html'; } $output_path = $this->project->getPublicHtmlDirPath() . DS . $filename; \Curator\Console::stdout(' Creating public_html' . DS . $filename); if (!file_put_contents($output_path, $tmpl_data)) { throw new \Exception('Could not write: ' . $output_path); } } }