tagging: Allow using titles in for related stories.
[matthijs/upstream/blosxom-plugins.git] / general / textile2
1 # Blosxom Plugin: textile2                                         -*- perl -*-
2 # Author: Todd Larason (jtl@molehill.org)
3 # Version: 0+1i
4 # Blosxom Home/Docs/Licensing: http://www.raelity.org/blosxom
5 # Textile plugin Home/Docs/Licensing:
6 #   http://molelog.molehill.org/blox/Computers/Internet/Web/Blosxom/Plugins/Textile/
7
8 package textile2;
9
10 # Configuration Variables
11 # ----------------------------------------------------------------------------
12 #$flavour           = "xhtml"           unless defined $flavour;
13 $flavor             = "xhtml"           unless defined $flavour;
14 $character_encoding = 1                 unless defined $character_encoding;
15 $character_set      = "utf-8"           unless defined $character_set;
16 $head_offset        = 3                 unless defined $head_offset;
17 $image_root         = $blosxom::datadir unless defined $image_root;
18 $handle_textile_1   = 0                 unless defined $handle_textile_1;
19 # ----------------------------------------------------------------------------
20 use strict;
21
22 # Blosxom interface
23
24 use lib qq{$blosxom::plugin_dir/lib};
25 use vars qw/$flavor $character_encoding $character_set $head_offset 
26     $image_root $handle_textile_1 $textile/;
27 sub start {
28     eval {require Text::Textile;};
29     return 1 if (!$@);
30     return 0;
31 }
32
33 sub story {
34   my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
35   if ($meta::markup eq 'textile2' ||
36       ($handle_textile_1 && $meta::markup eq 'textile')) {
37       my $c = {
38           image_root => $image_root,
39           image_rel_base => "$blosxom::datadir/$path"
40           };
41
42       $$body_ref = blostile($$body_ref, $c);
43   }
44   return 1;
45 }
46
47 # Simplified interface to Blosxom::Textile
48
49 # context items:
50 #   image_root          absolute image filenames relative to this
51 #   image_rel_base      relative image filenames relative to this
52
53 sub blostile {
54         # modif : http://groups.yahoo.com/group/blosxom/message/4052
55         local $_;
56     my $str = shift @_;
57     my $ctx = shift @_;
58     if (!$textile) {
59         $textile = new Blosxom::Textile;
60         # XXX named filters
61         $textile->char_encoding($character_encoding);
62         $textile->charset($character_set);
63     }
64     $textile->flavor($flavor);
65     $textile->filter_param($ctx);
66
67     $str = $textile->process($str);
68
69     if (defined $head_offset && ($head_offset > 1)) {
70         for (my $h = 7 - $head_offset; $h > 0; $h--) {
71             my $h2 = $h + $head_offset - 1;
72             $str =~ s/(<\/?)h$h([ >])/$1h$h2$2/g;
73         }
74     }
75
76     # XXX beautifier
77     # XXX smarty?
78
79     return $str;
80 }
81
82 # Blosxom::Textile
83 package Blosxom::Textile;
84 use Text::Textile;
85 @Blosxom::Textile::ISA = qw(Text::Textile);
86
87 sub image_size {
88     my ($self, $src) = @_;
89     my $ctx = $self->filter_param;
90     if ($src !~ m{^http:}) {
91         eval {require Image::Size;};
92         if (!$@) {
93             my $file;
94             if ($src =~ m{^/}) {
95                 $file = "$ctx->{image_root}$src";
96             } else {
97                 $file = "$ctx->{image_rel_base}/$src";
98             }
99             my ($w, $h) = Image::Size::imgsize($file);
100             return ($w, $h);
101         }
102     }
103     warn "Image sizing without Image::Size not implemented";
104     undef;
105 }
106
107 # XXX format_link ?
108 # XXX format_url ?
109
110 1;