aboutsummaryrefslogtreecommitdiff
path: root/rtest.pl
blob: df031fd7cd8cfeaa6ae1168dd23a6b5e7531a5b6 (plain)
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
#!/usr/bin/perl 
use strict;
use warnings;
use XML::LibXML;
use Text::Restructured;
use YAML;
 
my $input=<<'EOF';
======
 Test
======
:Author: dakkar
 
paragrafo
 
link
 
mathml: :mathml:`x^2`
 
.. mathml::
 
  (x^2)/(y^2)
 
pre::
 
  gino  pino
 rino
 
normale
 
:titlink:`/path/al/doc/`
 
EOF
my $name='--inline--';
 
my $opts={
    => {
        generator => 0,
        'source-link' => 0,
        'time' => 0,
    },
};
my $parser=Text::Restructured->new($opts,'gino');
if (@ARGV) {
    $name=$ARGV[0];
    $input=do{open my $fh,'<',$name;local $/;<$fh>};
}
 
# bad MNODINE, no cookie 
# MY_ROLES gets re-inited before every Parse, so I have to change a 
# global variable 
$Text::Restructured::ROLES{titlink}={
    tag => 'reference',
    attr => {
        refuri => '%s',
        role => sub {
            my ($parser,$attr,$text,$role)=@_;
            $role;
        }
    },
    text => 'dummy',
};
 
my $dudom=$parser->Parse($input,$name);
 
my $xdoc=XML::LibXML->createDocument();
 
my $MATHML='http://www.w3.org/1998/Math/MathML';
sub mathml2xml {
    my ($mnode)=@_;
 
    if ($mnode->isText) {
        return $xdoc->createTextNode($mnode->nodeValue);
    }
 
 
    my @children=map {mathml2xml($_)}
        $mnode->childNodes();
 
    my $elem=$xdoc->createElementNS($MATHML,$mnode->nodeName);
    for my $attname ($mnode->attributeList) {
        next if $attname eq 'xmlns';
        $elem->setAttribute($attname,
                            $mnode->attribute($attname))
    }
 
    $elem->appendChild($_for @children;
 
    return $elem;
}
 
sub docutils2xml {
    my ($dunode)=@_;
 
    if ($dunode->{tageq '#PCDATA') {
        return $xdoc->createTextNode($dunode->{text} || '');
    }
 
    if ($dunode->{tageq 'mathml') {
        return mathml2xml($dunode->{attr}{mathml});
    }
 
    my @children=map {docutils2xml($_)}
        @{ $dunode->{content} || [] };
 
    my $elem=$xdoc->createElement($dunode->{tag});
 
    if (defined $dunode->{attr}) {
        while (my ($attname,$attval)=each %{$dunode->{attr}}) {
            if (! defined $attval) {
                $attval='';
            }
            elsif (ref($attvaleq 'ARRAY') {
                $attval=join ' ',@$attval;
            }
            $elem->setAttribute($attname,$attval);
        }
    }
    $elem->appendChild($_for @children;
 
    return $elem;
}
$xdoc->setDocumentElement(docutils2xml($dudom));
print $xdoc->toString();