~ubuntu-branches/ubuntu/vivid/libhtml-wikiconverter-perl/vivid

« back to all changes in this revision

Viewing changes to WikiConverter/PhpWiki.pm

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Genannt
  • Date: 2005-12-26 23:42:31 UTC
  • Revision ID: james.westby@ubuntu.com-20051226234231-66czoq4xko7s5p96
Tags: upstream-0.30
ImportĀ upstreamĀ versionĀ 0.30

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package HTML::WikiConverter::PhpWiki;
 
2
use base 'HTML::WikiConverter';
 
3
use warnings;
 
4
use strict;
 
5
 
 
6
sub rules {
 
7
  my %rules = (
 
8
    hr => { replace => "\n----\n" },
 
9
    br => { replace => '%%%' },
 
10
 
 
11
    blockquote => { start => \&_blockquote_start, block => 1, line_format => 'multi' },
 
12
    p => { block => 1, trim => 'both', line_format => 'multi' },
 
13
    i => { start => "_", end => "_" },
 
14
    em => { alias => 'i' },
 
15
    b => { start => "*", end => "*" },
 
16
    strong => { alias => 'b' },
 
17
 
 
18
    img => { replace => \&_image },
 
19
    a => { replace => \&_link },
 
20
 
 
21
    ul => { line_format => 'multi', block => 1 },
 
22
    ol => { alias => 'ul' },
 
23
    dl => { line_format => 'blocks', block => 1 },
 
24
 
 
25
    li => { start => \&_li_start, trim => 'leading' },
 
26
    dt => { trim => 'both', end => ":\n" },
 
27
    dd => { line_prefix => '  ' },
 
28
 
 
29
    td => { start => \&_td_start, end => \&_td_end, trim => 'both' },
 
30
    th => { alias => 'td' },
 
31
 
 
32
    h1 => { start => '!!! ', block => 1, trim => 'both', line_format => 'single' },
 
33
    h2 => { start => '!!! ', block => 1, trim => 'both', line_format => 'single' },
 
34
    h3 => { start => '!! ',  block => 1, trim => 'both', line_format => 'single' },
 
35
    h4 => { start => '! ',   block => 1, trim => 'both', line_format => 'single' },
 
36
    h5 => { start => '! ',   block => 1, trim => 'both', line_format => 'single' },
 
37
    h6 => { start => '! ',   block => 1, trim => 'both', line_format => 'single' },
 
38
 
 
39
    pre => { preserve => 1 },
 
40
  );
 
41
 
 
42
  # HTML tags allowed in wiki markup
 
43
  foreach my $tag ( qw/ big small tt abbr acronym cite code dfn kbd samp var sup sub / ) {
 
44
    $rules{$tag} = { preserve => 1 }
 
45
  }
 
46
 
 
47
  return \%rules;
 
48
}
 
49
 
 
50
# Calculates the prefix that will be placed before each list item.
 
51
# List item include ordered and unordered list items.
 
52
sub _li_start {
 
53
  my( $self, $node, $rules ) = @_;
 
54
  my @parent_lists = $node->look_up( _tag => qr/ul|ol/ );
 
55
  my $depth = @parent_lists;
 
56
 
 
57
  my $bullet = '';
 
58
  $bullet = '*' if $node->parent->tag eq 'ul';
 
59
  $bullet = '#' if $node->parent->tag eq 'ol';
 
60
 
 
61
  my $prefix = ( $bullet ) x $depth;
 
62
  return "\n$prefix ";
 
63
}
 
64
 
 
65
sub _image {
 
66
  my( $self, $node, $rules ) = @_;
 
67
  return $node->attr('src') || '';
 
68
}
 
69
 
 
70
sub _link {
 
71
  my( $self, $node, $rules ) = @_;
 
72
  my $url = $node->attr('href') || '';
 
73
  my $text = $self->get_elem_contents($node) || '';
 
74
  return "[$text|$url]";
 
75
}
 
76
 
 
77
# Not quite right yet (e.g. doesn't handle rowspan)
 
78
sub _td_start {
 
79
  my( $self, $node, $rules ) = @_;
 
80
  my @left = $node->left;
 
81
  return '' unless @left;
 
82
  return ( ( '  ' ) x scalar(@left) );
 
83
}
 
84
 
 
85
sub _td_end {
 
86
  my( $self, $node, $rules ) = @_;
 
87
  my $right_tag = $node->right && $node->right->tag ? $node->right->tag : '';
 
88
  return $right_tag =~ /td|th/ ? " |\n" : "\n";
 
89
}
 
90
 
 
91
sub _blockquote_start {
 
92
  my( $self, $node, $rules ) = @_;
 
93
  my @bq_lineage = $node->look_up( _tag => 'blockquote' );
 
94
  my $depth = @bq_lineage;
 
95
  return "\n" . ( ( '  ' ) x $depth );
 
96
}
 
97
 
 
98
sub preprocess_node {
 
99
  my( $self, $node ) = @_;
 
100
  $self->strip_aname($node) if $node->tag eq 'a';
 
101
  $self->caption2para($node) if $node->tag eq 'caption';
 
102
}
 
103
 
 
104
1;
 
105
 
 
106
__END__
 
107
 
 
108
=head1 NAME
 
109
 
 
110
HTML::WikiConverter::PhpWiki - HTML-to-wiki conversion rules for PhpWiki
 
111
 
 
112
=head1 SYNOPSIS
 
113
 
 
114
  use HTML::WikiConverter;
 
115
  my $wc = new HTML::WikiConverter( dialect => 'PhpWiki' );
 
116
  print $wc->html2wiki( $html );
 
117
 
 
118
=head1 DESCRIPTION
 
119
 
 
120
This module contains rules for converting HTML into PhpWiki
 
121
markup. See L<HTML::WikiConverter> for additional usage details.
 
122
 
 
123
=head1 AUTHOR
 
124
 
 
125
David J. Iberri <diberri@yahoo.com>
 
126
 
 
127
=head1 COPYRIGHT
 
128
 
 
129
Copyright (c) 2005 David J. Iberri
 
130
 
 
131
This library is free software; you can redistribute it and/or modify
 
132
it under the same terms as Perl itself.
 
133
 
 
134
See http://www.perl.com/perl/misc/Artistic.html
 
135
 
 
136
=cut