~ubuntu-branches/ubuntu/lucid/dblatex/lucid

« back to all changes in this revision

Viewing changes to scripts/xml2sgml.pl

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Hoenen
  • Date: 2006-05-15 19:59:06 UTC
  • Revision ID: james.westby@ubuntu.com-20060515195906-jg9x08tsfbi35m2x
Tags: upstream-0.1.9
Import upstream version 0.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
    eval 'exec /usr/bin/env perl -S $0 ${1+"$@"}'
 
3
        if $running_under_some_shell;
 
4
 
 
5
use File::Basename;
 
6
 
 
7
%empty_tags = (
 
8
  "area"          => '',
 
9
  "audiodata"     => '',
 
10
  "beginpage"     => '',
 
11
  "co"            => '',
 
12
  "colspec"       => '',
 
13
  "constraint"    => '',
 
14
  "footnoteref"   => '',
 
15
  "graphic"       => '',
 
16
  "imagedata"     => '',
 
17
  "inlinegraphic" => '',
 
18
  "xref"          => ''
 
19
  );
 
20
 
 
21
sub parse_entities
 
22
{
 
23
  local($l) = $_[0];
 
24
 
 
25
  $l =~ s/�/\â/g;
 
26
  $l =~ s/�/\à/g;
 
27
  $l =~ s/�/\é/g;
 
28
  $l =~ s/�/\ê/g;
 
29
  $l =~ s/�/\è/g;
 
30
  $l =~ s/�/\î/g;
 
31
  $l =~ s/�/\ô/g;
 
32
  $l =~ s/�/\ù/g;
 
33
  $l =~ s/�/\û/g;
 
34
  $l =~ s/�/\ü/g;
 
35
  $l =~ s/�/\ç/g;
 
36
 
 
37
  return $l;
 
38
}
 
39
 
 
40
#
 
41
# variable globale pour la gestion des tags EMPTY
 
42
#
 
43
@open_tags=();
 
44
$take=0;
 
45
 
 
46
sub parse_empty_tags
 
47
{
 
48
  my @tags = split(/(\<)|(\/?\>)/) ;
 
49
  my $i,$take;
 
50
  
 
51
  for($i=0;$i <= $#tags;$i++) {
 
52
    $_=$tags[$i];
 
53
    if (/^\</) {
 
54
      $take++;
 
55
      next;
 
56
    } elsif (/^(\/?\w+)/ && $take) {
 
57
      #store in the array the tag name
 
58
      push @open_tags,$1;
 
59
      $take=0;
 
60
    } elsif (/\>/) { 
 
61
            if (exists $empty_tags{pop(@open_tags)}) {
 
62
        #Here we have an empty tag end, just transform it.
 
63
              $tags[$i] =~ s/\/\>/\>/g;
 
64
      }
 
65
    }
 
66
  }
 
67
  return join('',@tags);
 
68
}
 
69
 
 
70
sub parse_xml
 
71
{
 
72
  local($xmlfile) = $_[0];
 
73
  local($sgmlfile) = $_[1];
 
74
  local($noheader) = $_[2];
 
75
  my $line = "";
 
76
  my $file = "";
 
77
  my $SGML = "f$sgmlfile";
 
78
  my $XML = "f$xmlfile";
 
79
 
 
80
  print "$xmlfile -> $sgmlfile\n";
 
81
 
 
82
  if (-f $sgmlfile) {
 
83
    print "***Warning: $sgmlfile already exists\n";
 
84
    system("mv $sgmlfile $sgmlfile~");
 
85
  }
 
86
 
 
87
  $sgmldir = dirname($sgmlfile);
 
88
  if (not(-d $sgmldir)) {
 
89
    print "***Warning: creating the directory $sgmldir\n";
 
90
    system("mkdir -p $sgmldir");
 
91
  }
 
92
 
 
93
  open($XML, "<$xmlfile") || die "Cannot open $xmlfile\n";
 
94
  open($SGML, ">$sgmlfile") || die "Cannot open $sgmlfile\n";
 
95
  
 
96
  while (<$XML>) {
 
97
    $line = $_;
 
98
    if (/\<\?xml .*\?\>/) {
 
99
      #just skip the xml description type
 
100
      next;
 
101
    } 
 
102
    if ($noheader == 0) {
 
103
      #Don't print .dtd location for sgml
 
104
      $line =~ s/\".*\.dtd\"//g;
 
105
      if (/doctype/ || /DOCTYPE/) {
 
106
        # Change the file header
 
107
        $line =~ s/doctype/DOCTYPE/;
 
108
        $line =~ s/DOCTYPE ([a-z]*) .*\"/DOCTYPE $1 $system/;
 
109
        
 
110
      } elsif (/ENTITY.*\.xml/) {
 
111
        
 
112
        # Get the included file name
 
113
        ($file = $line) =~ s/.*\"(.*)\.xml\".*\n/$1/;
 
114
 
 
115
        # Output follows the main output path
 
116
        $outfile = dirname($sgmlfile) . "/$file";
 
117
 
 
118
        # The root file directory *is* the root directory
 
119
        $outfile =~ s/\.\.\//dotdot\//g;
 
120
 
 
121
        # Now the included file is the SGML one
 
122
        $line =~ s/(.*)\"(.*)\.xml\"/$1\"$outfile.sgml\"/;
 
123
 
 
124
        # Relative or absolute path?
 
125
        if (not($file =~ /^\//)) {
 
126
          $file = dirname($sgmlfile) . "/$file";
 
127
        }
 
128
 
 
129
        # The included files need not header
 
130
        parse_xml("$file.xml", "$outfile.sgml", 1);
 
131
      
 
132
      } elsif (/\]\>/) {
 
133
        #End of header
 
134
        $noheader++;
 
135
      }
 
136
      print $SGML $line ;
 
137
      next;
 
138
    } else {
 
139
      # Body of the document
 
140
      
 
141
      # Case of entities
 
142
      # $line = parse_entities($line);
 
143
 
 
144
      # Case of empty tags
 
145
      $line = parse_empty_tags($line);
 
146
 
 
147
      print $SGML $line;
 
148
    }
 
149
  }
 
150
 
 
151
  close($SGML);
 
152
  close($XML);
 
153
}
 
154
 
 
155
#
 
156
# Script start
 
157
#
 
158
if (not(@ARGV)) {
 
159
  print "$0 input.xml [output.sgml]\n";
 
160
  exit 1;
 
161
}
 
162
 
 
163
$system = " PUBLIC \"-//OASIS//DTD DocBook V4.1.2//EN\"";
 
164
 
 
165
$xmlfile = $ARGV[0];
 
166
$sgmlfile = basename($xmlfile, '.xml');
 
167
$sgmlfile = dirname($xmlfile). "/$sgmlfile.sgml";
 
168
shift;
 
169
 
 
170
if (@ARGV) {
 
171
  $sgmlfile = $ARGV[0];
 
172
}
 
173
 
 
174
parse_xml($xmlfile, $sgmlfile, 0);
 
175