File: function.mtfiletemplate.php

package info (click to toggle)
movabletype-opensource 4.2.3-1%2Blenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 21,268 kB
  • ctags: 15,862
  • sloc: perl: 178,892; php: 26,178; sh: 161; makefile: 82
file content (95 lines) | stat: -rw-r--r-- 3,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

function _file_template_format($m) {
    static $f = array(
        'a' => "<MTAuthorBasename DIR>",
        '-a' => "<MTAuthorBasename separator='-'>",
        '_a' => "<MTAuthorBasename separator='_'>",
        'b' => "<MTEntryBasename SEP>",
        '-b' => "<MTEntryBasename separator='-'>",
        '_b' => "<MTEntryBasename separator='_'>",
        'c' => "<MTSubCategoryPath SEP>",
        '-c' => "<MTSubCategoryPath separator='-'>",
        '_c' => "<MTSubCategoryPath separator='_'>",
        'C' => "<MTArchiveCategory DIR default=''>",
        '-C' => "<MTArchiveCategory dirify='-' default=''>",
        'd' => "<MTArchiveDate format='%d'>",
        'D' => "<MTArchiveDate format='%e' trim='1'>",
        'e' => "<MTEntryID pad='1'>",
        'E' => "<MTEntryID pad='0'>",
        'f' => "<MTArchiveFile SEP>",
        '-f' => "<MTArchiveFile separator='-'>",
        'F' => "<MTArchiveFile extension='0' SEP>",
        '-F' => "<MTArchiveFile extension='0' separator='-'>",
        'h' => "<MTArchiveDate format='%H'>",
        'H' => "<MTArchiveDate format='%k' trim='1'>",
        'i' => '<MTIndexBasename extension="1">',
        'I' => "<MTIndexBasename>",
        'j' => "<MTArchiveDate format='%j'>",  # 3-digit day of year
        'm' => "<MTArchiveDate format='%m'>",  # 2-digit month
        'n' => "<MTArchiveDate format='%M'>",  # 2-digit minute
        's' => "<MTArchiveDate format='%S'>",  # 2-digit second
        'x' => "<MTBlogFileExtension>",
        'y' => "<MTArchiveDate format='%Y'>",  # year
        'Y' => "<MTArchiveDate format='%y'>",  # 2-digit year
    );
    return isset($f[$m[1]]) ? $f[$m[1]] : $m[1];
}

function smarty_function_mtfiletemplate($args, &$ctx) {
    static $_file_template_cache = array();
    $at = $ctx->stash('archive_type');
    $at or $at = $ctx->stash('current_archive_type');
    $format = $args['format'];
    if (!$format) {
        $formats = array(
            'Individual' => '%y/%m/%f',
            'Category' => '%c/%f',
            'Monthly' => '%y/%m/%f',
            'Weekly' => '%y/%m/%d-week/%f',
            'Daily' => '%y/%m/%d/%f'
        );
        $format = $formats[ $at ];
    }
    if (!$format) return '';

    #my ($dir, $sep);
    if ($args['separator']) {
        $dir = "dirify='" . $args['separator'] . "'";
        $sep = "separator='" . $args['separator'] . "'";
    } else {
        $dir = "dirify='1'";
        $sep = "";
    }
    $orig_format = $format;
    $format = preg_replace_callback('/%([_-]?[A-Za-z])/',
        '_file_template_format', $format);
    $format = preg_replace('/SEP/', $sep, $format);
    $format = preg_replace('/DIR/', $dir, $format);

    # now build this template and return result
    if (isset($_file_template_cache[$format])) {
        $_var_compiled = $_file_template_cache[$format];
    } else {
        if ($ctx->_compile_source('evaluated template', $format, $_var_compiled)) {
            $_file_template_cache[$format] = $_var_compiled;
        } else {
            return $ctx->error("Error compiling file template: '$orig_format'");
        }
    }

    ob_start();
    $ctx->_eval('?>' . $_var_compiled);
    $file = ob_get_contents();
    ob_end_clean();

    $file = preg_replace('/\/{2,}/', '/', $file);
    $file = preg_replace('/(^\/|\/$)/', '', $file);
    return $file;
}
?>