📄 context.pm
字号:
# Copyright 2001-2005 Six Apart.# SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS## $Id: Context.pm 12367 2005-05-24 18:37:59Z bchoate $package MT::Template::Context;use strict;use MT::Util qw( start_end_day start_end_week start_end_month week2ymd munge_comment archive_file_for format_ts offset_time_list first_n_words dirify get_entry encode_html encode_js remove_html wday_from_ts days_in spam_protect encode_php encode_url decode_html encode_xml decode_xml );use MT::ConfigMgr;use MT::Request;use Time::Local qw( timegm );use MT::ErrorHandler;use MT::Promise qw(delay force);@MT::Template::Context::ISA = qw( MT::ErrorHandler );use constant FALSE => -99999;use Exporter;*import = \&Exporter::import;use vars qw( @EXPORT );@EXPORT = qw( FALSE );use vars qw( %Global_handlers %Global_filters );sub add_tag { my $class = shift; my($name, $code) = @_; $Global_handlers{$name} = { code => $code, is_container => 0 };}sub add_container_tag { my $class = shift; my($name, $code) = @_; $Global_handlers{$name} = { code => $code, is_container => 1 };}sub add_conditional_tag { my $class = shift; my($name, $condition) = @_; $Global_handlers{$name} = { code => sub { if ($condition->(@_)) { return _hdlr_pass_tokens(@_); } else { return _hdlr_pass_tokens_else(@_); } }, is_container => 1 };}sub add_global_filter { my $class = shift; my($name, $code) = @_; $Global_filters{$name} = $code;}sub new { my $class = shift; my $ctx = bless {}, $class; $ctx->init(@_);}sub init { my $ctx = shift; $ctx->init_default_handlers; for my $tag (keys %Global_handlers) { my $arg = $Global_handlers{$tag}{is_container} ? [ $Global_handlers{$tag}{code}, 1 ] : $Global_handlers{$tag}{code}; $ctx->register_handler($tag => $arg); } $ctx;}sub init_default_handlers { my $ctx = shift; $ctx->register_handler(Else => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(CGIPath => \&_hdlr_cgi_path); $ctx->register_handler(CGIRelativeURL => \&_hdlr_cgi_relative_url); $ctx->register_handler(StaticWebPath => \&_hdlr_static_path); $ctx->register_handler(CommentScript => \&_hdlr_comment_script); $ctx->register_handler(TrackbackScript => \&_hdlr_trackback_script); $ctx->register_handler(SearchScript => \&_hdlr_search_script); $ctx->register_handler(XMLRPCScript => \&_hdlr_xmlrpc_script); $ctx->register_handler(Date => \&_hdlr_sys_date); $ctx->register_handler(Version => \&_hdlr_mt_version); $ctx->register_handler(PublishCharset => \&_hdlr_publish_charset); $ctx->register_handler(DefaultLanguage => \&_hdlr_default_language); $ctx->register_handler(CGIServerPath => \&_hdlr_cgi_server_path); $ctx->register_handler(IfNonEmpty => [ \&_hdlr_if_nonempty, 1 ]); $ctx->register_handler(IfNonZero => [ \&_hdlr_if_nonzero, 1 ]); $ctx->register_handler(CommenterNameThunk => \&_hdlr_commenter_name_thunk); $ctx->register_handler(CommenterName => \&_hdlr_commenter_name); $ctx->register_handler(CommenterEmail => \&_hdlr_commenter_email); $ctx->register_handler(Blogs => [ \&_hdlr_blogs, 1 ]); $ctx->register_handler(BlogID => \&_hdlr_blog_id); $ctx->register_handler(BlogName => \&_hdlr_blog_name); $ctx->register_handler(BlogDescription => \&_hdlr_blog_description); $ctx->register_handler(BlogURL => \&_hdlr_blog_url); $ctx->register_handler(BlogArchiveURL => \&_hdlr_blog_archive_url); $ctx->register_handler(BlogRelativeURL => \&_hdlr_blog_relative_url); $ctx->register_handler(BlogSitePath => \&_hdlr_blog_site_path); $ctx->register_handler(BlogLanguage => \&_hdlr_blog_language); $ctx->register_handler(BlogHost => \&_hdlr_blog_host); $ctx->register_handler(BlogTimezone => \&_hdlr_blog_timezone); $ctx->register_handler(BlogEntryCount => \&_hdlr_blog_entry_count); $ctx->register_handler(BlogCommentCount => \&_hdlr_blog_comment_count); $ctx->register_handler(BlogCCLicenseURL => \&_hdlr_blog_cc_license_url); $ctx->register_handler(BlogCCLicenseImage => \&_hdlr_blog_cc_license_image); $ctx->register_handler(CCLicenseRDF => \&_hdlr_cc_license_rdf); $ctx->register_handler(BlogIfCCLicense => [ \&_hdlr_blog_if_cc_license, 1 ]); $ctx->register_handler(Entries => [ \&_hdlr_entries, 1 ]); $ctx->register_handler(EntriesHeader => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(EntriesFooter => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(EntryID => \&_hdlr_entry_id); $ctx->register_handler(EntryTitle => \&_hdlr_entry_title); $ctx->register_handler(EntryStatus => \&_hdlr_entry_status); $ctx->register_handler(EntryFlag => \&_hdlr_entry_flag); $ctx->register_handler(EntryCategory => \&_hdlr_entry_category); $ctx->register_handler(EntryCategories => [ \&_hdlr_entry_categories, 1 ]); $ctx->register_handler(EntryBody => \&_hdlr_entry_body); $ctx->register_handler(EntryMore => \&_hdlr_entry_more); $ctx->register_handler(EntryExcerpt => \&_hdlr_entry_excerpt); $ctx->register_handler(EntryKeywords => \&_hdlr_entry_keywords); $ctx->register_handler(EntryLink => \&_hdlr_entry_link); $ctx->register_handler(EntryBasename => \&_hdlr_entry_basename); $ctx->register_handler(EntryPermalink => \&_hdlr_entry_permalink); $ctx->register_handler(EntryAuthor => \&_hdlr_entry_author); $ctx->register_handler(EntryAuthorEmail => \&_hdlr_entry_author_email); $ctx->register_handler(EntryAuthorURL => \&_hdlr_entry_author_url); $ctx->register_handler(EntryAuthorLink => \&_hdlr_entry_author_link); $ctx->register_handler(EntryAuthorNickname => \&_hdlr_entry_author_nick); $ctx->register_handler(EntryDate => \&_hdlr_date); $ctx->register_handler(EntryModifiedDate => \&_hdlr_entry_mod_date); $ctx->register_handler(EntryCommentCount => \&_hdlr_entry_comments); $ctx->register_handler(EntryTrackbackCount => \&_hdlr_entry_ping_count); $ctx->register_handler(EntryIfExtended => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(EntryIfAllowComments => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(EntryIfCommentsOpen => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(EntryIfAllowPings => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(EntryTrackbackLink => \&_hdlr_entry_tb_link); $ctx->register_handler(EntryTrackbackData => \&_hdlr_entry_tb_data); $ctx->register_handler(EntryTrackbackID => \&_hdlr_entry_tb_id); $ctx->register_handler(EntryPrevious => [ \&_hdlr_entry_previous, 1 ]); $ctx->register_handler(EntryNext => [ \&_hdlr_entry_next, 1 ]); $ctx->register_handler(DateHeader => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(DateFooter => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(ArchivePrevious => [ \&_hdlr_archive_prev_next, 1 ]); $ctx->register_handler(ArchiveNext => [ \&_hdlr_archive_prev_next, 1 ]); $ctx->register_handler(Include => \&_hdlr_include); $ctx->register_handler(Link => \&_hdlr_link); $ctx->register_handler(ErrorMessage => \&_hdlr_error_message); $ctx->register_handler(GetVar => \&_hdlr_var); $ctx->register_handler(SetVar => \&_hdlr_var); $ctx->register_handler(IfAllowCommentHTML => [ \&_hdlr_pass_tokens, 1] ); $ctx->register_handler(IfRegistrationRequired => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(IfRegistrationAllowed => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(IfUnregisteredAllowed => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(IfCommentsAllowed => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(IfNeedEmail => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(IfCommenterPending => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(IfDynamicComments => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(TypeKeyToken => \&_hdlr_typekey_token); $ctx->register_handler(CommentFields => [ \&_hdlr_comment_fields ]); $ctx->register_handler(RemoteSignOutLink => [ \&_hdlr_remote_sign_out_link ]); $ctx->register_handler(RemoteSignInLink => [ \&_hdlr_remote_sign_in_link ]); $ctx->register_handler(Comments => [ \&_hdlr_comments, 1 ]); $ctx->register_handler(CommentID => \&_hdlr_comment_id); $ctx->register_handler(CommentEntryID => \&_hdlr_comment_entry_id); $ctx->register_handler(CommentName => \&_hdlr_comment_author); $ctx->register_handler(CommentIP => \&_hdlr_comment_ip); $ctx->register_handler(CommentAuthor => \&_hdlr_comment_author); $ctx->register_handler(CommentAuthorLink => \&_hdlr_comment_author_link); $ctx->register_handler(CommentAuthorIdentity => \&_hdlr_comment_author_identity); $ctx->register_handler(CommentEmail => \&_hdlr_comment_email); $ctx->register_handler(CommentURL => \&_hdlr_comment_url); $ctx->register_handler(CommentBody => \&_hdlr_comment_body); $ctx->register_handler(CommentOrderNumber => \&_hdlr_comment_order_num); $ctx->register_handler(CommentDate => \&_hdlr_date); $ctx->register_handler(CommentEntry => [ \&_hdlr_comment_entry, 1 ]); $ctx->register_handler(CommentPreviewAuthor => \&_hdlr_comment_author); $ctx->register_handler(CommentPreviewIP => \&_hdlr_comment_ip); $ctx->register_handler(CommentPreviewAuthorLink => \&_hdlr_comment_author_link); $ctx->register_handler(CommentPreviewEmail => \&_hdlr_comment_email); $ctx->register_handler(CommentPreviewURL => \&_hdlr_comment_url); $ctx->register_handler(CommentPreviewBody => \&_hdlr_comment_body); $ctx->register_handler(CommentPreviewDate => \&_hdlr_date); $ctx->register_handler(CommentPreviewState => \&_hdlr_comment_prev_state); $ctx->register_handler(CommentPreviewIsStatic => \&_hdlr_comment_prev_static); $ctx->register_handler(ArchiveList => [ \&_hdlr_archives, 1 ]); $ctx->register_handler(ArchiveLink => \&_hdlr_archive_link); $ctx->register_handler(ArchiveTitle => \&_hdlr_archive_title); $ctx->register_handler(ArchiveCount => \&_hdlr_archive_count); $ctx->register_handler(ArchiveDate => \&_hdlr_date); $ctx->register_handler(ArchiveDateEnd => \&_hdlr_archive_date_end); $ctx->register_handler(ArchiveCategory => \&_hdlr_archive_category); $ctx->register_handler(ImageURL => \&_hdlr_image_url); $ctx->register_handler(ImageWidth => \&_hdlr_image_width); $ctx->register_handler(ImageHeight => \&_hdlr_image_height); $ctx->register_handler(Calendar => [ \&_hdlr_calendar, 1 ]); $ctx->register_handler(CalendarDay => \&_hdlr_calendar_day); $ctx->register_handler(CalendarCellNumber => \&_hdlr_calendar_cell_num); $ctx->register_handler(CalendarDate => \&_hdlr_date); $ctx->register_handler(CalendarWeekHeader => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(CalendarWeekFooter => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(CalendarIfBlank => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(CalendarIfToday => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(CalendarIfEntries => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(CalendarIfNoEntries => [ \&_hdlr_pass_tokens, 1 ]); $ctx->register_handler(Categories => [ \&_hdlr_categories, 1 ]); $ctx->register_handler(CategoryID => \&_hdlr_category_id); $ctx->register_handler(CategoryLabel => \&_hdlr_category_label); $ctx->register_handler(CategoryDescription => \&_hdlr_category_desc); $ctx->register_handler(CategoryArchiveLink => \&_hdlr_category_archive); $ctx->register_handler(CategoryCount => \&_hdlr_category_count); $ctx->register_handler(CategoryTrackbackLink => \&_hdlr_category_tb_link); $ctx->register_handler(GoogleSearch => [ \&_hdlr_google_search, 1 ]); $ctx->register_handler(GoogleSearchResult => \&_hdlr_google_search_result); $ctx->register_handler(Pings => [ \&_hdlr_pings, 1 ]); $ctx->register_handler(PingsSent => [ \&_hdlr_pings_sent, 1 ]); $ctx->register_handler(PingsSentURL => \&_hdlr_pings_sent_url); $ctx->register_handler(PingTitle => \&_hdlr_ping_title); $ctx->register_handler(PingID => \&_hdlr_ping_id); $ctx->register_handler(PingURL => \&_hdlr_ping_url); $ctx->register_handler(PingExcerpt => \&_hdlr_ping_excerpt); $ctx->register_handler(PingBlogName => \&_hdlr_ping_blog_name); $ctx->register_handler(PingIP => \&_hdlr_ping_ip); $ctx->register_handler(PingDate => \&_hdlr_date); $ctx->register_handler(SignOnURL => \&_hdlr_signon_url);}sub sanitize_on { $_[0]->{'sanitize'} = 1 unless exists $_[0]->{'sanitize'};}sub post_process_handler { sub { my($ctx, $args, $str) = @_; if ($args) { my %local_args = %$args; for my $arg (keys %local_args) { if (my $code = $Global_filters{$arg}) { $str = $code->($str, $args->{$arg}, $ctx); delete $local_args{$arg}; } } if (my $f = $local_args{'filters'}) { $str = MT->apply_text_filters($str, [ split /\s*,\s*/, $f ], $ctx); } if (my $len = $local_args{trim_to}) { $str = substr $str, 0, $len if $len < length($str); } if ($local_args{'decode_html'}) { $str = decode_html($str); } if ($local_args{'decode_xml'}) { $str = decode_xml($str); } if ($local_args{'remove_html'}) { $str = remove_html($str); } if ($local_args{'dirify'}) { $str = dirify($str); } if (my $spec = $local_args{'sanitize'}) { require MT::Sanitize; if ($spec eq '1') { $spec = $ctx->stash('blog')->sanitize_spec || MT::ConfigMgr->instance->GlobalSanitizeSpec; } $str = MT::Sanitize->sanitize($str, $spec); } if ($local_args{'encode_html'}) { $str = encode_html($str, 1); } if ($local_args{'encode_xml'}) { $str = encode_xml($str); } if ($local_args{'encode_js'}) { $str = encode_js($str); } if (my $meth = $local_args{'encode_php'}) { $str = encode_php($str, $meth); } if ($local_args{'encode_url'}) { $str = encode_url($str); } if ($local_args{upper_case}) { $str = uc($str); } if ($local_args{lower_case}) { $str = lc($str); } if ($local_args{strip_linefeeds}) { $str =~ tr(\r\n)()d; } if (my $len = $local_args{space_pad}) { $str = sprintf "%${len}s", $str; } if (my $len = $local_args{zero_pad}) { $str = sprintf "%0${len}s", $str; } if (my $format = $local_args{'sprintf'}) { $str = sprintf($format, $str); } } $str; }}sub stash { my $ctx = shift; my $key = shift; $ctx->{__stash}->{$key} = shift if @_; if (ref $ctx->{__stash}->{$key} eq 'MT::Promise') { return force($ctx->{__stash}->{$key}); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -