File: DocBook.pm

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (186 lines) | stat: -rw-r--r-- 4,075 bytes parent folder | download | duplicates (5)
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
package MojoMojo::Formatter::DocBook;

use strict;
use warnings;
use parent qw/MojoMojo::Formatter/;

eval
  "use XML::LibXSLT;use XML::SAX::ParserFactory (); use XML::LibXML::Reader;";
my $eval_res = $@;
use MojoMojo::Formatter::DocBook::Colorize;

my $xsltfile =
  "/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/xhtml/docbook.xsl";

=head2 module_loaded

Return true if the module is loaded.

=cut

sub module_loaded
{
    return 0 unless -f $xsltfile;
    return $eval_res ? 0 : 1;
}

my $debug = 0;

=head1 NAME

MojoMojo::Formatter::DocBook - format part of content as DocBook

=head1 DESCRIPTION

This formatter will format content between two =docbook blocks as
DocBook document.

=head1 METHODS

=head2 format_content_order

Format order can be 1-99. The DocBook formatter runs on 10.

=cut

sub format_content_order { 10 }

=head2 format_content

Calls the formatter. Takes a ref to the content as well as the
context object.

=cut

sub format_content
{
    my ($class, $content, $c) = @_;

    my @lines = split /\n/, $$content;
    my $dbk;
    $$content = "";
    my $start_re = $class->gen_re(qr/docbook/);
    my $end_re   = $class->gen_re(qr/end/);
    foreach my $line (@lines)
    {
        if ($dbk)
        {
            if ($line =~ m/^(.*)$end_re(.*)$/)
            {
                $$content .= $class->to_xhtml($dbk);
                $dbk = "";
            }
            else { $dbk .= $line . "\n"; }
        }
        else
        {
            if ($line =~ m/^(.*)$start_re(.*)$/)
            {
                $$content .= $1;
                $dbk = " " . $2;    # make it true :)
            }
            else { $$content .= $line . "\n"; }
        }
    }
}

=head2 to_xhtml <dbk>

Takes DocBook documentation and renders it as XHTML.

=cut

sub to_xhtml
{
    my ($class, $dbk) = @_;
    my $result;

    # Beurk
    $dbk =~ s/&/_-_amp_-_;/g;

    $dbk =~ s/^\s+//;
    $dbk =~ s/^\n+//;

    # 1 - Mark lang
    # <programlisting lang="..."> to <programlisting lang="...">[lang=...] code [/lang]
    my $my_Handler = MojoMojo::Formatter::DocBook::Colorize->new($debug);
    $my_Handler->step('marklang');

    my $parsersax = XML::SAX::ParserFactory->parser(Handler => $my_Handler,);

    my @markeddbk = eval { $parsersax->parse_string($dbk) };
    if ($@)
    {
        return "\nDocument malformed : $@\n";
    }

    # 2 - Transform with xslt
    my $parser = XML::LibXML->new();
    my $xslt   = XML::LibXSLT->new();

    my $source = eval { $parser->parse_string("@markeddbk") };

    if ($@)
    {
        return "\nDocument malformed : line $@\n";
    }

    my $style_doc = $parser->parse_file($xsltfile);
    my $stylesheet = eval { $xslt->parse_stylesheet($style_doc); };

    #    warn "@_" if @_;

    #return "XHTML XHTML XHTML";

    # C'est ici que l'on peut ajouter le css, LANG ...
    # voir http://docbook.sourceforge.net/release/xsl/current/doc/html/index.html
    # et   http://www.sagehill.net/docbookxsl
    my $results = $stylesheet->transform(
        $source,
        XML::LibXSLT::xpath_to_string(
            'section.autolabel'   => '1',
            'chapter.autolabel'   => '1',
            'suppress.navigation' => '1',
            'generate.toc'        => '0'
        )
    );

    my $format = 0;

    my $string = eval { $results->toString($format); };

    # 3 - Colorize Code [lang=...] ... code ... [/lang]
    $my_Handler->step('colorize');

    my @colorized = $parsersax->parse_string($string);

    $string = "@colorized";
    $string =~ s/_-_amp_-_;/&/g;

    # 4 - filter
    # To adapt to mojomojo
    # delete <?xml version ...>, <html>,</html>,<head>,</head>,<body>,</body>
    $string =~ s/^.*<body>//s;
    $string =~ s/<\/body>.*<\/html>//s;
    $string =~ s/<a id=\"id\d*\"><\/a>//g;
    $string =~ s/clear:\sboth//g;

    return $string;
}

=head1 SEE ALSO

L<MojoMojo>, L<Module::Pluggable::Ordered>

=head1 AUTHORS

Daniel Brosseau <dab@catapulse.org>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;