File: Input.pm

package info (click to toggle)
libirc-formatting-html-perl 0.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92 kB
  • sloc: perl: 464; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download | duplicates (3)
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
package IRC::Formatting::HTML::Input;

use warnings;
use strict;

use IRC::Formatting::HTML::Common;
use HTML::Parser ();

my $p = HTML::Parser->new(api_version => 3,
            text_h  => [\&_text, 'dtext'],
            start_h => [\&_tag_start, 'tagname, attr'],
            end_h   => [\&_tag_end, 'tagname']);

my $nbsp = chr(160);
my @states;
my $irctext = "";

sub parse {
  $irctext = "";
  _reset();
  my $html = shift;
  $html =~ s/\n//;
  $p->parse($html);
  $p->eof;
  $irctext =~ s/\n{2,}/\n/;
  $irctext =~ s/^\n+//;
  $irctext =~ s/\n+$//;
  return $irctext;
}

sub _reset {
  @states = ({
    b => 0,
    i => 0,
    u => 0,
    fg => "",
    bg => "",
  });
}

sub _text {
  my $text = shift;
  $text =~ s/$nbsp/ /g;
  $irctext .= $text if defined $text and length $text;
}

sub clone {
  my $state = $states[0];
  return {
    b => $state->{b},
    i => $state->{i},
    u => $state->{u},
    fg => $state->{fg},
    bg => $state->{bg},
  };
}

sub _tag_start {
  my ($tag, $attr) = @_;

  my $state = clone();

  if ($tag eq "br" or $tag eq "p" or $tag eq "div" or $tag =~ /^h[\dr]$/) {
    $irctext .= "\n";
  }

  if ($attr->{style}) {
    if ($attr->{style} =~ /(?:^|;\s*)color:\s*([^;"]+)/) {
      my $color = IRC::Formatting::HTML::Common::html_color_to_irc($1);
      if ($color) {
        $state->{fg} = $color;
        $irctext .= $COLOR.$color;
        $irctext .=",$state->{bg}" if length $state->{bg};
      }
    }
    if ($attr->{style} =~ /font-weight:\s*bold/) {
      $irctext .= $BOLD unless $state->{b};
      $state->{b} = 1;
    }
    if ($attr->{style} =~ /font-style:\s*italic/) {
      $irctext .= $INVERSE unless $state->{i};
      $state->{i} = 1;
    }
    if ($attr->{style} =~ /text-decoration:\s*underline/) {
      $irctext .= $UNDERLINE unless $state->{u};
      $state->{u} = 1;
    }
    if ($attr->{style} =~ /background-color:\s*([^;"]+)/) {
      my $color = IRC::Formatting::HTML::Common::html_color_to_irc($1);
      if ($color) {
        $state->{bg} = $color;
        my $fg = length $state->{fg} ? $state->{fg} : "01";
        $irctext .= $COLOR."$fg,$color";
      }
    }
  }

  if ($attr->{color}) {
    my $color = IRC::Formatting::HTML::Common::html_color_to_irc($attr->{color});
    if ($color) {
      $state->{fg} = $color;
      $irctext .= $COLOR.$color;
      $irctext .=",$state->{bg}" if length $state->{bg};
    }
  }

  if ($tag eq "strong" or $tag eq "b" or $tag =~ /^h\d$/) {
    $irctext .= $BOLD unless $state->{b};
    $state->{b} = 1;
  } elsif ($tag eq "em" or $tag eq "i") {
    $irctext .= $INVERSE unless $state->{i};
    $state->{i} = 1;
  } elsif ($tag eq "u") {
    $irctext .= $UNDERLINE unless $state->{u};
    $state->{u} = 1;
  }

  unshift @states, $state;
}

sub _tag_end {
  my $tag = shift;

  my $prev = shift @states;
  my $next = $states[0];

  $irctext .= $BOLD if $next->{b} ne $prev->{b};
  $irctext .= $INVERSE if $next->{i} ne $prev->{i};
  $irctext .= $UNDERLINE if $next->{u} ne $prev->{u};

  if ($next->{fg} ne $prev->{fg} or $next->{bg} ne $prev->{bg}) {
    $irctext .= $COLOR;

    my ($fg, $bg) = ("","");
    
    if (length $next->{fg}) {
      $fg = $next->{fg};
    }
    if (length $next->{bg}) {
      $bg = $next->{bg};
      $fg = "01" unless length $fg;
    }

    $irctext .= $fg;
    $irctext .= ",$bg" if length $bg;
  }

  if ($tag eq "p" or $tag eq "div" or $tag =~ /^h[\dr]$/) {
    $irctext .= "\n";
  }
}

1