Imported Robodoc.
[robodoc.git] / Source / css_to_c.pl
1 #!/usr/bin/perl -w
2 #****h* Module/css_to_c.pl
3 # USAGE: 
4 #   ./css_to_c.pl 
5 #
6 # FUNCTION
7 #   Turns a .css file into a piece of C code.
8 #
9 # AUTHOR  
10 #   Frans Slothouber (FSL), <rfsber@xs4all.nl>  Gumpu Consulting
11 # CREATED:  
12 #   6-7-2006 12:45:01 W. Europe Standard Time
13 #     REVISION:  ---
14 #
15 #******************************************************************************
16 # $Id: css_to_c.pl,v 1.4 2006/07/27 19:12:18 gumpu Exp $
17
18 use strict;
19 use warnings;
20 use IO::File;
21
22
23 # First preprocess the CSS code.
24 my $robodoc_css_file_name = "robodoc_basic_style.css";
25 my $robodoc_css_file = IO::File->new("<$robodoc_css_file_name") or die;
26 my @css = <$robodoc_css_file>;
27 $robodoc_css_file->close();
28
29 # Remove \n
30 # Replace all % by %%
31 # Replace all " by \"
32 # wrap inside a " .. \n"
33 @css = map { chomp($_); s/%/%%/g;  s/"/\\"/g; "\"$_\\n\"\n" } @css;
34
35 # Split the whole file into several parts each with
36 # it's own  fprintf().
37 # This to avoid compiler warning about string that
38 # are too long.
39 my @split_css_code = ();
40 my $l = 0; # Length of the string
41 push( @split_css_code, "            fprintf( css_file,\n" );
42 foreach my $line ( @css )
43 {
44     push( @split_css_code, $line );
45     $l += length( $line );
46     if ( $l > 2000 ) {
47         $l = 0;
48         push( @split_css_code, "                    );\n" );
49         push( @split_css_code, "            fprintf( css_file,\n" );
50     }
51 }
52 push( @split_css_code, "                    );\n" );
53
54
55 # Insert CSS code into  html_generator.c
56
57 # Read the original code.
58 my $html_generator_file = IO::File->new("<html_generator.c") or die;
59 my @code = <$html_generator_file>;
60 $html_generator_file->close();
61
62 # Create a backup.
63 my $html_generator_file_bak = IO::File->new(">html_generator_bak.c") or die;
64 print $html_generator_file_bak @code;
65 $html_generator_file_bak->close();
66
67 # Merge the C code with the CSS code.
68 my @new_code = ();
69 my $skip = 0;
70 foreach my $line ( @code ) {
71     if ( $line =~ m/BEGIN\sBEGIN\s/ ) {
72         $skip = 1;
73         push( @new_code, $line );
74         push( @new_code, @split_css_code );
75
76     } elsif ( $line =~ m/END\sEND\s/ ) {
77         push( @new_code, $line );
78         $skip = 0;
79     } else {
80         if ( $skip ) {
81             #nothing
82         } else {
83             push( @new_code, $line );
84         }
85     }
86 }
87
88 # Write the result.
89 $html_generator_file = IO::File->new(">html_generator.c") or die;
90
91 print $html_generator_file @new_code;
92
93 $html_generator_file->close();
94