File: insert_example_code.pl

package info (click to toggle)
gtkmm2.0 2.2.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,872 kB
  • ctags: 51,600
  • sloc: xml: 73,173; cpp: 20,565; sh: 8,608; perl: 2,702; makefile: 1,233
file content (82 lines) | stat: -rwxr-xr-x 1,621 bytes parent folder | download | duplicates (10)
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
#! /usr/bin/perl -w

#sub main()
{
  my $examples_base = shift(@ARGV);

  $examples_base .= "/" unless($examples_base =~ /\/$/);

  foreach $file (@ARGV)
  {
    open(FILE, $file);

    while(<FILE>)
    {
      print $_;

      #Beginning of comment:
      # Look for
      # <para><ulink url="&url_examples_base;helloworld">Source Code</ulink></para>

      if(/<para><ulink url=\"&url_examples_base;([\/\w]+)\">Source Code<\/ulink><\/para>/)
      {
        #List all the source files in that directory:
        my $directory = $examples_base . $1;

        opendir(DIR, $directory);
        my @dir_contents = readdir(DIR);
        closedir(DIR);

        my @source_files = grep(/\.cc$/, @dir_contents);
        my @header_files = grep(/\.h$/,  @dir_contents);

        print "<!-- start inserted example code -->\n";

        foreach $source_file (@header_files, @source_files)
        {
           print "<para>File: ${source_file}\n";
           print "<programlisting>\n";

           &process_source_file("${directory}/${source_file}");

           print "</programlisting>\n";
           print "</para>\n";
        }

        print "<!-- end inserted example code -->\n";
      }
    }

    close(FILE);
  }

  exit 0;
}

sub process_source_file($)
{
  my ($source_file) = @_;
  my $found_start = 0;

  open(SOURCE_FILE, $source_file);

  while(<SOURCE_FILE>)
  {
    # Skip all text until the first code line.
    if(!$found_start)
    {
      next unless /^[#\w]/;
      $found_start = 1;
    }

    s/&/&amp;/g;
    s/</&lt;/g;
    s/>/&gt;/g;
    s/"/&quot;/g;

    print $_;
  }

  close(SOURCE_FILE);
}