Ejemplo n.º 1
0
/**
 * Parse special Navigate CMS tags like:
 * <ul>
 * <li>&lt;nv object="include" file="" id="" /&gt;</li>
 * <li>curly bracket tags {{nv object=""}}</li>
 * </ul>
 *
 * Generate the final HTML code for these special tags or convert them
 * to a simpler nv tags.
 *
 * @param $html
 * @return mixed
 */
function nvweb_template_parse_special($html)
{
    global $website;
    global $current;
    // find <pre> and <code> tags, save its contents and leave a placeholder to restore the content later
    $tags_pre = nvweb_tags_extract($html, 'pre', false, true, 'UTF-8');
    for ($t = count($tags_pre); $t--; $t >= 0) {
        $tag = $tags_pre[$t];
        if (empty($tag)) {
            continue;
        }
        $tag_uid = uniqid('nv-tags-pre-');
        $current['delayed_tags_pre'][$tag_uid] = $tag['full_tag'];
        $html = substr_replace($html, '<!--#' . $tag_uid . '#-->', $tag['offset'], strlen($tag['full_tag']));
    }
    $tags_code = nvweb_tags_extract($html, 'code', false, true, 'UTF-8');
    for ($t = count($tags_code); $t--; $t >= 0) {
        $tag = $tags_code[$t];
        if (empty($tag)) {
            continue;
        }
        $tag_uid = uniqid('nv-tags-code-');
        $current['delayed_tags_code'][$tag_uid] = $tag['full_tag'];
        $html = substr_replace($html, '<!--#' . $tag_uid . '#-->', $tag['offset'], strlen($tag['full_tag']));
    }
    $changed = false;
    // translate "{{nv object='list' " tags to "<nv object='list' " version
    preg_match_all("/{{nv\\s object=[\"']list[\"'] ([^}]+)}}/ixsm", $html, $curly_tags);
    for ($c = 0; $c < count($curly_tags[0]); $c++) {
        if (stripos($curly_tags[0], 'object="list"')) {
            $tmp = str_ireplace(array('{{nv object="list" ', '}}'), array('<nv object="list" ', '>'), $curly_tags[0][$c]);
            $html = str_ireplace($curly_tags[0][$c], $tmp, $html);
        } else {
            $tmp = str_ireplace(array("{{nv object='list' ", '}}'), array('<nv object="list" ', '>'), $curly_tags[0][$c]);
            $html = str_ireplace($curly_tags[0][$c], $tmp, $html);
        }
        $changed = true;
    }
    // translate "{{/nv}}" tags to "</nv>" version
    $html = str_ireplace('{{/nv}}', '</nv>', $html);
    // translate "{{nvlist_conditional }}" tags to "<nvlist_conditional >" version
    preg_match_all("/{{nvlist_conditional \\s([^}]+)}}/ixsm", $html, $curly_tags);
    for ($c = 0; $c < count($curly_tags[0]); $c++) {
        $tmp = str_replace(array('{{nvlist_conditional ', '}}'), array('<nvlist_conditional ', '>'), $curly_tags[0][$c]);
        $html = str_ireplace($curly_tags[0][$c], $tmp, $html);
        $changed = true;
    }
    // translate "{{/nvlist_conditional}}" tags to "</nvlist_conditional>" version
    $html = str_ireplace('{{/nvlist_conditional}}', '</nvlist_conditional>', $html);
    // translate "{{nv }}" tags to "<nv />" version
    preg_match_all("/{{nv\\s([^}]+)}}/ixsm", $html, $curly_tags);
    for ($c = 0; $c < count($curly_tags[0]); $c++) {
        $tmp = str_replace(array('{{nv ', '}}'), array('<nv ', ' />'), $curly_tags[0][$c]);
        $html = str_ireplace($curly_tags[0][$c], $tmp, $html);
        $changed = true;
    }
    // translate "{{nvlist }}" tags to "<nvlist />" version
    preg_match_all("/{{nvlist\\s([^}]+)}}/ixsm", $html, $curly_tags);
    for ($c = 0; $c < count($curly_tags[0]); $c++) {
        $tmp = str_replace(array('{{nvlist ', '}}'), array('<nvlist ', ' />'), $curly_tags[0][$c]);
        $html = str_ireplace($curly_tags[0][$c], $tmp, $html);
        $changed = true;
    }
    if ($changed) {
        return nvweb_template_parse_special($html);
    }
    // parse includes (we must do it before parsing list or search)
    $tags = nvweb_tags_extract($html, 'nv', true, true, 'UTF-8');
    foreach ($tags as $tag) {
        $content = '';
        $changed = false;
        $tag['length'] = strlen($tag['full_tag']);
        if ($tag['attributes']['object'] == 'include') {
            $tid = $tag['attributes']['id'];
            $file = $tag['attributes']['file'];
            if (!empty($tid)) {
                $template = new template();
                $template->load($tid);
                if ($template->website == $website->id) {
                    $content = file_get_contents(NAVIGATE_PRIVATE . '/' . $website->id . '/templates/' . $template->file);
                }
            } else {
                if (!empty($file)) {
                    $content = file_get_contents(NAVIGATE_PATH . '/themes/' . $website->theme . '/' . $file);
                }
            }
            $html = substr_replace($html, $content, $tag['offset'], $tag['length']);
            $changed = true;
        }
        // if an object="include" has been found, we need to restart the parse_special tags function
        // as it may contain other "includes" or "{{nv" tags that need transformation
        if ($changed) {
            $html = nvweb_template_parse_special($html);
            break;
        }
    }
    return $html;
}
Ejemplo n.º 2
0
 $current['plugins_called'] = nvweb_plugins_called_in_template($html);
 $html = nvweb_plugins_event('before_parse', $html);
 $html = nvweb_theme_settings($html);
 $html = nvweb_template_parse_lists($html);
 $html = nvweb_template_parse($html);
 // if the content has added any nv tag, process them
 if (strpos($html, '{{nv ') !== false || strpos($html, '<nv ')) {
     $html = nvweb_template_parse_special($html);
     $html = nvweb_template_parse_lists($html);
     $html = nvweb_template_parse($html);
 }
 // if we have a delayed nv list we need to parse it now
 if (!empty($current['delayed_nvlists']) || !empty($current['delayed_nvsearches'])) {
     $html = nvweb_template_parse_lists($html, true);
     if (strpos($html, '{{nv ') !== false || strpos($html, '<nv ')) {
         $html = nvweb_template_parse_special($html);
         $html = nvweb_template_parse_lists($html);
         $html = nvweb_template_parse($html);
     }
 }
 $html = nvweb_template_oembed_parse($html);
 $html = nvweb_template_processes($html);
 $end .= nvweb_after_body('php');
 $end = nvweb_after_body('html');
 $end .= nvweb_after_body('js');
 $end .= "\n\n";
 $end .= '</body>';
 $html = str_replace('</body>', $end, $html);
 $html = nvweb_template_tweaks($html);
 $html = nvweb_template_restore_special($html);
 $events->trigger('theme', 'after_parse', array('html' => &$html));