[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(mgp-users 00104) small contribution



Hello,

For those who might be interested, here is a perl script which allow to
include and beautify source codes into mgp presentations.

Fabien.

Fabien COELHO __ http://www.cri.ensmp.fr/~coelho __ coelho@cri.ensmp.fr
 CRI-ENSMP, 35, rue Saint-Honoré, F-77305 Fontainebleau cedex, France
  phone: (+33|0) 1 64 69 {voice: 48 52, fax: 47 09, standard: 47 08}
      ________  All opinions expressed here are mine  _________
#! /usr/local/bin/perl -w
#
# $Revision: 1.4 $
#
# $Date: 1999/03/04 15:22:43 $
#
# a basic program beautifier for Magic Point
# to hilight Perl and Java.
#
# (c) Fabien COELHO (coelho@cri.ensmp.fr)
#

use Getopt::Long;

# number of lines per slide.
$line = 16;
# slide title
$title = '';
# hack for spaces in title.
$space = '_';
# font to be used.
$font = 'ttr';
# whether to number slides
$number = 0;

# color hilighting
$comment = 'orange';
$keyword = 'green';
$code = 'white';

# current page number
$page = 0;

&GetOptions("title=s" => \$title,
            "line=i" => \$line,
	    "space=s" => \$space,
	    "font=s" => \$font,
	    "number" => \$number,
	    "lang" => \$lang,
	    "comment=s" => \$comment,
	    "keyword=s" => \$keyword,
	    "code=s" => \$code) 
    or die "cannot parse options";

# default language
if (! defined($lang)) {
    $lang = 'Perl' if $ARGV[0] =~ /pl/;
    $lang = 'Java' if $ARGV[0] =~ /java/;
}

# default title
$title = "$lang code: $ARGV[0]" if ! $title;
$title =~ s/$space/ /g;

# language selection.
if ($lang eq 'Perl') {
  @keywords = qw( if use foreach while else elsif for
		  do return eq ne le lt ge gt and or);
  $mlc_start = '';
  $mlc_stop = '';
  $lc = '#';
} elsif ($lang eq 'Java') {
  @keywords = qw( if else while class package import for
		  do switch case default static final int
		  char float double long short boolean
		  public protected private return void 
		  throw throws new byte goto try catch
		  finally );
  $mlc_start = '\/\*';
  $mlc_stop = '\*\/';
  $lc = '//';
} else {
    die "invalid language $lang";
}

while (<>) {

  # untab
  s/\t/        /g;
  $_ = " $_"; # avoids leading %#

  if ($. % $line == 1) {
    $page++;
    print "%page\n\n$title";
    print " ($page)" if $number;
    print "\n\n";
    print "%font \"$font\"\n" if $font;
  }

  # whether in multi-line comment
  $in_mlc = $mlc_start && (/$mlc_start/ .. /$mlc_stop/);

  $inc = $in_mlc || /^\s*$lc/;

  if (! $inc) {
    foreach $kw (@keywords) {
      $rp = "\n%cont, fore \"$keyword\"\n"
	  . "$kw\n%cont, fore \"$code\"\n";
      s/\b$kw\b/$rp/g;
    }
  } 

  if (! $inc && /$lc/) {
      s/([^\']$lc.*)/\n%cont, fore \"$comment\"\n$1\n%cont, fore \"$code\"\n/;
      print;
  } else {
      print "\n%cont, fore \"$comment\"\n" if $inc;
      print;
      print "%cont, fore \"$code\"\n\n" if $inc;
  }
}
%deffont "std" xfont "helvetica-medium-r", tfont "arial.ttf", hgap 5
%deffont "thk" xfont "helvetica-bold-r", tfont "arialbd.ttf", hgap 5
%deffont "ttr" xfont "helvetica narrow-medium-r", tfont "courbd.ttf", hgap 1
%default 1 leftfill, size 1, fore "yellow", back "black", font "std", bgrad
%default 2 size 7, vgap 10, prefix " ", font "thk"
%default 3 size 2, bar "red" 10, vgap 10
%default 4 size 5, fore "white", vgap 30, prefix " ", font "std"
%tab 1 size 5, vgap 40, prefix "  ", icon box "green" 50
%tab 2 size 4, vgap 40, prefix "      ", icon arc "yellow" 50
%tab 3 size 3, vgap 40, prefix "            ", icon delta3 "white" 40
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%page

Perl script



%center, fore "red", size 8
Here is a magically displayed perl script.

%left, fore "white", size 5
 %filter "./a2mgp.pl --number a2mgp.pl"
 %endfilter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%filter "./a2mgp.pl --number a2mgp.pl"
%endfilter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%page

Now Java...



%center, fore "red", size 8
Here is a magically displayed java source.

%left, fore "white", size 5
 %filter "./a2mgp.pl test.java"
 %endfilter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%filter "./a2mgp.pl test.java"
%endfilter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%page

Bye.


// comment

public class test {
  static final int hello = 1; // hi!

/* multi
   line
   comment if possible */
  static public void main(String[] args)
  {
    System.out.println("hello");
    /* on one line */
    return;
  }
}