/** * Executes PHP code */ public function php() { $this->CI = get_instance(); // Prep our content $content = '<?php' . $this->tag_content . '?>'; // Run our content through the parser $parser = new Lex_Parser(); $parser->scope_glue(':'); $content = $parser->parse($content, $this->CI->vars, array($this->CI->parse, 'callback'), true); return $content; }
/** * Return a parsed embedded template */ public function embed() { $CI = get_instance(); if (!$this->get_param('file')) { return NULL; } $CI->load->helper('file'); // Load the file. Always an .html $embed_content = read_file(FCPATH . $CI->vars['assets_folder'] . '/embeds/' . $this->get_param('file') . '.html'); if (!$embed_content) { return NULL; } // We don't want our file variable // being parsed with the rest. unset($this->attributes['file']); $vars = array_merge($this->attributes, $CI->vars); $parser = new Lex_Parser(); $parser->scope_glue(':'); return $parser->parse($embed_content, $CI->vars, array($CI->parse, 'callback')); }
/** * Markdown the content */ public function format() { $this->CI = get_instance(); $method = $this->get_param('method', 'markdown'); // Prep our content $content = trim($this->tag_content); // Run our content through the parser $parser = new Lex_Parser(); $parser->scope_glue(':'); $content = $parser->parse($content, $this->CI->vars, array($this->CI->parse, 'callback')); switch ($method) { // Textile case 'textile': require_once 'textile.php'; return TextileThis($content); break; // Default is markdown // Default is markdown default: require_once 'markdown.php'; return Markdown($content); break; } }
/** * Streams content parse * * Special content parser for streams. This accomplishes the following * important functions: * * a. Takes care of legacy multiple relationship parsing * b. Finds and formats special plugin fields * * @access public * @param string - the tag content * @param array - the return data * @param string - stream slug * @param string - stream namespace * @param [bool - whether or not to loop through the results or not] * @param [mixed - null or obj - stream fields. If they are availble, it will * save a mysql query.] * @param string [$id_name] The name of the id we want to pass via 'row_id'. This is almost alway 'id' * @return string - the parsed data */ public function parse_tag_content($content, $data, $stream_slug, $stream_namespace, $loop = false, $fields = null, $id_name = 'id') { // ------------------------------------- // Legacy multiple relationship provision // ------------------------------------- // We should respect our elders. This makes // sure older instances of multiple // relationships won't break. We can probably // remove this after PyroCMS 2.2 // ------------------------------------- $rep = array('{{ streams_core:related', '{{streams_core:related'); $content = str_replace($rep, '{{ streams:related stream="' . $stream_slug . '" base_namespace="' . $stream_namespace . '" entry=id ', $content); $rep = array('{{ streams_core:multiple', '{{streams_core:multiple'); $content = str_replace($rep, '{{ streams_core:multiple stream="' . $stream_slug . '" base_namespace="' . $stream_namespace . '" entry=id ', $content); // ------------------------------------- // Make sure we have our stream fields // ------------------------------------- if (is_null($fields)) { $stream = $this->stream_obj($stream_slug, $stream_namespace); $fields = $this->CI->streams_m->get_stream_fields($stream->id); } // ------------------------------------- // Custom Call Provision // ------------------------------------- // Go through the fields for this stream, and // see if any of them have the magic 'plugin_override' // function. This will allow us to call functions // from within the field type itself. // ------------------------------------- if ($fields) { foreach ($fields as $field) { if (method_exists($this->CI->type->types->{$field->field_type}, 'plugin_override')) { $content = preg_replace('/\\{\\{\\s?' . $field->field_slug . '\\s?/', '{{ streams_core:field row_id="{{ ' . $id_name . ' }}" stream_slug="' . $stream_slug . '" field_slug="' . $field->field_slug . '" namespace="' . $stream_namespace . '" field_type="' . $field->field_type . '" ', $content); $content = preg_replace('/\\{\\{\\s?\\/' . $field->field_slug . '\\s?\\}\\}/', '{{ /streams_core:field }}', $content); } } } // ------------------------------------- // Parse // ------------------------------------- $parser = new Lex_Parser(); $parser->scope_glue(':'); $parser->cumulative_noparse(true); if (!$loop) { return $parser->parse($content, $data, array($this->CI->parser, 'parser_callback')); } $out = ''; foreach ($data as $item) { $out .= $parser->parse($content, $item, array($this->CI->parser, 'parser_callback')); } return $out; }
/** * Callback from template parser * * @param array * @return mixed */ public function parser_callback($plugin, $attributes, $content) { $this->_ci->load->library('plugins'); $return_data = $this->_ci->plugins->locate($plugin, $attributes, $content); if (is_array($return_data) && $return_data) { if (!$this->_is_multi($return_data)) { $return_data = $this->_make_multi($return_data); } // $content = $data['content']; # TODO What was this doing other than throw warnings in 2.0? $parsed_return = ''; $parser = new Lex_Parser(); $parser->scope_glue(':'); foreach ($return_data as $result) { // if ($data['skip_content']) // { // $simpletags->set_skip_content($data['skip_content']); // } $parsed_return .= $parser->parse($content, $result, array($this, 'parser_callback')); } unset($parser); $return_data = $parsed_return; } return $return_data ? $return_data : null; }
/** * Parser Callback * * @param string $module * @param string $attribute * @param string $content * * @return mixed */ public function parser_callback($module, $attribute, $content) { $return_view = NULL; $parsed_return = ''; $output = self::get_view($module, $attribute); $return_view = $output; //loop it up, if its array no use in the template, gotta work it here. if (is_array($output)) { // Need to make sure we have a array and no objects inside the array too. $parser = new Lex_Parser(); $parser->scopeGlue(':'); foreach ($output as $result) { $parsed_return .= $parser->parse($content, $result, array($this, 'parser_callback')); } unset($parser); $return_view = $parsed_return; } return $return_view; }
/** * Streams content parse * * Special content parser for PyroStreams plugin * * @access private * @param string - the tag content * @param array - the return data * @param string - stream slug * @param [bool - whether or not to loop through the results or not] * @return string - the parsed data */ private function streams_content_parse($content, $data, $stream_slug, $loop = false) { // ------------------------------------- // Multiple Provision // ------------------------------------- // Automatically add in multiple streams data. // This makes it easier to call the multiple function // from within the streams tags // ------------------------------------- $rep = array('{{ streams:related', '{{streams:related'); $content = str_replace($rep, '{{ streams:related stream="' . $stream_slug . '" entry=id ', $content); $rep = array('{{ streams:multiple', '{{streams:multiple'); $content = str_replace($rep, '{{ streams:multiple stream="' . $stream_slug . '" entry=id ', $content); // ------------------------------------- // Parse // ------------------------------------- $parser = new Lex_Parser(); $parser->scope_glue(':'); if (!$loop) { return $parser->parse($content, $data, array($this->parser, 'parser_callback')); } $out = ''; foreach ($data as $item) { $out .= $parser->parse($content, $item, array($this->parser, 'parser_callback')); } return $out; }
/** * Parse chunk content * * @access private * @param string - the chunk content * @param string - parse Lex tags? - yes/no * @return string */ private function parse_chunk($content, $parse_tags) { // Lex tags are parsed by default. If you want to // turn off parsing Lex tags, just set parse_tags to 'no' if ($parse_tags == 'yes') { $parser = new Lex_Parser(); $parser->scope_glue(':'); return $parser->parse($content, array(), array($this->parser, 'parser_callback')); } else { return $content; } }
/** * Main Fizl Function * * Routes and processes all page requests * * @access public * @return void */ public function _remap() { $this->load->library('Plugin'); $this->load->library('Parse'); $this->load->config('fizl'); include APPPATH . 'libraries/Lex/Autoloader.php'; Lex_Autoloader::register(); $this->load->helper(array('file', 'url')); // ------------------------------------- // Site config load // ------------------------------------- $raw_configs = (require_once FCPATH . 'config.php'); foreach ($config as $key => $var) { $this->config->set_item($key, $var); } // ------------------------------------- // Configs // ------------------------------------- // We do this first since we need this // data // ------------------------------------- $this->vars = array('segment_1' => $this->uri->segment(1), 'segment_2' => $this->uri->segment(2), 'segment_3' => $this->uri->segment(3), 'segment_4' => $this->uri->segment(4), 'segment_5' => $this->uri->segment(5), 'segment_6' => $this->uri->segment(6), 'segment_7' => $this->uri->segment(7), 'current_year' => date('Y'), 'current_url' => current_url(), 'site_url' => site_url(), 'base_url' => $this->config->item('base_url')); // Set the site folder as a constant define('SITE_FOLDER', $this->config->item('site_folder')); // ------------------------------------- // Look for page // ------------------------------------- // So... does this file exist? $segments = $this->uri->segment_array(); $is_home = FALSE; // Blank mean it's the home page, ya hurd? if (empty($segments)) { $is_home = TRUE; $segments = array('index'); } // ------------------------------------- // Find filename // ------------------------------------- // Is this a folder? If so we are looking for the index // file in the folder. if (is_dir(SITE_FOLDER . '/' . implode('/', $segments))) { $file = 'index'; } else { // Okay let's take a look at the last element $file = array_pop($segments); } // Turn the URL into a file path $file_path = SITE_FOLDER; if ($segments) { $file_path .= '/' . implode('/', $segments); } // ------------------------------------- // Find file // ------------------------------------- // We just want two things $file_elems = array_slice(explode('.', $file), 0, 2); $supported_files = array('html', 'md', 'textile'); $file_ext = NULL; // If there is a file extenison, // we just add it here. if (count($file_elems) == 2) { $file_ext = $file_elems[1]; $file_path .= '/' . $file; } else { // Try and find a file to match // our URL foreach ($supported_files as $ext) { if (file_exists($file_path . '/' . $file . '.' . $ext)) { $file_ext = $ext; $file_path .= '/' . $file . '.' . $ext; break; } } } // ------------------------------------- // Set headers // ------------------------------------- if (!$file_ext) { // No file for this? Set us a 404 header('HTTP/1.0 404 Not Found'); $is_404 = true; } else { $is_404 = false; $this->output->set_content_type('text/html'); } // ------------------------------------- // Set Template // ------------------------------------- $template = FALSE; $template_path = FCPATH . $this->config->item('assets_folder') . '/templates/'; if ($is_home and is_file($template_path . 'home.html')) { $template = read_file($template_path . 'home.html'); } elseif ($is_404) { $template = read_file($template_path . '404.html'); // Do we have a template for this folder? } elseif (is_file($template_path . implode('_', $segments) . '.html')) { $template = read_file($template_path . implode('_', $segments) . '.html'); } elseif (is_file($template_path . 'sub.html')) { $template = read_file($template_path . 'sub.html'); } elseif (is_file($template_path . 'default.html')) { $template = read_file($template_path . 'default.html'); } // ------------------------------------- // Get Content // ------------------------------------- if (!$is_404) { $content = read_file($file_path); // ------------------------------------- // Prep content by filetype // ------------------------------------- // .md and .textile get formatted // automatically. // ------------------------------------- if ($file_ext == 'md') { $content = '{{ format }}' . $content . '{{ /format }}'; } elseif ($file_ext == 'textile') { $content = '{{ format method="textile" }}' . $content . '{{ /format }}'; } // ------------------------------------- // If we have no template, then // we just use the content. if (!$template) { $template = $content; } else { // If we have a template, let's be // sneakty and add in the content // variable manually. $template = str_replace(array('{{ content }}', '{{content}}'), $content, $template); } // Our content is avialble $this->vars['content'] = $content; } // ------------------------------------- // Prep and Output Content // ------------------------------------- $parser = new Lex_Parser(); $parser->scope_glue(':'); echo $parser->parse($template, $this->vars, array($this->parse, 'callback'), true); }
/** * Streams content parse * * Special content parser for PyroStreams plugin * * @access private * @param string - the tag content * @param array - the return data * @param string - stream slug * @return string - the parsed data */ private function streams_content_parse($content, $data, $stream_slug) { // ------------------------------------- // Multiple Provision // ------------------------------------- // Automatically add in multiple streams data. // This makes it easier to call the multiple function // from within the streams tags // ------------------------------------- $rep = array('{{ streams:related', '{{streams:related'); $content = str_replace($rep, '{{ streams:related stream="' . $stream_slug . '" entry="{{ id }}" ', $content); $rep = array('{{ streams:multiple', '{{streams:multiple'); $content = str_replace($rep, '{{ streams:multiple stream="' . $stream_slug . '" entry="{{ id }}" ', $content); // ------------------------------------- // Parse Rows // ------------------------------------- $parser = new Lex_Parser(); $parser->scope_glue(':'); return $parser->parse($content, $data, array($this->parser, 'parser_callback')); }
/** * Callback from template parser * * @param array * @return mixed */ public function parser_callback($plugin, $attributes, $content, $data) { $this->_ci->load->library('plugins'); $return_data = ''; // Check if there were local data callbacks defined if (isset($data['_callbacks'][$plugin])) { $callback = $data['_callbacks'][$plugin]; if (is_callable($callback)) { $return_data = call_user_func_array($callback, array($plugin, $attributes, $content, $data)); } } else { if (isset($this->callbacks[$plugin])) { $callback = $this->callbacks[$plugin]; if (is_callable($callback)) { $return_data = call_user_func_array($callback, array($plugin, $attributes, $content, $data)); } } else { // Locate and process plugin $return_data = $this->_ci->plugins->locate($plugin, $attributes, $content, $data); } } if (is_array($return_data)) { if (!$this->_is_multi($return_data)) { $return_data = $this->_make_multi($return_data); } // Check if plugin has made any changes to the default content if (isset($return_data['_content'])) { $content = $return_data['_content']; unset($return_data['_content']); } $parsed_return = ''; $parser = new Lex_Parser(); $parser->scopeGlue(':'); foreach ($return_data as $result) { // Check if there was content declared for the result // If no _content declared in result array use default content if (isset($result['_content'])) { $rendered_content = $result['_content']; unset($result['_content']); } else { $rendered_content = $content; } $parsed_return .= $parser->parse($rendered_content, $result, array($this, 'parser_callback')); } unset($parser); $return_data = $parsed_return; } return $return_data ? $return_data : NULL; }
/** * Parse Content * * @access public * @param array * @return string */ public function callback($name, $attributes, $content) { $this->CI = get_instance(); // ---------------------------- // Determine Call // ---------------------------- // Do we have a : in the name? If so, we need // to separate this into the plugin/call if (strpos($name, ':') === FALSE) { // If we do not have a call // specified, we can use a function // with the same name as the plugin. $plugin = $name; $call = $name; } else { $pieces = explode(':', $name, 2); if (count($pieces) != 2) { return NULL; } $plugin = $pieces[0]; $call = $pieces[1]; } // Easy out for configs. // We have a special place in our heart for config:config_item. if ($plugin == 'config') { return $this->CI->config->item($call); } // ---------------------------- // Find & Load Plugin Class // ---------------------------- $plugin_dirs = array(APPPATH . 'plugins/', FCPATH . 'addons/plugins/'); // We can either have plugin folders or plugin files. foreach ($plugin_dirs as $dir) { if (is_dir($dir . $plugin) and is_file($dir . $plugin . '/' . $plugin . '.php')) { $file = $dir . $plugin . '/' . $plugin . '.php'; break; } elseif (is_file($dir . $plugin . '.php')) { $file = $dir . $plugin . '.php'; break; } } if (!isset($file)) { return NULL; } require_once $file; $class = 'Plugin_' . $plugin; if (class_exists($class)) { $plug = new $class(); } // ---------------------------- // Attributes // ---------------------------- // Add our params to the library // as class variables // ---------------------------- foreach ($attributes as $key => $val) { $plug->attributes[$key] = $val; } // Add content to the library $plug->tag_content = $content; if (!method_exists($plug, $call)) { return NULL; } // ---------------------------- // Make Plugin Call // ---------------------------- $return = $plug->{$call}(); // ---------------------------- // Return data based on type // ---------------------------- if (is_array($return)) { $parser = new Lex_Parser(); $parser->scope_glue(':'); return $parser->parse($content, $return, array($this->CI->parse, 'callback')); } elseif (is_string($return)) { return $return; } else { return NULL; } }