File: textile2.pl

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (493 lines) | stat: -rw-r--r-- 14,926 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# ---------------------------------------------------------------------------
# MT-Textile Text Formatter
# A plugin for Movable Type
#
# Release 2.05
#
# Brad Choate
# http://www.bradchoate.com/
# ---------------------------------------------------------------------------
# This software is provided as-is.
# You may use it for commercial or personal use.
# If you distribute it, please keep this notice intact.
#
# Copyright (c) 2003-2008 Brad Choate
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

package MT::Plugin::Textile;

use strict;
use MT;
use base qw( MT::Plugin );

our $VERSION = 2.05;
our ( $_initialized, $Have_SmartyPants );

MT->add_plugin(
    __PACKAGE__->new(
        {   name        => "Textile",
            description => '<MT_TRANS phrase="A humane web text generator.">',
            author_name => "Brad Choate",
            author_link => "http://bradchoate.com/",
            version     => $VERSION,
            registry    => {
                text_filters => {
                    textile_2 => {
                        label => "Textile 2",
                        code  => \&textile_2,
                        docs =>
                            "http://www.movabletype.org/documentation/author/textile-2-syntax.html",
                    },
                },
                tags => {
                    help_url => sub {
                        MT->translate(
                            'http://www.movabletype.org/documentation/appendices/tags/%t.html'
                        );
                    },
                    block    => { Textile => \&Textile, },
                    function => {
                        TextileOptions    => \&TextileOptions,
                        TextileHeadOffset => \&TextileHeadOffset,
                    },
                },
            },
        }
    )
);

sub _init {
    require Text::Textile;
    @MT::Textile::ISA = qw(Text::Textile);
    $Have_SmartyPants = defined &SmartyPants::SmartyPants ? 1 : 0;
    $_initialized     = 1;
}

sub textile_2 {
    my ( $str, $ctx ) = @_;

    _init() unless $_initialized;

    my $textile;
    if ( ( defined $ctx ) && ( ref($ctx) eq 'MT::Template::Context' ) ) {
        $textile = $ctx->stash('TextileObj');
        unless ($textile) {
            $textile = _new_textile($ctx);
            $ctx->stash( 'TextileObj', $textile );
        }
        $textile->head_offset( $ctx->stash('TextileHeadOffsetStart') || 0 );
        if ( my $opts = $ctx->stash('TextileOptions') ) {
            $textile->set( $_, $opts->{$_} ) foreach keys %$opts;

            # now clear the options from the stash so we don't
            # repeat this for each invocation of textile...
            $ctx->stash( 'TextileOptions', undef );
        }

        # reduces circular references
        # $textile->filter_param($ctx);
        require MT::Util;
        MT::Util::weaken( $textile->{filter_param} = $ctx );
    }
    else {

        # no Context object...
        $textile = _new_textile();
    }

    $str = $textile->process($str);

    if ( ( defined $ctx ) && ( ref($ctx) eq 'MT::Template::Context' ) ) {
        my $entry = $ctx->stash('entry');
        if ( $entry && $entry->id ) {
            my $link = $entry->permalink;
            $link =~ s/#.+$//;
            $str  =~ s/(<a .*?(?<=[ ])href=")(#fn(?:\d)+".*?>)/$1$link$2/g;
        }
    }

    # invoke MT-CodeBeautifier for any <code> or <blockcode> tags that
    # specify a 'language' attribute:
    my $beautifier = defined &Voisen::CodeBeautifier::beautifier;
    if ($beautifier) {
        $str
            =~ s|<((block)?code)([^>]*?) language="([^"]+?)"([^>]*?)>(.+?)</\1>|_highlight($1, $3, $5, $4, $textile->decode_html($6))|ges
            ;    # "
    }

    $str;
}

sub _new_textile {
    my ($ctx) = @_;

    my $textile = new MT::Textile;

    # this copies the named filters from MT to TextileFormat
    my %list;
    my $filters = MT::all_text_filters();
    foreach my $name ( keys %$filters ) {
        $list{$name} = $filters->{$name}{on_format};
    }
    $textile->filters( \%list );

    my $cfg = MT::ConfigMgr->instance;

    if ( $cfg->NoHTMLEntities ) {
        $textile->char_encoding(0);
    }

    $textile->charset('utf-8');

    $textile;
}

sub Textile {
    my ( $ctx, $args, $cond ) = @_;
    _init() unless $_initialized;
    local $ctx->{__stash}{TextileObj} = _new_textile($ctx);
    local $ctx->{__stash}{TextileOptions} = $args if keys %$args;
    my $str = $ctx->slurp;
    textile_2( $str, $ctx );
}

sub TextileHeadOffset {
    my ( $ctx, $args, $cond ) = @_;
    my $start = $args->{start};
    if ( $start && $start =~ m/^\d+$/ && $start >= 1 && $start <= 6 ) {
        $ctx->stash( 'TextileHeadOffsetStart', $start );
    }
    '';
}

sub TextileOptions {
    my ( $ctx, $args, $cond ) = @_;
    $ctx->stash( 'TextileOptions', $args );
    '';
}

sub _highlight {
    my ( $tag, $attr1, $attr2, $lang, $code ) = @_;
    my $tagopen = '<' . $tag;
    $tagopen .= $attr1 if defined $attr1;
    $tagopen .= $attr2 if defined $attr2;
    $tagopen .= '>';
    if ( $lang =~ m/perl/i ) {
        $code = Voisen::CodeBeautifier::highlight_perl($code);
    }
    elsif ( $lang =~ m/php/i ) {
        $code = Voisen::CodeBeautifier::highlight_php3($code);
    }
    elsif ( $lang =~ m/java/i ) {
        $code = Voisen::CodeBeautifier::highlight_java($code);
    }
    elsif ( ( $lang =~ m/actionscript/i ) || ( $lang =~ m/as/i ) ) {
        $code = Voisen::CodeBeautifier::highlight_as($code);
    }
    elsif ( $lang =~ m/scheme/i ) {
        $code = Voisen::CodeBeautifier::highlight_scheme($code);
    }
    $code =~ s!^<pre>!!;
    $code =~ s!</pre>$!!;
    return $tagopen . $code . '</' . $tag . '>';
}

# This is a Text::Textile subclass that provides enhanced
# functionality for Movable Type integration

package MT::Textile;

sub image_size {
    my $self  = shift;
    my ($src) = @_;
    my $ctx   = $self->filter_param;
    if ( $src !~ m|^http:| && $ctx ) {
        my $blog = $ctx->stash('blog');
        if ($blog) {
            require File::Spec;

            # local image -- calc size
            my $file;
            if ( ( $src =~ m!^/! ) && ( exists $ENV{DOCUMENT_ROOT} ) ) {
                $file = File::Spec->catfile( $ENV{DOCUMENT_ROOT}, $src );
            }
            else {
                $file = File::Spec->catfile( $blog->site_path, $src );
            }
            if ( -f $file ) {
                eval { require MT::Image; };
                if ( !$@ ) {
                    my $img = MT::Image->new( Filename => $file );
                    if ($img) {
                        return $img->get_dimensions;
                    }
                }
            }
        }
    }
    undef;
}

sub format_link {
    my $self   = shift;
    my (%args) = @_;
    my $title  = exists $args{title} ? $args{title} : '';
    my $url    = exists $args{url} ? $args{url} : '';
    my $ctx    = $self->filter_param;
    if ( $url =~ m/^\d+$/ && $ctx ) {
        my $blog = $ctx->stash('blog');
        if ($blog) {
            require MT::Entry;
            my $entry
                = MT::Entry->load( { blog_id => $blog->id, id => $url } );
            if ($entry) {
                local $ctx->{__stash}{entry} = $entry;
                my $relurl    = MT::Template::Context::_hdlr_blog_url($ctx);
                my $regrelurl = quotemeta($relurl);
                $args{url}
                    = MT::Template::Context::_hdlr_entry_permalink($ctx);
                $args{url} =~ s/^.*?($regrelurl)/$1/;
                if ( ( !exists $args{title} ) && ( $entry->title ) ) {
                    require MT::Util;
                    my $title
                        = MT::Util::remove_html( $entry->title ); # strip HTML
                    $title
                        =~ s/"/&quot;/g;   # convert double quotes to entities
                    $args{title} = $title;
                }
            }
        }
    }

    $self->SUPER::format_link(%args);
}

sub process_quotes {
    my $self = shift;
    my ($str) = @_;
    return $str unless $self->{do_quotes};
    if ($plugins::textile2::Have_SmartyPants) {
        $str = SmartyPants::SmartyPants( $str, $self->{smarty_mode} );
    }
    $str;
}

sub format_url {
    my $self   = shift;
    my (%args) = @_;
    my $url    = exists $args{url} ? $args{url} : '';
    my $ctx    = $self->filter_param;
    if ( $url =~ m/^\d+$/ && $ctx ) {

        # looks like an entry id, so let's link it
        my $blog = $ctx->stash('blog');
        if ($blog) {
            require MT::Entry;
            my $entry
                = MT::Entry->load( { 'blog_id' => $blog->id, 'id' => $url } );
            if ($entry) {
                local $ctx->{__stash}{entry} = $entry;
                my $relurl
                    = MT::Template::Context::_hdlr_blog_relative_url($ctx);
                my $regrelurl = quotemeta($relurl);
                $args{url}
                    = MT::Template::Context::_hdlr_entry_permalink($ctx);
                $args{url} =~ s/^.+?($regrelurl)/$1/;
            }
        }
    }
    elsif ( $url =~ m/^imdb(?::(.+))?$/ ) {
        my $term = $1;
        $term ||= MT::Util::remove_html( $args{linktext} || '' );
        $args{url} = 'http://www.imdb.com/Find?for=' . $term;
    }
    elsif ( $url =~ m/^google(?::(.+))?$/ ) {
        my $term = $1;
        $term ||= MT::Util::remove_html( $args{linktext} || '' );
        $args{url} = 'http://www.google.com/search?q=' . $term;
    }
    elsif ( $url =~ m/^dict(?::(.+))?$/ ) {
        my $term = $1;
        $term ||= MT::Util::remove_html( $args{linktext} || '' );
        $args{url} = 'http://www.dictionary.com/search?q=' . $term;
    }
    elsif ( $url =~ m/^amazon(?::(.+))?$/ ) {
        my $term = $1;
        $term ||= MT::Util::remove_html( $args{linktext} || '' );
        $args{url}
            = 'http://www.amazon.com/exec/obidos/external-search?index=blended&keyword='
            . $term;
    }
    $self->SUPER::format_url(%args);
}

1;

__END__

=head1 NAME

Textile - A plugin for Movable Type.

=head1 DESCRIPTION

This plugin integrates the Perl Text::Textile module with Movable
Type.

=head1 INSTALLATION

To install, place the 'textile2.pl' file in your Movable Type
'plugins' directory. Install the 'Textile.pm' file in a 'Text'
subdirectory underneath the Movable Type 'extlib' directory.

Your installation should look like this:

    (mt home)/plugins/textile2.pl
    (mt home)/extlib/Text/Textile.pm

You may also consider installing the Smarty Pants plugin by
John Gruber. MT-Textile uses this plugin to translate normal
quote characters into typographic quotes.

The Code Beautifier plugin by Sean Voisen is also supported
by MT-Textile. If you specify a language modifier for "bc"
blocks or for "@...@" strings, it will invoke the Code Beautifier
to colorize the code.

=head1 TEXT FORMATTERS

=head2 textile_2

The text formatter identified by "textile_2" invokes the Textile
formatter. You can select "Textile 2" from the Movable Type interface
to use this option to format your entries and/or comments.

You may also invoke the formatter on any Movable Type tag, like this:

    <$MTBlogDescription filters="textile_2"$>

For specific documentation on how this text formatter processes
text (a writing guide), please refer to the online documentation
available here:

L<http://www.bradchoate.com/mt/docs/mtmanual_textile2.html>

A copy of that document is available in the distribution of this
plugin. Located in the file "docs/mtmanual_textile2.html".

=head1 TAGS

=head2 E<lt>MTTextileE<gt>

A container tag that also allows you to invoke the Textile formatter.
Anything placed within this tag will be fed into the formatter for
processing. Additionally, all attributes passed to this tag are
processed as options for the Textile engine.

Attributes available:

=over

=item charset

Set this to the character set of the incoming data. This value
will default to the "PublishCharset" value in your mt.cfg file.
If no charset is given anywhere, it will default to ISO-8859-1.

=item char_encoding

Set to 1 to encode special characters to HTML entities. If
you're outputting utf-8 data, this can be set to '0' to
output plaintext instead. In fact, if you set your PublishCharset
for Movable Type to utf-8, it will effectively set this
setting to '0'. Otherwise, the default value is 1.

=item do_quotes

Sets the option for processing quotes into curly quotes. Defaults
to 1. The "Smarty" plugin will be used as a default, if available.

=item trim_spaces

Set to '1' to cause any trailing whitespace on lines to be
trimmed. Default is 0 (disabled).

=item smarty_mode

Controls the Smarty plugin for processing quotes. The value
given is passed through to the Smarty plugin to control how it
behaves. Please refer to the documentation for the Smarty Pants
plugin for the available values for this attribute. You can
simply specify '1' for the default mode of operation.

=item preserve_spaces

Set to '1' to cause multiple, continuous spaces to be preserved
by changing them to the HTML entity &#8195;. If '0', spaces
are left as-is, which means they will appear as a single space
when rendered to HTML. Default is '0'.

=item head_offset

Set to a number from "1" to "5" to control the numbering of the
"h1" to "h6" paragraph block codes. For example, if a head_offset
of "1" is given, any "h1" paragraph blocks will produce "h2" HTML
tags. Or "h3" HTML tags if a head_offset of "2" is specified.

=item flavor

Use the flavor attribute to identify whether the HTML produced
should be HTML, XHTML 1.x or XHTML 2 compliant. Available
values include:

    html
    html/css
    xhtml1
    xhtml2

The default flavor is "xhtml1".
                
=item css

Set to '1' to allow the Textile formatter to use CSS classes
and style attributes to format any output. Specifying '0' will
avoid the use of style and class attributes. This attribute
will default to '1' when a flavor of "html/css" or "xhtml1"
or "xhtml2" is given.

=back

=head2 E<lt>$MTTextileOptions$E<gt>

A standalone tag that can also be used to configure the Textile
text formatter. See the attributes available to the E<lt>MTTextileE<gt>
tag for what is available.

=head2 E<lt>$MTTextileHeadOffset$E<gt>

Another tag that can be used to set the "head_offset" attribute.
This tag is left for backward compability. Please use the
E<lt>MTTextileOptionsE<gt> tag in the future.

=head1 LICENSE

Released under the MIT license. Please see

L<http://www.opensource.org/licenses/mit-license.php>

for details.

=head1 SUPPORT

If you have questions or need assistance with this plugin, please
use the following link:

L<http://www.bradchoate.com/mt-plugins/textile>

=head1 COPYRIGHT

Copyright 2002-2004, Brad Choate

=cut